aws.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package loadenv
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/DarthSim/godotenv"
  9. "github.com/aws/aws-sdk-go-v2/aws"
  10. awsConfig "github.com/aws/aws-sdk-go-v2/config"
  11. "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
  12. "github.com/aws/aws-sdk-go-v2/service/ssm"
  13. )
  14. func loadAWSSecret() error {
  15. secretID := os.Getenv("IMGPROXY_ENV_AWS_SECRET_ID")
  16. secretVersionID := os.Getenv("IMGPROXY_ENV_AWS_SECRET_VERSION_ID")
  17. secretVersionStage := os.Getenv("IMGPROXY_ENV_AWS_SECRET_VERSION_STAGE")
  18. secretRegion := os.Getenv("IMGPROXY_ENV_AWS_SECRET_REGION")
  19. if len(secretID) == 0 {
  20. return nil
  21. }
  22. conf, err := awsConfig.LoadDefaultConfig(context.Background())
  23. if err != nil {
  24. return fmt.Errorf("can't load AWS Secrets Manager config: %s", err)
  25. }
  26. if len(secretRegion) != 0 {
  27. conf.Region = secretRegion
  28. }
  29. if len(conf.Region) == 0 {
  30. conf.Region = "us-west-1"
  31. }
  32. client := secretsmanager.NewFromConfig(conf)
  33. input := secretsmanager.GetSecretValueInput{SecretId: aws.String(secretID)}
  34. if len(secretVersionID) > 0 {
  35. input.VersionId = aws.String(secretVersionID)
  36. } else if len(secretVersionStage) > 0 {
  37. input.VersionStage = aws.String(secretVersionStage)
  38. }
  39. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  40. defer cancel()
  41. output, err := client.GetSecretValue(ctx, &input)
  42. if err != nil {
  43. return fmt.Errorf("Can't retrieve config from AWS Secrets Manager: %s", err)
  44. }
  45. if output.SecretString == nil {
  46. return nil
  47. }
  48. envmap, err := godotenv.Unmarshal(*output.SecretString)
  49. if err != nil {
  50. return fmt.Errorf("Can't parse config from AWS Secrets Manager: %s", err)
  51. }
  52. for k, v := range envmap {
  53. if err = os.Setenv(k, v); err != nil {
  54. return fmt.Errorf("Can't set %s env variable from AWS Secrets Manager: %s", k, err)
  55. }
  56. }
  57. return nil
  58. }
  59. func loadAWSSystemManagerParams() error {
  60. paramsPath := os.Getenv("IMGPROXY_ENV_AWS_SSM_PARAMETERS_PATH")
  61. paramsRegion := os.Getenv("IMGPROXY_ENV_AWS_SSM_PARAMETERS_REGION")
  62. if len(paramsPath) == 0 {
  63. return nil
  64. }
  65. conf, err := awsConfig.LoadDefaultConfig(context.Background())
  66. if err != nil {
  67. return fmt.Errorf("can't load AWS SSM config: %s", err)
  68. }
  69. if len(paramsRegion) != 0 {
  70. conf.Region = paramsRegion
  71. }
  72. if len(conf.Region) == 0 {
  73. conf.Region = "us-west-1"
  74. }
  75. client := ssm.NewFromConfig(conf)
  76. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  77. defer cancel()
  78. var nextToken *string
  79. for {
  80. input := ssm.GetParametersByPathInput{
  81. Path: aws.String(paramsPath),
  82. WithDecryption: aws.Bool(true),
  83. NextToken: nextToken,
  84. }
  85. output, err := client.GetParametersByPath(ctx, &input)
  86. if err != nil {
  87. return fmt.Errorf("Can't retrieve parameters from AWS SSM: %s", err)
  88. }
  89. for _, p := range output.Parameters {
  90. if p.Name == nil || p.Value == nil {
  91. continue
  92. }
  93. if p.DataType == nil || *p.DataType != "text" {
  94. continue
  95. }
  96. name := *p.Name
  97. env := strings.ReplaceAll(
  98. strings.TrimPrefix(strings.TrimPrefix(name, paramsPath), "/"),
  99. "/", "_",
  100. )
  101. if err = os.Setenv(env, *p.Value); err != nil {
  102. return fmt.Errorf("Can't set %s env variable from AWS SSM: %s", env, err)
  103. }
  104. }
  105. if nextToken = output.NextToken; nextToken == nil {
  106. break
  107. }
  108. }
  109. return nil
  110. }