auth.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 akeyless
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  19. )
  20. const (
  21. errFetchAccessIDSecret = "could not fetch accessID secret: %w"
  22. errFetchAccessTypeSecret = "could not fetch AccessType secret: %w"
  23. errFetchAccessTypeParamSecret = "could not fetch AccessTypeParam secret: %w"
  24. errMissingSAK = "missing SecretAccessKey"
  25. errMissingAKID = "missing AccessKeyID"
  26. )
  27. func (a *akeylessBase) TokenFromSecretRef(ctx context.Context) (string, error) {
  28. prov, err := GetAKeylessProvider(a.store)
  29. if err != nil {
  30. return "", err
  31. }
  32. if prov.Auth.KubernetesAuth != nil {
  33. auth := prov.Auth.KubernetesAuth
  34. return a.GetToken(ctx, auth.AccessID, "k8s", auth.K8sConfName, auth)
  35. }
  36. accessID, err := resolvers.SecretKeyRef(
  37. ctx,
  38. a.kube,
  39. a.storeKind,
  40. a.namespace,
  41. &prov.Auth.SecretRef.AccessID,
  42. )
  43. if err != nil {
  44. return "", fmt.Errorf(errFetchAccessIDSecret, err)
  45. }
  46. accessType, err := resolvers.SecretKeyRef(
  47. ctx,
  48. a.kube,
  49. a.storeKind,
  50. a.namespace,
  51. &prov.Auth.SecretRef.AccessType,
  52. )
  53. if err != nil {
  54. return "", fmt.Errorf(errFetchAccessTypeSecret, err)
  55. }
  56. accessTypeParam, err := resolvers.SecretKeyRef(
  57. ctx,
  58. a.kube,
  59. a.storeKind,
  60. a.namespace,
  61. &prov.Auth.SecretRef.AccessTypeParam,
  62. )
  63. if err != nil {
  64. return "", fmt.Errorf(errFetchAccessTypeParamSecret, err)
  65. }
  66. if accessID == "" {
  67. return "", errors.New(errMissingSAK)
  68. }
  69. if accessType == "" {
  70. return "", errors.New(errMissingAKID)
  71. }
  72. return a.GetToken(ctx, accessID, accessType, accessTypeParam, prov.Auth.KubernetesAuth)
  73. }