utils.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "errors"
  17. "fmt"
  18. "net"
  19. "net/url"
  20. "reflect"
  21. "strings"
  22. "time"
  23. "unicode"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  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. // ConvertKeys converts a secret map into a valid key.
  35. // Replaces any non-alphanumeric characters depending on convert strategy.
  36. func ConvertKeys(strategy esv1beta1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  37. out := make(map[string][]byte, len(in))
  38. for k, v := range in {
  39. key := convert(strategy, k)
  40. if _, exists := out[key]; exists {
  41. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  42. }
  43. out[key] = v
  44. }
  45. return out, nil
  46. }
  47. func convert(strategy esv1beta1.ExternalSecretConversionStrategy, str string) string {
  48. rs := []rune(str)
  49. newName := make([]string, len(rs))
  50. for rk, rv := range rs {
  51. if !unicode.IsNumber(rv) &&
  52. !unicode.IsLetter(rv) &&
  53. rv != '-' &&
  54. rv != '.' &&
  55. rv != '_' {
  56. switch strategy {
  57. case esv1beta1.ExternalSecretConversionDefault:
  58. newName[rk] = "_"
  59. case esv1beta1.ExternalSecretConversionUnicode:
  60. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  61. }
  62. } else {
  63. newName[rk] = string(rv)
  64. }
  65. }
  66. return strings.Join(newName, "")
  67. }
  68. // MergeStringMap performs a deep clone from src to dest.
  69. func MergeStringMap(dest, src map[string]string) {
  70. for k, v := range src {
  71. dest[k] = v
  72. }
  73. }
  74. // IsNil checks if an Interface is nil.
  75. func IsNil(i interface{}) bool {
  76. if i == nil {
  77. return true
  78. }
  79. value := reflect.ValueOf(i)
  80. if value.Type().Kind() == reflect.Ptr {
  81. return value.IsNil()
  82. }
  83. return false
  84. }
  85. // ObjectHash calculates md5 sum of the data contained in the secret.
  86. // nolint:gosec
  87. func ObjectHash(object interface{}) string {
  88. textualVersion := fmt.Sprintf("%+v", object)
  89. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  90. }
  91. func ErrorContains(out error, want string) bool {
  92. if out == nil {
  93. return want == ""
  94. }
  95. if want == "" {
  96. return false
  97. }
  98. return strings.Contains(out.Error(), want)
  99. }
  100. var (
  101. errNamespaceNotAllowed = errors.New("namespace not allowed with namespaced SecretStore")
  102. errRequireNamespace = errors.New("cluster scope requires namespace")
  103. )
  104. // ValidateSecretSelector 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 ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  108. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  109. if clusterScope && ref.Namespace == nil {
  110. return errRequireNamespace
  111. }
  112. if !clusterScope && ref.Namespace != nil {
  113. return errNamespaceNotAllowed
  114. }
  115. return nil
  116. }
  117. // ValidateReferentSecretSelector allows
  118. // cluster scoped store without namespace
  119. // this should replace above ValidateServiceAccountSelector once all providers
  120. // support referent auth.
  121. func ValidateReferentSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  122. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  123. if !clusterScope && ref.Namespace != nil {
  124. return errNamespaceNotAllowed
  125. }
  126. return nil
  127. }
  128. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  129. // depending on the secret store type.
  130. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  131. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  132. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  133. if clusterScope && ref.Namespace == nil {
  134. return errRequireNamespace
  135. }
  136. if !clusterScope && ref.Namespace != nil {
  137. return errNamespaceNotAllowed
  138. }
  139. return nil
  140. }
  141. // ValidateReferentServiceAccountSelector allows
  142. // cluster scoped store without namespace
  143. // this should replace above ValidateServiceAccountSelector once all providers
  144. // support referent auth.
  145. func ValidateReferentServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  146. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  147. if !clusterScope && ref.Namespace != nil {
  148. return errNamespaceNotAllowed
  149. }
  150. return nil
  151. }
  152. func NetworkValidate(endpoint string, timeout time.Duration) error {
  153. hostname, err := url.Parse(endpoint)
  154. if err != nil {
  155. return fmt.Errorf("could not parse url: %w", err)
  156. }
  157. host := hostname.Hostname()
  158. port := hostname.Port()
  159. if port == "" {
  160. port = "443"
  161. }
  162. url := fmt.Sprintf("%v:%v", host, port)
  163. conn, err := net.DialTimeout("tcp", url, timeout)
  164. if err != nil {
  165. return fmt.Errorf("error accessing external store: %w", err)
  166. }
  167. defer conn.Close()
  168. return nil
  169. }