2015-10-21 14:41:42 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2015 The Kubernetes Authors .
2015-10-21 14:41:42 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2018-04-10 14:21:50 +00:00
package create
2015-10-21 14:41:42 +00:00
import (
"github.com/spf13/cobra"
2018-08-21 10:46:39 +00:00
"k8s.io/cli-runtime/pkg/genericclioptions"
2015-10-21 14:41:42 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2018-09-09 06:59:55 +00:00
"k8s.io/kubernetes/pkg/kubectl/generate"
generateversioned "k8s.io/kubernetes/pkg/kubectl/generate/versioned"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2018-10-10 18:29:30 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/templates"
2015-10-21 14:41:42 +00:00
)
// NewCmdCreateSecret groups subcommands to create various types of secrets
2018-04-19 21:43:28 +00:00
func NewCmdCreateSecret ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2015-10-21 14:41:42 +00:00
cmd := & cobra . Command {
Use : "secret" ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Create a secret using specified subcommand" ) ,
2015-10-21 14:41:42 +00:00
Long : "Create a secret using specified subcommand." ,
2018-04-19 21:43:28 +00:00
Run : cmdutil . DefaultSubCommandRun ( ioStreams . ErrOut ) ,
2015-10-21 14:41:42 +00:00
}
2018-04-19 21:43:28 +00:00
cmd . AddCommand ( NewCmdCreateSecretDockerRegistry ( f , ioStreams ) )
cmd . AddCommand ( NewCmdCreateSecretTLS ( f , ioStreams ) )
cmd . AddCommand ( NewCmdCreateSecretGeneric ( f , ioStreams ) )
2016-03-10 01:27:19 +00:00
2015-10-21 14:41:42 +00:00
return cmd
}
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
secretLong = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Create a secret based on a file , directory , or specified literal value .
2015-10-21 14:41:42 +00:00
2016-05-20 17:49:56 +00:00
A single secret may package one or more key / value pairs .
2015-10-21 14:41:42 +00:00
2016-05-20 17:49:56 +00:00
When creating a secret based on a file , the key will default to the basename of the file , and the value will
2017-09-22 22:58:28 +00:00
default to the file content . If the basename is an invalid key or you wish to chose your own , you may specify
an alternate key .
2015-10-21 14:41:42 +00:00
2016-05-20 17:49:56 +00:00
When creating a secret based on a directory , each file whose basename is a valid key in the directory will be
2017-09-22 22:58:28 +00:00
packaged into the secret . Any directory entries except regular files are ignored ( e . g . subdirectories ,
2017-03-15 03:49:10 +00:00
symlinks , devices , pipes , etc ) . ` ) )
2015-10-21 14:41:42 +00:00
2017-03-15 03:49:10 +00:00
secretExample = templates . Examples ( i18n . T ( `
2016-10-07 22:24:42 +00:00
# Create a new secret named my - secret with keys for each file in folder bar
kubectl create secret generic my - secret -- from - file = path / to / bar
2015-10-21 14:41:42 +00:00
2016-10-07 22:24:42 +00:00
# Create a new secret named my - secret with specified keys instead of names on disk
kubectl create secret generic my - secret -- from - file = ssh - privatekey = ~ / . ssh / id_rsa -- from - file = ssh - publickey = ~ / . ssh / id_rsa . pub
2015-10-21 14:41:42 +00:00
2016-10-07 22:24:42 +00:00
# Create a new secret named my - secret with key1 = supersecret and key2 = topsecret
2017-01-12 15:35:46 +00:00
kubectl create secret generic my - secret -- from - literal = key1 = supersecret -- from - literal = key2 = topsecret
2017-09-22 22:58:28 +00:00
# Create a new secret named my - secret using a combination of a file and a literal
kubectl create secret generic my - secret -- from - file = ssh - privatekey = ~ / . ssh / id_rsa -- from - literal = passphrase = topsecret
2017-01-12 15:35:46 +00:00
# Create a new secret named my - secret from an env file
2017-03-15 03:49:10 +00:00
kubectl create secret generic my - secret -- from - env - file = path / to / bar . env ` ) )
2015-10-21 14:41:42 +00:00
)
2018-04-05 22:39:17 +00:00
type SecretGenericOpts struct {
CreateSubcommandOptions * CreateSubcommandOptions
}
2015-10-21 14:41:42 +00:00
// NewCmdCreateSecretGeneric is a command to create generic secrets from files, directories, or literal values
2018-04-19 21:43:28 +00:00
func NewCmdCreateSecretGeneric ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-04-05 22:39:17 +00:00
options := & SecretGenericOpts {
2018-04-19 21:43:28 +00:00
CreateSubcommandOptions : NewCreateSubcommandOptions ( ioStreams ) ,
2018-04-05 22:39:17 +00:00
}
2015-10-21 14:41:42 +00:00
cmd := & cobra . Command {
2018-10-05 19:59:38 +00:00
Use : "generic NAME [--type=string] [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]" ,
2017-10-11 06:26:02 +00:00
DisableFlagsInUseLine : true ,
2018-10-05 19:59:38 +00:00
Short : i18n . T ( "Create a secret from a local file, directory or literal value" ) ,
Long : secretLong ,
Example : secretExample ,
2015-10-21 14:41:42 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-05-07 21:47:29 +00:00
cmdutil . CheckErr ( options . Complete ( f , cmd , args ) )
cmdutil . CheckErr ( options . Run ( ) )
2015-10-21 14:41:42 +00:00
} ,
}
2018-04-05 22:39:17 +00:00
options . CreateSubcommandOptions . PrintFlags . AddFlags ( cmd )
2015-10-21 14:41:42 +00:00
cmdutil . AddApplyAnnotationFlags ( cmd )
cmdutil . AddValidateFlags ( cmd )
2018-09-09 06:59:55 +00:00
cmdutil . AddGeneratorFlags ( cmd , generateversioned . SecretV1GeneratorName )
2015-10-21 14:41:42 +00:00
cmd . Flags ( ) . StringSlice ( "from-file" , [ ] string { } , "Key files can be specified using their file path, in which case a default name will be given to them, or optionally with a name and file path, in which case the given name will be used. Specifying a directory will iterate each named file in the directory that is a valid secret key." )
2016-10-20 09:43:51 +00:00
cmd . Flags ( ) . StringArray ( "from-literal" , [ ] string { } , "Specify a key and literal value to insert in secret (i.e. mykey=somevalue)" )
2017-01-12 15:35:46 +00:00
cmd . Flags ( ) . String ( "from-env-file" , "" , "Specify the path to a file to read lines of key=val pairs to create a secret (i.e. a Docker .env file)." )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "type" , "" , i18n . T ( "The type of secret to create" ) )
2017-07-28 17:18:08 +00:00
cmd . Flags ( ) . Bool ( "append-hash" , false , "Append a hash of the secret to its name." )
2015-10-21 14:41:42 +00:00
return cmd
}
2018-05-07 21:47:29 +00:00
func ( o * SecretGenericOpts ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
2015-10-21 14:41:42 +00:00
name , err := NameFromCommandArgs ( cmd , args )
if err != nil {
return err
}
2018-04-05 22:39:17 +00:00
2018-09-09 06:59:55 +00:00
var generator generate . StructuredGenerator
2015-10-21 14:41:42 +00:00
switch generatorName := cmdutil . GetFlagString ( cmd , "generator" ) ; generatorName {
2018-09-09 06:59:55 +00:00
case generateversioned . SecretV1GeneratorName :
generator = & generateversioned . SecretGeneratorV1 {
2015-10-21 14:41:42 +00:00
Name : name ,
Type : cmdutil . GetFlagString ( cmd , "type" ) ,
FileSources : cmdutil . GetFlagStringSlice ( cmd , "from-file" ) ,
2016-10-20 09:43:51 +00:00
LiteralSources : cmdutil . GetFlagStringArray ( cmd , "from-literal" ) ,
2017-01-12 15:35:46 +00:00
EnvFileSource : cmdutil . GetFlagString ( cmd , "from-env-file" ) ,
2017-07-28 17:18:08 +00:00
AppendHash : cmdutil . GetFlagBool ( cmd , "append-hash" ) ,
2015-10-21 14:41:42 +00:00
}
default :
2017-05-25 19:44:43 +00:00
return errUnsupportedGenerator ( cmd , generatorName )
2015-10-21 14:41:42 +00:00
}
2018-04-05 22:39:17 +00:00
2018-05-07 21:47:29 +00:00
return o . CreateSubcommandOptions . Complete ( f , cmd , args , generator )
2018-04-05 22:39:17 +00:00
}
// CreateSecretGeneric is the implementation of the create secret generic command
2018-05-07 21:47:29 +00:00
func ( o * SecretGenericOpts ) Run ( ) error {
return o . CreateSubcommandOptions . Run ( )
2015-10-21 14:41:42 +00:00
}
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
secretForDockerRegistryLong = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Create a new secret for use with Docker registries .
2015-10-21 14:41:42 +00:00
2016-05-20 17:49:56 +00:00
Dockercfg secrets are used to authenticate against Docker registries .
2015-10-21 14:41:42 +00:00
2017-09-22 22:58:28 +00:00
When using the Docker command line to push images , you can authenticate to a given registry by running :
' $ docker login DOCKER_REGISTRY_SERVER -- username = DOCKER_USER -- password = DOCKER_PASSWORD -- email = DOCKER_EMAIL ' .
2016-10-07 22:24:42 +00:00
2018-04-10 14:21:50 +00:00
That produces a ~ / . dockercfg file that is used by subsequent ' docker push ' and ' docker pull ' commands to
2017-02-27 22:12:16 +00:00
authenticate to the registry . The email address is optional .
2015-10-21 14:41:42 +00:00
2016-05-20 17:49:56 +00:00
When creating applications , you may have a Docker registry that requires authentication . In order for the
nodes to pull images on your behalf , they have to have the credentials . You can provide this information
2017-03-15 03:49:10 +00:00
by creating a dockercfg secret and attaching it to your service account . ` ) )
2015-10-21 14:41:42 +00:00
2017-03-15 03:49:10 +00:00
secretForDockerRegistryExample = templates . Examples ( i18n . T ( `
2016-05-20 17:49:56 +00:00
# If you don ' t already have a . dockercfg file , you can create a dockercfg secret directly by using :
2017-03-15 03:49:10 +00:00
kubectl create secret docker - registry my - secret -- docker - server = DOCKER_REGISTRY_SERVER -- docker - username = DOCKER_USER -- docker - password = DOCKER_PASSWORD -- docker - email = DOCKER_EMAIL ` ) )
2015-10-21 14:41:42 +00:00
)
2018-04-05 22:39:17 +00:00
type SecretDockerRegistryOpts struct {
CreateSubcommandOptions * CreateSubcommandOptions
}
2015-10-21 14:41:42 +00:00
// NewCmdCreateSecretDockerRegistry is a macro command for creating secrets to work with Docker registries
2018-04-19 21:43:28 +00:00
func NewCmdCreateSecretDockerRegistry ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-04-05 22:39:17 +00:00
options := & SecretDockerRegistryOpts {
2018-04-19 21:43:28 +00:00
CreateSubcommandOptions : NewCreateSubcommandOptions ( ioStreams ) ,
2018-04-05 22:39:17 +00:00
}
2015-10-21 14:41:42 +00:00
cmd := & cobra . Command {
2018-10-05 19:59:38 +00:00
Use : "docker-registry NAME --docker-username=user --docker-password=password --docker-email=email [--docker-server=string] [--from-literal=key1=value1] [--dry-run]" ,
2017-10-11 06:26:02 +00:00
DisableFlagsInUseLine : true ,
2018-10-05 19:59:38 +00:00
Short : i18n . T ( "Create a secret for use with a Docker registry" ) ,
Long : secretForDockerRegistryLong ,
Example : secretForDockerRegistryExample ,
2015-10-21 14:41:42 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-05-07 21:47:29 +00:00
cmdutil . CheckErr ( options . Complete ( f , cmd , args ) )
cmdutil . CheckErr ( options . Run ( ) )
2015-10-21 14:41:42 +00:00
} ,
}
2018-04-05 22:39:17 +00:00
options . CreateSubcommandOptions . PrintFlags . AddFlags ( cmd )
2015-10-21 14:41:42 +00:00
cmdutil . AddApplyAnnotationFlags ( cmd )
cmdutil . AddValidateFlags ( cmd )
2018-09-09 06:59:55 +00:00
cmdutil . AddGeneratorFlags ( cmd , generateversioned . SecretForDockerRegistryV1GeneratorName )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "docker-username" , "" , i18n . T ( "Username for Docker registry authentication" ) )
2015-10-21 14:41:42 +00:00
cmd . MarkFlagRequired ( "docker-username" )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "docker-password" , "" , i18n . T ( "Password for Docker registry authentication" ) )
2015-10-21 14:41:42 +00:00
cmd . MarkFlagRequired ( "docker-password" )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "docker-email" , "" , i18n . T ( "Email for Docker registry" ) )
cmd . Flags ( ) . String ( "docker-server" , "https://index.docker.io/v1/" , i18n . T ( "Server location for Docker registry" ) )
2017-07-28 17:18:08 +00:00
cmd . Flags ( ) . Bool ( "append-hash" , false , "Append a hash of the secret to its name." )
2018-03-05 19:15:46 +00:00
cmd . Flags ( ) . StringSlice ( "from-file" , [ ] string { } , "Key files can be specified using their file path, in which case a default name will be given to them, or optionally with a name and file path, in which case the given name will be used. Specifying a directory will iterate each named file in the directory that is a valid secret key." )
2017-10-13 21:36:54 +00:00
2015-10-21 14:41:42 +00:00
return cmd
}
2018-05-07 21:47:29 +00:00
func ( o * SecretDockerRegistryOpts ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
2015-10-21 14:41:42 +00:00
name , err := NameFromCommandArgs ( cmd , args )
if err != nil {
return err
}
2018-04-05 22:39:17 +00:00
2018-03-05 19:15:46 +00:00
fromFileFlag := cmdutil . GetFlagStringSlice ( cmd , "from-file" )
if len ( fromFileFlag ) == 0 {
requiredFlags := [ ] string { "docker-username" , "docker-password" , "docker-server" }
for _ , requiredFlag := range requiredFlags {
if value := cmdutil . GetFlagString ( cmd , requiredFlag ) ; len ( value ) == 0 {
return cmdutil . UsageErrorf ( cmd , "flag %s is required" , requiredFlag )
}
2015-10-21 14:41:42 +00:00
}
}
2018-03-05 19:15:46 +00:00
2018-09-09 06:59:55 +00:00
var generator generate . StructuredGenerator
2015-10-21 14:41:42 +00:00
switch generatorName := cmdutil . GetFlagString ( cmd , "generator" ) ; generatorName {
2018-09-09 06:59:55 +00:00
case generateversioned . SecretForDockerRegistryV1GeneratorName :
generator = & generateversioned . SecretForDockerRegistryGeneratorV1 {
2018-03-05 19:15:46 +00:00
Name : name ,
Username : cmdutil . GetFlagString ( cmd , "docker-username" ) ,
Email : cmdutil . GetFlagString ( cmd , "docker-email" ) ,
Password : cmdutil . GetFlagString ( cmd , "docker-password" ) ,
Server : cmdutil . GetFlagString ( cmd , "docker-server" ) ,
AppendHash : cmdutil . GetFlagBool ( cmd , "append-hash" ) ,
FileSources : cmdutil . GetFlagStringSlice ( cmd , "from-file" ) ,
2015-10-21 14:41:42 +00:00
}
default :
2017-05-25 19:44:43 +00:00
return errUnsupportedGenerator ( cmd , generatorName )
2015-10-21 14:41:42 +00:00
}
2018-04-05 22:39:17 +00:00
2018-05-07 21:47:29 +00:00
return o . CreateSubcommandOptions . Complete ( f , cmd , args , generator )
2018-04-05 22:39:17 +00:00
}
// CreateSecretDockerRegistry is the implementation of the create secret docker-registry command
2018-05-07 21:47:29 +00:00
func ( o * SecretDockerRegistryOpts ) Run ( ) error {
return o . CreateSubcommandOptions . Run ( )
2015-10-21 14:41:42 +00:00
}
2016-04-24 19:46:58 +00:00
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
secretForTLSLong = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Create a TLS secret from the given public / private key pair .
2016-04-24 19:46:58 +00:00
2017-09-22 22:58:28 +00:00
The public / private key pair must exist before hand . The public key certificate must be . PEM encoded and match
the given private key . ` ) )
2016-04-24 19:46:58 +00:00
2017-03-15 03:49:10 +00:00
secretForTLSExample = templates . Examples ( i18n . T ( `
2016-10-07 22:24:42 +00:00
# Create a new TLS secret named tls - secret with the given key pair :
2017-03-15 03:49:10 +00:00
kubectl create secret tls tls - secret -- cert = path / to / tls . cert -- key = path / to / tls . key ` ) )
2016-04-24 19:46:58 +00:00
)
2018-04-05 22:39:17 +00:00
type SecretTLSOpts struct {
CreateSubcommandOptions * CreateSubcommandOptions
}
2016-04-24 19:46:58 +00:00
// NewCmdCreateSecretTLS is a macro command for creating secrets to work with Docker registries
2018-04-19 21:43:28 +00:00
func NewCmdCreateSecretTLS ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-04-05 22:39:17 +00:00
options := & SecretTLSOpts {
2018-04-19 21:43:28 +00:00
CreateSubcommandOptions : NewCreateSubcommandOptions ( ioStreams ) ,
2018-04-05 22:39:17 +00:00
}
2016-04-24 19:46:58 +00:00
cmd := & cobra . Command {
2018-10-05 19:59:38 +00:00
Use : "tls NAME --cert=path/to/cert/file --key=path/to/key/file [--dry-run]" ,
2017-10-11 06:26:02 +00:00
DisableFlagsInUseLine : true ,
2018-10-05 19:59:38 +00:00
Short : i18n . T ( "Create a TLS secret" ) ,
Long : secretForTLSLong ,
Example : secretForTLSExample ,
2016-04-24 19:46:58 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-05-07 21:47:29 +00:00
cmdutil . CheckErr ( options . Complete ( f , cmd , args ) )
cmdutil . CheckErr ( options . Run ( ) )
2016-04-24 19:46:58 +00:00
} ,
}
2018-04-05 22:39:17 +00:00
options . CreateSubcommandOptions . PrintFlags . AddFlags ( cmd )
2016-04-24 19:46:58 +00:00
cmdutil . AddApplyAnnotationFlags ( cmd )
cmdutil . AddValidateFlags ( cmd )
2018-09-09 06:59:55 +00:00
cmdutil . AddGeneratorFlags ( cmd , generateversioned . SecretForTLSV1GeneratorName )
2017-02-07 06:26:44 +00:00
cmd . Flags ( ) . String ( "cert" , "" , i18n . T ( "Path to PEM encoded public key certificate." ) )
cmd . Flags ( ) . String ( "key" , "" , i18n . T ( "Path to private key associated with given certificate." ) )
2017-07-28 17:18:08 +00:00
cmd . Flags ( ) . Bool ( "append-hash" , false , "Append a hash of the secret to its name." )
2016-04-24 19:46:58 +00:00
return cmd
}
2018-05-07 21:47:29 +00:00
func ( o * SecretTLSOpts ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
2016-04-24 19:46:58 +00:00
name , err := NameFromCommandArgs ( cmd , args )
if err != nil {
return err
}
2018-04-05 22:39:17 +00:00
2016-04-24 19:46:58 +00:00
requiredFlags := [ ] string { "cert" , "key" }
for _ , requiredFlag := range requiredFlags {
if value := cmdutil . GetFlagString ( cmd , requiredFlag ) ; len ( value ) == 0 {
2017-06-14 21:14:42 +00:00
return cmdutil . UsageErrorf ( cmd , "flag %s is required" , requiredFlag )
2016-04-24 19:46:58 +00:00
}
}
2018-09-09 06:59:55 +00:00
var generator generate . StructuredGenerator
2016-04-24 19:46:58 +00:00
switch generatorName := cmdutil . GetFlagString ( cmd , "generator" ) ; generatorName {
2018-09-09 06:59:55 +00:00
case generateversioned . SecretForTLSV1GeneratorName :
generator = & generateversioned . SecretForTLSGeneratorV1 {
2017-07-28 17:18:08 +00:00
Name : name ,
Key : cmdutil . GetFlagString ( cmd , "key" ) ,
Cert : cmdutil . GetFlagString ( cmd , "cert" ) ,
AppendHash : cmdutil . GetFlagBool ( cmd , "append-hash" ) ,
2016-04-24 19:46:58 +00:00
}
default :
2017-05-25 19:44:43 +00:00
return errUnsupportedGenerator ( cmd , generatorName )
2016-04-24 19:46:58 +00:00
}
2018-04-05 22:39:17 +00:00
2018-05-07 21:47:29 +00:00
return o . CreateSubcommandOptions . Complete ( f , cmd , args , generator )
2018-04-05 22:39:17 +00:00
}
// CreateSecretTLS is the implementation of the create secret tls command
2018-05-07 21:47:29 +00:00
func ( o * SecretTLSOpts ) Run ( ) error {
return o . CreateSubcommandOptions . Run ( )
2016-04-24 19:46:58 +00:00
}