diff --git a/config.go b/config.go index 2890668e..eb1a9a35 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,8 @@ package main import ( "fmt" "os" + "os/exec" + "os/user" "path/filepath" "reflect" "regexp" @@ -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" ) @@ -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 } @@ -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 } @@ -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) + } } } @@ -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} { diff --git a/glide.lock b/glide.lock index ccc83b14..1b051d40 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 532fbc26ba2c52756754517aa5beacd42508fbbb1b6e9d8f54202bfec009053e -updated: 2018-02-23T16:36:10.039215-05:00 +hash: f385734dda5759d8637ecbce7cae33aa5980b1c2e6f5df67a25da5cbe1b5fc18 +updated: 2018-08-14T17:16:00.673938-04:00 imports: - name: github.com/agext/levenshtein version: 5f10fee965225ac1eecdc234c09daf5cd9e7f7b6 @@ -9,6 +9,8 @@ imports: - parse - name: github.com/alecthomas/units version: 2efee857e7cfd4f3d0138cc3cbb1b4966962b93a +- name: github.com/andybalholm/cascadia + version: 901648c87902174f774fac311d7f176f8647bdaa - name: github.com/aokoli/goutils version: 3391d3790d23d03408670993e957e8f408993c34 - name: github.com/apparentlymart/go-cidr @@ -20,9 +22,9 @@ imports: subpackages: - textseg - name: github.com/armon/go-radix - version: 1fca145dffbcaa8fe914309b1ec0cfc67500fe61 + version: 7fddfc383310abc091d79a27f116d30cf0424032 - name: github.com/aws/aws-sdk-go - version: f7f1ee9550cc76ceea731d99c8dbe00a0c39f66d + version: f831d5a0822a1ad72420ab18c6269bca1ddaf490 subpackages: - aws - aws/awserr @@ -34,15 +36,20 @@ imports: - aws/credentials/ec2rolecreds - aws/credentials/endpointcreds - aws/credentials/stscreds + - aws/csm - aws/defaults - aws/ec2metadata - aws/endpoints - aws/request - aws/session - aws/signer/v4 + - internal/sdkio - internal/sdkrand + - internal/sdkuri - internal/shareddefaults - private/protocol + - private/protocol/eventstream + - private/protocol/eventstream/eventstreamapi - private/protocol/json/jsonutil - private/protocol/jsonrpc - private/protocol/query @@ -60,44 +67,54 @@ imports: - name: github.com/bgentry/speakeasy version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd - name: github.com/blang/semver - version: a088618caaf34f5392024fc3386143873033fbea + version: 3c1074078d32d767e08ab2c8564867292da86926 - name: github.com/coveo/gotemplate - version: 539b197bd088fbfd09008ac5edebf363fb374e46 + version: 40ed0a800cccab9868456b03b30bc1914bfc3b12 subpackages: + - collections + - collections/implementation - errors - hcl + - json + - template - utils + - xml + - yaml - name: github.com/drhodes/goLorem version: ecccc744c2d953a1e13cbe5e5fc5d4cbc9b8daeb - name: github.com/fatih/color - version: 507f6050b8568533fb3f5504de8e5205fa62a114 + version: 2d684516a8861da43017284349b7e303e809ac21 - name: github.com/go-errors/errors - version: 3afebba5a48dbc89b574d890b6b34d9ee10b4785 + version: d98b870cc4e05f1545532a80e9909be8216095b6 - name: github.com/go-ini/ini - version: 32e4be5f41bb918afb6e37c07426e2ddbcb6647e + version: d58d458bec3cb5adec4b7ddb41131855eac0b33f +- name: github.com/google/uuid + version: 064e2069ce9c359c118179501254f67d7d37ba24 - name: github.com/gruntwork-io/terragrunt - version: 2516f40111314bf6b4044ccdb2d327fa4c891694 + version: fe4af2d1ec00067d8e473a2ddfb969db3bc85017 repo: https://github.com/coveo/terragrunt.git subpackages: - aws_helper - errors - util - name: github.com/hashicorp/errwrap - version: 7554cd9344cec97297fa6649b055a8c98c2a1e55 + version: d6c0cd88035724dd42e0f335ae30161c20575ecc - name: github.com/hashicorp/go-cleanhttp version: d5fe4b57a186c716b0e00b8c301cbd9b4182694d - name: github.com/hashicorp/go-getter - version: da1332ad370dd543e363ee5ad0098987f60a429a + version: 4bda8fa99001c61db3cad96b421d4c12a81f256d subpackages: - helper/url - name: github.com/hashicorp/go-multierror - version: b7773ae218740a7be65057fc60b366a49b538a44 + version: 3d5d8f294aa03d8e98859feac328afbdf1ae0703 +- name: github.com/hashicorp/go-safetemp + version: b1a1dbde6fdc11e3ae79efd9039009e22d4ae240 - name: github.com/hashicorp/go-uuid - version: 64130c7a86d732268a38cb04cfbaf0cc987fda98 + version: 27454136f0364f2d44b1276c552d69105cf8c498 - name: github.com/hashicorp/go-version - version: 4fe82ae3040f80a03d04d2cccb5606a626b8e1ee + version: 270f2f71b1ee587f3b609f00f422b76a6b28f348 - name: github.com/hashicorp/hcl - version: 23c074d0eceb2b8a5bfdbb271ab780cde70f05a8 + version: ef8a98b0bbce4a65b5aa4c368430a80ddc533168 subpackages: - hcl/ast - hcl/parser @@ -108,7 +125,7 @@ imports: - json/scanner - json/token - name: github.com/hashicorp/hcl2 - version: a42f1fdb237b38714f3bf58a3498fe9706c46981 + version: c79a9bd509c8897b83598ec971ae4db54eab4e85 subpackages: - gohcl - hcl @@ -122,13 +139,14 @@ imports: - parser - scanner - name: github.com/hashicorp/terraform - version: 3802b14260603f90c7a1faf55994dcc8933e2069 + version: 41e50bd32a8825a84535e353c3674af8ce799161 subpackages: - config - config/hcl2shim - config/module - dag - helper/hilmapstructure + - httpclient - plugin/discovery - registry - registry/regsrc @@ -139,7 +157,7 @@ imports: - tfdiags - version - name: github.com/huandu/xstrings - version: 2bf18b218c51864a87384c06996e40ff9dcff8e1 + version: 7bb0250b58e5c15670406e6f93ffda43281305b1 - name: github.com/imdario/mergo version: 7fe0c75c13abdee74b09fcacef5ea1c6bba6a874 - name: github.com/jmespath/go-jmespath @@ -147,17 +165,17 @@ imports: - name: github.com/Masterminds/semver version: 59c29afe1a994eacb71c833025ca7acf874bb1da - name: github.com/Masterminds/sprig - version: 9d9aa1f74c86fd9d36ecfe3f2a44a3093c3d4c15 + version: 77bb58b7f5e10889a1195c21b9e7a96ee166f199 - name: github.com/mattn/go-colorable - version: 5411d3eea5978e6cdc258b30de592b60df6aba96 + version: efa589957cd060542a26d2dd7832fd6a6c6c3ade - name: github.com/mattn/go-isatty - version: 57fdcb988a5c543893cc61bce354a6e24ab70022 + version: 6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c - name: github.com/mitchellh/cli - version: 518dc677a1e1222682f4e7db06721942cb8e9e4c + version: c48282d14eba4b0817ddef3f832ff8d13851aefd - name: github.com/mitchellh/copystructure version: d23ffcb85de31694d6ccaa23ccb4a03e55c1303f - name: github.com/mitchellh/go-homedir - version: b8bc1bf767474819792c23f32d8286a45736f1c6 + version: 58046073cbffe2f25d425fe1331102f55cf719de - name: github.com/mitchellh/go-testing-interface version: a61a99592b77c9ba629d254a693acffaeb4b7e28 - name: github.com/mitchellh/go-wordwrap @@ -165,27 +183,31 @@ imports: - name: github.com/mitchellh/hashstructure version: 2bca23e0e452137f789efbc8610126fd8b94f73b - name: github.com/mitchellh/mapstructure - version: 00c29f56e2386353d58c599509e8dc3801b0d716 + version: f15292f7a699fcc1a38a80977f80a046874ba8ac - name: github.com/mitchellh/reflectwalk version: 63d60e9d0dbc60cf9164e6510889b0db6683d98c - name: github.com/op/go-logging version: 970db520ece77730c7e4724c61121037378659d9 - name: github.com/posener/complete - version: cdc49b71388c2ab059f57997ef2575c9e8b4f146 + version: e037c22b2fcfa85e74495388f03892ed194bba76 subpackages: - cmd - cmd/install - match -- name: github.com/satori/go.uuid - version: f58768cc1a7a7e77a3bd49e98cdd21419399b6a3 +- name: github.com/PuerkitoBio/goquery + version: dc2ec5c7ca4d9aae063b79b9f581dd3ea6afd2b2 +- name: github.com/sergi/go-diff + version: da645544ed44df016359bd4c0e3dc60ee3a0da43 + subpackages: + - diffmatchpatch - name: github.com/ulikunitz/xz - version: 0c6b41e72360850ca4f98dc341fd999726ea007f + version: 636d36a76670e6c700f22fd5f4588679ff2896c4 subpackages: - internal/hash - internal/xlog - lzma - name: github.com/zclconf/go-cty - version: a314db697f753da23560cb1a2a58cf344eff916b + version: 02bd58e97b5759d478019c5a6333edbfdfed16a0 subpackages: - cty - cty/convert @@ -195,7 +217,7 @@ imports: - cty/json - cty/set - name: golang.org/x/crypto - version: 49796115aa4b964c318aad4f3084fdb41e9aa067 + version: de0752318171da717af4ce24d0a2e8626afaeb11 subpackages: - bcrypt - blowfish @@ -208,18 +230,20 @@ imports: - openpgp/s2k - pbkdf2 - scrypt + - ssh/terminal - name: golang.org/x/net - version: cbe0f9307d0156177f9dd5dc85da1a31abc5f2fb + version: c39426892332e1bb5ec0a434a079bf82f5d30c54 subpackages: - html - html/atom - idna - name: golang.org/x/sys - version: 88d2dcc510266da9f7f8c7f34e1940716cab5f5c + version: 98c5dad5d1a0e8a73845ecc8897d0bd56586511d subpackages: - unix + - windows - name: golang.org/x/text - version: 27420a1a391f5504f73155051cd274311bf70883 + version: 6e3c4e7365ddcc329f090f96e4348398f6310088 subpackages: - secure/bidirule - transform @@ -228,5 +252,5 @@ imports: - name: gopkg.in/alecthomas/kingpin.v2 version: 947dcec5ba9c011838740e680966fd7087a71d0d - name: gopkg.in/yaml.v2 - version: 7f97868eec74b32b0982dd158a51a446d1da7eb5 + version: 5420a8b6744d3b0345ab293f6fcba19c978f1183 testImports: [] diff --git a/glide.yaml b/glide.yaml index 0e32396b..7aab90f4 100644 --- a/glide.yaml +++ b/glide.yaml @@ -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