key.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 crd
  14. import (
  15. "fmt"
  16. "strings"
  17. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  18. )
  19. // parseRemoteRefKey interprets ExternalSecret remoteRef.key (and PushSecret remote key).
  20. //
  21. // SecretStore: '/' is not allowed; the object name is the full key. The API
  22. // namespace comes only from the store namespace, never from the key.
  23. //
  24. // ClusterSecretStore: if the key contains '/', it must be namespace/objectName (first
  25. // slash separates). If there is no '/', the key is the object name for a cluster-scoped
  26. // resource only.
  27. //
  28. // Returns objectName, keyNamespace (non-nil when namespace/objectName form was used), err.
  29. func parseRemoteRefKey(storeKind, remoteKey string) (objectName string, keyNamespace *string, err error) {
  30. if remoteKey == "" {
  31. return "", nil, fmt.Errorf("crd: remoteRef.key must not be empty")
  32. }
  33. switch storeKind {
  34. case esv1.SecretStoreKind:
  35. if strings.Contains(remoteKey, "/") {
  36. return "", nil, fmt.Errorf("crd: remoteRef.key must not contain '/' for SecretStore; namespace is fixed to the store namespace")
  37. }
  38. return remoteKey, nil, nil
  39. case esv1.ClusterSecretStoreKind:
  40. ns, name, ok := strings.Cut(remoteKey, "/")
  41. if !ok {
  42. return remoteKey, nil, nil
  43. }
  44. if ns == "" {
  45. return "", nil, fmt.Errorf("crd: invalid remoteRef.key %q: namespace segment before '/' must not be empty", remoteKey)
  46. }
  47. if name == "" {
  48. return "", nil, fmt.Errorf("crd: invalid remoteRef.key %q: object name after '/' must not be empty", remoteKey)
  49. }
  50. if strings.Contains(name, "/") {
  51. return "", nil, fmt.Errorf("crd: invalid remoteRef.key %q: must be in \"namespace/objectName\" form (exactly one '/')", remoteKey)
  52. }
  53. return name, &ns, nil
  54. default:
  55. return remoteKey, nil, nil
  56. }
  57. }