aws.go 2.7 KB

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