-
Notifications
You must be signed in to change notification settings - Fork 564
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dist/ | ||
.idea/ | ||
helmfile | ||
helmfile | ||
vendor |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we could support multiple paths. Perhaps use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
region: us-west-2
prefix: /path/in/ssm The idea is then the prefix is optional when you start using ssm directly
instead of
But of course you can do something like..
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For greater interoperability with |
||
value = strings.Replace(value, "\n", "\\n", -1) | ||
|
||
os.Setenv(key, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() }
?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.☺️