options.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package provisioner
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/cli-utils/step"
  7. "go.step.sm/crypto/jose"
  8. "go.step.sm/crypto/x509util"
  9. "github.com/smallstep/certificates/authority/policy"
  10. "github.com/smallstep/certificates/authority/provisioner/wire"
  11. )
  12. // CertificateOptions is an interface that returns a list of options passed when
  13. // creating a new certificate.
  14. type CertificateOptions interface {
  15. Options(SignOptions) []x509util.Option
  16. }
  17. type certificateOptionsFunc func(SignOptions) []x509util.Option
  18. func (fn certificateOptionsFunc) Options(so SignOptions) []x509util.Option {
  19. return fn(so)
  20. }
  21. // Options are a collection of custom options that can be added to
  22. // each provisioner.
  23. type Options struct {
  24. X509 *X509Options `json:"x509,omitempty"`
  25. SSH *SSHOptions `json:"ssh,omitempty"`
  26. // Webhooks is a list of webhooks that can augment template data
  27. Webhooks []*Webhook `json:"webhooks,omitempty"`
  28. // Wire holds the options used for the ACME Wire integration
  29. Wire *wire.Options `json:"wire,omitempty"`
  30. }
  31. // GetX509Options returns the X.509 options.
  32. func (o *Options) GetX509Options() *X509Options {
  33. if o == nil {
  34. return nil
  35. }
  36. return o.X509
  37. }
  38. // GetSSHOptions returns the SSH options.
  39. func (o *Options) GetSSHOptions() *SSHOptions {
  40. if o == nil {
  41. return nil
  42. }
  43. return o.SSH
  44. }
  45. // GetWireOptions returns the Wire options if available. It
  46. // returns an error if they're not available.
  47. func (o *Options) GetWireOptions() (*wire.Options, error) {
  48. if o == nil {
  49. return nil, errors.New("no options available")
  50. }
  51. if o.Wire == nil {
  52. return nil, errors.New("no Wire options available")
  53. }
  54. return o.Wire, nil
  55. }
  56. // GetWebhooks returns the webhooks options.
  57. func (o *Options) GetWebhooks() []*Webhook {
  58. if o == nil {
  59. return nil
  60. }
  61. return o.Webhooks
  62. }
  63. // X509Options contains specific options for X.509 certificates.
  64. type X509Options struct {
  65. // Template contains a X.509 certificate template. It can be a JSON template
  66. // escaped in a string or it can be also encoded in base64.
  67. Template string `json:"template,omitempty"`
  68. // TemplateFile points to a file containing a X.509 certificate template.
  69. TemplateFile string `json:"templateFile,omitempty"`
  70. // TemplateData is a JSON object with variables that can be used in custom
  71. // templates.
  72. TemplateData json.RawMessage `json:"templateData,omitempty"`
  73. // AllowedNames contains the SANs the provisioner is authorized to sign
  74. AllowedNames *policy.X509NameOptions `json:"-"`
  75. // DeniedNames contains the SANs the provisioner is not authorized to sign
  76. DeniedNames *policy.X509NameOptions `json:"-"`
  77. // AllowWildcardNames indicates if literal wildcard names
  78. // like *.example.com are allowed. Defaults to false.
  79. AllowWildcardNames bool `json:"-"`
  80. }
  81. // HasTemplate returns true if a template is defined in the provisioner options.
  82. func (o *X509Options) HasTemplate() bool {
  83. return o != nil && (o.Template != "" || o.TemplateFile != "")
  84. }
  85. // GetAllowedNameOptions returns the AllowedNames, which models the
  86. // SANs that a provisioner is authorized to sign x509 certificates for.
  87. func (o *X509Options) GetAllowedNameOptions() *policy.X509NameOptions {
  88. if o == nil {
  89. return nil
  90. }
  91. return o.AllowedNames
  92. }
  93. // GetDeniedNameOptions returns the DeniedNames, which models the
  94. // SANs that a provisioner is NOT authorized to sign x509 certificates for.
  95. func (o *X509Options) GetDeniedNameOptions() *policy.X509NameOptions {
  96. if o == nil {
  97. return nil
  98. }
  99. return o.DeniedNames
  100. }
  101. func (o *X509Options) AreWildcardNamesAllowed() bool {
  102. if o == nil {
  103. return true
  104. }
  105. return o.AllowWildcardNames
  106. }
  107. // TemplateOptions generates a CertificateOptions with the template and data
  108. // defined in the ProvisionerOptions, the provisioner generated data, and the
  109. // user data provided in the request. If no template has been provided,
  110. // x509util.DefaultLeafTemplate will be used.
  111. func TemplateOptions(o *Options, data x509util.TemplateData) (CertificateOptions, error) {
  112. return CustomTemplateOptions(o, data, x509util.DefaultLeafTemplate)
  113. }
  114. // CustomTemplateOptions generates a CertificateOptions with the template, data
  115. // defined in the ProvisionerOptions, the provisioner generated data and the
  116. // user data provided in the request. If no template has been provided in the
  117. // ProvisionerOptions, the given template will be used.
  118. func CustomTemplateOptions(o *Options, data x509util.TemplateData, defaultTemplate string) (CertificateOptions, error) {
  119. opts := o.GetX509Options()
  120. if data == nil {
  121. data = x509util.NewTemplateData()
  122. }
  123. if opts != nil {
  124. // Add template data if any.
  125. if len(opts.TemplateData) > 0 && string(opts.TemplateData) != "null" {
  126. if err := json.Unmarshal(opts.TemplateData, &data); err != nil {
  127. return nil, errors.Wrap(err, "error unmarshaling template data")
  128. }
  129. }
  130. }
  131. return certificateOptionsFunc(func(so SignOptions) []x509util.Option {
  132. // We're not provided user data without custom templates.
  133. if !opts.HasTemplate() {
  134. return []x509util.Option{
  135. x509util.WithTemplate(defaultTemplate, data),
  136. }
  137. }
  138. // Add user provided data.
  139. if len(so.TemplateData) > 0 {
  140. userObject := make(map[string]interface{})
  141. if err := json.Unmarshal(so.TemplateData, &userObject); err != nil {
  142. data.SetUserData(map[string]interface{}{})
  143. } else {
  144. data.SetUserData(userObject)
  145. }
  146. }
  147. // Load a template from a file if Template is not defined.
  148. if opts.Template == "" && opts.TemplateFile != "" {
  149. return []x509util.Option{
  150. x509util.WithTemplateFile(step.Abs(opts.TemplateFile), data),
  151. }
  152. }
  153. // Load a template from the Template fields
  154. // 1. As a JSON in a string.
  155. template := strings.TrimSpace(opts.Template)
  156. if strings.HasPrefix(template, "{") {
  157. return []x509util.Option{
  158. x509util.WithTemplate(template, data),
  159. }
  160. }
  161. // 2. As a base64 encoded JSON.
  162. return []x509util.Option{
  163. x509util.WithTemplateBase64(template, data),
  164. }
  165. }), nil
  166. }
  167. // unsafeParseSigned parses the given token and returns all the claims without
  168. // verifying the signature of the token.
  169. func unsafeParseSigned(s string) (map[string]interface{}, error) {
  170. token, err := jose.ParseSigned(s)
  171. if err != nil {
  172. return nil, err
  173. }
  174. claims := make(map[string]interface{})
  175. if err := token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  176. return nil, err
  177. }
  178. return claims, nil
  179. }