aws.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. ID: a.ID,
  39. UserID: a.UserID,
  40. ProjectID: a.ProjectID,
  41. AWSArn: a.AWSArn,
  42. }
  43. }
  44. // GetSession retrieves an AWS session to use based on the access key and secret
  45. // access key
  46. func (a *AWSIntegration) GetSession() (*session.Session, error) {
  47. awsConf := &aws.Config{
  48. Credentials: credentials.NewStaticCredentials(
  49. string(a.AWSAccessKeyID),
  50. string(a.AWSSecretAccessKey),
  51. string(a.AWSSessionToken),
  52. ),
  53. }
  54. if a.AWSRegion != "" {
  55. awsConf.Region = &a.AWSRegion
  56. }
  57. return session.NewSessionWithOptions(session.Options{
  58. SharedConfigState: session.SharedConfigEnable,
  59. Config: *awsConf,
  60. })
  61. }
  62. // PopulateAWSArn uses the access key/secret to get the caller identity, and
  63. // attaches it to the AWS integration
  64. func (a *AWSIntegration) PopulateAWSArn() error {
  65. sess, err := a.GetSession()
  66. if err != nil {
  67. return err
  68. }
  69. svc := sts.New(sess)
  70. result, err := svc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
  71. if err != nil {
  72. return err
  73. }
  74. a.AWSArn = *result.Arn
  75. return nil
  76. }
  77. // GetBearerToken retrieves a bearer token for an AWS account
  78. func (a *AWSIntegration) GetBearerToken(
  79. getTokenCache GetTokenCacheFunc,
  80. setTokenCache SetTokenCacheFunc,
  81. ) (string, error) {
  82. cache, err := getTokenCache()
  83. // check the token cache for a non-expired token
  84. if cache != nil {
  85. if tok := cache.Token; err == nil && !cache.IsExpired() && len(tok) > 0 {
  86. return string(tok), nil
  87. }
  88. }
  89. generator, err := token.NewGenerator(false, false)
  90. if err != nil {
  91. return "", err
  92. }
  93. sess, err := a.GetSession()
  94. if err != nil {
  95. return "", err
  96. }
  97. tok, err := generator.GetWithOptions(&token.GetTokenOptions{
  98. Session: sess,
  99. ClusterID: string(a.AWSClusterID),
  100. })
  101. if err != nil {
  102. return "", err
  103. }
  104. setTokenCache(tok.Token, tok.Expiration)
  105. return tok.Token, nil
  106. }