template.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "encoding/base64"
  16. "encoding/json"
  17. "encoding/pem"
  18. "fmt"
  19. "strings"
  20. tpl "text/template"
  21. "github.com/youmark/pkcs8"
  22. "golang.org/x/crypto/pkcs12"
  23. corev1 "k8s.io/api/core/v1"
  24. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  25. )
  26. var tplFuncs = tpl.FuncMap{
  27. "pkcs12key": pkcs12key,
  28. "pkcs12keyPass": pkcs12keyPass,
  29. "pkcs12cert": pkcs12cert,
  30. "pkcs12certPass": pkcs12certPass,
  31. "pemPrivateKey": pemPrivateKey,
  32. "pemCertificate": pemCertificate,
  33. "base64decode": base64decode,
  34. "base64encode": base64encode,
  35. "fromJSON": fromJSON,
  36. "toJSON": toJSON,
  37. "toString": toString,
  38. "toBytes": toBytes,
  39. "upper": strings.ToUpper,
  40. "lower": strings.ToLower,
  41. }
  42. const (
  43. errParse = "unable to parse template at key %s: %s"
  44. errExecute = "unable to execute template at key %s: %s"
  45. errDecodePKCS12WithPass = "unable to decode pkcs12 with password: %s"
  46. errConvertPrivKey = "unable to convert pkcs12 private key: %s"
  47. errDecodeCertWithPass = "unable to decode pkcs12 certificate with password: %s"
  48. errEncodePEMKey = "unable to encode pem private key: %s"
  49. errEncodePEMCert = "unable to encode pem certificate: %s"
  50. errDecodeBase64 = "unable to decode base64: %s"
  51. errUnmarshalJSON = "unable to unmarshal json: %s"
  52. errMarshalJSON = "unable to marshal json: %s"
  53. )
  54. // Execute renders the secret data as template. If an error occurs processing is stopped immediately.
  55. func Execute(template *esv1alpha1.ExternalSecretTemplate, secret *corev1.Secret, data map[string][]byte) error {
  56. if template == nil {
  57. return nil
  58. }
  59. for k, v := range template.Data {
  60. val, err := execute(k, v, data)
  61. if err != nil {
  62. return fmt.Errorf(errExecute, k, err)
  63. }
  64. secret.Data[k] = val
  65. }
  66. return nil
  67. }
  68. func execute(k, val string, data map[string][]byte) ([]byte, error) {
  69. t, err := tpl.New(k).
  70. Funcs(tplFuncs).
  71. Parse(val)
  72. if err != nil {
  73. return nil, fmt.Errorf(errParse, k, err)
  74. }
  75. buf := bytes.NewBuffer(nil)
  76. err = t.Execute(buf, data)
  77. if err != nil {
  78. return nil, fmt.Errorf(errExecute, k, err)
  79. }
  80. return buf.Bytes(), nil
  81. }
  82. func pkcs12keyPass(pass string, input []byte) ([]byte, error) {
  83. key, _, err := pkcs12.Decode(input, pass)
  84. if err != nil {
  85. return nil, fmt.Errorf(errDecodePKCS12WithPass, err)
  86. }
  87. kb, err := pkcs8.ConvertPrivateKeyToPKCS8(key)
  88. if err != nil {
  89. return nil, fmt.Errorf(errConvertPrivKey, err)
  90. }
  91. return kb, nil
  92. }
  93. func pkcs12key(input []byte) ([]byte, error) {
  94. return pkcs12keyPass("", input)
  95. }
  96. func pkcs12certPass(pass string, input []byte) ([]byte, error) {
  97. _, cert, err := pkcs12.Decode(input, pass)
  98. if err != nil {
  99. return nil, fmt.Errorf(errDecodeCertWithPass, err)
  100. }
  101. return cert.Raw, nil
  102. }
  103. func pkcs12cert(input []byte) ([]byte, error) {
  104. return pkcs12certPass("", input)
  105. }
  106. func pemPrivateKey(key []byte) (string, error) {
  107. buf := bytes.NewBuffer(nil)
  108. err := pem.Encode(buf, &pem.Block{Type: "PRIVATE KEY", Bytes: key})
  109. if err != nil {
  110. return "", fmt.Errorf(errEncodePEMKey, err)
  111. }
  112. return buf.String(), err
  113. }
  114. func pemCertificate(cert []byte) (string, error) {
  115. buf := bytes.NewBuffer(nil)
  116. err := pem.Encode(buf, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
  117. if err != nil {
  118. return "", fmt.Errorf(errEncodePEMCert, err)
  119. }
  120. return buf.String(), nil
  121. }
  122. func base64decode(in []byte) ([]byte, error) {
  123. out := make([]byte, len(in))
  124. l, err := base64.StdEncoding.Decode(out, in)
  125. if err != nil {
  126. return nil, fmt.Errorf(errDecodeBase64, err)
  127. }
  128. return out[:l], nil
  129. }
  130. func base64encode(in []byte) []byte {
  131. out := make([]byte, base64.StdEncoding.EncodedLen(len(in)))
  132. base64.StdEncoding.Encode(out, in)
  133. return out
  134. }
  135. func fromJSON(in []byte) (interface{}, error) {
  136. var out interface{}
  137. err := json.Unmarshal(in, &out)
  138. if err != nil {
  139. return nil, fmt.Errorf(errUnmarshalJSON, err)
  140. }
  141. return out, nil
  142. }
  143. func toJSON(in interface{}) (string, error) {
  144. output, err := json.Marshal(in)
  145. if err != nil {
  146. return "", fmt.Errorf(errMarshalJSON, err)
  147. }
  148. return string(output), nil
  149. }
  150. func toString(in []byte) string {
  151. return string(in)
  152. }
  153. func toBytes(in string) []byte {
  154. return []byte(in)
  155. }