template.go 4.3 KB

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