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

Added SSM integration #569

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
.idea/
helmfile
helmfile
vendor
51 changes: 51 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/roboll/helmfile/args"
"github.com/roboll/helmfile/cmd"
"github.com/roboll/helmfile/helmexec"
"github.com/roboll/helmfile/pkg/app"
"github.com/roboll/helmfile/ssm"
"github.com/roboll/helmfile/state"
"github.com/urfave/cli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"strings"
)

var Version string
Expand Down Expand Up @@ -569,6 +571,7 @@ Do you really want to delete?
},
}

ssm.Run()
Copy link
Collaborator

Choose a reason for hiding this comment

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

You've brought me a very good idea making environment variables as the interface between helmfile and kv-backend like ssm. Good job.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can merge this as an experimental feature and start implementing more like this once you wrap behind a feature flag. Perhaps something like if _, exists := os.LookupEnv("SSM_PATH"); exists { ssm.Run() }?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree this would be nice to have the ssm.Run not triggerd everytime helmfile is launched and you don't care about ssm at all.

Copy link
Author

Choose a reason for hiding this comment

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

@sgandon @mumoshu look at the second line of the ssm.Run() function. It does that check already. I just didn’t want to make the main function messy with a bunch of checks.

Copy link
Author

Choose a reason for hiding this comment

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

But also, I don’t mind just working on that more flexible solution proposed above. Like, we can keep this PR open (but on hold) just in case I hit a snag with that code implementation.

I really just wanted to throw something together here to start the conversation so we could structure something awesome together. ☺️

err := cliApp.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
Expand Down
117 changes: 117 additions & 0 deletions ssm/ssm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package ssm

import (
"errors"
"fmt"
"os"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/roboll/helmfile/helmexec"
"go.uber.org/zap"
)

var (
svcSSM *ssm.SSM
ssmPath string
logger *zap.SugaredLogger
)

func Run() {
logger = helmexec.NewLogger(os.Stdout, "debug")
ssmPath, exists := os.LookupEnv("SSM_PATH")
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we could support multiple paths. Perhaps use ; delimiter like other PATH vars

Choose a reason for hiding this comment

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

Definitely think we should have the ability to support multiple paths.

Would you rather define each path in the environment? Or just directly in the helmfile? I think it makes more sense to just put right in the helmfile.

Copy link
Author

@rms1000watt rms1000watt May 2, 2019

Choose a reason for hiding this comment

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

@lanmalkieri I'm looking at updating the helmfile.yml spec to include ssm: at the root level of the YAML.

ssm:
  region: us-west-2
  prefix: /path/in/ssm

The idea is then the prefix is optional when you start using ssm directly

{{ ssm "postgres_pass" }}

instead of

{{ ssm "/path/in/ssm/postgres_pass" }}

But of course you can do something like..

{{ ssm "/path/one/postgres_pass" }}
{{ ssm "/path/two/redis_pass" }}

if !exists {
return
}

if strings.TrimSpace(ssmPath) == "" {
ssmPath = "/"
}

logger.Debugf("Attempting to populate environment with SSM values at path: %s", ssmPath)
if err := configureAWS(); err != nil {
logger.Error("Failed to configure AWS")
return
}

fmt.Println("SSM path:", ssmPath)
getSet(ssmPath, "")
}

func configureAWS() (err error) {
awsRegion := os.Getenv("AWS_REGION")
awsDefaultRegion := os.Getenv("AWS_DEFAULT_REGION")

if strings.TrimSpace(awsRegion) == "" && strings.TrimSpace(awsDefaultRegion) == "" {
logger.Debug("ERROR: $AWS_REGION && $AWS_DEFAULT_REGION are empty (need 1 exported). Unable to set SSM parameters")
err = errors.New("Bad region env vars")
return
}

region := awsDefaultRegion
if strings.TrimSpace(awsRegion) != "" {
region = awsRegion
}
cfg := aws.NewConfig().WithRegion(region)

sess := session.New(cfg)
svcSSM = ssm.New(sess)

return
}

func getSet(ssmPath, nextToken string) {
in := &ssm.GetParametersByPathInput{
Path: &ssmPath,
WithDecryption: aws.Bool(true),
}

if nextToken != "" {
in.SetNextToken(nextToken)
}

out, err := svcSSM.GetParametersByPath(in)
if err != nil {
logger.Error("Failed getting parameter by path:", err)
return
}

for _, parameter := range out.Parameters {
setParameter(ssmPath, parameter)
}

if out.NextToken != nil {
getSet(ssmPath, *out.NextToken)
}

}

func setParameter(ssmPath string, parameter *ssm.Parameter) {
if parameter == nil {
logger.Error("SSM parameter is nil")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is more of a warning or debug.

return
}

name := ""
if parameter.Name != nil {
name = *parameter.Name
}

value := ""
if parameter.Value != nil {
value = *parameter.Value
}

length := len(ssmPath)
if len(ssmPath) == 1 {
length = 0
}

key := strings.Replace(strings.Trim(name[length:], "/"), "/", "_", -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

For greater interoperability with chamber which lowercases all parameters, it would be nice if there was an option to uppercase them for environment variables.

value = strings.Replace(value, "\n", "\\n", -1)

os.Setenv(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe warn if overwriting an existing variable

logger.Debugf("Setenv: key=%s", key)
}