ssh_options.go 4.9 KB

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