template.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/pem"
  17. "fmt"
  18. tpl "text/template"
  19. "github.com/Masterminds/sprig/v3"
  20. "github.com/lestrrat-go/jwx/jwk"
  21. "github.com/youmark/pkcs8"
  22. "golang.org/x/crypto/pkcs12"
  23. corev1 "k8s.io/api/core/v1"
  24. )
  25. var tplFuncs = tpl.FuncMap{
  26. "pkcs12key": pkcs12key,
  27. "pkcs12keyPass": pkcs12keyPass,
  28. "pkcs12cert": pkcs12cert,
  29. "pkcs12certPass": pkcs12certPass,
  30. "pemPrivateKey": pemPrivateKey,
  31. "pemCertificate": pemCertificate,
  32. "jwkPublicKeyPem": jwkPublicKeyPem,
  33. "jwkPrivateKeyPem": jwkPrivateKeyPem,
  34. }
  35. // So other templating calls can use the same extra functions.
  36. func FuncMap() tpl.FuncMap {
  37. return tplFuncs
  38. }
  39. const (
  40. errParse = "unable to parse template at key %s: %s"
  41. errExecute = "unable to execute template at key %s: %s"
  42. errDecodePKCS12WithPass = "unable to decode pkcs12 with password: %s"
  43. errConvertPrivKey = "unable to convert pkcs12 private key: %s"
  44. errDecodeCertWithPass = "unable to decode pkcs12 certificate with password: %s"
  45. errEncodePEMKey = "unable to encode pem private key: %s"
  46. errEncodePEMCert = "unable to encode pem certificate: %s"
  47. )
  48. func init() {
  49. fmt.Printf("calling init in v2 pkg")
  50. sprigFuncs := sprig.TxtFuncMap()
  51. delete(sprigFuncs, "env")
  52. delete(sprigFuncs, "expandenv")
  53. for k, v := range sprigFuncs {
  54. fmt.Printf("adding func %s\n", k)
  55. tplFuncs[k] = v
  56. }
  57. }
  58. // Execute renders the secret data as template. If an error occurs processing is stopped immediately.
  59. func Execute(tpl, data map[string][]byte, secret *corev1.Secret) error {
  60. if tpl == nil {
  61. return nil
  62. }
  63. for k, v := range tpl {
  64. val, err := execute(k, string(v), data)
  65. if err != nil {
  66. return fmt.Errorf(errExecute, k, err)
  67. }
  68. secret.Data[k] = val
  69. }
  70. return nil
  71. }
  72. func execute(k, val string, data map[string][]byte) ([]byte, error) {
  73. strValData := make(map[string]string, len(data))
  74. for k := range data {
  75. strValData[k] = string(data[k])
  76. }
  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, strValData)
  85. if err != nil {
  86. return nil, fmt.Errorf(errExecute, k, err)
  87. }
  88. return buf.Bytes(), nil
  89. }
  90. func pkcs12keyPass(pass, input string) (string, error) {
  91. key, _, err := pkcs12.Decode([]byte(input), pass)
  92. if err != nil {
  93. return "", fmt.Errorf(errDecodePKCS12WithPass, err)
  94. }
  95. kb, err := pkcs8.ConvertPrivateKeyToPKCS8(key)
  96. if err != nil {
  97. return "", fmt.Errorf(errConvertPrivKey, err)
  98. }
  99. return string(kb), nil
  100. }
  101. func pkcs12key(input string) (string, error) {
  102. return pkcs12keyPass("", input)
  103. }
  104. func pkcs12certPass(pass, input string) (string, error) {
  105. _, cert, err := pkcs12.Decode([]byte(input), pass)
  106. if err != nil {
  107. return "", fmt.Errorf(errDecodeCertWithPass, err)
  108. }
  109. return string(cert.Raw), nil
  110. }
  111. func pkcs12cert(input string) (string, error) {
  112. return pkcs12certPass("", input)
  113. }
  114. func jwkPublicKeyPem(jwkjson string) (string, error) {
  115. k, err := jwk.ParseKey([]byte(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(string(mpk), "PUBLIC KEY")
  129. }
  130. func jwkPrivateKeyPem(jwkjson string) (string, error) {
  131. k, err := jwk.ParseKey([]byte(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(string(mpk), "PRIVATE KEY")
  146. }
  147. func pemEncode(thing, kind string) (string, error) {
  148. buf := bytes.NewBuffer(nil)
  149. err := pem.Encode(buf, &pem.Block{Type: kind, Bytes: []byte(thing)})
  150. return buf.String(), err
  151. }
  152. func pemPrivateKey(key string) (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 string) (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. }