Skip to content

Commit

Permalink
Add apply command for creds and params (#1715)
Browse files Browse the repository at this point in the history
* Add apply command for creds and params

Add a new command for credential and parameter sets which allow you to
apply changes (or create net new) from a file.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Fix exmaple

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs authored Aug 13, 2021
1 parent 086622d commit f801cc8
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 24 deletions.
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 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 myparams.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.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 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 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

0 comments on commit f801cc8

Please sign in to comment.