utils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package utils
  13. import (
  14. // nolint:gosec
  15. "crypto/md5"
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "unicode"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  22. )
  23. // MergeByteMap merges map of byte slices.
  24. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  25. for k, v := range src {
  26. dst[k] = v
  27. }
  28. return dst
  29. }
  30. // ConvertKeys converts a secret map into a valid key.
  31. // Replaces any non-alphanumeric characters depending on convert strategy.
  32. func ConvertKeys(strategy esv1beta1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  33. out := make(map[string][]byte)
  34. for k, v := range in {
  35. rs := []rune(k)
  36. newName := make([]string, len(rs))
  37. for rk, rv := range rs {
  38. if !unicode.IsNumber(rv) &&
  39. !unicode.IsLetter(rv) &&
  40. rv != '-' &&
  41. rv != '.' &&
  42. rv != '_' {
  43. switch strategy {
  44. case esv1beta1.ExternalSecretConversionDefault:
  45. newName[rk] = "_"
  46. case esv1beta1.ExternalSecretConversionUnicode:
  47. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  48. default:
  49. return nil, fmt.Errorf("unknown conversion strategy: %s", strategy)
  50. }
  51. } else {
  52. newName[rk] = string(rv)
  53. }
  54. }
  55. out[strings.Join(newName, "")] = v
  56. }
  57. return out, nil
  58. }
  59. // MergeStringMap performs a deep clone from src to dest.
  60. func MergeStringMap(dest, src map[string]string) {
  61. for k, v := range src {
  62. dest[k] = v
  63. }
  64. }
  65. // IsNil checks if an Interface is nil.
  66. func IsNil(i interface{}) bool {
  67. if i == nil {
  68. return true
  69. }
  70. value := reflect.ValueOf(i)
  71. if value.Type().Kind() == reflect.Ptr {
  72. return value.IsNil()
  73. }
  74. return false
  75. }
  76. // ObjectHash calculates md5 sum of the data contained in the secret.
  77. // nolint:gosec
  78. func ObjectHash(object interface{}) string {
  79. textualVersion := fmt.Sprintf("%+v", object)
  80. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  81. }
  82. func ErrorContains(out error, want string) bool {
  83. if out == nil {
  84. return want == ""
  85. }
  86. if want == "" {
  87. return false
  88. }
  89. return strings.Contains(out.Error(), want)
  90. }
  91. // ValidateSecretSelector just checks if the namespace field is present/absent
  92. // depending on the secret store type.
  93. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  94. func ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  95. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  96. if clusterScope && ref.Namespace == nil {
  97. return fmt.Errorf("cluster scope requires namespace")
  98. }
  99. if !clusterScope && ref.Namespace != nil {
  100. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  101. }
  102. return nil
  103. }
  104. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  105. // depending on the secret store type.
  106. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  107. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  108. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  109. if clusterScope && ref.Namespace == nil {
  110. return fmt.Errorf("cluster scope requires namespace")
  111. }
  112. if !clusterScope && ref.Namespace != nil {
  113. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  114. }
  115. return nil
  116. }