utils.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. "strconv"
  25. "strings"
  26. tpl "text/template"
  27. "time"
  28. "unicode"
  29. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  30. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  31. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  32. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  33. "github.com/external-secrets/external-secrets/pkg/template/v2"
  34. )
  35. const (
  36. errParse = "unable to parse transform template: %s"
  37. errExecute = "unable to execute transform template: %s"
  38. )
  39. var (
  40. errKeyNotFound = errors.New("key not found")
  41. unicodeRegex = regexp.MustCompile(`_U([0-9a-fA-F]{4,5})_`)
  42. )
  43. // JSONMarshal takes an interface and returns a new escaped and encoded byte slice.
  44. func JSONMarshal(t any) ([]byte, error) {
  45. buffer := &bytes.Buffer{}
  46. encoder := json.NewEncoder(buffer)
  47. encoder.SetEscapeHTML(false)
  48. err := encoder.Encode(t)
  49. return bytes.TrimRight(buffer.Bytes(), "\n"), err
  50. }
  51. // MergeByteMap merges map of byte slices.
  52. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  53. for k, v := range src {
  54. dst[k] = v
  55. }
  56. return dst
  57. }
  58. func RewriteMap(operations []esv1beta1.ExternalSecretRewrite, in map[string][]byte) (map[string][]byte, error) {
  59. out := in
  60. var err error
  61. for i, op := range operations {
  62. if op.Regexp != nil {
  63. out, err = RewriteRegexp(*op.Regexp, out)
  64. if err != nil {
  65. return nil, fmt.Errorf("failed rewriting regexp operation[%v]: %w", i, err)
  66. }
  67. }
  68. if op.Transform != nil {
  69. out, err = RewriteTransform(*op.Transform, out)
  70. if err != nil {
  71. return nil, fmt.Errorf("failed rewriting transform operation[%v]: %w", i, err)
  72. }
  73. }
  74. }
  75. return out, nil
  76. }
  77. // RewriteRegexp rewrites a single Regexp Rewrite Operation.
  78. func RewriteRegexp(operation esv1beta1.ExternalSecretRewriteRegexp, in map[string][]byte) (map[string][]byte, error) {
  79. out := make(map[string][]byte)
  80. re, err := regexp.Compile(operation.Source)
  81. if err != nil {
  82. return nil, err
  83. }
  84. for key, value := range in {
  85. newKey := re.ReplaceAllString(key, operation.Target)
  86. out[newKey] = value
  87. }
  88. return out, nil
  89. }
  90. // RewriteTransform applies string transformation on each secret key name to rewrite.
  91. func RewriteTransform(operation esv1beta1.ExternalSecretRewriteTransform, in map[string][]byte) (map[string][]byte, error) {
  92. out := make(map[string][]byte)
  93. for key, value := range in {
  94. data := map[string][]byte{
  95. "value": []byte(key),
  96. }
  97. result, err := transform(operation.Template, data)
  98. if err != nil {
  99. return nil, err
  100. }
  101. newKey := string(result)
  102. out[newKey] = value
  103. }
  104. return out, nil
  105. }
  106. func transform(val string, data map[string][]byte) ([]byte, error) {
  107. strValData := make(map[string]string, len(data))
  108. for k := range data {
  109. strValData[k] = string(data[k])
  110. }
  111. t, err := tpl.New("transform").
  112. Funcs(template.FuncMap()).
  113. Parse(val)
  114. if err != nil {
  115. return nil, fmt.Errorf(errParse, err)
  116. }
  117. buf := bytes.NewBuffer(nil)
  118. err = t.Execute(buf, strValData)
  119. if err != nil {
  120. return nil, fmt.Errorf(errExecute, err)
  121. }
  122. return buf.Bytes(), nil
  123. }
  124. // DecodeValues decodes values from a secretMap.
  125. func DecodeMap(strategy esv1beta1.ExternalSecretDecodingStrategy, in map[string][]byte) (map[string][]byte, error) {
  126. out := make(map[string][]byte, len(in))
  127. for k, v := range in {
  128. val, err := Decode(strategy, v)
  129. if err != nil {
  130. return nil, fmt.Errorf("failure decoding key %v: %w", k, err)
  131. }
  132. out[k] = val
  133. }
  134. return out, nil
  135. }
  136. func Decode(strategy esv1beta1.ExternalSecretDecodingStrategy, in []byte) ([]byte, error) {
  137. switch strategy {
  138. case esv1beta1.ExternalSecretDecodeBase64:
  139. out, err := base64.StdEncoding.DecodeString(string(in))
  140. if err != nil {
  141. return nil, err
  142. }
  143. return out, nil
  144. case esv1beta1.ExternalSecretDecodeBase64URL:
  145. out, err := base64.URLEncoding.DecodeString(string(in))
  146. if err != nil {
  147. return nil, err
  148. }
  149. return out, nil
  150. case esv1beta1.ExternalSecretDecodeNone:
  151. return in, nil
  152. // default when stored version is v1alpha1
  153. case "":
  154. return in, nil
  155. case esv1beta1.ExternalSecretDecodeAuto:
  156. out, err := Decode(esv1beta1.ExternalSecretDecodeBase64, in)
  157. if err != nil {
  158. out, err := Decode(esv1beta1.ExternalSecretDecodeBase64URL, in)
  159. if err != nil {
  160. return Decode(esv1beta1.ExternalSecretDecodeNone, in)
  161. }
  162. return out, nil
  163. }
  164. return out, nil
  165. default:
  166. return nil, fmt.Errorf("decoding strategy %v is not supported", strategy)
  167. }
  168. }
  169. func ValidateKeys(in map[string][]byte) bool {
  170. for key := range in {
  171. for _, v := range key {
  172. if !unicode.IsNumber(v) &&
  173. !unicode.IsLetter(v) &&
  174. v != '-' &&
  175. v != '.' &&
  176. v != '_' {
  177. return false
  178. }
  179. }
  180. }
  181. return true
  182. }
  183. // ConvertKeys converts a secret map into a valid key.
  184. // Replaces any non-alphanumeric characters depending on convert strategy.
  185. func ConvertKeys(strategy esv1beta1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  186. out := make(map[string][]byte, len(in))
  187. for k, v := range in {
  188. key := convert(strategy, k)
  189. if _, exists := out[key]; exists {
  190. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  191. }
  192. out[key] = v
  193. }
  194. return out, nil
  195. }
  196. func convert(strategy esv1beta1.ExternalSecretConversionStrategy, str string) string {
  197. rs := []rune(str)
  198. newName := make([]string, len(rs))
  199. for rk, rv := range rs {
  200. if !unicode.IsNumber(rv) &&
  201. !unicode.IsLetter(rv) &&
  202. rv != '-' &&
  203. rv != '.' &&
  204. rv != '_' {
  205. switch strategy {
  206. case esv1beta1.ExternalSecretConversionDefault:
  207. newName[rk] = "_"
  208. case esv1beta1.ExternalSecretConversionUnicode:
  209. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  210. default:
  211. newName[rk] = string(rv)
  212. }
  213. } else {
  214. newName[rk] = string(rv)
  215. }
  216. }
  217. return strings.Join(newName, "")
  218. }
  219. // ReverseKeys reverses a secret map into a valid key map as expected by push secrets.
  220. // Replaces the unicode encoded representation characters back to the actual unicode character depending on convert strategy.
  221. func ReverseKeys(strategy esv1alpha1.PushSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  222. out := make(map[string][]byte, len(in))
  223. for k, v := range in {
  224. key := reverse(strategy, k)
  225. if _, exists := out[key]; exists {
  226. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  227. }
  228. out[key] = v
  229. }
  230. return out, nil
  231. }
  232. func reverse(strategy esv1alpha1.PushSecretConversionStrategy, str string) string {
  233. switch strategy {
  234. case esv1alpha1.PushSecretConversionReverseUnicode:
  235. matches := unicodeRegex.FindAllStringSubmatchIndex(str, -1)
  236. for i := len(matches) - 1; i >= 0; i-- {
  237. match := matches[i]
  238. start := match[0]
  239. end := match[1]
  240. unicodeHex := str[match[2]:match[3]]
  241. unicodeInt, err := strconv.ParseInt(unicodeHex, 16, 32)
  242. if err != nil {
  243. continue // Skip invalid unicode representations
  244. }
  245. unicodeChar := fmt.Sprintf("%c", unicodeInt)
  246. str = str[:start] + unicodeChar + str[end:]
  247. }
  248. return str
  249. case esv1alpha1.PushSecretConversionNone:
  250. return str
  251. default:
  252. return str
  253. }
  254. }
  255. // MergeStringMap performs a deep clone from src to dest.
  256. func MergeStringMap(dest, src map[string]string) {
  257. for k, v := range src {
  258. dest[k] = v
  259. }
  260. }
  261. var (
  262. ErrUnexpectedKey = errors.New("unexpected key in data")
  263. ErrSecretType = errors.New("can not handle secret value with type")
  264. )
  265. func GetByteValueFromMap(data map[string]any, key string) ([]byte, error) {
  266. v, ok := data[key]
  267. if !ok {
  268. return nil, fmt.Errorf("%w: %s", ErrUnexpectedKey, key)
  269. }
  270. return GetByteValue(v)
  271. }
  272. func GetByteValue(v any) ([]byte, error) {
  273. switch t := v.(type) {
  274. case string:
  275. return []byte(t), nil
  276. case map[string]any:
  277. return json.Marshal(t)
  278. case []string:
  279. return []byte(strings.Join(t, "\n")), nil
  280. case json.RawMessage:
  281. return t, nil
  282. case []byte:
  283. return t, nil
  284. // also covers int and float32 due to json.Marshal
  285. case float64:
  286. return []byte(strconv.FormatFloat(t, 'f', -1, 64)), nil
  287. case json.Number:
  288. return []byte(t.String()), nil
  289. case []any:
  290. return json.Marshal(t)
  291. case bool:
  292. return []byte(strconv.FormatBool(t)), nil
  293. case nil:
  294. return []byte(nil), nil
  295. default:
  296. return nil, fmt.Errorf("%w: %T", ErrSecretType, t)
  297. }
  298. }
  299. // IsNil checks if an Interface is nil.
  300. func IsNil(i any) bool {
  301. if i == nil {
  302. return true
  303. }
  304. value := reflect.ValueOf(i)
  305. if value.Type().Kind() == reflect.Ptr {
  306. return value.IsNil()
  307. }
  308. return false
  309. }
  310. // ObjectHash calculates md5 sum of the data contained in the secret.
  311. //
  312. //nolint:gosec
  313. func ObjectHash(object any) string {
  314. textualVersion := fmt.Sprintf("%+v", object)
  315. return fmt.Sprintf("%x", md5.Sum([]byte(textualVersion)))
  316. }
  317. func ErrorContains(out error, want string) bool {
  318. if out == nil {
  319. return want == ""
  320. }
  321. if want == "" {
  322. return false
  323. }
  324. return strings.Contains(out.Error(), want)
  325. }
  326. var (
  327. errNamespaceNotAllowed = errors.New("namespace not allowed with namespaced SecretStore")
  328. errRequireNamespace = errors.New("cluster scope requires namespace")
  329. )
  330. // ValidateSecretSelector just checks if the namespace field is present/absent
  331. // depending on the secret store type.
  332. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  333. func ValidateSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  334. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  335. if clusterScope && ref.Namespace == nil {
  336. return errRequireNamespace
  337. }
  338. if !clusterScope && ref.Namespace != nil {
  339. return errNamespaceNotAllowed
  340. }
  341. return nil
  342. }
  343. // ValidateReferentSecretSelector allows
  344. // cluster scoped store without namespace
  345. // this should replace above ValidateServiceAccountSelector once all providers
  346. // support referent auth.
  347. func ValidateReferentSecretSelector(store esv1beta1.GenericStore, ref esmeta.SecretKeySelector) error {
  348. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  349. if !clusterScope && ref.Namespace != nil {
  350. return errNamespaceNotAllowed
  351. }
  352. return nil
  353. }
  354. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  355. // depending on the secret store type.
  356. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  357. func ValidateServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  358. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  359. if clusterScope && ref.Namespace == nil {
  360. return errRequireNamespace
  361. }
  362. if !clusterScope && ref.Namespace != nil {
  363. return errNamespaceNotAllowed
  364. }
  365. return nil
  366. }
  367. // ValidateReferentServiceAccountSelector allows
  368. // cluster scoped store without namespace
  369. // this should replace above ValidateServiceAccountSelector once all providers
  370. // support referent auth.
  371. func ValidateReferentServiceAccountSelector(store esv1beta1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  372. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind
  373. if !clusterScope && ref.Namespace != nil {
  374. return errNamespaceNotAllowed
  375. }
  376. return nil
  377. }
  378. func NetworkValidate(endpoint string, timeout time.Duration) error {
  379. hostname, err := url.Parse(endpoint)
  380. if err != nil {
  381. return fmt.Errorf("could not parse url: %w", err)
  382. }
  383. host := hostname.Hostname()
  384. port := hostname.Port()
  385. if port == "" {
  386. port = "443"
  387. }
  388. url := fmt.Sprintf("%v:%v", host, port)
  389. conn, err := net.DialTimeout("tcp", url, timeout)
  390. if err != nil {
  391. return fmt.Errorf("error accessing external store: %w", err)
  392. }
  393. defer conn.Close()
  394. return nil
  395. }
  396. func Deref[V any](v *V) V {
  397. if v == nil {
  398. // Create zero value
  399. var res V
  400. return res
  401. }
  402. return *v
  403. }
  404. func Ptr[T any](i T) *T {
  405. return &i
  406. }
  407. func ConvertToType[T any](obj any) (T, error) {
  408. var v T
  409. data, err := json.Marshal(obj)
  410. if err != nil {
  411. return v, fmt.Errorf("failed to marshal object: %w", err)
  412. }
  413. if err = json.Unmarshal(data, &v); err != nil {
  414. return v, fmt.Errorf("failed to unmarshal object: %w", err)
  415. }
  416. return v, nil
  417. }
  418. // FetchValueFromMetadata fetches a key from a metadata if it exists. It will recursively look in
  419. // embedded values as well. Must be a unique key, otherwise it will just return the first
  420. // occurrence.
  421. func FetchValueFromMetadata[T any](key string, data *apiextensionsv1.JSON, def T) (t T, _ error) {
  422. if data == nil {
  423. return def, nil
  424. }
  425. m := map[string]any{}
  426. if err := json.Unmarshal(data.Raw, &m); err != nil {
  427. return t, fmt.Errorf("failed to parse JSON raw data: %w", err)
  428. }
  429. v, err := dig[T](key, m)
  430. if err != nil {
  431. if errors.Is(err, errKeyNotFound) {
  432. return def, nil
  433. }
  434. }
  435. return v, nil
  436. }
  437. func dig[T any](key string, data map[string]any) (t T, _ error) {
  438. if v, ok := data[key]; ok {
  439. c, k := v.(T)
  440. if !k {
  441. return t, fmt.Errorf("failed to convert value to the desired type; was: %T", v)
  442. }
  443. return c, nil
  444. }
  445. for _, v := range data {
  446. if ty, ok := v.(map[string]any); ok {
  447. return dig[T](key, ty)
  448. }
  449. }
  450. return t, errKeyNotFound
  451. }