auth_approle.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. "strings"
  18. "github.com/hashicorp/vault/api/auth/approle"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. "github.com/external-secrets/external-secrets/pkg/constants"
  21. "github.com/external-secrets/external-secrets/pkg/esutils/resolvers"
  22. "github.com/external-secrets/external-secrets/pkg/metrics"
  23. )
  24. const (
  25. errInvalidAppRoleID = "invalid Auth.AppRole: neither `roleId` nor `roleRef` was supplied"
  26. )
  27. func setAppRoleToken(ctx context.Context, v *client) (bool, error) {
  28. appRole := v.store.Auth.AppRole
  29. if appRole != nil {
  30. err := v.requestTokenWithAppRoleRef(ctx, appRole)
  31. if err != nil {
  32. return true, err
  33. }
  34. return true, nil
  35. }
  36. return false, nil
  37. }
  38. func (c *client) requestTokenWithAppRoleRef(ctx context.Context, appRole *esv1.VaultAppRole) error {
  39. var err error
  40. var roleID string // becomes the RoleID used to authenticate with HashiCorp Vault
  41. // prefer .auth.appRole.roleId, fallback to .auth.appRole.roleRef, give up after that.
  42. if appRole.RoleID != "" { // use roleId from CRD, if configured
  43. roleID = strings.TrimSpace(appRole.RoleID)
  44. } else if appRole.RoleRef != nil { // use RoleID from Secret, if configured
  45. roleID, err = resolvers.SecretKeyRef(ctx, c.kube, c.storeKind, c.namespace, appRole.RoleRef)
  46. if err != nil {
  47. return err
  48. }
  49. } else { // we ran out of ways to get RoleID. return an appropriate error
  50. return errors.New(errInvalidAppRoleID)
  51. }
  52. secretID, err := resolvers.SecretKeyRef(ctx, c.kube, c.storeKind, c.namespace, &appRole.SecretRef)
  53. if err != nil {
  54. return err
  55. }
  56. secret := approle.SecretID{FromString: secretID}
  57. appRoleClient, err := approle.NewAppRoleAuth(roleID, &secret, approle.WithMountPath(appRole.Path))
  58. if err != nil {
  59. return err
  60. }
  61. _, err = c.auth.Login(ctx, appRoleClient)
  62. metrics.ObserveAPICall(constants.ProviderHCVault, constants.CallHCVaultLogin, err)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }