utils.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package esutils provides utility functions for the external-secrets resources.
  14. package esutils
  15. import (
  16. "bytes"
  17. "context"
  18. "crypto/sha3"
  19. "crypto/x509"
  20. "encoding/base64"
  21. "encoding/json"
  22. "encoding/pem"
  23. "errors"
  24. "fmt"
  25. "maps"
  26. "net"
  27. "net/url"
  28. "reflect"
  29. "regexp"
  30. "slices"
  31. "sort"
  32. "strconv"
  33. "strings"
  34. template "text/template"
  35. "time"
  36. "unicode"
  37. "github.com/go-logr/logr"
  38. authv1 "k8s.io/api/authentication/v1"
  39. corev1 "k8s.io/api/core/v1"
  40. discoveryv1 "k8s.io/api/discovery/v1"
  41. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  42. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  43. "k8s.io/client-go/kubernetes"
  44. "sigs.k8s.io/controller-runtime/pkg/client"
  45. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  46. "sigs.k8s.io/controller-runtime/pkg/event"
  47. "sigs.k8s.io/controller-runtime/pkg/predicate"
  48. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  49. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  50. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  51. "github.com/external-secrets/external-secrets/runtime/decoding"
  52. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  53. estemplate "github.com/external-secrets/external-secrets/runtime/template/v2"
  54. )
  55. var (
  56. errAddressesNotReady = errors.New("addresses not ready")
  57. errEndpointSlicesNotReady = errors.New("endpointSlice objects not ready")
  58. errKeyNotFound = errors.New("key not found")
  59. unicodeRegex = regexp.MustCompile(`_U([0-9a-fA-F]{4,5})_`)
  60. )
  61. // JSONMarshal takes an interface and returns a new escaped and encoded byte slice.
  62. func JSONMarshal(t any) ([]byte, error) {
  63. buffer := &bytes.Buffer{}
  64. encoder := json.NewEncoder(buffer)
  65. encoder.SetEscapeHTML(false)
  66. err := encoder.Encode(t)
  67. return bytes.TrimRight(buffer.Bytes(), "\n"), err
  68. }
  69. // MergeByteMap merges map of byte slices.
  70. func MergeByteMap(dst, src map[string][]byte) map[string][]byte {
  71. maps.Copy(dst, src)
  72. return dst
  73. }
  74. // RewriteMap applies a series of rewrite operations to the input map.
  75. func RewriteMap(operations []esv1.ExternalSecretRewrite, in map[string][]byte) (map[string][]byte, error) {
  76. out := in
  77. var err error
  78. for i, op := range operations {
  79. out, err = handleRewriteOperation(op, out)
  80. if err != nil {
  81. return nil, fmt.Errorf("failed rewrite operation[%v]: %w", i, err)
  82. }
  83. }
  84. return out, nil
  85. }
  86. func handleRewriteOperation(op esv1.ExternalSecretRewrite, in map[string][]byte) (map[string][]byte, error) {
  87. switch {
  88. case op.Merge != nil:
  89. return RewriteMerge(*op.Merge, in)
  90. case op.Regexp != nil:
  91. return RewriteRegexp(*op.Regexp, in)
  92. case op.Transform != nil:
  93. return RewriteTransform(*op.Transform, in)
  94. default:
  95. return in, nil
  96. }
  97. }
  98. // RewriteMerge merges input values according to the operation's strategy and conflict policy.
  99. func RewriteMerge(operation esv1.ExternalSecretRewriteMerge, in map[string][]byte) (map[string][]byte, error) {
  100. var out map[string][]byte
  101. mergedMap, conflicts, err := merge(operation, in)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if operation.ConflictPolicy != esv1.ExternalSecretRewriteMergeConflictPolicyIgnore {
  106. if len(conflicts) > 0 {
  107. return nil, fmt.Errorf("merge failed with conflicts: %v", strings.Join(conflicts, ", "))
  108. }
  109. }
  110. switch operation.Strategy {
  111. case esv1.ExternalSecretRewriteMergeStrategyExtract, "":
  112. out = make(map[string][]byte)
  113. for k, v := range mergedMap {
  114. byteValue, err := GetByteValue(v)
  115. if err != nil {
  116. return nil, fmt.Errorf("merge failed with failed to convert value to []byte: %w", err)
  117. }
  118. out[k] = byteValue
  119. }
  120. case esv1.ExternalSecretRewriteMergeStrategyJSON:
  121. out = make(map[string][]byte)
  122. if operation.Into == "" {
  123. return nil, fmt.Errorf("merge failed with missing 'into' field")
  124. }
  125. mergedBytes, err := JSONMarshal(mergedMap)
  126. if err != nil {
  127. return nil, fmt.Errorf("merge failed with failed to marshal merged map: %w", err)
  128. }
  129. maps.Copy(out, in)
  130. out[operation.Into] = mergedBytes
  131. }
  132. return out, nil
  133. }
  134. // merge merges the input maps and returns the merged map and a list of conflicting keys.
  135. func merge(operation esv1.ExternalSecretRewriteMerge, in map[string][]byte) (map[string]any, []string, error) {
  136. mergedMap := make(map[string]any)
  137. conflicts := make([]string, 0)
  138. // sort keys with priority keys at the end in their specified order
  139. keys := sortKeysWithPriority(operation, in)
  140. for _, key := range keys {
  141. value, exists := in[key]
  142. if !exists {
  143. if operation.PriorityPolicy == esv1.ExternalSecretRewriteMergePriorityPolicyIgnoreNotFound {
  144. continue
  145. }
  146. return nil, nil, fmt.Errorf("merge failed with key %q not found in input map", key)
  147. }
  148. var jsonMap map[string]any
  149. if err := json.Unmarshal(value, &jsonMap); err != nil {
  150. return nil, nil, fmt.Errorf("merge failed with failed to unmarshal JSON: %w", err)
  151. }
  152. for k, v := range jsonMap {
  153. if _, conflict := mergedMap[k]; conflict {
  154. conflicts = append(conflicts, k)
  155. }
  156. mergedMap[k] = v
  157. }
  158. }
  159. return mergedMap, conflicts, nil
  160. }
  161. // sortKeysWithPriority sorts keys with priority keys at the end in their specified order.
  162. // Non-priority keys are sorted alphabetically and placed before priority keys.
  163. func sortKeysWithPriority(operation esv1.ExternalSecretRewriteMerge, in map[string][]byte) []string {
  164. keys := make([]string, 0, len(in))
  165. for k := range in {
  166. if !slices.Contains(operation.Priority, k) {
  167. keys = append(keys, k)
  168. }
  169. }
  170. sort.Strings(keys)
  171. keys = append(keys, operation.Priority...)
  172. return keys
  173. }
  174. // RewriteRegexp rewrites a single Regexp Rewrite Operation.
  175. func RewriteRegexp(operation esv1.ExternalSecretRewriteRegexp, in map[string][]byte) (map[string][]byte, error) {
  176. out := make(map[string][]byte)
  177. re, err := regexp.Compile(operation.Source)
  178. if err != nil {
  179. return nil, fmt.Errorf("regexp failed with failed to compile: %w", err)
  180. }
  181. for key, value := range in {
  182. newKey := re.ReplaceAllString(key, operation.Target)
  183. out[newKey] = value
  184. }
  185. return out, nil
  186. }
  187. // RewriteTransform applies string transformation on each secret key name to rewrite.
  188. func RewriteTransform(operation esv1.ExternalSecretRewriteTransform, in map[string][]byte) (map[string][]byte, error) {
  189. out := make(map[string][]byte)
  190. tmpl, err := template.New("transform").Funcs(estemplate.FuncMap()).Parse(operation.Template)
  191. if err != nil {
  192. return nil, fmt.Errorf("transform failed with failed to parse template: %w", err)
  193. }
  194. for key, value := range in {
  195. var buf bytes.Buffer
  196. if err := tmpl.Execute(&buf, map[string]string{"value": key}); err != nil {
  197. return nil, fmt.Errorf("transform failed with failed to execute template for key %q: %w", key, err)
  198. }
  199. out[buf.String()] = value
  200. }
  201. return out, nil
  202. }
  203. // ValidateKeys checks if the keys in the secret map are valid keys for a Kubernetes secret.
  204. func ValidateKeys(log logr.Logger, in map[string][]byte) error {
  205. for key := range in {
  206. keyLength := len(key)
  207. if keyLength == 0 {
  208. delete(in, key)
  209. log.V(1).Info("key was deleted from the secret output because it did not exist upstream", "key", key)
  210. continue
  211. }
  212. if keyLength > 253 {
  213. return fmt.Errorf("key has length %d but max is 253: (following is truncated): %s", keyLength, key[:253])
  214. }
  215. for _, c := range key {
  216. if !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '-' && c != '.' && c != '_' {
  217. return fmt.Errorf("key has invalid character %c, only alphanumeric, '-', '.' and '_' are allowed: %s", c, key)
  218. }
  219. }
  220. }
  221. return nil
  222. }
  223. // ConvertKeys converts a secret map into a valid key.
  224. // Replaces any non-alphanumeric characters depending on convert strategy.
  225. func ConvertKeys(strategy esv1.ExternalSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  226. return transformKeys(in, func(key string) string {
  227. return convert(strategy, key)
  228. })
  229. }
  230. func convert(strategy esv1.ExternalSecretConversionStrategy, str string) string {
  231. rs := []rune(str)
  232. newName := make([]string, len(rs))
  233. for rk, rv := range rs {
  234. if !unicode.IsNumber(rv) &&
  235. !unicode.IsLetter(rv) &&
  236. rv != '-' &&
  237. rv != '.' &&
  238. rv != '_' {
  239. switch strategy {
  240. // An empty strategy means the field was omitted, which the API
  241. // documents as Default. Decode() folds "" in the same way.
  242. case esv1.ExternalSecretConversionDefault, "":
  243. newName[rk] = "_"
  244. case esv1.ExternalSecretConversionUnicode:
  245. newName[rk] = fmt.Sprintf("_U%04x_", rv)
  246. default:
  247. newName[rk] = string(rv)
  248. }
  249. } else {
  250. newName[rk] = string(rv)
  251. }
  252. }
  253. return strings.Join(newName, "")
  254. }
  255. // ReverseKeys reverses a secret map into a valid key map as expected by push secrets.
  256. // Replaces the unicode encoded representation characters back to the actual unicode character depending on convert strategy.
  257. func ReverseKeys(strategy esv1alpha1.PushSecretConversionStrategy, in map[string][]byte) (map[string][]byte, error) {
  258. return transformKeys(in, func(key string) string {
  259. return reverse(strategy, key)
  260. })
  261. }
  262. func transformKeys(in map[string][]byte, transform func(string) string) (map[string][]byte, error) {
  263. out := make(map[string][]byte, len(in))
  264. for k, v := range in {
  265. key := transform(k)
  266. if _, exists := out[key]; exists {
  267. return nil, fmt.Errorf("secret name collision during conversion: %s", key)
  268. }
  269. out[key] = v
  270. }
  271. return out, nil
  272. }
  273. // ReverseKey applies the conversion strategy to a single key name.
  274. func ReverseKey(strategy esv1alpha1.PushSecretConversionStrategy, key string) string {
  275. return reverse(strategy, key)
  276. }
  277. func reverse(strategy esv1alpha1.PushSecretConversionStrategy, str string) string {
  278. switch strategy {
  279. case esv1alpha1.PushSecretConversionReverseUnicode:
  280. matches := unicodeRegex.FindAllStringSubmatchIndex(str, -1)
  281. for i := len(matches) - 1; i >= 0; i-- {
  282. match := matches[i]
  283. start := match[0]
  284. end := match[1]
  285. unicodeHex := str[match[2]:match[3]]
  286. unicodeInt, err := strconv.ParseInt(unicodeHex, 16, 32)
  287. if err != nil {
  288. continue // Skip invalid unicode representations
  289. }
  290. unicodeChar := fmt.Sprintf("%c", unicodeInt)
  291. str = str[:start] + unicodeChar + str[end:]
  292. }
  293. return str
  294. case esv1alpha1.PushSecretConversionNone:
  295. return str
  296. default:
  297. return str
  298. }
  299. }
  300. // MergeStringMap performs a deep clone from src to dest.
  301. func MergeStringMap(dest, src map[string]string) {
  302. maps.Copy(dest, src)
  303. }
  304. var (
  305. // ErrUnexpectedKey is returned when an unexpected key is found in the data.
  306. ErrUnexpectedKey = errors.New("unexpected key in data")
  307. // ErrSecretType is returned when a secret value cannot be handled due to its type.
  308. ErrSecretType = errors.New("can not handle secret value with type")
  309. )
  310. // JSONToSecretDataMap unmarshals a JSON object into secret key/value pairs.
  311. // String values are unquoted; all other JSON types are kept as raw JSON bytes.
  312. func JSONToSecretDataMap(data []byte) (map[string][]byte, error) {
  313. kv := make(map[string]json.RawMessage)
  314. if err := json.Unmarshal(data, &kv); err != nil {
  315. return nil, err
  316. }
  317. secretData := make(map[string][]byte, len(kv))
  318. for k, v := range kv {
  319. var strVal string
  320. if err := json.Unmarshal(v, &strVal); err == nil {
  321. secretData[k] = []byte(strVal)
  322. } else {
  323. secretData[k] = v
  324. }
  325. }
  326. return secretData, nil
  327. }
  328. // GetByteValueFromMap retrieves a byte value from a map by key.
  329. func GetByteValueFromMap(data map[string]any, key string) ([]byte, error) {
  330. v, ok := data[key]
  331. if !ok {
  332. return nil, fmt.Errorf("%w: %s", ErrUnexpectedKey, key)
  333. }
  334. return GetByteValue(v)
  335. }
  336. // GetByteValue converts an interface value to a byte slice.
  337. func GetByteValue(v any) ([]byte, error) {
  338. switch t := v.(type) {
  339. case string:
  340. return []byte(t), nil
  341. case map[string]any:
  342. return json.Marshal(t)
  343. case []string:
  344. return []byte(strings.Join(t, "\n")), nil
  345. case json.RawMessage:
  346. return t, nil
  347. case []byte:
  348. return t, nil
  349. // also covers int and float32 due to json.Marshal
  350. case float64:
  351. return []byte(strconv.FormatFloat(t, 'f', -1, 64)), nil
  352. case json.Number:
  353. return []byte(t.String()), nil
  354. case []any:
  355. return json.Marshal(t)
  356. case bool:
  357. return []byte(strconv.FormatBool(t)), nil
  358. case nil:
  359. return []byte(nil), nil
  360. default:
  361. return nil, fmt.Errorf("%w: %T", ErrSecretType, t)
  362. }
  363. }
  364. // IsNil checks if an Interface is nil.
  365. func IsNil(i any) bool {
  366. if i == nil {
  367. return true
  368. }
  369. value := reflect.ValueOf(i)
  370. if value.Type().Kind() == reflect.Ptr {
  371. return value.IsNil()
  372. }
  373. return false
  374. }
  375. // ObjectHash calculates sha3 sum of the data contained in the secret.
  376. func ObjectHash(object any) string {
  377. textualVersion := fmt.Sprintf("%+v", object)
  378. return fmt.Sprintf("%x", sha3.Sum224([]byte(textualVersion)))
  379. }
  380. // ErrorContains checks if the error message contains the specified substring.
  381. func ErrorContains(out error, want string) bool {
  382. if out == nil {
  383. return want == ""
  384. }
  385. if want == "" {
  386. return false
  387. }
  388. return strings.Contains(out.Error(), want)
  389. }
  390. var (
  391. errNamespaceNotAllowed = errors.New("namespace should either be empty or match the namespace of the SecretStore for a namespaced SecretStore")
  392. errRequireNamespace = errors.New("cluster scope requires namespace")
  393. )
  394. // ValidateSecretSelector just checks if the namespace field is present/absent
  395. // depending on the secret store type.
  396. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  397. func ValidateSecretSelector(store esv1.GenericStore, ref esmeta.SecretKeySelector) error {
  398. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind
  399. if clusterScope && ref.Namespace == nil {
  400. return errRequireNamespace
  401. }
  402. if !clusterScope && ref.Namespace != nil && *ref.Namespace != store.GetNamespace() {
  403. return errNamespaceNotAllowed
  404. }
  405. return nil
  406. }
  407. // ValidateReferentSecretSelector allows
  408. // cluster scoped store without namespace
  409. // this should replace above ValidateServiceAccountSelector once all providers
  410. // support referent auth.
  411. func ValidateReferentSecretSelector(store esv1.GenericStore, ref esmeta.SecretKeySelector) error {
  412. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind
  413. if !clusterScope && ref.Namespace != nil && *ref.Namespace != store.GetNamespace() {
  414. return errNamespaceNotAllowed
  415. }
  416. return nil
  417. }
  418. // ValidateServiceAccountSelector just checks if the namespace field is present/absent
  419. // depending on the secret store type.
  420. // We MUST NOT check the name or key property here. It MAY be defaulted by the provider.
  421. func ValidateServiceAccountSelector(store esv1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  422. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind
  423. if clusterScope && ref.Namespace == nil {
  424. return errRequireNamespace
  425. }
  426. if !clusterScope && ref.Namespace != nil && *ref.Namespace != store.GetNamespace() {
  427. return errNamespaceNotAllowed
  428. }
  429. return nil
  430. }
  431. // ValidateReferentServiceAccountSelector allows
  432. // cluster scoped store without namespace
  433. // this should replace above ValidateServiceAccountSelector once all providers
  434. // support referent auth.
  435. func ValidateReferentServiceAccountSelector(store esv1.GenericStore, ref esmeta.ServiceAccountSelector) error {
  436. clusterScope := store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind
  437. if !clusterScope && ref.Namespace != nil && *ref.Namespace != store.GetNamespace() {
  438. return errNamespaceNotAllowed
  439. }
  440. return nil
  441. }
  442. // NetworkValidate checks if a network endpoint is reachable within the given timeout.
  443. func NetworkValidate(endpoint string, timeout time.Duration) error {
  444. hostname, err := url.Parse(endpoint)
  445. if err != nil {
  446. return fmt.Errorf("could not parse url: %w", err)
  447. }
  448. host := hostname.Hostname()
  449. port := hostname.Port()
  450. if port == "" {
  451. port = "443"
  452. }
  453. url := fmt.Sprintf("%v:%v", host, port)
  454. conn, err := net.DialTimeout("tcp", url, timeout)
  455. if err != nil {
  456. return fmt.Errorf("error accessing external store: %w", err)
  457. }
  458. defer func() {
  459. _ = conn.Close()
  460. }()
  461. return nil
  462. }
  463. // Deref returns the value pointed to by v, or the zero value if v is nil.
  464. func Deref[V any](v *V) V {
  465. if v == nil {
  466. // Create zero value
  467. var res V
  468. return res
  469. }
  470. return *v
  471. }
  472. // Ptr returns a pointer to the given value.
  473. //
  474. //go:fix inline
  475. func Ptr[T any](i T) *T {
  476. return new(i)
  477. }
  478. // ConvertToType converts an object to the specified type using JSON marshaling.
  479. func ConvertToType[T any](obj any) (T, error) {
  480. var v T
  481. data, err := json.Marshal(obj)
  482. if err != nil {
  483. return v, fmt.Errorf("failed to marshal object: %w", err)
  484. }
  485. if err = json.Unmarshal(data, &v); err != nil {
  486. return v, fmt.Errorf("failed to unmarshal object: %w", err)
  487. }
  488. return v, nil
  489. }
  490. // FetchValueFromMetadata fetches a key from a metadata if it exists. It will recursively look in
  491. // embedded values as well. Must be a unique key, otherwise it will just return the first
  492. // occurrence.
  493. func FetchValueFromMetadata[T any](key string, data *apiextensionsv1.JSON, def T) (t T, _ error) {
  494. if data == nil {
  495. return def, nil
  496. }
  497. m := map[string]any{}
  498. if err := json.Unmarshal(data.Raw, &m); err != nil {
  499. return t, fmt.Errorf("failed to parse JSON raw data: %w", err)
  500. }
  501. v, err := dig[T](key, m)
  502. if err != nil {
  503. if errors.Is(err, errKeyNotFound) {
  504. return def, nil
  505. }
  506. }
  507. return v, nil
  508. }
  509. func dig[T any](key string, data map[string]any) (t T, _ error) {
  510. if v, ok := data[key]; ok {
  511. c, k := v.(T)
  512. if !k {
  513. return t, fmt.Errorf("failed to convert value to the desired type; was: %T", v)
  514. }
  515. return c, nil
  516. }
  517. for _, v := range data {
  518. if ty, ok := v.(map[string]any); ok {
  519. return dig[T](key, ty)
  520. }
  521. }
  522. return t, errKeyNotFound
  523. }
  524. // CompareStringAndByteSlices compares a string pointer and a byte slice for equality.
  525. func CompareStringAndByteSlices(valueString *string, valueByte []byte) bool {
  526. if valueString == nil {
  527. return false
  528. }
  529. return bytes.Equal(valueByte, []byte(*valueString))
  530. }
  531. // ExtractSecretData extracts secret data from a Kubernetes Secret based on PushSecretData configuration.
  532. func ExtractSecretData(data esv1.PushSecretData, secret *corev1.Secret) ([]byte, error) {
  533. var (
  534. err error
  535. value []byte
  536. ok bool
  537. )
  538. if data.GetSecretKey() == "" {
  539. decodedMap := make(map[string]string)
  540. for k, v := range secret.Data {
  541. decodedMap[k] = string(v)
  542. }
  543. value, err = JSONMarshal(decodedMap)
  544. if err != nil {
  545. return nil, fmt.Errorf("failed to marshal secret data: %w", err)
  546. }
  547. } else {
  548. value, ok = secret.Data[data.GetSecretKey()]
  549. if !ok {
  550. return nil, fmt.Errorf("failed to find secret key in secret with key: %s", data.GetSecretKey())
  551. }
  552. }
  553. return value, nil
  554. }
  555. // CreateCertOpts contains options for a cert pool creation.
  556. type CreateCertOpts struct {
  557. CABundle []byte
  558. CAProvider *esv1.CAProvider
  559. StoreKind string
  560. Namespace string
  561. Client client.Client
  562. }
  563. // FetchCACertFromSource creates a CertPool using either a CABundle directly, or
  564. // a ConfigMap / Secret.
  565. func FetchCACertFromSource(ctx context.Context, opts CreateCertOpts) ([]byte, error) {
  566. if len(opts.CABundle) == 0 && opts.CAProvider == nil {
  567. return nil, nil
  568. }
  569. if len(opts.CABundle) > 0 {
  570. pem, err := base64decode(opts.CABundle)
  571. if err != nil {
  572. return nil, fmt.Errorf("failed to decode ca bundle: %w", err)
  573. }
  574. return pem, nil
  575. }
  576. if opts.CAProvider != nil &&
  577. opts.StoreKind != esv1.ClusterSecretStoreKind &&
  578. opts.CAProvider.Namespace != nil &&
  579. *opts.CAProvider.Namespace != opts.Namespace {
  580. return nil, errNamespaceNotAllowed
  581. }
  582. if opts.CAProvider != nil &&
  583. opts.StoreKind == esv1.ClusterSecretStoreKind &&
  584. opts.CAProvider.Namespace == nil {
  585. return nil, errors.New("missing namespace on caProvider secret")
  586. }
  587. switch opts.CAProvider.Type {
  588. case esv1.CAProviderTypeSecret:
  589. cert, err := getCertFromSecret(ctx, opts.Client, opts.CAProvider, opts.StoreKind, opts.Namespace)
  590. if err != nil {
  591. return nil, fmt.Errorf("failed to get cert from secret: %w", err)
  592. }
  593. return cert, nil
  594. case esv1.CAProviderTypeConfigMap:
  595. cert, err := getCertFromConfigMap(ctx, opts.Namespace, opts.Client, opts.CAProvider, opts.StoreKind)
  596. if err != nil {
  597. return nil, fmt.Errorf("failed to get cert from configmap: %w", err)
  598. }
  599. return cert, nil
  600. }
  601. return nil, fmt.Errorf("unsupported CA provider type: %s", opts.CAProvider.Type)
  602. }
  603. // GetTargetNamespaces extracts namespaces based on selectors.
  604. func GetTargetNamespaces(ctx context.Context, cl client.Client, namespaceList []string, lbs []*metav1.LabelSelector) ([]corev1.Namespace, error) {
  605. // make sure we don't alter the passed in slice.
  606. selectors := make([]*metav1.LabelSelector, 0, len(namespaceList)+len(lbs))
  607. for _, ns := range namespaceList {
  608. selectors = append(selectors, &metav1.LabelSelector{
  609. MatchLabels: map[string]string{
  610. "kubernetes.io/metadata.name": ns,
  611. },
  612. })
  613. }
  614. selectors = append(selectors, lbs...)
  615. var namespaces []corev1.Namespace
  616. namespaceSet := make(map[string]struct{})
  617. for _, selector := range selectors {
  618. labelSelector, err := metav1.LabelSelectorAsSelector(selector)
  619. if err != nil {
  620. return nil, fmt.Errorf("failed to convert label selector %s: %w", selector, err)
  621. }
  622. var nl corev1.NamespaceList
  623. err = cl.List(ctx, &nl, &client.ListOptions{LabelSelector: labelSelector})
  624. if err != nil {
  625. return nil, fmt.Errorf("failed to list namespaces by label selector %s: %w", selector, err)
  626. }
  627. for _, n := range nl.Items {
  628. if _, exist := namespaceSet[n.Name]; exist {
  629. continue
  630. }
  631. namespaceSet[n.Name] = struct{}{}
  632. namespaces = append(namespaces, n)
  633. }
  634. }
  635. return namespaces, nil
  636. }
  637. // NamespacePredicate can be used to watch for new or updated or deleted namespaces.
  638. func NamespacePredicate() predicate.Predicate {
  639. return predicate.Funcs{
  640. CreateFunc: func(_ event.CreateEvent) bool {
  641. return true
  642. },
  643. UpdateFunc: func(e event.UpdateEvent) bool {
  644. if e.ObjectOld == nil || e.ObjectNew == nil {
  645. return false
  646. }
  647. return !reflect.DeepEqual(e.ObjectOld.GetLabels(), e.ObjectNew.GetLabels())
  648. },
  649. DeleteFunc: func(_ event.DeleteEvent) bool {
  650. return true
  651. },
  652. }
  653. }
  654. func base64decode(cert []byte) ([]byte, error) {
  655. if c, err := parseCertificateBytes(cert); err == nil {
  656. return c, nil
  657. }
  658. // try decoding and test for validity again...
  659. certificate, err := decoding.Decode(esv1.ExternalSecretDecodeAuto, cert)
  660. if err != nil {
  661. return nil, fmt.Errorf("failed to decode base64: %w", err)
  662. }
  663. return parseCertificateBytes(certificate)
  664. }
  665. func parseCertificateBytes(certBytes []byte) ([]byte, error) {
  666. block, _ := pem.Decode(certBytes)
  667. if block == nil {
  668. return nil, errors.New("failed to parse the new certificate, not valid pem data")
  669. }
  670. if _, err := x509.ParseCertificate(block.Bytes); err != nil {
  671. return nil, fmt.Errorf("failed to validate certificate: %w", err)
  672. }
  673. return certBytes, nil
  674. }
  675. func getCertFromSecret(ctx context.Context, c client.Client, provider *esv1.CAProvider, storeKind, namespace string) ([]byte, error) {
  676. secretRef := esmeta.SecretKeySelector{
  677. Name: provider.Name,
  678. Key: provider.Key,
  679. }
  680. if provider.Namespace != nil {
  681. secretRef.Namespace = provider.Namespace
  682. }
  683. cert, err := resolvers.SecretKeyRef(ctx, c, storeKind, namespace, &secretRef)
  684. if err != nil {
  685. return nil, fmt.Errorf("failed to resolve secret key ref: %w", err)
  686. }
  687. return []byte(cert), nil
  688. }
  689. func getCertFromConfigMap(ctx context.Context, namespace string, c client.Client, provider *esv1.CAProvider, storeKind string) ([]byte, error) {
  690. objKey := client.ObjectKey{
  691. Name: provider.Name,
  692. Namespace: namespace,
  693. }
  694. if provider.Namespace != nil && storeKind == esv1.ClusterSecretStoreKind {
  695. objKey.Namespace = *provider.Namespace
  696. }
  697. configMapRef := &corev1.ConfigMap{}
  698. err := c.Get(ctx, objKey, configMapRef)
  699. if err != nil {
  700. return nil, fmt.Errorf("failed to get caProvider secret %s: %w", objKey.Name, err)
  701. }
  702. val, ok := configMapRef.Data[provider.Key]
  703. if !ok {
  704. return nil, fmt.Errorf("failed to get caProvider configMap %s -> %s", objKey.Name, provider.Key)
  705. }
  706. return []byte(val), nil
  707. }
  708. // CheckEndpointSlicesReady checks if there are any EndpointSlice objects for the given service
  709. // that have ready addresses.
  710. func CheckEndpointSlicesReady(ctx context.Context, c client.Client, svcName, svcNamespace string) error {
  711. var sliceList discoveryv1.EndpointSliceList
  712. err := c.List(ctx, &sliceList,
  713. client.InNamespace(svcNamespace),
  714. client.MatchingLabels{"kubernetes.io/service-name": svcName},
  715. )
  716. if err != nil {
  717. return err
  718. }
  719. if len(sliceList.Items) == 0 {
  720. return errEndpointSlicesNotReady
  721. }
  722. readyAddresses := 0
  723. for _, slice := range sliceList.Items {
  724. for _, ep := range slice.Endpoints {
  725. if ep.Conditions.Ready != nil && *ep.Conditions.Ready {
  726. readyAddresses += len(ep.Addresses)
  727. }
  728. }
  729. }
  730. if readyAddresses == 0 {
  731. return errAddressesNotReady
  732. }
  733. return nil
  734. }
  735. // ParseJWTClaims extracts claims from a JWT token string.
  736. func ParseJWTClaims(tokenString string) (map[string]any, error) {
  737. // Split the token into its three parts
  738. parts := strings.Split(tokenString, ".")
  739. if len(parts) != 3 {
  740. return nil, fmt.Errorf("invalid token format")
  741. }
  742. // Decode the payload (the second part of the token)
  743. payload, err := base64.RawURLEncoding.DecodeString(parts[1])
  744. if err != nil {
  745. return nil, fmt.Errorf("error decoding payload: %w", err)
  746. }
  747. var claims map[string]any
  748. if err := json.Unmarshal(payload, &claims); err != nil {
  749. return nil, fmt.Errorf("error un-marshaling claims: %w", err)
  750. }
  751. return claims, nil
  752. }
  753. // ExtractJWTExpiration extracts the expiration time from a JWT token string.
  754. func ExtractJWTExpiration(tokenString string) (string, error) {
  755. claims, err := ParseJWTClaims(tokenString)
  756. if err != nil {
  757. return "", fmt.Errorf("error getting claims: %w", err)
  758. }
  759. exp, ok := claims["exp"].(float64)
  760. if ok {
  761. return strconv.FormatFloat(exp, 'f', -1, 64), nil
  762. }
  763. return "", fmt.Errorf("exp claim not found or wrong type")
  764. }
  765. // FetchServiceAccountToken creates a service account token for the specified service account.
  766. func FetchServiceAccountToken(ctx context.Context, saRef esmeta.ServiceAccountSelector, namespace string) (string, error) {
  767. cfg, err := ctrlcfg.GetConfig()
  768. if err != nil {
  769. return "", err
  770. }
  771. kubeClient, err := kubernetes.NewForConfig(cfg)
  772. if err != nil {
  773. return "", fmt.Errorf("failed to create kubernetes client: %w", err)
  774. }
  775. tokenRequest := &authv1.TokenRequest{
  776. Spec: authv1.TokenRequestSpec{
  777. Audiences: saRef.Audiences,
  778. },
  779. }
  780. tokenResponse, err := kubeClient.CoreV1().ServiceAccounts(namespace).CreateToken(ctx, saRef.Name, tokenRequest, metav1.CreateOptions{})
  781. if err != nil {
  782. return "", fmt.Errorf("failed to create token: %w", err)
  783. }
  784. return tokenResponse.Status.Token, nil
  785. }