options.go 5.7 KB

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