(ctx context.Context)
| 28 | ) |
| 29 | |
| 30 | func loadAWSSecret(ctx context.Context) error { |
| 31 | var secretID, secretVersionID, secretVersionStage, secretRegion string |
| 32 | |
| 33 | IMGPROXY_ENV_AWS_SECRET_ID.Parse(&secretID) |
| 34 | IMGPROXY_ENV_AWS_SECRET_VERSION_ID.Parse(&secretVersionID) |
| 35 | IMGPROXY_ENV_AWS_SECRET_VERSION_STAGE.Parse(&secretVersionStage) |
| 36 | IMGPROXY_ENV_AWS_SECRET_REGION.Parse(&secretRegion) |
| 37 | |
| 38 | // No secret ID, no aws |
| 39 | if len(secretID) == 0 { |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // Let's form AWS default config |
| 44 | conf, err := awsConfig.LoadDefaultConfig(ctx) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("can't load AWS Secrets Manager config: %w", err) |
| 47 | } |
| 48 | |
| 49 | if len(secretRegion) > 0 { |
| 50 | conf.Region = secretRegion |
| 51 | } |
| 52 | |
| 53 | if len(conf.Region) == 0 { |
| 54 | conf.Region = defaultAWSRegion |
| 55 | } |
| 56 | |
| 57 | // Let's create secrets manager client |
| 58 | client := secretsmanager.NewFromConfig(conf) |
| 59 | |
| 60 | input := secretsmanager.GetSecretValueInput{SecretId: aws.String(secretID)} |
| 61 | if len(secretVersionID) > 0 { |
| 62 | input.VersionId = aws.String(secretVersionID) |
| 63 | } else if len(secretVersionStage) > 0 { |
| 64 | input.VersionStage = aws.String(secretVersionStage) |
| 65 | } |
| 66 | |
| 67 | ctx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 68 | defer cancel() |
| 69 | |
| 70 | output, err := client.GetSecretValue(ctx, &input) |
| 71 | if err != nil { |
| 72 | return fmt.Errorf("can't retrieve config from AWS Secrets Manager: %w", err) |
| 73 | } |
| 74 | |
| 75 | // No secret string, failed to initialize secrets manager, return |
| 76 | if output.SecretString == nil { |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | return unmarshalEnv(*output.SecretString, "AWS Secrets Manager") |
| 81 | } |
| 82 | |
| 83 | // loadAWSSystemManagerParams loads environment variables from AWS System Manager |
| 84 | func loadAWSSystemManagerParams(ctx context.Context) error { |
no test coverage detected