utils.go 3.4 KB

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