template.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 template
  13. import (
  14. "bytes"
  15. "crypto/x509"
  16. "encoding/base64"
  17. "encoding/json"
  18. "encoding/pem"
  19. "fmt"
  20. "strings"
  21. tpl "text/template"
  22. "github.com/lestrrat-go/jwx/jwk"
  23. "github.com/youmark/pkcs8"
  24. "golang.org/x/crypto/pkcs12"
  25. corev1 "k8s.io/api/core/v1"
  26. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. )
  28. var tplFuncs = tpl.FuncMap{
  29. "pkcs12key": pkcs12key,
  30. "pkcs12keyPass": pkcs12keyPass,
  31. "pkcs12cert": pkcs12cert,
  32. "pkcs12certPass": pkcs12certPass,
  33. "pemPrivateKey": pemPrivateKey,
  34. "pemCertificate": pemCertificate,
  35. "base64decode": base64decode,
  36. "base64encode": base64encode,
  37. "fromJSON": fromJSON,
  38. "toJSON": toJSON,
  39. "jwkPublicKeyPem": jwkPublicKeyPem,
  40. "jwkPrivateKeyPem": jwkPrivateKeyPem,
  41. "toString": toString,
  42. "toBytes": toBytes,
  43. "upper": strings.ToUpper,
  44. "lower": strings.ToLower,
  45. }
  46. // So other templating calls can use the same extra functions.
  47. func FuncMap() tpl.FuncMap {
  48. return tplFuncs
  49. }
  50. const (
  51. errParse = "unable to parse template at key %s: %s"
  52. errExecute = "unable to execute template at key %s: %s"
  53. errDecodePKCS12WithPass = "unable to decode pkcs12 with password: %s"
  54. errConvertPrivKey = "unable to convert pkcs12 private key: %s"
  55. errDecodeCertWithPass = "unable to decode pkcs12 certificate with password: %s"
  56. errEncodePEMKey = "unable to encode pem private key: %s"
  57. errEncodePEMCert = "unable to encode pem certificate: %s"
  58. errDecodeBase64 = "unable to decode base64: %s"
  59. errUnmarshalJSON = "unable to unmarshal json: %s"
  60. errMarshalJSON = "unable to marshal json: %s"
  61. )
  62. // Execute renders the secret data as template. If an error occurs processing is stopped immediately.
  63. func Execute(tpl, data map[string][]byte, _ esapi.TemplateScope, _ esapi.TemplateTarget, secret *corev1.Secret) error {
  64. if tpl == nil {
  65. return nil
  66. }
  67. for k, v := range tpl {
  68. val, err := execute(k, string(v), data)
  69. if err != nil {
  70. return fmt.Errorf(errExecute, k, err)
  71. }
  72. secret.Data[k] = val
  73. }
  74. return nil
  75. }
  76. func execute(k, val string, data map[string][]byte) ([]byte, error) {
  77. t, err := tpl.New(k).
  78. Funcs(tplFuncs).
  79. Parse(val)
  80. if err != nil {
  81. return nil, fmt.Errorf(errParse, k, err)
  82. }
  83. buf := bytes.NewBuffer(nil)
  84. err = t.Execute(buf, data)
  85. if err != nil {
  86. return nil, fmt.Errorf(errExecute, k, err)
  87. }
  88. return buf.Bytes(), nil
  89. }
  90. func pkcs12keyPass(pass string, input []byte) ([]byte, error) {
  91. key, _, err := pkcs12.Decode(input, pass)
  92. if err != nil {
  93. return nil, fmt.Errorf(errDecodePKCS12WithPass, err)
  94. }
  95. kb, err := pkcs8.ConvertPrivateKeyToPKCS8(key)
  96. if err != nil {
  97. return nil, fmt.Errorf(errConvertPrivKey, err)
  98. }
  99. return kb, nil
  100. }
  101. func pkcs12key(input []byte) ([]byte, error) {
  102. return pkcs12keyPass("", input)
  103. }
  104. func pkcs12certPass(pass string, input []byte) ([]byte, error) {
  105. _, cert, err := pkcs12.Decode(input, pass)
  106. if err != nil {
  107. return nil, fmt.Errorf(errDecodeCertWithPass, err)
  108. }
  109. return cert.Raw, nil
  110. }
  111. func pkcs12cert(input []byte) ([]byte, error) {
  112. return pkcs12certPass("", input)
  113. }
  114. func jwkPublicKeyPem(jwkjson []byte) (string, error) {
  115. k, err := jwk.ParseKey(jwkjson)
  116. if err != nil {
  117. return "", err
  118. }
  119. var rawkey interface{}
  120. err = k.Raw(&rawkey)
  121. if err != nil {
  122. return "", err
  123. }
  124. mpk, err := x509.MarshalPKIXPublicKey(rawkey)
  125. if err != nil {
  126. return "", err
  127. }
  128. return pemEncode(mpk, "PUBLIC KEY")
  129. }
  130. func jwkPrivateKeyPem(jwkjson []byte) (string, error) {
  131. k, err := jwk.ParseKey(jwkjson)
  132. if err != nil {
  133. return "", err
  134. }
  135. var mpk []byte
  136. var pk interface{}
  137. err = k.Raw(&pk)
  138. if err != nil {
  139. return "", err
  140. }
  141. mpk, err = x509.MarshalPKCS8PrivateKey(pk)
  142. if err != nil {
  143. return "", err
  144. }
  145. return pemEncode(mpk, "PRIVATE KEY")
  146. }
  147. func pemEncode(thing []byte, kind string) (string, error) {
  148. buf := bytes.NewBuffer(nil)
  149. err := pem.Encode(buf, &pem.Block{Type: kind, Bytes: thing})
  150. return buf.String(), err
  151. }
  152. func pemPrivateKey(key []byte) (string, error) {
  153. res, err := pemEncode(key, "PRIVATE KEY")
  154. if err != nil {
  155. return res, fmt.Errorf(errEncodePEMKey, err)
  156. }
  157. return res, nil
  158. }
  159. func pemCertificate(cert []byte) (string, error) {
  160. res, err := pemEncode(cert, "CERTIFICATE")
  161. if err != nil {
  162. return res, fmt.Errorf(errEncodePEMCert, err)
  163. }
  164. return res, nil
  165. }
  166. func base64decode(in []byte) ([]byte, error) {
  167. out := make([]byte, len(in))
  168. l, err := base64.StdEncoding.Decode(out, in)
  169. if err != nil {
  170. return nil, fmt.Errorf(errDecodeBase64, err)
  171. }
  172. return out[:l], nil
  173. }
  174. func base64encode(in []byte) []byte {
  175. out := make([]byte, base64.StdEncoding.EncodedLen(len(in)))
  176. base64.StdEncoding.Encode(out, in)
  177. return out
  178. }
  179. func fromJSON(in []byte) (interface{}, error) {
  180. var out interface{}
  181. err := json.Unmarshal(in, &out)
  182. if err != nil {
  183. return nil, fmt.Errorf(errUnmarshalJSON, err)
  184. }
  185. return out, nil
  186. }
  187. func toJSON(in interface{}) (string, error) {
  188. output, err := json.Marshal(in)
  189. if err != nil {
  190. return "", fmt.Errorf(errMarshalJSON, err)
  191. }
  192. return string(output), nil
  193. }
  194. func toString(in []byte) string {
  195. return string(in)
  196. }
  197. func toBytes(in string) []byte {
  198. return []byte(in)
  199. }