options.go 5.4 KB

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