template.go 11 KB

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