utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  21. )
  22. // MergeByteMap merges map of byte slices.
  23. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  24. for k, v := range src {
  25. dst[k] = v
  26. }
  27. return dst
  28. }
  29. // MergeStringMap performs a deep clone from src to dest.
  30. func MergeStringMap(dest, src map[string]string) {
  31. for k, v := range src {
  32. dest[k] = v
  33. }
  34. }
  35. // IsNil checks if an Interface is nil.
  36. func IsNil(i interface{}) bool {
  37. if i == nil {
  38. return true
  39. }
  40. value := reflect.ValueOf(i)
  41. if value.Type().Kind() == reflect.Ptr {
  42. return value.IsNil()
  43. }
  44. return false
  45. }
  46. // ObjectHash calculates md5 sum of the data contained in the secret.
  47. // nolint:gosec
  48. func ObjectHash(object interface{}) string {
  49. textualVersion := fmt.Sprintf("%+v", object)
  50. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  51. }
  52. func ErrorContains(out error, want string) bool {
  53. if out == nil {
  54. return want == ""
  55. }
  56. if want == "" {
  57. return false
  58. }
  59. return strings.Contains(out.Error(), want)
  60. }
  61. // ValidateSecretSelector just checks if the namespace field is present/absent
  62. // depending on the secret store type.
  63. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  64. func ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  65. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  66. if clusterScope && ref.Namespace == nil {
  67. return fmt.Errorf("cluster scope requires namespace")
  68. }
  69. if !clusterScope && ref.Namespace != nil {
  70. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  71. }
  72. return nil
  73. }
  74. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  75. // depending on the secret store type.
  76. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  77. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  78. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  79. if clusterScope && ref.Namespace == nil {
  80. return fmt.Errorf("cluster scope requires namespace")
  81. }
  82. if !clusterScope && ref.Namespace != nil {
  83. return fmt.Errorf("namespace not allowed with namespaced SecretStore")
  84. }
  85. return nil
  86. }