Skip to content

Commit

Permalink
add parameter store option to get env values
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Biffi committed Nov 24, 2023
1 parent ca458ad commit bb7f28b
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 10 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![GitHub release](https://img.shields.io/github/release/leonardobiffi/envctl.svg?style=flat)
![GitHub](https://img.shields.io/github/license/leonardobiffi/envctl.svg?style=flat)

A simple CLI tool to run a process with secrets from AWS Secrets Manager
A simple CLI tool to run a process with secrets from AWS Secrets Manager or AWS Parameter Store

forked from [pratishshr/envault](https://github.com/pratishshr/envault)

Expand Down Expand Up @@ -113,6 +113,15 @@ envctl list --secret=api/dev
envctl list --secret=api/uat
```

You also can list environments from AWS Parameter Store

```shell
envctl list --parameter=api/dev
```
```shell
envctl list --parameter=api/uat
```

### 6. Update secrets

This will update secrets with content in .env file
Expand All @@ -121,6 +130,11 @@ This will update secrets with content in .env file
envctl update --secret=/dev/service/app --envfile .env
```

Or update secret on Parameter Store
```shell
envctl update --parameter=/dev/service/app --envfile .env
```

### 7. Run with secrets

```shell
Expand All @@ -146,7 +160,7 @@ Instead of setting up a `~/.aws/credentials` file. You can also use the followin
| AWS_REGION | AWS region where you added your secret|
| ENVIRONMENT | Environment which you set in envctl.json |
| SECRET_NAME | AWS Secret Name |

| PARAMETER_NAME | AWS Parameter Store Path |

### 9. Using custom .env files
If you want to inject environment keys from a file instead of using AWS Secrets Manager. You can use the`-ef` flag.
Expand All @@ -155,7 +169,6 @@ If you want to inject environment keys from a file instead of using AWS Secrets
envctl run 'envctl run 'go run main.go' -ef env/staging.env
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
26 changes: 23 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Info struct {
// Initialize and bootstrap the CLI.
func Initialize(info *Info) error {
var secretName string
var parameterPath string
var env string
var region string
var profile string
Expand All @@ -41,6 +42,11 @@ func Initialize(info *Info) error {
Usage: "Secret's Name to fetch environment from",
Destination: &secretName,
},
cli.StringFlag{
Name: "parameter, ps",
Usage: "Parameter Store Path to fetch environment from",
Destination: &parameterPath,
},
cli.StringFlag{
Name: "env, e",
Usage: "Environment to use the secret name from",
Expand Down Expand Up @@ -79,10 +85,17 @@ func Initialize(info *Info) error {
},
{
Name: "list",
Usage: "List environment variables stored in Secrets Manager",
Usage: "List environment variables stored in Secrets Manager or Parameter Store",
Flags: flags,
Action: func(ctx *cli.Context) error {
List(secretName, env, region, profile, envFile, upper)
if secretName != "" {
GetSecrets(secretName, env, region, profile, envFile, upper)
return nil
}
if parameterPath != "" {
GetParameters(parameterPath, env, region, profile, envFile)
return nil
}
return nil
},
},
Expand All @@ -91,7 +104,14 @@ func Initialize(info *Info) error {
Usage: "Update environment variables from env file to Secrets Manager",
Flags: flags,
Action: func(ctx *cli.Context) error {
Update(secretName, env, region, profile, envFile)
if secretName != "" {
UpdateSecrets(secretName, env, region, profile, envFile)
return nil
}
if parameterPath != "" {
UpdateParameters(parameterPath, env, region, profile, envFile)
return nil
}
return nil
},
},
Expand Down
21 changes: 17 additions & 4 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/leonardobiffi/envctl/internal/cli/setup"
"github.com/leonardobiffi/envctl/internal/parameters"
"github.com/leonardobiffi/envctl/internal/secrets"
"github.com/leonardobiffi/envctl/util/shell"
"github.com/leonardobiffi/envctl/util/system/exit"
Expand All @@ -24,8 +25,8 @@ func Run(secretName string, command string, env string, region string, profile s
shell.Execute(command, secrets.GetSecrets(secretName, env, region, profile, envFile))
}

// List all environment from Secrets Manager
func List(secretName string, env string, region string, profile string, envFile string, upper bool) {
// Get all environment from Secrets Manager
func GetSecrets(secretName string, env string, region string, profile string, envFile string, upper bool) {
for key, value := range secrets.GetSecrets(secretName, env, region, profile, envFile) {
if upper {
key = strings.ToUpper(key)
Expand All @@ -35,7 +36,19 @@ func List(secretName string, env string, region string, profile string, envFile
}
}

// Update all environment from env file to Secrets Manager
func Update(secretName string, env string, region string, profile string, envFile string) {
// Get all environment from Parameters Store
func GetParameters(path string, env string, region string, profile string, envFile string) {
for key, value := range parameters.GetParameters(path, env, region, profile, envFile) {
fmt.Println(key + "=" + value)
}
}

// UpdateSecrets all environment from env file to Secrets Manager
func UpdateSecrets(secretName string, env string, region string, profile string, envFile string) {
secrets.UpdateSecrets(secretName, env, region, profile, envFile)
}

// UpdateParameters all environment from env file to Parameters Store
func UpdateParameters(path string, env string, region string, profile string, envFile string) {
parameters.UpdateParameters(path, env, region, profile, envFile)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/urfave/cli v1.20.0
)

require github.com/aws/aws-sdk-go-v2/service/ssm v1.43.1

require (
github.com/aws/aws-sdk-go-v2/credentials v1.16.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.5 // indirect
Expand All @@ -23,6 +25,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.20.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.25.4 // indirect
github.com/aws/smithy-go v1.17.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.4 h1:rdovz3rEu
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.4/go.mod h1:aYCGNjyUCUelhofxlZyj63srdxWUSsBSGg5l6MCuXuE=
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.23.3 h1:NurfTBFmaehSiWMv5drydRWs3On0kwoBe1gWYFt+5ws=
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.23.3/go.mod h1:LDD9wCQ1tvjMIWEIFPvZ8JgJsEOjded+X5jav9tD/zg=
github.com/aws/aws-sdk-go-v2/service/ssm v1.43.1 h1:QCZGFHZnzP0yRveI5X+5Cu54wdvpbgiuF3Qy3xBykyA=
github.com/aws/aws-sdk-go-v2/service/ssm v1.43.1/go.mod h1:Iw3+XCa7ARZWsPiV3Zozf5Hb3gD7pHDLKu9Xcc4iwDM=
github.com/aws/aws-sdk-go-v2/service/sso v1.17.3 h1:CdsSOGlFF3Pn+koXOIpTtvX7st0IuGsZ8kJqcWMlX54=
github.com/aws/aws-sdk-go-v2/service/sso v1.17.3/go.mod h1:oA6VjNsLll2eVuUoF2D+CMyORgNzPEW/3PyUdq6WQjI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.20.1 h1:cbRqFTVnJV+KRpwFl76GJdIZJKKCdTPnjUZ7uWh3pIU=
Expand All @@ -39,6 +41,10 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down Expand Up @@ -85,5 +91,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
109 changes: 109 additions & 0 deletions internal/parameters/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package parameters

import (
"os"

"github.com/joho/godotenv"
"github.com/leonardobiffi/envctl/config"
"github.com/leonardobiffi/envctl/platform/aws"
"github.com/leonardobiffi/envctl/util/system/exit"
)

func GetParameters(path string, env string, region string, profile string, envFile string) map[string]string {
if envFile != "" {
parameters, err := godotenv.Read(envFile)

if err != nil {
exit.Error("Could not read env file " + envFile)
}

return parameters
}

conf := config.GetConfig()

if env == "" {
env = os.Getenv("ENVIRONMENT")
}

if env == "" {
env = conf.DefaultEnvironment
}

if path == "" {
path = os.Getenv("PARAMETER_NAME")
}

if path == "" && env == "" {
exit.Error("Parameter Name is required to list environments. Set -parameter flag.")
}

if path == "" && env != "" {
if _, ok := conf.Environments[env]; !ok {
exit.Error("Environment '" + env + "' does not exist.")
}

path = conf.Environments[env]
}

if region == "" {
region = conf.Region
}

if profile == "" {
profile = conf.Profile
}

return aws.GetParameters(profile, region, path)
}

// UpdateParameters sets appropriate config and updates parameters on aws.
func UpdateParameters(parameterPath string, env string, region string, profile string, envFile string) {
if envFile == "" {
exit.Error("Env file is required to update parameters. Set --envfile flag.")
}

if envFile != "" {
parameters, err := godotenv.Read(envFile)

if err != nil {
exit.Error("Could not read env file " + envFile)
}

conf := config.GetConfig()

if env == "" {
env = os.Getenv("ENVIRONMENT")
}

if env == "" {
env = conf.DefaultEnvironment
}

if parameterPath == "" {
parameterPath = os.Getenv("PARAMETER_NAME")
}

if parameterPath == "" && env == "" {
exit.Error("Parameter Path is required to list environments. Set -parameter flag.")
}

if parameterPath == "" && env != "" {
if _, ok := conf.Environments[env]; !ok {
exit.Error("Environment '" + env + "' does not exist.")
}

parameterPath = conf.Environments[env]
}

if region == "" {
region = conf.Region
}

if profile == "" {
profile = conf.Profile
}

aws.UpdateParameters(profile, region, parameterPath, parameters)
}
}
Loading

0 comments on commit bb7f28b

Please sign in to comment.