options.go 5.7 KB

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