auth_userpass.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "strings"
  17. authuserpass "github.com/hashicorp/vault/api/auth/userpass"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. "github.com/external-secrets/external-secrets/runtime/constants"
  20. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  21. "github.com/external-secrets/external-secrets/runtime/metrics"
  22. )
  23. func setUserPassAuthToken(ctx context.Context, v *client) (bool, error) {
  24. userPassAuth := v.store.Auth.UserPass
  25. if userPassAuth != nil {
  26. err := v.requestTokenWithUserPassAuth(ctx, userPassAuth)
  27. if err != nil {
  28. return true, err
  29. }
  30. return true, nil
  31. }
  32. return false, nil
  33. }
  34. func (c *client) requestTokenWithUserPassAuth(ctx context.Context, userPassAuth *esv1.VaultUserPassAuth) error {
  35. username := strings.TrimSpace(userPassAuth.Username)
  36. password, err := resolvers.SecretKeyRef(ctx, c.kube, c.storeKind, c.namespace, &userPassAuth.SecretRef)
  37. if err != nil {
  38. return err
  39. }
  40. pass := authuserpass.Password{FromString: password}
  41. l, err := authuserpass.NewUserpassAuth(username, &pass, authuserpass.WithMountPath(userPassAuth.Path))
  42. if err != nil {
  43. return err
  44. }
  45. _, err = c.auth.Login(ctx, l)
  46. metrics.ObserveAPICall(constants.ProviderHCVault, constants.CallHCVaultLogin, err)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }