Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

cli: prevent empty environment variable keys #3112

Merged
merged 1 commit into from Jul 13, 2016

Conversation

slurms
Copy link
Contributor

@slurms slurms commented Jul 13, 2016

Fixes #3029. Not sure if this is at all correct... first attempt at go :).

@slurms slurms force-pushed the validate_env_vars_before_release branch from 8a4a8ba to 2b06c63 Compare July 13, 2016 09:00
@@ -63,6 +63,15 @@ func (r *ReleaseRepo) Add(data interface{}) error {
release.ArtifactIDs = []string{release.LegacyArtifactID}
}

for key := range release.Env {
if key == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since release.Env is a map, you can query for the key directly:

if _, ok := release.Env[""]; ok {
    // empty key exists
    return ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I'll do that instead.

@slurms slurms force-pushed the validate_env_vars_before_release branch from 2b06c63 to c2e75b6 Compare July 13, 2016 10:27
@slurms
Copy link
Contributor Author

slurms commented Jul 13, 2016

Not sure what those test failures are caused by :/

@josephglanville
Copy link
Contributor

@slurms those failures are unrelated, your changes aren't to blame.
Unfortunately some flakiness has crept into the test suite, we are working on making it green again. 😄

if _, ok := release.Env[""]; ok {
return ct.ValidationError{
Field: "env",
Message: "you can't create an env var with an empty key",
Copy link
Contributor

Choose a reason for hiding this comment

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

Given this is usually done by mistake, it would be useful to include the value in the response, for example:

$ flynn env set =BAR
validation_error: env you can't create an env var with an empty key (tried to set ""="BAR")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yep -- seems like a more helpful message 👍

@slurms slurms force-pushed the validate_env_vars_before_release branch from c2e75b6 to 507d883 Compare July 13, 2016 13:27
if _, ok := release.Env[""]; ok {
return ct.ValidationError{
Field: "env",
Message: fmt.Sprintf("you can't create an env var with an empty key (tried to set \"\"=\"%s\")", release.env[""]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the if statement so that it assigns the value to a variable and then use it here (rather than using release.Env[""] again):

if value, ok := release.Env[""]; ok {
    // ... can reference `value` here
}

You can also use %q rather than \"%s\" to double-quote the variable in the output string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, sorry about the small issues (and thanks for reviewing)

Signed-off-by: Nick Sandford <nick@sandford.id.au>
@slurms slurms force-pushed the validate_env_vars_before_release branch from 507d883 to d3c60a4 Compare July 13, 2016 13:40
@lmars
Copy link
Contributor

lmars commented Jul 13, 2016

LGTM

@lmars lmars merged commit 228cb2e into flynn:master Jul 13, 2016
@lmars
Copy link
Contributor

lmars commented Jul 13, 2016

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants