env_config.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package config
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "github.com/aws/aws-sdk-go-v2/aws"
  12. "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
  13. )
  14. // CredentialsSourceName provides a name of the provider when config is
  15. // loaded from environment.
  16. const CredentialsSourceName = "EnvConfigCredentials"
  17. // Environment variables that will be read for configuration values.
  18. const (
  19. awsAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  20. awsAccessKeyEnvVar = "AWS_ACCESS_KEY"
  21. awsSecretAccessKeyEnvVar = "AWS_SECRET_ACCESS_KEY"
  22. awsSecretKeyEnvVar = "AWS_SECRET_KEY"
  23. awsSessionTokenEnvVar = "AWS_SESSION_TOKEN"
  24. awsContainerCredentialsEndpointEnvVar = "AWS_CONTAINER_CREDENTIALS_FULL_URI"
  25. awsContainerCredentialsRelativePathEnvVar = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
  26. awsContainerPProviderAuthorizationEnvVar = "AWS_CONTAINER_AUTHORIZATION_TOKEN"
  27. awsRegionEnvVar = "AWS_REGION"
  28. awsDefaultRegionEnvVar = "AWS_DEFAULT_REGION"
  29. awsProfileEnvVar = "AWS_PROFILE"
  30. awsDefaultProfileEnvVar = "AWS_DEFAULT_PROFILE"
  31. awsSharedCredentialsFileEnvVar = "AWS_SHARED_CREDENTIALS_FILE"
  32. awsConfigFileEnvVar = "AWS_CONFIG_FILE"
  33. awsCustomCABundleEnvVar = "AWS_CA_BUNDLE"
  34. awsWebIdentityTokenFilePathEnvVar = "AWS_WEB_IDENTITY_TOKEN_FILE"
  35. awsRoleARNEnvVar = "AWS_ROLE_ARN"
  36. awsRoleSessionNameEnvVar = "AWS_ROLE_SESSION_NAME"
  37. awsEnableEndpointDiscoveryEnvVar = "AWS_ENABLE_ENDPOINT_DISCOVERY"
  38. awsS3UseARNRegionEnvVar = "AWS_S3_USE_ARN_REGION"
  39. awsEc2MetadataServiceEndpointModeEnvVar = "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE"
  40. awsEc2MetadataServiceEndpointEnvVar = "AWS_EC2_METADATA_SERVICE_ENDPOINT"
  41. awsEc2MetadataDisabled = "AWS_EC2_METADATA_DISABLED"
  42. awsS3DisableMultiRegionAccessPointEnvVar = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS"
  43. awsUseDualStackEndpoint = "AWS_USE_DUALSTACK_ENDPOINT"
  44. awsUseFIPSEndpoint = "AWS_USE_FIPS_ENDPOINT"
  45. awsDefaultMode = "AWS_DEFAULTS_MODE"
  46. awsRetryMaxAttempts = "AWS_MAX_ATTEMPTS"
  47. awsRetryMode = "AWS_RETRY_MODE"
  48. )
  49. var (
  50. credAccessEnvKeys = []string{
  51. awsAccessKeyIDEnvVar,
  52. awsAccessKeyEnvVar,
  53. }
  54. credSecretEnvKeys = []string{
  55. awsSecretAccessKeyEnvVar,
  56. awsSecretKeyEnvVar,
  57. }
  58. regionEnvKeys = []string{
  59. awsRegionEnvVar,
  60. awsDefaultRegionEnvVar,
  61. }
  62. profileEnvKeys = []string{
  63. awsProfileEnvVar,
  64. awsDefaultProfileEnvVar,
  65. }
  66. )
  67. // EnvConfig is a collection of environment values the SDK will read
  68. // setup config from. All environment values are optional. But some values
  69. // such as credentials require multiple values to be complete or the values
  70. // will be ignored.
  71. type EnvConfig struct {
  72. // Environment configuration values. If set both Access Key ID and Secret Access
  73. // Key must be provided. Session Token and optionally also be provided, but is
  74. // not required.
  75. //
  76. // # Access Key ID
  77. // AWS_ACCESS_KEY_ID=AKID
  78. // AWS_ACCESS_KEY=AKID # only read if AWS_ACCESS_KEY_ID is not set.
  79. //
  80. // # Secret Access Key
  81. // AWS_SECRET_ACCESS_KEY=SECRET
  82. // AWS_SECRET_KEY=SECRET # only read if AWS_SECRET_ACCESS_KEY is not set.
  83. //
  84. // # Session Token
  85. // AWS_SESSION_TOKEN=TOKEN
  86. Credentials aws.Credentials
  87. // ContainerCredentialsEndpoint value is the HTTP enabled endpoint to retrieve credentials
  88. // using the endpointcreds.Provider
  89. ContainerCredentialsEndpoint string
  90. // ContainerCredentialsRelativePath is the relative URI path that will be used when attempting to retrieve
  91. // credentials from the container endpoint.
  92. ContainerCredentialsRelativePath string
  93. // ContainerAuthorizationToken is the authorization token that will be included in the HTTP Authorization
  94. // header when attempting to retrieve credentials from the container credentials endpoint.
  95. ContainerAuthorizationToken string
  96. // Region value will instruct the SDK where to make service API requests to. If is
  97. // not provided in the environment the region must be provided before a service
  98. // client request is made.
  99. //
  100. // AWS_REGION=us-west-2
  101. // AWS_DEFAULT_REGION=us-west-2
  102. Region string
  103. // Profile name the SDK should load use when loading shared configuration from the
  104. // shared configuration files. If not provided "default" will be used as the
  105. // profile name.
  106. //
  107. // AWS_PROFILE=my_profile
  108. // AWS_DEFAULT_PROFILE=my_profile
  109. SharedConfigProfile string
  110. // Shared credentials file path can be set to instruct the SDK to use an alternate
  111. // file for the shared credentials. If not set the file will be loaded from
  112. // $HOME/.aws/credentials on Linux/Unix based systems, and
  113. // %USERPROFILE%\.aws\credentials on Windows.
  114. //
  115. // AWS_SHARED_CREDENTIALS_FILE=$HOME/my_shared_credentials
  116. SharedCredentialsFile string
  117. // Shared config file path can be set to instruct the SDK to use an alternate
  118. // file for the shared config. If not set the file will be loaded from
  119. // $HOME/.aws/config on Linux/Unix based systems, and
  120. // %USERPROFILE%\.aws\config on Windows.
  121. //
  122. // AWS_CONFIG_FILE=$HOME/my_shared_config
  123. SharedConfigFile string
  124. // Sets the path to a custom Credentials Authority (CA) Bundle PEM file
  125. // that the SDK will use instead of the system's root CA bundle.
  126. // Only use this if you want to configure the SDK to use a custom set
  127. // of CAs.
  128. //
  129. // Enabling this option will attempt to merge the Transport
  130. // into the SDK's HTTP client. If the client's Transport is
  131. // not a http.Transport an error will be returned. If the
  132. // Transport's TLS config is set this option will cause the
  133. // SDK to overwrite the Transport's TLS config's RootCAs value.
  134. //
  135. // Setting a custom HTTPClient in the aws.Config options will override this setting.
  136. // To use this option and custom HTTP client, the HTTP client needs to be provided
  137. // when creating the config. Not the service client.
  138. //
  139. // AWS_CA_BUNDLE=$HOME/my_custom_ca_bundle
  140. CustomCABundle string
  141. // Enables endpoint discovery via environment variables.
  142. //
  143. // AWS_ENABLE_ENDPOINT_DISCOVERY=true
  144. EnableEndpointDiscovery aws.EndpointDiscoveryEnableState
  145. // Specifies the WebIdentity token the SDK should use to assume a role
  146. // with.
  147. //
  148. // AWS_WEB_IDENTITY_TOKEN_FILE=file_path
  149. WebIdentityTokenFilePath string
  150. // Specifies the IAM role arn to use when assuming an role.
  151. //
  152. // AWS_ROLE_ARN=role_arn
  153. RoleARN string
  154. // Specifies the IAM role session name to use when assuming a role.
  155. //
  156. // AWS_ROLE_SESSION_NAME=session_name
  157. RoleSessionName string
  158. // Specifies if the S3 service should allow ARNs to direct the region
  159. // the client's requests are sent to.
  160. //
  161. // AWS_S3_USE_ARN_REGION=true
  162. S3UseARNRegion *bool
  163. // Specifies if the EC2 IMDS service client is enabled.
  164. //
  165. // AWS_EC2_METADATA_DISABLED=true
  166. EC2IMDSClientEnableState imds.ClientEnableState
  167. // Specifies the EC2 Instance Metadata Service default endpoint selection mode (IPv4 or IPv6)
  168. //
  169. // AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE=IPv6
  170. EC2IMDSEndpointMode imds.EndpointModeState
  171. // Specifies the EC2 Instance Metadata Service endpoint to use. If specified it overrides EC2IMDSEndpointMode.
  172. //
  173. // AWS_EC2_METADATA_SERVICE_ENDPOINT=http://fd00:ec2::254
  174. EC2IMDSEndpoint string
  175. // Specifies if the S3 service should disable multi-region access points
  176. // support.
  177. //
  178. // AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS=true
  179. S3DisableMultiRegionAccessPoints *bool
  180. // Specifies that SDK clients must resolve a dual-stack endpoint for
  181. // services.
  182. //
  183. // AWS_USE_DUALSTACK_ENDPOINT=true
  184. UseDualStackEndpoint aws.DualStackEndpointState
  185. // Specifies that SDK clients must resolve a FIPS endpoint for
  186. // services.
  187. //
  188. // AWS_USE_FIPS_ENDPOINT=true
  189. UseFIPSEndpoint aws.FIPSEndpointState
  190. // Specifies the SDK Defaults Mode used by services.
  191. //
  192. // AWS_DEFAULTS_MODE=standard
  193. DefaultsMode aws.DefaultsMode
  194. // Specifies the maximum number attempts an API client will call an
  195. // operation that fails with a retryable error.
  196. //
  197. // AWS_MAX_ATTEMPTS=3
  198. RetryMaxAttempts int
  199. // Specifies the retry model the API client will be created with.
  200. //
  201. // aws_retry_mode=standard
  202. RetryMode aws.RetryMode
  203. }
  204. // loadEnvConfig reads configuration values from the OS's environment variables.
  205. // Returning the a Config typed EnvConfig to satisfy the ConfigLoader func type.
  206. func loadEnvConfig(ctx context.Context, cfgs configs) (Config, error) {
  207. return NewEnvConfig()
  208. }
  209. // NewEnvConfig retrieves the SDK's environment configuration.
  210. // See `EnvConfig` for the values that will be retrieved.
  211. func NewEnvConfig() (EnvConfig, error) {
  212. var cfg EnvConfig
  213. creds := aws.Credentials{
  214. Source: CredentialsSourceName,
  215. }
  216. setStringFromEnvVal(&creds.AccessKeyID, credAccessEnvKeys)
  217. setStringFromEnvVal(&creds.SecretAccessKey, credSecretEnvKeys)
  218. if creds.HasKeys() {
  219. creds.SessionToken = os.Getenv(awsSessionTokenEnvVar)
  220. cfg.Credentials = creds
  221. }
  222. cfg.ContainerCredentialsEndpoint = os.Getenv(awsContainerCredentialsEndpointEnvVar)
  223. cfg.ContainerCredentialsRelativePath = os.Getenv(awsContainerCredentialsRelativePathEnvVar)
  224. cfg.ContainerAuthorizationToken = os.Getenv(awsContainerPProviderAuthorizationEnvVar)
  225. setStringFromEnvVal(&cfg.Region, regionEnvKeys)
  226. setStringFromEnvVal(&cfg.SharedConfigProfile, profileEnvKeys)
  227. cfg.SharedCredentialsFile = os.Getenv(awsSharedCredentialsFileEnvVar)
  228. cfg.SharedConfigFile = os.Getenv(awsConfigFileEnvVar)
  229. cfg.CustomCABundle = os.Getenv(awsCustomCABundleEnvVar)
  230. cfg.WebIdentityTokenFilePath = os.Getenv(awsWebIdentityTokenFilePathEnvVar)
  231. cfg.RoleARN = os.Getenv(awsRoleARNEnvVar)
  232. cfg.RoleSessionName = os.Getenv(awsRoleSessionNameEnvVar)
  233. if err := setEndpointDiscoveryTypeFromEnvVal(&cfg.EnableEndpointDiscovery, []string{awsEnableEndpointDiscoveryEnvVar}); err != nil {
  234. return cfg, err
  235. }
  236. if err := setBoolPtrFromEnvVal(&cfg.S3UseARNRegion, []string{awsS3UseARNRegionEnvVar}); err != nil {
  237. return cfg, err
  238. }
  239. setEC2IMDSClientEnableState(&cfg.EC2IMDSClientEnableState, []string{awsEc2MetadataDisabled})
  240. if err := setEC2IMDSEndpointMode(&cfg.EC2IMDSEndpointMode, []string{awsEc2MetadataServiceEndpointModeEnvVar}); err != nil {
  241. return cfg, err
  242. }
  243. cfg.EC2IMDSEndpoint = os.Getenv(awsEc2MetadataServiceEndpointEnvVar)
  244. if err := setBoolPtrFromEnvVal(&cfg.S3DisableMultiRegionAccessPoints, []string{awsS3DisableMultiRegionAccessPointEnvVar}); err != nil {
  245. return cfg, err
  246. }
  247. if err := setUseDualStackEndpointFromEnvVal(&cfg.UseDualStackEndpoint, []string{awsUseDualStackEndpoint}); err != nil {
  248. return cfg, err
  249. }
  250. if err := setUseFIPSEndpointFromEnvVal(&cfg.UseFIPSEndpoint, []string{awsUseFIPSEndpoint}); err != nil {
  251. return cfg, err
  252. }
  253. if err := setDefaultsModeFromEnvVal(&cfg.DefaultsMode, []string{awsDefaultMode}); err != nil {
  254. return cfg, err
  255. }
  256. if err := setIntFromEnvVal(&cfg.RetryMaxAttempts, []string{awsRetryMaxAttempts}); err != nil {
  257. return cfg, err
  258. }
  259. if err := setRetryModeFromEnvVal(&cfg.RetryMode, []string{awsRetryMode}); err != nil {
  260. return cfg, err
  261. }
  262. return cfg, nil
  263. }
  264. func (c EnvConfig) getDefaultsMode(ctx context.Context) (aws.DefaultsMode, bool, error) {
  265. if len(c.DefaultsMode) == 0 {
  266. return "", false, nil
  267. }
  268. return c.DefaultsMode, true, nil
  269. }
  270. // GetRetryMaxAttempts returns the value of AWS_MAX_ATTEMPTS if was specified,
  271. // and not 0.
  272. func (c EnvConfig) GetRetryMaxAttempts(ctx context.Context) (int, bool, error) {
  273. if c.RetryMaxAttempts == 0 {
  274. return 0, false, nil
  275. }
  276. return c.RetryMaxAttempts, true, nil
  277. }
  278. // GetRetryMode returns the RetryMode of AWS_RETRY_MODE if was specified, and a
  279. // valid value.
  280. func (c EnvConfig) GetRetryMode(ctx context.Context) (aws.RetryMode, bool, error) {
  281. if len(c.RetryMode) == 0 {
  282. return "", false, nil
  283. }
  284. return c.RetryMode, true, nil
  285. }
  286. func setEC2IMDSClientEnableState(state *imds.ClientEnableState, keys []string) {
  287. for _, k := range keys {
  288. value := os.Getenv(k)
  289. if len(value) == 0 {
  290. continue
  291. }
  292. switch {
  293. case strings.EqualFold(value, "true"):
  294. *state = imds.ClientDisabled
  295. case strings.EqualFold(value, "false"):
  296. *state = imds.ClientEnabled
  297. default:
  298. continue
  299. }
  300. break
  301. }
  302. }
  303. func setDefaultsModeFromEnvVal(mode *aws.DefaultsMode, keys []string) error {
  304. for _, k := range keys {
  305. if value := os.Getenv(k); len(value) > 0 {
  306. if ok := mode.SetFromString(value); !ok {
  307. return fmt.Errorf("invalid %s value: %s", k, value)
  308. }
  309. break
  310. }
  311. }
  312. return nil
  313. }
  314. func setRetryModeFromEnvVal(mode *aws.RetryMode, keys []string) (err error) {
  315. for _, k := range keys {
  316. if value := os.Getenv(k); len(value) > 0 {
  317. *mode, err = aws.ParseRetryMode(value)
  318. if err != nil {
  319. return fmt.Errorf("invalid %s value, %w", k, err)
  320. }
  321. break
  322. }
  323. }
  324. return nil
  325. }
  326. func setEC2IMDSEndpointMode(mode *imds.EndpointModeState, keys []string) error {
  327. for _, k := range keys {
  328. value := os.Getenv(k)
  329. if len(value) == 0 {
  330. continue
  331. }
  332. if err := mode.SetFromString(value); err != nil {
  333. return fmt.Errorf("invalid value for environment variable, %s=%s, %v", k, value, err)
  334. }
  335. }
  336. return nil
  337. }
  338. // GetRegion returns the AWS Region if set in the environment. Returns an empty
  339. // string if not set.
  340. func (c EnvConfig) getRegion(ctx context.Context) (string, bool, error) {
  341. if len(c.Region) == 0 {
  342. return "", false, nil
  343. }
  344. return c.Region, true, nil
  345. }
  346. // GetSharedConfigProfile returns the shared config profile if set in the
  347. // environment. Returns an empty string if not set.
  348. func (c EnvConfig) getSharedConfigProfile(ctx context.Context) (string, bool, error) {
  349. if len(c.SharedConfigProfile) == 0 {
  350. return "", false, nil
  351. }
  352. return c.SharedConfigProfile, true, nil
  353. }
  354. // getSharedConfigFiles returns a slice of filenames set in the environment.
  355. //
  356. // Will return the filenames in the order of:
  357. // * Shared Config
  358. func (c EnvConfig) getSharedConfigFiles(context.Context) ([]string, bool, error) {
  359. var files []string
  360. if v := c.SharedConfigFile; len(v) > 0 {
  361. files = append(files, v)
  362. }
  363. if len(files) == 0 {
  364. return nil, false, nil
  365. }
  366. return files, true, nil
  367. }
  368. // getSharedCredentialsFiles returns a slice of filenames set in the environment.
  369. //
  370. // Will return the filenames in the order of:
  371. // * Shared Credentials
  372. func (c EnvConfig) getSharedCredentialsFiles(context.Context) ([]string, bool, error) {
  373. var files []string
  374. if v := c.SharedCredentialsFile; len(v) > 0 {
  375. files = append(files, v)
  376. }
  377. if len(files) == 0 {
  378. return nil, false, nil
  379. }
  380. return files, true, nil
  381. }
  382. // GetCustomCABundle returns the custom CA bundle's PEM bytes if the file was
  383. func (c EnvConfig) getCustomCABundle(context.Context) (io.Reader, bool, error) {
  384. if len(c.CustomCABundle) == 0 {
  385. return nil, false, nil
  386. }
  387. b, err := ioutil.ReadFile(c.CustomCABundle)
  388. if err != nil {
  389. return nil, false, err
  390. }
  391. return bytes.NewReader(b), true, nil
  392. }
  393. // GetS3UseARNRegion returns whether to allow ARNs to direct the region
  394. // the S3 client's requests are sent to.
  395. func (c EnvConfig) GetS3UseARNRegion(ctx context.Context) (value, ok bool, err error) {
  396. if c.S3UseARNRegion == nil {
  397. return false, false, nil
  398. }
  399. return *c.S3UseARNRegion, true, nil
  400. }
  401. // GetS3DisableMultRegionAccessPoints returns whether to disable multi-region access point
  402. // support for the S3 client.
  403. func (c EnvConfig) GetS3DisableMultRegionAccessPoints(ctx context.Context) (value, ok bool, err error) {
  404. if c.S3DisableMultiRegionAccessPoints == nil {
  405. return false, false, nil
  406. }
  407. return *c.S3DisableMultiRegionAccessPoints, true, nil
  408. }
  409. // GetUseDualStackEndpoint returns whether the service's dual-stack endpoint should be
  410. // used for requests.
  411. func (c EnvConfig) GetUseDualStackEndpoint(ctx context.Context) (value aws.DualStackEndpointState, found bool, err error) {
  412. if c.UseDualStackEndpoint == aws.DualStackEndpointStateUnset {
  413. return aws.DualStackEndpointStateUnset, false, nil
  414. }
  415. return c.UseDualStackEndpoint, true, nil
  416. }
  417. // GetUseFIPSEndpoint returns whether the service's FIPS endpoint should be
  418. // used for requests.
  419. func (c EnvConfig) GetUseFIPSEndpoint(ctx context.Context) (value aws.FIPSEndpointState, found bool, err error) {
  420. if c.UseFIPSEndpoint == aws.FIPSEndpointStateUnset {
  421. return aws.FIPSEndpointStateUnset, false, nil
  422. }
  423. return c.UseFIPSEndpoint, true, nil
  424. }
  425. func setStringFromEnvVal(dst *string, keys []string) {
  426. for _, k := range keys {
  427. if v := os.Getenv(k); len(v) > 0 {
  428. *dst = v
  429. break
  430. }
  431. }
  432. }
  433. func setIntFromEnvVal(dst *int, keys []string) error {
  434. for _, k := range keys {
  435. if v := os.Getenv(k); len(v) > 0 {
  436. i, err := strconv.ParseInt(v, 10, 64)
  437. if err != nil {
  438. return fmt.Errorf("invalid value %s=%s, %w", k, v, err)
  439. }
  440. *dst = int(i)
  441. break
  442. }
  443. }
  444. return nil
  445. }
  446. func setBoolPtrFromEnvVal(dst **bool, keys []string) error {
  447. for _, k := range keys {
  448. value := os.Getenv(k)
  449. if len(value) == 0 {
  450. continue
  451. }
  452. if *dst == nil {
  453. *dst = new(bool)
  454. }
  455. switch {
  456. case strings.EqualFold(value, "false"):
  457. **dst = false
  458. case strings.EqualFold(value, "true"):
  459. **dst = true
  460. default:
  461. return fmt.Errorf(
  462. "invalid value for environment variable, %s=%s, need true or false",
  463. k, value)
  464. }
  465. break
  466. }
  467. return nil
  468. }
  469. func setEndpointDiscoveryTypeFromEnvVal(dst *aws.EndpointDiscoveryEnableState, keys []string) error {
  470. for _, k := range keys {
  471. value := os.Getenv(k)
  472. if len(value) == 0 {
  473. continue // skip if empty
  474. }
  475. switch {
  476. case strings.EqualFold(value, endpointDiscoveryDisabled):
  477. *dst = aws.EndpointDiscoveryDisabled
  478. case strings.EqualFold(value, endpointDiscoveryEnabled):
  479. *dst = aws.EndpointDiscoveryEnabled
  480. case strings.EqualFold(value, endpointDiscoveryAuto):
  481. *dst = aws.EndpointDiscoveryAuto
  482. default:
  483. return fmt.Errorf(
  484. "invalid value for environment variable, %s=%s, need true, false or auto",
  485. k, value)
  486. }
  487. }
  488. return nil
  489. }
  490. func setUseDualStackEndpointFromEnvVal(dst *aws.DualStackEndpointState, keys []string) error {
  491. for _, k := range keys {
  492. value := os.Getenv(k)
  493. if len(value) == 0 {
  494. continue // skip if empty
  495. }
  496. switch {
  497. case strings.EqualFold(value, "true"):
  498. *dst = aws.DualStackEndpointStateEnabled
  499. case strings.EqualFold(value, "false"):
  500. *dst = aws.DualStackEndpointStateDisabled
  501. default:
  502. return fmt.Errorf(
  503. "invalid value for environment variable, %s=%s, need true, false",
  504. k, value)
  505. }
  506. }
  507. return nil
  508. }
  509. func setUseFIPSEndpointFromEnvVal(dst *aws.FIPSEndpointState, keys []string) error {
  510. for _, k := range keys {
  511. value := os.Getenv(k)
  512. if len(value) == 0 {
  513. continue // skip if empty
  514. }
  515. switch {
  516. case strings.EqualFold(value, "true"):
  517. *dst = aws.FIPSEndpointStateEnabled
  518. case strings.EqualFold(value, "false"):
  519. *dst = aws.FIPSEndpointStateDisabled
  520. default:
  521. return fmt.Errorf(
  522. "invalid value for environment variable, %s=%s, need true, false",
  523. k, value)
  524. }
  525. }
  526. return nil
  527. }
  528. // GetEnableEndpointDiscovery returns resolved value for EnableEndpointDiscovery env variable setting.
  529. func (c EnvConfig) GetEnableEndpointDiscovery(ctx context.Context) (value aws.EndpointDiscoveryEnableState, found bool, err error) {
  530. if c.EnableEndpointDiscovery == aws.EndpointDiscoveryUnset {
  531. return aws.EndpointDiscoveryUnset, false, nil
  532. }
  533. return c.EnableEndpointDiscovery, true, nil
  534. }
  535. // GetEC2IMDSClientEnableState implements a EC2IMDSClientEnableState options resolver interface.
  536. func (c EnvConfig) GetEC2IMDSClientEnableState() (imds.ClientEnableState, bool, error) {
  537. if c.EC2IMDSClientEnableState == imds.ClientDefaultEnableState {
  538. return imds.ClientDefaultEnableState, false, nil
  539. }
  540. return c.EC2IMDSClientEnableState, true, nil
  541. }
  542. // GetEC2IMDSEndpointMode implements a EC2IMDSEndpointMode option resolver interface.
  543. func (c EnvConfig) GetEC2IMDSEndpointMode() (imds.EndpointModeState, bool, error) {
  544. if c.EC2IMDSEndpointMode == imds.EndpointModeStateUnset {
  545. return imds.EndpointModeStateUnset, false, nil
  546. }
  547. return c.EC2IMDSEndpointMode, true, nil
  548. }
  549. // GetEC2IMDSEndpoint implements a EC2IMDSEndpoint option resolver interface.
  550. func (c EnvConfig) GetEC2IMDSEndpoint() (string, bool, error) {
  551. if len(c.EC2IMDSEndpoint) == 0 {
  552. return "", false, nil
  553. }
  554. return c.EC2IMDSEndpoint, true, nil
  555. }