(AWSProfile string, version string, AwsMfaToken string)
| 114 | } |
| 115 | |
| 116 | func AWSConfigFileLoader(AWSProfile string, version string, AwsMfaToken string) aws.Config { |
| 117 | // Loads the AWS config file and returns a config object |
| 118 | |
| 119 | var cfg aws.Config |
| 120 | var err error |
| 121 | // cacheKey := fmt.Sprintf("AWSConfigFileLoader-%s", AWSProfile) |
| 122 | // cached, found := Cache.Get(cacheKey) |
| 123 | // if found { |
| 124 | // cfg = cached.(aws.Config) |
| 125 | // return cfg |
| 126 | // } |
| 127 | |
| 128 | // Check if the profile is already in the config map. If not, load it and retrieve the credentials. If it is, return the cached config object |
| 129 | // The AssumeRoleOptions below are used to pass the MFA token to the AssumeRole call (when applicable) |
| 130 | if _, ok := ConfigMap[AWSProfile]; !ok { |
| 131 | // Ensures the profile in the aws config file meets all requirements (valid keys and a region defined). I noticed some calls fail without a default region. |
| 132 | if AwsMfaToken != "" { |
| 133 | cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(AWSProfile), config.WithDefaultRegion("us-east-1"), config.WithRetryer( |
| 134 | func() aws.Retryer { |
| 135 | return retry.AddWithMaxAttempts(retry.NewStandard(), 3) |
| 136 | }), config.WithAssumeRoleCredentialOptions(func(options *stscreds.AssumeRoleOptions) { |
| 137 | options.TokenProvider = func() (string, error) { |
| 138 | return AwsMfaToken, nil |
| 139 | } |
| 140 | }), |
| 141 | ) |
| 142 | } else { |
| 143 | cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(AWSProfile), config.WithDefaultRegion("us-east-1"), config.WithRetryer( |
| 144 | func() aws.Retryer { |
| 145 | return retry.AddWithMaxAttempts(retry.NewStandard(), 3) |
| 146 | }), config.WithAssumeRoleCredentialOptions(func(options *stscreds.AssumeRoleOptions) { |
| 147 | options.TokenProvider = stscreds.StdinTokenProvider |
| 148 | }), |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | if err != nil { |
| 153 | //fmt.Println(err) |
| 154 | if AWSProfile != "" { |
| 155 | TxtLog.Println(err) |
| 156 | fmt.Printf("[%s][%s] The specified profile [%s] does not exist or there was an error loading the credentials.\n", cyan(emoji.Sprintf(":fox:cloudfox v%s :fox:", version)), cyan(AWSProfile), AWSProfile) |
| 157 | TxtLog.Fatalf("Could not retrieve the specified profile name %s", err) |
| 158 | } else { |
| 159 | fmt.Printf("[%s][%s] Error retrieving credentials from environment variables, or the instance metadata service.\n", cyan(emoji.Sprintf(":fox:cloudfox v%s :fox:", version)), cyan(AWSProfile)) |
| 160 | TxtLog.Fatalf("[%s][%s]Error retrieving credentials from environment variables, or the instance metadata service.\n", cyan(emoji.Sprintf(":fox:cloudfox v%s :fox:", version)), cyan(AWSProfile)) |
| 161 | } |
| 162 | //os.Exit(1) |
| 163 | } |
| 164 | |
| 165 | _, err := cfg.Credentials.Retrieve(context.TODO()) |
| 166 | |
| 167 | if err != nil { |
| 168 | fmt.Printf("[%s][%s] Error retrieving credentials from environment variables, or the instance metadata service.\n", cyan(emoji.Sprintf(":fox:cloudfox v%s :fox:", version)), cyan(AWSProfile)) |
| 169 | |
| 170 | } else { |
| 171 | // update the config map with the new config for future lookups |
| 172 | ConfigMap[AWSProfile] = cfg |
| 173 | //return the config object for this first iteration |
no outgoing calls
no test coverage detected