utils.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "bytes"
  15. "crypto/md5" //nolint:gosec
  16. "encoding/base64"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "net"
  21. "net/url"
  22. "reflect"
  23. "regexp"
  24. "strings"
  25. tpl "text/template"
  26. "time"
  27. "unicode"
  28. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  29. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  30. template "github.com/external-secrets/external-secrets/pkg/template/v2"
  31. )
  32. const (
  33. errParse = "unable to parse transform template: %s"
  34. errExecute = "unable to execute transform template: %s"
  35. )
  36. // MergeByteMap merges map of byte slices.
  37. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  38. for k, v := range src {
  39. dst[k] = v
  40. }
  41. return dst
  42. }
  43. func RewriteMap(operations []esv1beta1.ExternalSecretRewrite, in map[string][]byte) (map[string][]byte, error) {
  44. out := in
  45. var err error
  46. for i, op := range operations {
  47. if op.Regexp != nil {
  48. out, err = RewriteRegexp(*op.Regexp, out)
  49. if err != nil {
  50. return nil, fmt.Errorf("failed rewriting regexp operation[%v]: %w", i, err)
  51. }
  52. }
  53. if op.Transform != nil {
  54. out, err = RewriteTransform(*op.Transform, out)
  55. if err != nil {
  56. return nil, fmt.Errorf("failed rewriting transform operation[%v]: %w", i, err)
  57. }
  58. }
  59. }
  60. return out, nil
  61. }
  62. // RewriteRegexp rewrites a single Regexp Rewrite Operation.
  63. func RewriteRegexp(operation esv1beta1.ExternalSecretRewriteRegexp, in map[string][]byte) (map[string][]byte, error) {
  64. out := make(map[string][]byte)
  65. re, err := regexp.Compile(operation.Source)
  66. if err != nil {
  67. return nil, err
  68. }
  69. for key, value := range in {
  70. newKey := re.ReplaceAllString(key, operation.Target)
  71. out[newKey] = value
  72. }
  73. return out, nil
  74. }
  75. // RewriteTransform applies string transformation on each secret key name to rewrite.
  76. func RewriteTransform(operation esv1beta1.ExtermalSecretRewriteTransform, in map[string][]byte) (map[string][]byte, error) {
  77. out := make(map[string][]byte)
  78. for key, value := range in {
  79. data := map[string][]byte{
  80. "value": []byte(key),
  81. }
  82. result, err := transform(operation.Template, data)
  83. if err != nil {
  84. return nil, err
  85. }
  86. newKey := string(result)
  87. out[newKey] = value
  88. }
  89. return out, nil
  90. }
  91. func transform(val string, data map[string][]byte) ([]byte, error) {
  92. strValData := make(map[string]string, len(data))
  93. for k := range data {
  94. strValData[k] = string(data[k])
  95. }
  96. t, err := tpl.New("transform").
  97. Funcs(template.FuncMap()).
  98. Parse(val)
  99. if err != nil {
  100. return nil, fmt.Errorf(errParse, err)
  101. }
  102. buf := bytes.NewBuffer(nil)
  103. err = t.Execute(buf, strValData)
  104. if err != nil {
  105. return nil, fmt.Errorf(errExecute, err)
  106. }
  107. return buf.Bytes(), nil
  108. }
  109. // DecodeValues decodes values from a secretMap.
  110. func DecodeMap(strategy esv1beta1.ExternalSecretDecodingStrategy, in map[string][]byte) (map[string][]byte, error) {
  111. out := make(map[string][]byte, len(in))
  112. for k, v := range in {
  113. val, err := Decode(strategy, v)
  114. if err != nil {
  115. return nil, fmt.Errorf("failure decoding key %v: %w", k, err)
  116. }
  117. out[k] = val
  118. }
  119. return out, nil
  120. }
  121. func Decode(strategy esv1beta1.ExternalSecretDecodingStrategy, in []byte) ([]byte, error) {
  122. switch strategy {
  123. case esv1beta1.ExternalSecretDecodeBase64:
  124. out, err := base64.StdEncoding.DecodeString(string(in))
  125. if err != nil {
  126. return nil, err
  127. }
  128. return out, nil
  129. case esv1beta1.ExternalSecretDecodeBase64URL:
  130. out, err := base64.URLEncoding.DecodeString(string(in))
  131. if err != nil {
  132. return nil, err
  133. }
  134. return out, nil
  135. case esv1beta1.ExternalSecretDecodeNone:
  136. return in, nil
  137. // default when stored version is v1alpha1
  138. case "":
  139. return in, nil
  140. case esv1beta1.ExternalSecretDecodeAuto:
  141. out, err := Decode(esv1beta1.ExternalSecretDecodeBase64, in)
  142. if err != nil {
  143. out, err := Decode(esv1beta1.ExternalSecretDecodeBase64URL, in)
  144. if err != nil {
  145. return Decode(esv1beta1.ExternalSecretDecodeNone, in)
  146. }
  147. return out, nil
  148. }
  149. return out, nil
  150. default:
  151. return nil, fmt.Errorf("decoding strategy %v is not supported", strategy)
  152. }
  153. }
  154. func ValidateKeys(in map[string][]byte) bool {
  155. for key := range in {
  156. for _, v := range key {
  157. if !unicode.IsNumber(v) &&
  158. !unicode.IsLetter(v) &&
  159. v != '-' &&
  160. v != '.' &&
  161. v != '_' {
  162. return false
  163. }
  164. }
  165. }
  166. return true
  167. }
  168. // ConvertKeys converts a secret map into a valid key.
  169. // Replaces any non-alphanumeric characters depending on convert strategy.
  170. func ConvertKeys(strategy esv1beta1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  171. out := make(map[string][]byte, len(in))
  172. for k, v := range in {
  173. key := convert(strategy, k)
  174. if _, exists := out[key]; exists {
  175. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  176. }
  177. out[key] = v
  178. }
  179. return out, nil
  180. }
  181. func convert(strategy esv1beta1.ExternalSecretConversionStrategy, str string) string {
  182. rs := []rune(str)
  183. newName := make([]string, len(rs))
  184. for rk, rv := range rs {
  185. if !unicode.IsNumber(rv) &&
  186. !unicode.IsLetter(rv) &&
  187. rv != '-' &&
  188. rv != '.' &&
  189. rv != '_' {
  190. switch strategy {
  191. case esv1beta1.ExternalSecretConversionDefault:
  192. newName[rk] = "_"
  193. case esv1beta1.ExternalSecretConversionUnicode:
  194. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  195. default:
  196. newName[rk] = string(rv)
  197. }
  198. } else {
  199. newName[rk] = string(rv)
  200. }
  201. }
  202. return strings.Join(newName, "")
  203. }
  204. // MergeStringMap performs a deep clone from src to dest.
  205. func MergeStringMap(dest, src map[string]string) {
  206. for k, v := range src {
  207. dest[k] = v
  208. }
  209. }
  210. // IsNil checks if an Interface is nil.
  211. func IsNil(i interface{}) bool {
  212. if i == nil {
  213. return true
  214. }
  215. value := reflect.ValueOf(i)
  216. if value.Type().Kind() == reflect.Ptr {
  217. return value.IsNil()
  218. }
  219. return false
  220. }
  221. // ObjectHash calculates md5 sum of the data contained in the secret.
  222. //
  223. //nolint:gosec
  224. func ObjectHash(object interface{}) string {
  225. textualVersion := fmt.Sprintf("%+v", object)
  226. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  227. }
  228. func ErrorContains(out error, want string) bool {
  229. if out == nil {
  230. return want == ""
  231. }
  232. if want == "" {
  233. return false
  234. }
  235. return strings.Contains(out.Error(), want)
  236. }
  237. var (
  238. errNamespaceNotAllowed = errors.New("namespace not allowed with namespaced SecretStore")
  239. errRequireNamespace = errors.New("cluster scope requires namespace")
  240. )
  241. // ValidateSecretSelector just checks if the namespace field is present/absent
  242. // depending on the secret store type.
  243. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  244. func ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  245. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  246. if clusterScope && ref.Namespace == nil {
  247. return errRequireNamespace
  248. }
  249. if !clusterScope && ref.Namespace != nil {
  250. return errNamespaceNotAllowed
  251. }
  252. return nil
  253. }
  254. // ValidateReferentSecretSelector allows
  255. // cluster scoped store without namespace
  256. // this should replace above ValidateServiceAccountSelector once all providers
  257. // support referent auth.
  258. func ValidateReferentSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  259. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  260. if !clusterScope && ref.Namespace != nil {
  261. return errNamespaceNotAllowed
  262. }
  263. return nil
  264. }
  265. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  266. // depending on the secret store type.
  267. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  268. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  269. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  270. if clusterScope && ref.Namespace == nil {
  271. return errRequireNamespace
  272. }
  273. if !clusterScope && ref.Namespace != nil {
  274. return errNamespaceNotAllowed
  275. }
  276. return nil
  277. }
  278. // ValidateReferentServiceAccountSelector allows
  279. // cluster scoped store without namespace
  280. // this should replace above ValidateServiceAccountSelector once all providers
  281. // support referent auth.
  282. func ValidateReferentServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  283. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  284. if !clusterScope && ref.Namespace != nil {
  285. return errNamespaceNotAllowed
  286. }
  287. return nil
  288. }
  289. func NetworkValidate(endpoint string, timeout time.Duration) error {
  290. hostname, err := url.Parse(endpoint)
  291. if err != nil {
  292. return fmt.Errorf("could not parse url: %w", err)
  293. }
  294. host := hostname.Hostname()
  295. port := hostname.Port()
  296. if port == "" {
  297. port = "443"
  298. }
  299. url := fmt.Sprintf("%v:%v", host, port)
  300. conn, err := net.DialTimeout("tcp", url, timeout)
  301. if err != nil {
  302. return fmt.Errorf("error accessing external store: %w", err)
  303. }
  304. defer conn.Close()
  305. return nil
  306. }
  307. func Deref[V any](v *V) V {
  308. if v == nil {
  309. // Create zero value
  310. var res V
  311. return res
  312. }
  313. return *v
  314. }
  315. func Ptr[T any](i T) *T {
  316. return &i
  317. }
  318. func ConvertToType[T any](obj interface{}) (T, error) {
  319. var v T
  320. data, err := json.Marshal(obj)
  321. if err != nil {
  322. return v, fmt.Errorf("failed to marshal object: %w", err)
  323. }
  324. if err = json.Unmarshal(data, &v); err != nil {
  325. return v, fmt.Errorf("failed to unmarshal object: %w", err)
  326. }
  327. return v, nil
  328. }