credentials.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // /*
  2. // Copyright © The ESO Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // */
  16. package auth
  17. import (
  18. "context"
  19. "fmt"
  20. "strings"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  24. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  25. )
  26. // CredentialRequest represents a lazily resolved credential request.
  27. type CredentialRequest struct {
  28. cacheKey string
  29. resolve func(context.Context) (TokenExchangeCredentials, error)
  30. }
  31. // CacheKey returns the key used to cache the credential request.
  32. func (r CredentialRequest) CacheKey() string {
  33. return r.cacheKey
  34. }
  35. // Resolve resolves the credential request.
  36. func (r CredentialRequest) Resolve(ctx context.Context) (TokenExchangeCredentials, error) {
  37. return r.resolve(ctx)
  38. }
  39. // TokenExchangeCredentials represents credentials accepted by the IAM token exchanger.
  40. type TokenExchangeCredentials interface {
  41. isTokenExchangeCredentials()
  42. }
  43. // TokenCredentials contains an IAM token.
  44. type TokenCredentials struct {
  45. Token string
  46. }
  47. // GetTokenCredentials reads token credentials from a Kubernetes Secret.
  48. func GetTokenCredentials(ctx context.Context, secret *esmeta.SecretKeySelector, store esv1.GenericStore, kube client.Client, namespace string) (TokenCredentials, error) {
  49. iamToken, err := resolvers.SecretKeyRef(
  50. ctx,
  51. kube,
  52. store.GetKind(),
  53. namespace,
  54. secret,
  55. )
  56. if err != nil {
  57. return TokenCredentials{}, fmt.Errorf("read token secret %s/%s: %w", namespace, secret.Name, err)
  58. }
  59. return TokenCredentials{
  60. Token: strings.TrimSpace(iamToken),
  61. }, nil
  62. }
  63. // NewCredentialRequest creates a credential request with the provided cache key and resolver.
  64. func NewCredentialRequest(cacheKey string, resolve func(context.Context) (TokenExchangeCredentials, error)) *CredentialRequest {
  65. return &CredentialRequest{
  66. cacheKey: cacheKey,
  67. resolve: resolve,
  68. }
  69. }
  70. var _ TokenExchangeCredentials = &ResolvedServiceAccountCreds{}
  71. var _ TokenExchangeCredentials = &ResolvedFederatedCredentials{}