Skip to content

Commit

Permalink
Handling for not existent SSM keys with properly error
Browse files Browse the repository at this point in the history
  • Loading branch information
wobondar committed Jun 14, 2018
1 parent 8f0e9ed commit 6ab8614
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
43 changes: 14 additions & 29 deletions src/command_dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
Expand Down Expand Up @@ -212,7 +211,7 @@ func (c *DotEnvCommand) processDotEnv() error {
j = len(names)
}

values, err := c.getParameters(names[i:j], c.decrypt)
values, err := c.getParameters(names[i:j], c.decrypt, ssmVars[i:j])
if err != nil {
return err
}
Expand All @@ -228,7 +227,7 @@ func (c *DotEnvCommand) processDotEnv() error {
return nil
}

func (c *DotEnvCommand) getParameters(names []string, decrypt bool) (map[string]string, error) {
func (c *DotEnvCommand) getParameters(names []string, decrypt bool, ssmVars []ssmVar) (map[string]string, error) {
values := make(map[string]string)

input := &ssm.GetParametersInput{
Expand All @@ -242,35 +241,21 @@ func (c *DotEnvCommand) getParameters(names []string, decrypt bool) (map[string]
resp, err := c.ssm.GetParameters(input)
c.log.must(err)

if len(resp.InvalidParameters) > 0 {
return values, newInvalidParametersError(resp)
}

for _, p := range resp.Parameters {
values[*p.Name] = *p.Value
}

return values, nil
}

type invalidParametersError struct {
InvalidParameters []string
}

func newInvalidParametersError(resp *ssm.GetParametersOutput) *invalidParametersError {
e := new(invalidParametersError)
for _, p := range resp.InvalidParameters {
if p == nil {
continue
for _, v := range ssmVars {
for _, p := range resp.Parameters {
if strings.EqualFold(v.parameter, *p.Name) {
values[v.parameter] = *p.Value
}
}
for _, p := range resp.InvalidParameters {
if strings.EqualFold(v.parameter, *p) {
values[v.parameter] = "VALUE_NOT_EXISTS"
c.log.Warning("Value for parameter: %s not exists in AWS Parameter Store. Environment variable: %s", v.parameter, v.envVar)
}
}

e.InvalidParameters = append(e.InvalidParameters, *p)
}
return e
}

func (e *invalidParametersError) Error() string {
return fmt.Sprintf("invalid parameters: %v", e.InvalidParameters)
return values, nil
}

func (c *DotEnvCommand) parameter(k, v string) (*string, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

const Version = "v0.2.2"
const Version = "v0.3.0"

const CiEnvVar = "CI"
const TerraformEnvVar = "TF_ENV"
Expand Down

0 comments on commit 6ab8614

Please sign in to comment.