utils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "runtime"
  19. "strings"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. )
  22. const (
  23. notImplemented = "not implemented: %s"
  24. testSecret = "test-secret"
  25. defaultVersion = "default"
  26. )
  27. // MergeByteMap merges map of byte slices.
  28. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  29. for k, v := range src {
  30. dst[k] = v
  31. }
  32. return dst
  33. }
  34. // MergeStringMap performs a deep clone from src to dest.
  35. func MergeStringMap(dest, src map[string]string) {
  36. for k, v := range src {
  37. dest[k] = v
  38. }
  39. }
  40. // IsNil checks if an Interface is nil.
  41. func IsNil(i interface{}) bool {
  42. if i == nil {
  43. return true
  44. }
  45. value := reflect.ValueOf(i)
  46. if value.Type().Kind() == reflect.Ptr {
  47. return value.IsNil()
  48. }
  49. return false
  50. }
  51. // ObjectHash calculates md5 sum of the data contained in the secret.
  52. // nolint:gosec
  53. func ObjectHash(object interface{}) string {
  54. textualVersion := fmt.Sprintf("%+v", object)
  55. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  56. }
  57. func ErrorContains(out error, want string) bool {
  58. if out == nil {
  59. return want == ""
  60. }
  61. if want == "" {
  62. return false
  63. }
  64. return strings.Contains(out.Error(), want)
  65. }
  66. func MakeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  67. return MakeValidRefWithParams(testSecret, "", defaultVersion)
  68. }
  69. func MakeValidRefWithParams(key, property, version string) *esv1alpha1.ExternalSecretDataRemoteRef {
  70. return &esv1alpha1.ExternalSecretDataRemoteRef{
  71. Key: key,
  72. Property: property,
  73. Version: version,
  74. }
  75. }
  76. func MakeValidRefFrom() *esv1alpha1.ExternalSecretDataFromRemoteRef {
  77. return MakeValidRefFromWithParams(testSecret, "", defaultVersion)
  78. }
  79. func MakeValidRefFromWithParams(key, property, version string) *esv1alpha1.ExternalSecretDataFromRemoteRef {
  80. return &esv1alpha1.ExternalSecretDataFromRemoteRef{
  81. Extract: esv1alpha1.ExternalSecretExtract{
  82. Key: key,
  83. Property: property,
  84. Version: version,
  85. },
  86. }
  87. }
  88. func ThrowNotImplemented() error {
  89. pc := make([]uintptr, 10) // at least 1 entry needed
  90. runtime.Callers(2, pc)
  91. f := runtime.FuncForPC(pc[0])
  92. return fmt.Errorf(notImplemented, f.Name())
  93. }