ssh_options.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/sshutil"
  8. "github.com/smallstep/certificates/authority/policy"
  9. )
  10. // SSHCertificateOptions is an interface that returns a list of options passed when
  11. // creating a new certificate.
  12. type SSHCertificateOptions interface {
  13. Options(SignSSHOptions) []sshutil.Option
  14. }
  15. type sshCertificateOptionsFunc func(SignSSHOptions) []sshutil.Option
  16. func (fn sshCertificateOptionsFunc) Options(so SignSSHOptions) []sshutil.Option {
  17. return fn(so)
  18. }
  19. // SSHOptions are a collection of custom options that can be added to each
  20. // provisioner.
  21. type SSHOptions struct {
  22. // Template contains an SSH certificate template. It can be a JSON template
  23. // escaped in a string or it can be also encoded in base64.
  24. Template string `json:"template,omitempty"`
  25. // TemplateFile points to a file containing a SSH certificate template.
  26. TemplateFile string `json:"templateFile,omitempty"`
  27. // TemplateData is a JSON object with variables that can be used in custom
  28. // templates.
  29. TemplateData json.RawMessage `json:"templateData,omitempty"`
  30. // User contains SSH user certificate options.
  31. User *policy.SSHUserCertificateOptions `json:"-"`
  32. // Host contains SSH host certificate options.
  33. Host *policy.SSHHostCertificateOptions `json:"-"`
  34. }
  35. // GetAllowedUserNameOptions returns the SSHNameOptions that are
  36. // allowed when SSH User certificates are requested.
  37. func (o *SSHOptions) GetAllowedUserNameOptions() *policy.SSHNameOptions {
  38. if o == nil {
  39. return nil
  40. }
  41. if o.User == nil {
  42. return nil
  43. }
  44. return o.User.AllowedNames
  45. }
  46. // GetDeniedUserNameOptions returns the SSHNameOptions that are
  47. // denied when SSH user certificates are requested.
  48. func (o *SSHOptions) GetDeniedUserNameOptions() *policy.SSHNameOptions {
  49. if o == nil {
  50. return nil
  51. }
  52. if o.User == nil {
  53. return nil
  54. }
  55. return o.User.DeniedNames
  56. }
  57. // GetAllowedHostNameOptions returns the SSHNameOptions that are
  58. // allowed when SSH host certificates are requested.
  59. func (o *SSHOptions) GetAllowedHostNameOptions() *policy.SSHNameOptions {
  60. if o == nil {
  61. return nil
  62. }
  63. if o.Host == nil {
  64. return nil
  65. }
  66. return o.Host.AllowedNames
  67. }
  68. // GetDeniedHostNameOptions returns the SSHNameOptions that are
  69. // denied when SSH host certificates are requested.
  70. func (o *SSHOptions) GetDeniedHostNameOptions() *policy.SSHNameOptions {
  71. if o == nil {
  72. return nil
  73. }
  74. if o.Host == nil {
  75. return nil
  76. }
  77. return o.Host.DeniedNames
  78. }
  79. // HasTemplate returns true if a template is defined in the provisioner options.
  80. func (o *SSHOptions) HasTemplate() bool {
  81. return o != nil && (o.Template != "" || o.TemplateFile != "")
  82. }
  83. // TemplateSSHOptions generates a SSHCertificateOptions with the template and
  84. // data defined in the ProvisionerOptions, the provisioner generated data, and
  85. // the user data provided in the request. If no template has been provided,
  86. // x509util.DefaultLeafTemplate will be used.
  87. func TemplateSSHOptions(o *Options, data sshutil.TemplateData) (SSHCertificateOptions, error) {
  88. return CustomSSHTemplateOptions(o, data, sshutil.DefaultTemplate)
  89. }
  90. // CustomSSHTemplateOptions 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 CustomSSHTemplateOptions(o *Options, data sshutil.TemplateData, defaultTemplate string) (SSHCertificateOptions, error) {
  95. opts := o.GetSSHOptions()
  96. if data == nil {
  97. data = sshutil.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 sshCertificateOptionsFunc(func(so SignSSHOptions) []sshutil.Option {
  108. // We're not provided user data without custom templates.
  109. if !opts.HasTemplate() {
  110. return []sshutil.Option{
  111. sshutil.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 []sshutil.Option{
  126. sshutil.WithTemplateFile(step.Abs(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 []sshutil.Option{
  134. sshutil.WithTemplate(template, data),
  135. }
  136. }
  137. // 2. As a base64 encoded JSON.
  138. return []sshutil.Option{
  139. sshutil.WithTemplateBase64(template, data),
  140. }
  141. }), nil
  142. }