Skip to content

Commit

Permalink
Restore AWS environment detection (#24)
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
jocgir authored Aug 14, 2018
1 parent fa57371 commit 2c9c42c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 55 deletions.
63 changes: 44 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"regexp"
Expand All @@ -12,9 +14,8 @@ import (
"time"

"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/blang/semver"
"github.com/coveo/gotemplate/utils"
"github.com/coveo/gotemplate/collections"
"github.com/gruntwork-io/terragrunt/aws_helper"
)

Expand Down Expand Up @@ -109,12 +110,7 @@ func (config TGFConfig) String() (result string) {

// InitAWS tries to open an AWS session and init AWS environment variable on success
func (config *TGFConfig) InitAWS(profile string) error {
session, err := aws_helper.InitAwsSession(profile)
if err != nil {
return err
}

_, err = sts.New(session).GetCallerIdentity(&sts.GetCallerIdentityInput{})
_, err := aws_helper.InitAwsSession(profile)
if err != nil {
return err
}
Expand All @@ -138,7 +134,7 @@ func (config *TGFConfig) SetDefaultValues() {
if debug {
printfDebug(os.Stderr, "# Reading configuration from %s\n", configFile)
}
if err := utils.LoadData(configFile, &content); err != nil {
if err := collections.LoadData(configFile, &content); err != nil {
fmt.Fprintln(os.Stderr, errorString("Error while loading configuration file %s\nConfiguration file must be valid YAML, JSON or HCL", configFile))
continue
}
Expand Down Expand Up @@ -194,16 +190,18 @@ func (config *TGFConfig) SetDefaultValues() {
apply(content)
}

// If we need to read the parameter store, we must init the session first to ensure that
// the credentials are only initialized once (avoiding asking multiple time the MFA)
if err := config.InitAWS(""); err != nil {
fmt.Fprintln(os.Stderr, errorString("Unable to authentify to AWS: %v\nPararameter store is ignored\n", err))
} else {
if debug {
printfDebug(os.Stderr, "# Reading configuration from AWS parameter store %s\n", parameterFolder)
}
for _, parameter := range Must(aws_helper.GetSSMParametersByPath(parameterFolder, "")).([]*ssm.Parameter) {
config.SetValue((*parameter.Name)[len(parameterFolder)+1:], *parameter.Value)
if awsConfigExist() {
// If we need to read the parameter store, we must init the session first to ensure that
// the credentials are only initialized once (avoiding asking multiple time the MFA)
if err := config.InitAWS(""); err != nil {
fmt.Fprintln(os.Stderr, errorString("Unable to authentify to AWS: %v\nPararameter store is ignored\n", err))
} else {
if debug {
printfDebug(os.Stderr, "# Reading configuration from AWS parameter store %s\n", parameterFolder)
}
for _, parameter := range Must(aws_helper.GetSSMParametersByPath(parameterFolder, "")).([]*ssm.Parameter) {
config.SetValue((*parameter.Name)[len(parameterFolder)+1:], *parameter.Value)
}
}
}

Expand Down Expand Up @@ -395,6 +393,33 @@ func (config *TGFConfig) apply(key, value string) {
}
}

// Check if there is an AWS configuration available.
//
// We call this function before trying to init an AWS session. This avoid trying to init a session in a non AWS context
// and having to wait for metadata resolution or generating an error.
func awsConfigExist() bool {
if os.Getenv("AWS_PROFILE")+os.Getenv("AWS_ACCESS_KEY_ID")+os.Getenv("AWS_CONFIG_FILE") != "" {
// If any AWS identification variable is defined, we consider that we are in an AWS environment.
return true
}

if _, err := exec.LookPath("aws"); err != nil {
// If aws program is installed, we also consider that we are in an AWS environment.
return true
}

// Otherwise, we check if the current user has a folder named .aws defined under its home directory.
currentUser, err := user.Current()
if err != nil {
return false
}
awsFolder, err := os.Stat(filepath.Join(currentUser.HomeDir, ".aws"))
if err != nil {
return false
}
return awsFolder.IsDir()
}

// Return the list of configuration files found from the current working directory up to the root folder
func findConfigFiles(folder string) (result []string) {
for _, file := range []string{userConfigFile, configFile} {
Expand Down
94 changes: 59 additions & 35 deletions glide.lock

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

1 change: 0 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ owners:
import:
- package: github.com/gruntwork-io/terragrunt
repo: https://github.com/coveo/terragrunt.git
version: v0.12.27-coveo.3
- package: github.com/go-errors/errors
- package: github.com/op/go-logging

0 comments on commit 2c9c42c

Please sign in to comment.