utils.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, len(in))
  34. for k, v := range in {
  35. key := convert(strategy, k)
  36. if _, exists := out[key]; exists {
  37. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  38. }
  39. out[key] = v
  40. }
  41. return out, nil
  42. }
  43. func convert(strategy esv1beta1.ExternalSecretConversionStrategy, str string) string {
  44. rs := []rune(str)
  45. newName := make([]string, len(rs))
  46. for rk, rv := range rs {
  47. if !unicode.IsNumber(rv) &&
  48. !unicode.IsLetter(rv) &&
  49. rv != '-' &&
  50. rv != '.' &&
  51. rv != '_' {
  52. switch strategy {
  53. case esv1beta1.ExternalSecretConversionDefault:
  54. newName[rk] = "_"
  55. case esv1beta1.ExternalSecretConversionUnicode:
  56. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  57. }
  58. } else {
  59. newName[rk] = string(rv)
  60. }
  61. }
  62. return strings.Join(newName, "")
  63. }
  64. // MergeStringMap performs a deep clone from src to dest.
  65. func MergeStringMap(dest, src map[string]string) {
  66. for k, v := range src {
  67. dest[k] = v
  68. }
  69. }
  70. // IsNil checks if an Interface is nil.
  71. func IsNil(i interface{}) bool {
  72. if i == nil {
  73. return true
  74. }
  75. value := reflect.ValueOf(i)
  76. if value.Type().Kind() == reflect.Ptr {
  77. return value.IsNil()
  78. }
  79. return false
  80. }
  81. // ObjectHash calculates md5 sum of the data contained in the secret.
  82. // nolint:gosec
  83. func ObjectHash(object interface{}) string {
  84. textualVersion := fmt.Sprintf("%+v", object)
  85. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  86. }
  87. func ErrorContains(out error, want string) bool {
  88. if out == nil {
  89. return want == ""
  90. }
  91. if want == "" {
  92. return false
  93. }
  94. return strings.Contains(out.Error(), want)
  95. }
  96. // ValidateSecretSelector just checks if the namespace field is present/absent
  97. // depending on the secret store type.
  98. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  99. func ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  100. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  101. if clusterScope && ref.Namespace == nil {
  102. return fmt.Errorf("cluster scope requires namespace")
  103. }
  104. if !clusterScope && ref.Namespace != nil {
  105. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  106. }
  107. return nil
  108. }
  109. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  110. // depending on the secret store type.
  111. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  112. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  113. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  114. if clusterScope && ref.Namespace == nil {
  115. return fmt.Errorf("cluster scope requires namespace")
  116. }
  117. if !clusterScope && ref.Namespace != nil {
  118. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  119. }
  120. return nil
  121. }