template.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 template
  14. import (
  15. "bytes"
  16. "fmt"
  17. "maps"
  18. "strings"
  19. tpl "text/template"
  20. "github.com/Masterminds/sprig/v3"
  21. "github.com/spf13/pflag"
  22. corev1 "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/util/yaml"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  27. "github.com/external-secrets/external-secrets/runtime/feature"
  28. )
  29. var tplFuncs = tpl.FuncMap{
  30. "pkcs12key": pkcs12key,
  31. "pkcs12keyPass": pkcs12keyPass,
  32. "pkcs12cert": pkcs12cert,
  33. "pkcs12certPass": pkcs12certPass,
  34. "pemToPkcs12": pemToPkcs12,
  35. "pemToPkcs12Pass": pemToPkcs12Pass,
  36. "fullPemToPkcs12": fullPemToPkcs12,
  37. "fullPemToPkcs12Pass": fullPemToPkcs12Pass,
  38. "pemTruststoreToPKCS12": pemTruststoreToPKCS12,
  39. "pemTruststoreToPKCS12Pass": pemTruststoreToPKCS12Pass,
  40. "filterPEM": filterPEM,
  41. "filterCertChain": filterCertChain,
  42. "certSANs": certSANs,
  43. "jwkPublicKeyPem": jwkPublicKeyPem,
  44. "jwkPrivateKeyPem": jwkPrivateKeyPem,
  45. "toYaml": toYAML,
  46. "fromYaml": fromYAML,
  47. "rsaDecrypt": rsaDecrypt,
  48. }
  49. var leftDelim, rightDelim string
  50. var (
  51. errConvertingToUnstructured = "failed to convert object to unstructured: %w"
  52. errConvertingToObject = "failed to convert unstructured to object: %w"
  53. )
  54. // FuncMap returns the template function map so other templating calls can use the same extra functions.
  55. func FuncMap() tpl.FuncMap {
  56. return tplFuncs
  57. }
  58. const (
  59. errParse = "unable to parse template at key %s: %s"
  60. errExecute = "unable to execute template at key %s: %s"
  61. errDecodePKCS12WithPass = "unable to decode pkcs12 with password: %s"
  62. errDecodeCertWithPass = "unable to decode pkcs12 certificate with password: %s"
  63. errParsePrivKey = "unable to parse private key type"
  64. pemTypeCertificate = "CERTIFICATE"
  65. pemTypeKey = "PRIVATE KEY"
  66. )
  67. func init() {
  68. sprigFuncs := sprig.TxtFuncMap()
  69. delete(sprigFuncs, "env")
  70. delete(sprigFuncs, "expandenv")
  71. maps.Copy(tplFuncs, sprigFuncs)
  72. fs := pflag.NewFlagSet("template", pflag.ExitOnError)
  73. fs.StringVar(&leftDelim, "template-left-delimiter", "{{", "templating left delimiter")
  74. fs.StringVar(&rightDelim, "template-right-delimiter", "}}", "templating right delimiter")
  75. feature.Register(feature.Feature{
  76. Flags: fs,
  77. })
  78. }
  79. func applyToTarget(k string, val []byte, target string, obj client.Object) error {
  80. target = strings.ToLower(target)
  81. switch target {
  82. case "annotations":
  83. annotations := obj.GetAnnotations()
  84. if annotations == nil {
  85. annotations = make(map[string]string)
  86. }
  87. annotations[k] = string(val)
  88. obj.SetAnnotations(annotations)
  89. case "labels":
  90. labels := obj.GetLabels()
  91. if labels == nil {
  92. labels = make(map[string]string)
  93. }
  94. labels[k] = string(val)
  95. obj.SetLabels(labels)
  96. case "data":
  97. if err := setField(obj, "data", k, val); err != nil {
  98. return fmt.Errorf("failed to set data field on object: %w", err)
  99. }
  100. case "spec":
  101. if err := setField(obj, "spec", k, val); err != nil {
  102. return fmt.Errorf("failed to set data field on object: %w", err)
  103. }
  104. default:
  105. parts := strings.Split(target, ".")
  106. if len(parts) == 0 {
  107. return fmt.Errorf("invalid path: %s", target)
  108. }
  109. unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  110. if err != nil {
  111. return fmt.Errorf(errConvertingToUnstructured, err)
  112. }
  113. // Navigate to the parent of the target field
  114. current := unstructured
  115. for i := range len(parts) - 1 {
  116. part := parts[i]
  117. if current[part] == nil {
  118. current[part] = make(map[string]any)
  119. }
  120. next, ok := current[part].(map[string]any)
  121. if !ok {
  122. return fmt.Errorf("path %s is not a map at segment %s", target, part)
  123. }
  124. current = next
  125. }
  126. // Set the value at the final key
  127. // Convert []byte to string to avoid base64 encoding when serializing
  128. lastPart := parts[len(parts)-1]
  129. current[lastPart] = tryParseYAML(string(val))
  130. // Convert back to the original object type
  131. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, obj); err != nil {
  132. return fmt.Errorf(errConvertingToObject, err)
  133. }
  134. }
  135. // all fields have been nilled out if they weren't set.
  136. if obj.GetLabels() == nil {
  137. obj.SetLabels(make(map[string]string))
  138. }
  139. if obj.GetAnnotations() == nil {
  140. obj.SetAnnotations(make(map[string]string))
  141. }
  142. return nil
  143. }
  144. func valueScopeApply(tplMap, data map[string][]byte, target string, secret client.Object) error {
  145. for k, v := range tplMap {
  146. val, err := execute(k, string(v), data)
  147. if err != nil {
  148. return fmt.Errorf(errExecute, k, err)
  149. }
  150. if err := applyToTarget(k, val, target, secret); err != nil {
  151. return fmt.Errorf("failed to apply to target: %w", err)
  152. }
  153. }
  154. return nil
  155. }
  156. func mapScopeApply(tpl string, data map[string][]byte, target string, secret client.Object) error {
  157. val, err := execute(tpl, tpl, data)
  158. if err != nil {
  159. return fmt.Errorf(errExecute, tpl, err)
  160. }
  161. target = strings.ToLower(target)
  162. switch target {
  163. case "annotations", "labels", "data":
  164. // normal route
  165. src := make(map[string]string)
  166. err = yaml.Unmarshal(val, &src)
  167. if err != nil {
  168. return fmt.Errorf("could not unmarshal template to 'map[string][]byte': %w", err)
  169. }
  170. for k, val := range src {
  171. if err := applyToTarget(k, []byte(val), target, secret); err != nil {
  172. return fmt.Errorf("failed to apply to target: %w", err)
  173. }
  174. }
  175. // we are done
  176. return nil
  177. }
  178. // for more complex path, we need to navigate to the last element of the path
  179. // creating objects in that path if they don't exist and then apply the parsed
  180. // structure at that location to the entire object.
  181. var parsed any
  182. if err := yaml.Unmarshal(val, &parsed); err != nil {
  183. return fmt.Errorf("could not unmarshal template YAML: %w", err)
  184. }
  185. return applyParsedToPath(parsed, target, secret)
  186. }
  187. // Execute renders the secret data as template. If an error occurs processing is stopped immediately.
  188. func Execute(tpl, data map[string][]byte, scope esapi.TemplateScope, target string, secret client.Object) error {
  189. if tpl == nil {
  190. return nil
  191. }
  192. switch scope {
  193. case esapi.TemplateScopeKeysAndValues:
  194. for _, v := range tpl {
  195. err := mapScopeApply(string(v), data, target, secret)
  196. if err != nil {
  197. return err
  198. }
  199. }
  200. case esapi.TemplateScopeValues:
  201. err := valueScopeApply(tpl, data, target, secret)
  202. if err != nil {
  203. return err
  204. }
  205. default:
  206. return fmt.Errorf("unknown scope '%v': expected 'Values' or 'KeysAndValues'", scope)
  207. }
  208. return nil
  209. }
  210. func execute(k, val string, data map[string][]byte) ([]byte, error) {
  211. strValData := make(map[string]string, len(data))
  212. for k := range data {
  213. strValData[k] = string(data[k])
  214. }
  215. t, err := tpl.New(k).
  216. Option("missingkey=error").
  217. Funcs(tplFuncs).
  218. Delims(leftDelim, rightDelim).
  219. Parse(val)
  220. if err != nil {
  221. return nil, fmt.Errorf(errParse, k, err)
  222. }
  223. buf := bytes.NewBuffer(nil)
  224. err = t.Execute(buf, strValData)
  225. if err != nil {
  226. return nil, fmt.Errorf(errExecute, k, err)
  227. }
  228. return buf.Bytes(), nil
  229. }
  230. // setData sets the data field of the object.
  231. func setField(obj client.Object, field, k string, val []byte) error {
  232. m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  233. if err != nil {
  234. return fmt.Errorf(errConvertingToUnstructured, err)
  235. }
  236. _, ok := m[field]
  237. if !ok {
  238. m[field] = map[string]any{}
  239. }
  240. specMap, ok := m[field].(map[string]any)
  241. if !ok {
  242. return fmt.Errorf("failed to convert data to map[string][]byte")
  243. }
  244. // Secrets require base64-encoded []byte values in the data field
  245. // Other resources (ConfigMaps, custom resources) need plain string values
  246. _, isSecret := obj.(*corev1.Secret)
  247. if isSecret {
  248. // For Secrets, keep as []byte (will be base64-encoded during serialization)
  249. specMap[k] = val
  250. } else {
  251. // For generic (ConfigMaps, custom resources), use plain strings
  252. specMap[k] = string(val)
  253. }
  254. m[field] = specMap
  255. // Convert back to the original object type
  256. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, obj); err != nil {
  257. return fmt.Errorf(errConvertingToObject, err)
  258. }
  259. return nil
  260. }
  261. // tryParseYAML attempts to parse a string value as YAML, returns original value if parsing fails.
  262. func tryParseYAML(value any) any {
  263. str, ok := value.(string)
  264. if !ok {
  265. return value
  266. }
  267. var parsed any
  268. if err := yaml.Unmarshal([]byte(str), &parsed); err == nil {
  269. return parsed
  270. }
  271. return value
  272. }
  273. // applyParsedToPath applies a parsed YAML structure to a specific path in the object.
  274. func applyParsedToPath(parsed any, target string, obj client.Object) error {
  275. unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  276. if err != nil {
  277. return fmt.Errorf(errConvertingToUnstructured, err)
  278. }
  279. parts := strings.Split(target, ".")
  280. if len(parts) == 0 {
  281. return fmt.Errorf("invalid path: %s", target)
  282. }
  283. // single value, aka "spec"
  284. if len(parts) == 1 {
  285. unstructured[parts[0]] = parsed
  286. } else {
  287. // navigate to the last element of the path and apply the entire struct at that location.
  288. // build up the entire map structure that we are eventually going to apply.
  289. current := unstructured
  290. // this STOPS at the last part! That is important. for _, part := range parts does _include_ the last part
  291. for i := 0; i < len(parts)-1; i++ {
  292. part := parts[i]
  293. if current[part] == nil {
  294. current[part] = make(map[string]any)
  295. }
  296. next, ok := current[part].(map[string]any)
  297. if !ok {
  298. return fmt.Errorf("path %s is not a map at segment %s", target, part)
  299. }
  300. current = next
  301. }
  302. // once we constructed the entire segment, we finally apply our parsed object
  303. // MERGE the parsed content into existing content instead of replacing it
  304. lastPart := parts[len(parts)-1]
  305. if existing, exists := current[lastPart]; exists {
  306. // if both existing and new values are maps, merge them
  307. existingMap, existingOk := existing.(map[string]any)
  308. parsedMap, parsedOk := parsed.(map[string]any)
  309. if existingOk && parsedOk {
  310. maps.Copy(existingMap, parsedMap)
  311. current[lastPart] = existingMap
  312. } else {
  313. // existing or parsed value is not a map, replace entirely.
  314. // this might break if people are trying to overwrite
  315. // fields that aren't supposed to do that. but that's
  316. // on the user to keep in mind. If they are trying to
  317. // update a number field with a complex value, that's
  318. // going to error on update anyway.
  319. current[lastPart] = parsed
  320. }
  321. } else {
  322. // field doesn't exist yet, create it
  323. current[lastPart] = parsed
  324. }
  325. }
  326. // convert back to original object
  327. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, obj); err != nil {
  328. return fmt.Errorf(errConvertingToObject, err)
  329. }
  330. return nil
  331. }