aws.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // The assumed role ARN to use for sessions
  24. AWSAssumeRoleArn string
  25. // ------------------------------------------------------------------
  26. // All fields encrypted before storage.
  27. // ------------------------------------------------------------------
  28. // The AWS cluster ID
  29. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  30. AWSClusterID []byte `json:"aws_cluster_id"`
  31. // The AWS access key for this IAM user
  32. AWSAccessKeyID []byte `json:"aws_access_key_id"`
  33. // The AWS secret key for this IAM user
  34. AWSSecretAccessKey []byte `json:"aws_secret_access_key"`
  35. // An optional session token, if the user is assuming a role
  36. AWSSessionToken []byte `json:"aws_session_token"`
  37. }
  38. func (a *AWSIntegration) ToAWSIntegrationType() *types.AWSIntegration {
  39. return &types.AWSIntegration{
  40. CreatedAt: a.CreatedAt,
  41. ID: a.ID,
  42. UserID: a.UserID,
  43. ProjectID: a.ProjectID,
  44. AWSArn: a.AWSArn,
  45. }
  46. }
  47. // GetSession retrieves an AWS session to use based on the access key and secret
  48. // access key
  49. func (a *AWSIntegration) GetSession() (*session.Session, error) {
  50. awsConf := &aws.Config{
  51. Credentials: credentials.NewStaticCredentials(
  52. string(a.AWSAccessKeyID),
  53. string(a.AWSSecretAccessKey),
  54. string(a.AWSSessionToken),
  55. ),
  56. }
  57. if a.AWSRegion != "" {
  58. awsConf.Region = &a.AWSRegion
  59. }
  60. return session.NewSessionWithOptions(session.Options{
  61. SharedConfigState: session.SharedConfigEnable,
  62. Config: *awsConf,
  63. })
  64. }
  65. // PopulateAWSArn uses the access key/secret to get the caller identity, and
  66. // attaches it to the AWS integration
  67. func (a *AWSIntegration) PopulateAWSArn() error {
  68. sess, err := a.GetSession()
  69. if err != nil {
  70. return err
  71. }
  72. svc := sts.New(sess)
  73. result, err := svc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
  74. if err != nil {
  75. return err
  76. }
  77. a.AWSArn = *result.Arn
  78. return nil
  79. }
  80. // GetBearerToken retrieves a bearer token for an AWS account
  81. func (a *AWSIntegration) GetBearerToken(
  82. getTokenCache GetTokenCacheFunc,
  83. setTokenCache SetTokenCacheFunc,
  84. clusterID string,
  85. shouldClusterIdOverride bool,
  86. ) (string, error) {
  87. cache, err := getTokenCache()
  88. // check the token cache for a non-expired token
  89. if cache != nil {
  90. if tok := cache.Token; err == nil && !cache.IsExpired() && len(tok) > 0 {
  91. return string(tok), nil
  92. }
  93. }
  94. generator, err := token.NewGenerator(false, false)
  95. if err != nil {
  96. return "", err
  97. }
  98. sess, err := a.GetSession()
  99. if err != nil {
  100. return "", err
  101. }
  102. var validClusterId string
  103. if shouldClusterIdOverride {
  104. validClusterId = clusterID
  105. } else {
  106. validClusterId = string(a.AWSClusterID)
  107. if validClusterId == "" {
  108. validClusterId = clusterID
  109. }
  110. }
  111. tok, err := generator.GetWithOptions(&token.GetTokenOptions{
  112. AssumeRoleARN: a.AWSAssumeRoleArn,
  113. Session: sess,
  114. ClusterID: validClusterId,
  115. })
  116. if err != nil {
  117. return "", err
  118. }
  119. setTokenCache(tok.Token, tok.Expiration)
  120. return tok.Token, nil
  121. }