get.porter.sh/porter@v1.3.0/cmd/porter/credentials.go (about)

     1  package main
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/porter"
     5  	"github.com/spf13/cobra"
     6  )
     7  
     8  func buildCredentialsCommands(p *porter.Porter) *cobra.Command {
     9  	cmd := &cobra.Command{
    10  		Use:         "credentials",
    11  		Aliases:     []string{"credential", "cred", "creds"},
    12  		Annotations: map[string]string{"group": "resource"},
    13  		Short:       "Credentials commands",
    14  	}
    15  
    16  	cmd.AddCommand(buildCredentialsApplyCommand(p))
    17  	cmd.AddCommand(buildCredentialsEditCommand(p))
    18  	cmd.AddCommand(buildCredentialsGenerateCommand(p))
    19  	cmd.AddCommand(buildCredentialsListCommand(p))
    20  	cmd.AddCommand(buildCredentialsDeleteCommand(p))
    21  	cmd.AddCommand(buildCredentialsShowCommand(p))
    22  	cmd.AddCommand(buildCredentialsCreateCommand(p))
    23  
    24  	return cmd
    25  }
    26  
    27  func buildCredentialsApplyCommand(p *porter.Porter) *cobra.Command {
    28  	opts := porter.ApplyOptions{}
    29  
    30  	cmd := &cobra.Command{
    31  		Use:   "apply FILE",
    32  		Short: "Apply changes to a credential set",
    33  		Long: `Apply changes from the specified file to a credential set. If the credential set doesn't already exist, it is created.
    34  
    35  Supported file extensions: json and yaml.
    36  
    37  You can use the generate and show commands to create the initial file:
    38    porter credentials generate mycreds --reference SOME_BUNDLE
    39    porter credentials show mycreds --output yaml > mycreds.yaml
    40  `,
    41  		Example: `  porter credentials apply mycreds.yaml`,
    42  		PreRunE: func(cmd *cobra.Command, args []string) error {
    43  			return opts.Validate(p.Context, args)
    44  		},
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return p.CredentialsApply(cmd.Context(), opts)
    47  		},
    48  	}
    49  
    50  	f := cmd.Flags()
    51  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
    52  		"Namespace in which the credential set is defined. The namespace in the file, if set, takes precedence.")
    53  
    54  	return cmd
    55  }
    56  
    57  func buildCredentialsEditCommand(p *porter.Porter) *cobra.Command {
    58  	opts := porter.CredentialEditOptions{}
    59  
    60  	cmd := &cobra.Command{
    61  		Use:     "edit",
    62  		Short:   "Edit Credential",
    63  		Long:    `Edit a named credential set.`,
    64  		Example: `  porter credentials edit github --namespace dev`,
    65  		PreRunE: func(cmd *cobra.Command, args []string) error {
    66  			return opts.Validate(args)
    67  		},
    68  		RunE: func(cmd *cobra.Command, args []string) error {
    69  			return p.EditCredential(cmd.Context(), opts)
    70  		},
    71  	}
    72  
    73  	f := cmd.Flags()
    74  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
    75  		"Namespace in which the credential set is defined. Defaults to the global namespace.")
    76  
    77  	return cmd
    78  }
    79  
    80  func buildCredentialsGenerateCommand(p *porter.Porter) *cobra.Command {
    81  	opts := porter.CredentialOptions{}
    82  	cmd := &cobra.Command{
    83  		Use:   "generate [NAME]",
    84  		Short: "Generate Credential Set",
    85  		Long: `Generate a named set of credentials.
    86  
    87  The first argument is the name of credential set you wish to generate. If not
    88  provided, this will default to the bundle name. By default, Porter will
    89  generate a credential set for the bundle in the current directory. You may also
    90  specify a bundle with --file.
    91  
    92  Bundles define 1 or more credential(s) that are required to interact with a
    93  bundle. The bundle definition defines where the credential should be delivered
    94  to the bundle, i.e. at /home/nonroot/.kube. A credential set, on the other hand,
    95  represents the source data that you wish to use when interacting with the
    96  bundle. These will typically be environment variables or files on your local
    97  file system.
    98  
    99  When you wish to install, upgrade or delete a bundle, Porter will use the
   100  credential set to determine where to read the necessary information from and
   101  will then provide it to the bundle in the correct location. `,
   102  		Example: `  porter credentials generate
   103    porter credentials generate kubecred --reference getporter/mysql:v0.1.4 --namespace test
   104    porter credentials generate kubekred --label owner=myname --reference getporter/mysql:v0.1.4
   105    porter credentials generate kubecred --reference localhost:5000/getporter/mysql:v0.1.4 --insecure-registry --force
   106    porter credentials generate kubecred --file myapp/porter.yaml
   107    porter credentials generate kubecred --cnab-file myapp/bundle.json
   108  `,
   109  		PreRunE: func(cmd *cobra.Command, args []string) error {
   110  			return opts.Validate(cmd.Context(), args, p)
   111  		},
   112  		RunE: func(cmd *cobra.Command, args []string) error {
   113  			return p.GenerateCredentials(cmd.Context(), opts)
   114  		},
   115  	}
   116  
   117  	f := cmd.Flags()
   118  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   119  		"Namespace in which the credential set is defined. Defaults to the global namespace.")
   120  	f.StringSliceVarP(&opts.Labels, "label", "l", nil,
   121  		"Associate the specified labels with the credential set. May be specified multiple times.")
   122  	addBundleDefinitionFlags(f, &opts.BundleDefinitionOptions)
   123  	addBundlePullFlags(f, &opts.BundlePullOptions)
   124  
   125  	return cmd
   126  }
   127  
   128  func buildCredentialsListCommand(p *porter.Porter) *cobra.Command {
   129  	opts := porter.ListOptions{}
   130  
   131  	cmd := &cobra.Command{
   132  		Use:     "list",
   133  		Aliases: []string{"ls"},
   134  		Short:   "List credentials",
   135  		Long: `List named sets of credentials defined by the user.
   136  
   137  Optionally filters the results name, which returns all results whose name contain the provided query.
   138  The results may also be filtered by associated labels and the namespace in which the credential set is defined.`,
   139  		Example: `  porter credentials list
   140    porter credentials list --namespace prod
   141    porter credentials list --all-namespaces,
   142    porter credentials list --name myapp
   143    porter credentials list --label env=dev
   144    porter credentials list --skip 2 --limit 2`,
   145  		PreRunE: func(cmd *cobra.Command, args []string) error {
   146  			return opts.Validate()
   147  		},
   148  		RunE: func(cmd *cobra.Command, args []string) error {
   149  			return p.PrintCredentials(cmd.Context(), opts)
   150  		},
   151  	}
   152  
   153  	f := cmd.Flags()
   154  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   155  		"Namespace in which the credential set is defined. Defaults to the global namespace. Use * to list across all namespaces.")
   156  	f.BoolVar(&opts.AllNamespaces, "all-namespaces", false,
   157  		"Include all namespaces in the results.")
   158  	f.StringVar(&opts.Name, "name", "",
   159  		"Filter the credential sets where the name contains the specified substring.")
   160  	f.StringSliceVarP(&opts.Labels, "label", "l", nil,
   161  		"Filter the credential sets by a label formatted as: KEY=VALUE. May be specified multiple times.")
   162  	f.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
   163  		"Specify an output format.  Allowed values: plaintext, json, yaml")
   164  	f.Int64Var(&opts.Skip, "skip", 0,
   165  		"Skip the number of credential sets by a certain amount. Defaults to 0.")
   166  	f.Int64Var(&opts.Limit, "limit", 0,
   167  		"Limit the number of credential sets by a certain amount. Defaults to 0.")
   168  
   169  	return cmd
   170  }
   171  
   172  func buildCredentialsDeleteCommand(p *porter.Porter) *cobra.Command {
   173  	opts := porter.CredentialDeleteOptions{}
   174  
   175  	cmd := &cobra.Command{
   176  		Use:     "delete NAME",
   177  		Short:   "Delete a Credential",
   178  		Long:    `Delete a named credential set.`,
   179  		Example: `  porter credentials delete github --namespace dev`,
   180  		PreRunE: func(_ *cobra.Command, args []string) error {
   181  			return opts.Validate(args)
   182  		},
   183  		RunE: func(cmd *cobra.Command, _ []string) error {
   184  			return p.DeleteCredential(cmd.Context(), opts)
   185  		},
   186  	}
   187  
   188  	f := cmd.Flags()
   189  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   190  		"Namespace in which the credential set is defined. Defaults to the global namespace.")
   191  
   192  	return cmd
   193  }
   194  
   195  func buildCredentialsShowCommand(p *porter.Porter) *cobra.Command {
   196  	opts := porter.CredentialShowOptions{}
   197  
   198  	cmd := &cobra.Command{
   199  		Use:   "show",
   200  		Short: "Show a Credential",
   201  		Long:  `Show a particular credential set, including all named credentials and their corresponding mappings.`,
   202  		Example: `  porter credential show github --namespace dev
   203    porter credential show prodcluster --output json`,
   204  		PreRunE: func(cmd *cobra.Command, args []string) error {
   205  			return opts.Validate(args)
   206  		},
   207  		RunE: func(cmd *cobra.Command, args []string) error {
   208  			return p.ShowCredential(cmd.Context(), opts)
   209  		},
   210  	}
   211  
   212  	f := cmd.Flags()
   213  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   214  		"Namespace in which the credential set is defined. Defaults to the global namespace.")
   215  	f.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
   216  		"Specify an output format.  Allowed values: plaintext, json, yaml")
   217  
   218  	return cmd
   219  }
   220  
   221  func buildCredentialsCreateCommand(p *porter.Porter) *cobra.Command {
   222  	opts := porter.CredentialCreateOptions{}
   223  
   224  	cmd := &cobra.Command{
   225  		Use:   "create",
   226  		Short: "Create a Credential",
   227  		Long:  "Create a new blank resource for the definition of a Credential Set.",
   228  		Example: `  porter credentials create FILE [--output yaml|json]
   229    porter credentials create credential-set.json
   230    porter credentials create credential-set --output yaml`,
   231  		PreRunE: func(cmd *cobra.Command, args []string) error {
   232  			return opts.Validate(args)
   233  		},
   234  		RunE: func(cmd *cobra.Command, argrs []string) error {
   235  			return p.CreateCredential(cmd.Context(), opts)
   236  		},
   237  	}
   238  
   239  	f := cmd.Flags()
   240  	f.StringVar(&opts.OutputType, "output", "", "Credential set resource file format")
   241  
   242  	return cmd
   243  }