aws.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package integrations
  2. import (
  3. "gorm.io/gorm"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/service/sts"
  6. "github.com/aws/aws-sdk-go/aws/credentials"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/porter-dev/porter/api/types"
  9. "sigs.k8s.io/aws-iam-authenticator/pkg/token"
  10. )
  11. // AWSIntegration is an auth mechanism that uses a AWS IAM user to
  12. // authenticate
  13. type AWSIntegration struct {
  14. gorm.Model
  15. // The id of the user that linked this auth mechanism
  16. UserID uint `json:"user_id"`
  17. // The project that this integration belongs to
  18. ProjectID uint `json:"project_id"`
  19. // The AWS arn this is integration is linked to
  20. AWSArn string `json:"aws_arn"`
  21. // The optional AWS region (required by some session configurations)
  22. AWSRegion string `json:"aws_region"`
  23. // ------------------------------------------------------------------
  24. // All fields encrypted before storage.
  25. // ------------------------------------------------------------------
  26. // The AWS cluster ID
  27. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  28. AWSClusterID []byte `json:"aws_cluster_id"`
  29. // The AWS access key for this IAM user
  30. AWSAccessKeyID []byte `json:"aws_access_key_id"`
  31. // The AWS secret key for this IAM user
  32. AWSSecretAccessKey []byte `json:"aws_secret_access_key"`
  33. // An optional session token, if the user is assuming a role
  34. AWSSessionToken []byte `json:"aws_session_token"`
  35. }
  36. func (a *AWSIntegration) ToAWSIntegrationType() *types.AWSIntegration {
  37. return &types.AWSIntegration{
  38. CreatedAt: a.CreatedAt,
  39. ID: a.ID,
  40. UserID: a.UserID,
  41. ProjectID: a.ProjectID,
  42. AWSArn: a.AWSArn,
  43. }
  44. }
  45. // GetSession retrieves an AWS session to use based on the access key and secret
  46. // access key
  47. func (a *AWSIntegration) GetSession() (*session.Session, error) {
  48. awsConf := &aws.Config{
  49. Credentials: credentials.NewStaticCredentials(
  50. string(a.AWSAccessKeyID),
  51. string(a.AWSSecretAccessKey),
  52. string(a.AWSSessionToken),
  53. ),
  54. }
  55. if a.AWSRegion != "" {
  56. awsConf.Region = &a.AWSRegion
  57. }
  58. return session.NewSessionWithOptions(session.Options{
  59. SharedConfigState: session.SharedConfigEnable,
  60. Config: *awsConf,
  61. })
  62. }
  63. // PopulateAWSArn uses the access key/secret to get the caller identity, and
  64. // attaches it to the AWS integration
  65. func (a *AWSIntegration) PopulateAWSArn() error {
  66. sess, err := a.GetSession()
  67. if err != nil {
  68. return err
  69. }
  70. svc := sts.New(sess)
  71. result, err := svc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
  72. if err != nil {
  73. return err
  74. }
  75. a.AWSArn = *result.Arn
  76. return nil
  77. }
  78. // GetBearerToken retrieves a bearer token for an AWS account
  79. func (a *AWSIntegration) GetBearerToken(
  80. getTokenCache GetTokenCacheFunc,
  81. setTokenCache SetTokenCacheFunc,
  82. clusterID string,
  83. shouldClusterIdOverride bool,
  84. ) (string, error) {
  85. cache, err := getTokenCache()
  86. // check the token cache for a non-expired token
  87. if cache != nil {
  88. if tok := cache.Token; err == nil && !cache.IsExpired() && len(tok) > 0 {
  89. return string(tok), nil
  90. }
  91. }
  92. generator, err := token.NewGenerator(false, false)
  93. if err != nil {
  94. return "", err
  95. }
  96. sess, err := a.GetSession()
  97. if err != nil {
  98. return "", err
  99. }
  100. var validClusterId string
  101. if shouldClusterIdOverride {
  102. validClusterId = clusterID
  103. } else {
  104. validClusterId = string(a.AWSClusterID)
  105. if validClusterId == "" {
  106. validClusterId = clusterID
  107. }
  108. }
  109. tok, err := generator.GetWithOptions(&token.GetTokenOptions{
  110. Session: sess,
  111. ClusterID: validClusterId,
  112. })
  113. if err != nil {
  114. return "", err
  115. }
  116. setTokenCache(tok.Token, tok.Expiration)
  117. return tok.Token, nil
  118. }