auth_iam.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vault
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "github.com/aws/aws-sdk-go-v2/aws"
  21. "github.com/aws/aws-sdk-go-v2/config"
  22. "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
  23. "github.com/golang-jwt/jwt/v5"
  24. authaws "github.com/hashicorp/vault/api/auth/aws"
  25. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  26. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  27. vaultiamauth "github.com/external-secrets/external-secrets/providers/v1/vault/iamauth"
  28. vaultutil "github.com/external-secrets/external-secrets/providers/v1/vault/util"
  29. "github.com/external-secrets/external-secrets/runtime/constants"
  30. "github.com/external-secrets/external-secrets/runtime/metrics"
  31. )
  32. const (
  33. defaultAWSRegion = "us-east-1"
  34. defaultAWSAuthMountPath = "aws"
  35. errNoAWSAuthMethodFound = "no AWS authentication method found: expected either IRSA or Pod Identity"
  36. errIrsaTokenFileNotFoundOnPod = "web identity token file not found at %s location: %w"
  37. errIrsaTokenFileNotReadable = "could not read the web identity token from the file %s: %w"
  38. errIrsaTokenNotValidJWT = "could not parse web identity token available at %s. not a valid jwt?: %w"
  39. errIrsaTokenNotValidClaims = "could not find pod identity info on token %s"
  40. )
  41. func setIamAuthToken(ctx context.Context, v *client, jwtProvider vaultutil.JwtProviderFactory, assumeRoler vaultiamauth.STSProvider) (bool, error) {
  42. iamAuth := v.store.Auth.Iam
  43. isClusterKind := v.storeKind == esv1.ClusterSecretStoreKind
  44. if iamAuth != nil {
  45. err := v.requestTokenWithIamAuth(ctx, iamAuth, isClusterKind, v.kube, v.namespace, jwtProvider, assumeRoler)
  46. if err != nil {
  47. return true, err
  48. }
  49. return true, nil
  50. }
  51. return false, nil
  52. }
  53. func (c *client) requestTokenWithIamAuth(
  54. ctx context.Context,
  55. iamAuth *esv1.VaultIamAuth,
  56. isClusterKind bool,
  57. k kclient.Client,
  58. n string,
  59. jwtProvider vaultutil.JwtProviderFactory,
  60. assumeRoler vaultiamauth.STSProvider,
  61. ) error {
  62. jwtAuth := iamAuth.JWTAuth
  63. secretRefAuth := iamAuth.SecretRef
  64. regionAWS := c.getRegionOrDefault(iamAuth.Region)
  65. awsAuthMountPath := c.getAuthMountPathOrDefault(iamAuth.Path)
  66. var credsProvider aws.CredentialsProvider
  67. var err error
  68. if jwtAuth != nil { // use credentials from a sa explicitly defined and referenced. Highest preference is given to this method/configuration.
  69. credsProvider, err = vaultiamauth.CredsFromServiceAccount(ctx, *iamAuth, regionAWS, isClusterKind, k, n, jwtProvider)
  70. if err != nil {
  71. return err
  72. }
  73. } else if secretRefAuth != nil { // if jwtAuth is not defined, check if secretRef is defined. Second preference.
  74. logger.V(1).Info("using credentials from secretRef")
  75. credsProvider, err = vaultiamauth.CredsFromSecretRef(ctx, *iamAuth, c.storeKind, k, n)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. // Neither of jwtAuth or secretRefAuth defined. Last preference.
  81. // Default to controller pod's identity
  82. if jwtAuth == nil && secretRefAuth == nil {
  83. credsProvider, err = c.getControllerPodCredentials(ctx, regionAWS, k, jwtProvider)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. var loadCfgOpts []func(*config.LoadOptions) error
  89. if credsProvider != nil {
  90. loadCfgOpts = append(loadCfgOpts, config.WithCredentialsProvider(credsProvider))
  91. }
  92. if regionAWS != "" {
  93. loadCfgOpts = append(loadCfgOpts, config.WithRegion(regionAWS))
  94. }
  95. cfg, err := config.LoadDefaultConfig(ctx, loadCfgOpts...)
  96. if err != nil {
  97. return err
  98. }
  99. if iamAuth.AWSIAMRole != "" {
  100. stsclient := assumeRoler(&cfg)
  101. if iamAuth.ExternalID != "" {
  102. cfg.Credentials = stscreds.NewAssumeRoleProvider(stsclient, iamAuth.AWSIAMRole, func(opts *stscreds.AssumeRoleOptions) {
  103. opts.ExternalID = aws.String(iamAuth.ExternalID)
  104. })
  105. } else {
  106. cfg.Credentials = stscreds.NewAssumeRoleProvider(stsclient, iamAuth.AWSIAMRole)
  107. }
  108. }
  109. getCreds, err := cfg.Credentials.Retrieve(ctx)
  110. if err != nil {
  111. return err
  112. }
  113. // Set environment variables. These would be fetched by Login
  114. _ = os.Setenv("AWS_ACCESS_KEY_ID", getCreds.AccessKeyID)
  115. _ = os.Setenv("AWS_SECRET_ACCESS_KEY", getCreds.SecretAccessKey)
  116. _ = os.Setenv("AWS_SESSION_TOKEN", getCreds.SessionToken)
  117. var awsAuthClient *authaws.AWSAuth
  118. if iamAuth.VaultAWSIAMServerID != "" {
  119. awsAuthClient, err = authaws.NewAWSAuth(
  120. authaws.WithRegion(regionAWS),
  121. authaws.WithIAMAuth(),
  122. authaws.WithRole(iamAuth.Role),
  123. authaws.WithMountPath(awsAuthMountPath),
  124. authaws.WithIAMServerIDHeader(iamAuth.VaultAWSIAMServerID),
  125. )
  126. if err != nil {
  127. return err
  128. }
  129. } else {
  130. awsAuthClient, err = authaws.NewAWSAuth(authaws.WithRegion(regionAWS), authaws.WithIAMAuth(), authaws.WithRole(iamAuth.Role), authaws.WithMountPath(awsAuthMountPath))
  131. if err != nil {
  132. return err
  133. }
  134. }
  135. _, err = c.auth.Login(ctx, awsAuthClient)
  136. metrics.ObserveAPICall(constants.ProviderHCVault, constants.CallHCVaultLogin, err)
  137. if err != nil {
  138. return err
  139. }
  140. return nil
  141. }
  142. func (c *client) getRegionOrDefault(region string) string {
  143. if region != "" {
  144. return region
  145. }
  146. return defaultAWSRegion
  147. }
  148. func (c *client) getAuthMountPathOrDefault(path string) string {
  149. if path != "" {
  150. return path
  151. }
  152. return defaultAWSAuthMountPath
  153. }
  154. func (c *client) getControllerPodCredentials(ctx context.Context, region string, k kclient.Client, jwtProvider vaultutil.JwtProviderFactory) (aws.CredentialsProvider, error) {
  155. // First try IRSA (Web Identity Token) - checking if controller pod's service account is IRSA enabled
  156. tokenFile := os.Getenv(vaultiamauth.AWSWebIdentityTokenFileEnvVar)
  157. if tokenFile != "" {
  158. logger.V(1).Info("using IRSA token for authentication")
  159. return c.getCredsFromIRSAToken(ctx, tokenFile, region, k, jwtProvider)
  160. }
  161. // Check for Pod Identity environment variables.
  162. podIdentityURI := os.Getenv(vaultiamauth.AWSContainerCredentialsFullURIEnvVar)
  163. if podIdentityURI != "" {
  164. logger.V(1).Info("using Pod Identity for authentication")
  165. // Return nil to let AWS SDK v2 container credential provider handle Pod Identity automatically
  166. return nil, nil
  167. }
  168. // No IRSA or Pod Identity found.
  169. return nil, errors.New(errNoAWSAuthMethodFound)
  170. }
  171. func (c *client) getCredsFromIRSAToken(ctx context.Context, tokenFile, region string, k kclient.Client, jwtProvider vaultutil.JwtProviderFactory) (aws.CredentialsProvider, error) {
  172. // IRSA enabled service account, let's check that the jwt token filemount and file exists
  173. if _, err := os.Stat(filepath.Clean(tokenFile)); err != nil {
  174. return nil, fmt.Errorf(errIrsaTokenFileNotFoundOnPod, tokenFile, err)
  175. }
  176. // everything looks good so far, let's fetch the jwt token from AWS_WEB_IDENTITY_TOKEN_FILE
  177. jwtByte, err := os.ReadFile(filepath.Clean(tokenFile))
  178. if err != nil {
  179. return nil, fmt.Errorf(errIrsaTokenFileNotReadable, tokenFile, err)
  180. }
  181. // Parse the JWT token to extract metadata (namespace and service account).
  182. // Note: Signature verification is intentionally skipped here as we only need to extract
  183. // claims from the IRSA token that comes from a trusted source (AWS-mounted file).
  184. // The token itself will be validated by AWS STS when used for authentication.
  185. parser := jwt.NewParser(jwt.WithoutClaimsValidation())
  186. token, _, err := parser.ParseUnverified(string(jwtByte), jwt.MapClaims{})
  187. if err != nil {
  188. return nil, fmt.Errorf(errIrsaTokenNotValidJWT, tokenFile, err) // JWT token parser error
  189. }
  190. var ns string
  191. var sa string
  192. // let's fetch the namespace and serviceaccount from parsed jwt token
  193. claims, ok := token.Claims.(jwt.MapClaims)
  194. if !ok {
  195. return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
  196. }
  197. k8s, ok := claims["kubernetes.io"].(map[string]any)
  198. if !ok {
  199. return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
  200. }
  201. ns, ok = k8s["namespace"].(string)
  202. if !ok {
  203. return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
  204. }
  205. saMap, ok := k8s["serviceaccount"].(map[string]any)
  206. if !ok {
  207. return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
  208. }
  209. sa, ok = saMap["name"].(string)
  210. if !ok {
  211. return nil, fmt.Errorf(errIrsaTokenNotValidClaims, tokenFile)
  212. }
  213. return vaultiamauth.CredsFromControllerServiceAccount(ctx, sa, ns, region, k, jwtProvider)
  214. }