options.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package provisioner
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "go.step.sm/crypto/jose"
  7. "go.step.sm/crypto/x509util"
  8. )
  9. // CertificateOptions is an interface that returns a list of options passed when
  10. // creating a new certificate.
  11. type CertificateOptions interface {
  12. Options(SignOptions) []x509util.Option
  13. }
  14. type certificateOptionsFunc func(SignOptions) []x509util.Option
  15. func (fn certificateOptionsFunc) Options(so SignOptions) []x509util.Option {
  16. return fn(so)
  17. }
  18. // Options are a collection of custom options that can be added to
  19. // each provisioner.
  20. type Options struct {
  21. X509 *X509Options `json:"x509,omitempty"`
  22. SSH *SSHOptions `json:"ssh,omitempty"`
  23. }
  24. // GetX509Options returns the X.509 options.
  25. func (o *Options) GetX509Options() *X509Options {
  26. if o == nil {
  27. return nil
  28. }
  29. return o.X509
  30. }
  31. // GetSSHOptions returns the SSH options.
  32. func (o *Options) GetSSHOptions() *SSHOptions {
  33. if o == nil {
  34. return nil
  35. }
  36. return o.SSH
  37. }
  38. // X509Options contains specific options for X.509 certificates.
  39. type X509Options struct {
  40. // Template contains a X.509 certificate template. It can be a JSON template
  41. // escaped in a string or it can be also encoded in base64.
  42. Template string `json:"template,omitempty"`
  43. // TemplateFile points to a file containing a X.509 certificate template.
  44. TemplateFile string `json:"templateFile,omitempty"`
  45. // TemplateData is a JSON object with variables that can be used in custom
  46. // templates.
  47. TemplateData json.RawMessage `json:"templateData,omitempty"`
  48. }
  49. // HasTemplate returns true if a template is defined in the provisioner options.
  50. func (o *X509Options) HasTemplate() bool {
  51. return o != nil && (o.Template != "" || o.TemplateFile != "")
  52. }
  53. // TemplateOptions generates a CertificateOptions with the template and data
  54. // defined in the ProvisionerOptions, the provisioner generated data, and the
  55. // user data provided in the request. If no template has been provided,
  56. // x509util.DefaultLeafTemplate will be used.
  57. func TemplateOptions(o *Options, data x509util.TemplateData) (CertificateOptions, error) {
  58. return CustomTemplateOptions(o, data, x509util.DefaultLeafTemplate)
  59. }
  60. // CustomTemplateOptions generates a CertificateOptions with the template, data
  61. // defined in the ProvisionerOptions, the provisioner generated data and the
  62. // user data provided in the request. If no template has been provided in the
  63. // ProvisionerOptions, the given template will be used.
  64. func CustomTemplateOptions(o *Options, data x509util.TemplateData, defaultTemplate string) (CertificateOptions, error) {
  65. opts := o.GetX509Options()
  66. if data == nil {
  67. data = x509util.NewTemplateData()
  68. }
  69. if opts != nil {
  70. // Add template data if any.
  71. if len(opts.TemplateData) > 0 && string(opts.TemplateData) != "null" {
  72. if err := json.Unmarshal(opts.TemplateData, &data); err != nil {
  73. return nil, errors.Wrap(err, "error unmarshaling template data")
  74. }
  75. }
  76. }
  77. return certificateOptionsFunc(func(so SignOptions) []x509util.Option {
  78. // We're not provided user data without custom templates.
  79. if !opts.HasTemplate() {
  80. return []x509util.Option{
  81. x509util.WithTemplate(defaultTemplate, data),
  82. }
  83. }
  84. // Add user provided data.
  85. if len(so.TemplateData) > 0 {
  86. userObject := make(map[string]interface{})
  87. if err := json.Unmarshal(so.TemplateData, &userObject); err != nil {
  88. data.SetUserData(map[string]interface{}{})
  89. } else {
  90. data.SetUserData(userObject)
  91. }
  92. }
  93. // Load a template from a file if Template is not defined.
  94. if opts.Template == "" && opts.TemplateFile != "" {
  95. return []x509util.Option{
  96. x509util.WithTemplateFile(opts.TemplateFile, data),
  97. }
  98. }
  99. // Load a template from the Template fields
  100. // 1. As a JSON in a string.
  101. template := strings.TrimSpace(opts.Template)
  102. if strings.HasPrefix(template, "{") {
  103. return []x509util.Option{
  104. x509util.WithTemplate(template, data),
  105. }
  106. }
  107. // 2. As a base64 encoded JSON.
  108. return []x509util.Option{
  109. x509util.WithTemplateBase64(template, data),
  110. }
  111. }), nil
  112. }
  113. // unsafeParseSigned parses the given token and returns all the claims without
  114. // verifying the signature of the token.
  115. func unsafeParseSigned(s string) (map[string]interface{}, error) {
  116. token, err := jose.ParseSigned(s)
  117. if err != nil {
  118. return nil, err
  119. }
  120. claims := make(map[string]interface{})
  121. if err := token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  122. return nil, err
  123. }
  124. return claims, nil
  125. }