Skip to content

Commit

Permalink
Merge pull request #1 from jaydlowrider/main
Browse files Browse the repository at this point in the history
Adding DRY-RUN option
  • Loading branch information
julienp authored Aug 5, 2021
2 parents fd0b5b5 + a123127 commit db6776d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ make build
PROJECT_ID=<my project id> KUSTOMIZE_PLUGIN_HOME=`pwd`/bin kustomize build --enable_alpha_plugins ./example
```

`DRY_RUN` as env variable to mock the creation of secrets, useful if we don't want this plugin to hit secrets manager.

```bash
make build
DRY_RUN=1 PROJECT_ID=<my project id> KUSTOMIZE_PLUGIN_HOME=`pwd`/bin kustomize build --enable_alpha_plugins ./example
```


This will generate the following secret:

```yaml
Expand Down
24 changes: 19 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"

secretmanager "cloud.google.com/go/secretmanager/apiv1"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
Expand Down Expand Up @@ -48,8 +50,13 @@ func main() {
}
generator := flag.Arg(0)

dry_run, cast_err := strconv.ParseBool(os.Getenv("DRY_RUN"))
if cast_err != nil {
dry_run = false
}

projectId := os.Getenv("PROJECT_ID")
if projectId == "" {
if !dry_run && projectId == "" {
log.Fatal("Expected env PROJECT_ID to be set")
}

Expand All @@ -66,14 +73,21 @@ func main() {
secretData := map[string]string{}
for _, secret := range parsed.Secrets {
name := "projects/" + projectId + "/secrets/" + secret.Name + "/versions/latest"
value, err := accessSecretVersion(name)
if err != nil {
log.Fatalf("Failed to load secret from secret manager: %s", err)
}

var value []byte
key := secret.Key
if key == "" {
key = secret.Name
}
if !dry_run {
value, err = accessSecretVersion(name)
if err != nil {
log.Fatalf("Failed to load secret from secret manager: %s", err)
}
} else {
value = make([]byte, 30)
rand.Read(value)
}
secretData[key] = base64.StdEncoding.EncodeToString(value)
}

Expand Down

0 comments on commit db6776d

Please sign in to comment.