Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apply command for creds and params #1715

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cmd/porter/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func buildCredentialsCommands(p *porter.Porter) *cobra.Command {
Short: "Credentials commands",
}

cmd.AddCommand(buildCredentialsApplyCommand(p))
cmd.AddCommand(buildCredentialsEditCommand(p))
cmd.AddCommand(buildCredentialsGenerateCommand(p))
cmd.AddCommand(buildCredentialsListCommand(p))
Expand All @@ -22,6 +23,36 @@ func buildCredentialsCommands(p *porter.Porter) *cobra.Command {
return cmd
}

func buildCredentialsApplyCommand(p *porter.Porter) *cobra.Command {
opts := porter.ApplyOptions{}

cmd := &cobra.Command{
Use: "apply FILE",
Short: "Apply changes to a credential set",
Long: `Apply changes from the specified file to a credential set. If the credential set doesn't already exist, it is created.

Supported file extensions: json and yaml.

You can use the generate and show commands to create the initial file:
porter credentials generate mycreds --reference SOME_BUNDLE
porter credentials show mycreds --output yaml > mycreds.yaml
`,
Example: ` porter credentials apply --file mycreds.yaml`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(p.Context, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.CredentialsApply(opts)
},
}

f := cmd.Flags()
f.StringVarP(&opts.Namespace, "namespace", "n", "",
"Namespace in which the credential set is defined. The namespace in the file, if set, takes precedence.")

return cmd
}

func buildCredentialsEditCommand(p *porter.Porter) *cobra.Command {
opts := porter.CredentialEditOptions{}

Expand Down
31 changes: 31 additions & 0 deletions cmd/porter/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func buildParametersCommands(p *porter.Porter) *cobra.Command {
Short: "Parameter set commands",
}

cmd.AddCommand(buildParametersApplyCommand(p))
cmd.AddCommand(buildParametersEditCommand(p))
cmd.AddCommand(buildParametersGenerateCommand(p))
cmd.AddCommand(buildParametersListCommand(p))
Expand All @@ -22,6 +23,36 @@ func buildParametersCommands(p *porter.Porter) *cobra.Command {
return cmd
}

func buildParametersApplyCommand(p *porter.Porter) *cobra.Command {
opts := porter.ApplyOptions{}

cmd := &cobra.Command{
Use: "apply FILE",
Short: "Apply changes to a parameter set",
Long: `Apply changes from the specified file to a parameter set. If the parameter set doesn't already exist, it is created.

Supported file extensions: json and yaml.

You can use the generate and show commands to create the initial file:
porter parameters generate myparams --reference SOME_BUNDLE
porter parameters show myparams --output yaml > myparams.yaml
`,
Example: ` porter parameters apply --file myparams.yaml`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be porter parameters apply myparams.yaml, right? If so, same update needed for creds.

This is a good time to mention: thoughts on using a -f/--file flag to pass in filename vs positional arg? I'm sure you'd thought of it. Endorsement for the former is that it'd be natural for users of kubectl -- but that doesn't nec. mean it's the right way to go for Porter. No strong opinion here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had strongly considered the -f flag (as you can see since I forgot to fix that example). I like to use a positional argument when the argument is required, is always specified, and is the object of the (command) sentence.

The file parameter fits all those characteristics and that is how the other commands in porter act as well (such as install). Having file be a positional argument is more consistent with porter itself, which I feel is more important that being consistent with kubectl.

Also there aren't a lot of other arguments that would make someone likely to be unsure what the positional argument should be. At most you are specifying porter installation apply FILE --namespace and to me namespace falls into the same category as say debug, or output. It's a general modifier for a bunch of commands, but it isn't the focus of the command, and therefore is unlikely to be accidentally specified as the positional argument.

If we think that people will instinctively type -f FILE from muscle memory, we can do what we do for the help text (which is supported through --help and a help subcommand). We could add a hidden file flag and bind it, so that either way works, but we document the way that matches the rest of porter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that I should add that hidden flag?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call! I'd say we proceed w/o but if it's trivially easy to add... maybe so :)

PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(p.Context, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.ParametersApply(opts)
},
}

f := cmd.Flags()
f.StringVarP(&opts.Namespace, "namespace", "n", "",
"Namespace in which the parameter set is defined. The namespace in the file, if set, takes precedence.")

return cmd
}

func buildParametersEditCommand(p *porter.Porter) *cobra.Command {
opts := porter.ParameterEditOptions{}

Expand Down
1 change: 1 addition & 0 deletions docs/content/cli/credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Credentials commands
### SEE ALSO

* [porter](/cli/porter/) - I am porter 👩🏽‍✈️, the friendly neighborhood CNAB authoring tool
* [porter credentials apply](/cli/porter_credentials_apply/) - Apply changes to a credential set
* [porter credentials delete](/cli/porter_credentials_delete/) - Delete a Credential
* [porter credentials edit](/cli/porter_credentials_edit/) - Edit Credential
* [porter credentials generate](/cli/porter_credentials_generate/) - Generate Credential Set
Expand Down
49 changes: 49 additions & 0 deletions docs/content/cli/credentials_apply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: "porter credentials apply"
slug: porter_credentials_apply
url: /cli/porter_credentials_apply/
---
## porter credentials apply

Apply changes to a credential set

### Synopsis

Apply changes from the specified file to a credential set. If the credential set doesn't already exist, it is created.

Supported file extensions: json and yaml.

You can use the generate and show commands to create the initial file:
porter credentials generate mycreds --reference SOME_BUNDLE
porter credentials show mycreds --output yaml > mycreds.yaml


```
porter credentials apply FILE [flags]
```

### Examples

```
porter credentials apply --file mycreds.yaml
```

### Options

```
-h, --help help for apply
-n, --namespace string Namespace in which the credential set is defined. The namespace in the file, if set, takes precedence.
```

### Options inherited from parent commands

```
--debug Enable debug logging
--debug-plugins Enable plugin debug logging
--experimental strings Comma separated list of experimental features to enable. See https://porter.sh/configuration/#experimental-feature-flags for available feature flags.
```

### SEE ALSO

* [porter credentials](/cli/porter_credentials/) - Credentials commands

1 change: 1 addition & 0 deletions docs/content/cli/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Parameter set commands
### SEE ALSO

* [porter](/cli/porter/) - I am porter 👩🏽‍✈️, the friendly neighborhood CNAB authoring tool
* [porter parameters apply](/cli/porter_parameters_apply/) - Apply changes to a parameter set
* [porter parameters delete](/cli/porter_parameters_delete/) - Delete a Parameter Set
* [porter parameters edit](/cli/porter_parameters_edit/) - Edit Parameter Set
* [porter parameters generate](/cli/porter_parameters_generate/) - Generate Parameter Set
Expand Down
49 changes: 49 additions & 0 deletions docs/content/cli/parameters_apply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: "porter parameters apply"
slug: porter_parameters_apply
url: /cli/porter_parameters_apply/
---
## porter parameters apply

Apply changes to a parameter set

### Synopsis

Apply changes from the specified file to a parameter set. If the parameter set doesn't already exist, it is created.

Supported file extensions: json and yaml.

You can use the generate and show commands to create the initial file:
porter parameters generate myparams --reference SOME_BUNDLE
porter parameters show myparams --output yaml > myparams.yaml


```
porter parameters apply FILE [flags]
```

### Examples

```
porter parameters apply --file myparams.yaml
```

### Options

```
-h, --help help for apply
-n, --namespace string Namespace in which the parameter set is defined. The namespace in the file, if set, takes precedence.
```

### Options inherited from parent commands

```
--debug Enable debug logging
--debug-plugins Enable plugin debug logging
--experimental strings Comma separated list of experimental features to enable. See https://porter.sh/configuration/#experimental-feature-flags for available feature flags.
```

### SEE ALSO

* [porter parameters](/cli/porter_parameters/) - Parameter set commands

5 changes: 5 additions & 0 deletions docs/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ You may set a default value for a configuration value in the config file,
override it in a shell session with an environment variable and then override
both in a particular command with a flag.

* [Set Current Namespace](#namespace)
* [Enable Debug Output](#debug)
* [Debug Plugins](#debug-plugins)
* [Output Formatting](#output)
* [Allow Docker Host Access](#allow-docker-host-access)

## Flags

### Namespace
`--namespace` specifies the current namespace.

### Debug

`--debug` is a flag that is understood not only by the porter client but also the
Expand Down Expand Up @@ -86,6 +90,7 @@ Below is an example configuration file in TOML

**~/.porter/config.toml**
```toml
namespace = "dev"
debug = true
debug-plugins = true
output = "json"
Expand Down
26 changes: 18 additions & 8 deletions pkg/cnab/provider/credentials.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cnabprovider

import (
"encoding/json"
"path/filepath"
"strings"

"get.porter.sh/porter/pkg/credentials"
"get.porter.sh/porter/pkg/encoding"
"get.porter.sh/porter/pkg/secrets"
"get.porter.sh/porter/pkg/storage"
"github.com/cnabio/cnab-go/bundle"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
)

Expand All @@ -26,8 +28,21 @@ func (r *Runtime) loadCredentials(b bundle.Bundle, args ActionArguments) (secret
if r.isPathy(name) {
cset, err = r.loadCredentialFromFile(name)
} else {
cset, err = r.credentials.GetCredentialSet(args.Namespace, name)
// Try to get the creds in the local namespace first, fallback to the global creds
query := storage.FindOptions{
Sort: []string{"-namespace"},
Filter: bson.M{
"name": name,
"$or": []bson.M{
{"namespace": ""},
{"namespace": args.Namespace},
},
},
}
store := r.credentials.GetDataStore()
err = store.FindOne(credentials.CollectionCredentials, query, &cset)
}

if err != nil {
return nil, err
}
Expand All @@ -50,12 +65,7 @@ func (r *Runtime) isPathy(name string) bool {
}

func (r *Runtime) loadCredentialFromFile(path string) (credentials.CredentialSet, error) {
data, err := r.FileSystem.ReadFile(path)
if err != nil {
return credentials.CredentialSet{}, errors.Wrapf(err, "could not read file %s", path)
}

var cs credentials.CredentialSet
err = json.Unmarshal(data, &cs)
err := encoding.UnmarshalFile(r.FileSystem, path, &cs)
return cs, errors.Wrapf(err, "error loading credential set in %s", path)
}
4 changes: 4 additions & 0 deletions pkg/credentials/credential_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (s CredentialStore) Initialize() error {
return err
}

func (s CredentialStore) GetDataStore() storage.Store {
return s.Documents
}

/*
Secrets
*/
Expand Down
6 changes: 5 additions & 1 deletion pkg/credentials/provider.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package credentials

import "get.porter.sh/porter/pkg/secrets"
import (
"get.porter.sh/porter/pkg/secrets"
"get.porter.sh/porter/pkg/storage"
)

// Provider is Porter's interface for managing and resolving credentials.
type Provider interface {
GetDataStore() storage.Store
ResolveAll(creds CredentialSet) (secrets.Set, error)
Validate(creds CredentialSet) error
InsertCredentialSet(creds CredentialSet) error
Expand Down
4 changes: 4 additions & 0 deletions pkg/parameters/parameter_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (s ParameterStore) Initialize() error {
return err
}

func (s ParameterStore) GetDataStore() storage.Store {
return s.Documents
}

func (s ParameterStore) ResolveAll(params ParameterSet) (secrets.Set, error) {
resolvedParams := make(secrets.Set)
var resolveErrors error
Expand Down
3 changes: 3 additions & 0 deletions pkg/parameters/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package parameters

import (
"get.porter.sh/porter/pkg/secrets"
"get.porter.sh/porter/pkg/storage"
)

// Provider interface for managing sets of parameters.
type Provider interface {
GetDataStore() storage.Store

// ResolveAll parameter values in the parameter set.
ResolveAll(params ParameterSet) (secrets.Set, error)

Expand Down
Loading