4
0

ssh_options.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package provisioner
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "go.step.sm/crypto/sshutil"
  7. )
  8. // SSHCertificateOptions is an interface that returns a list of options passed when
  9. // creating a new certificate.
  10. type SSHCertificateOptions interface {
  11. Options(SignSSHOptions) []sshutil.Option
  12. }
  13. type sshCertificateOptionsFunc func(SignSSHOptions) []sshutil.Option
  14. func (fn sshCertificateOptionsFunc) Options(so SignSSHOptions) []sshutil.Option {
  15. return fn(so)
  16. }
  17. // SSHOptions are a collection of custom options that can be added to each
  18. // provisioner.
  19. type SSHOptions struct {
  20. // Template contains an SSH certificate template. It can be a JSON template
  21. // escaped in a string or it can be also encoded in base64.
  22. Template string `json:"template,omitempty"`
  23. // TemplateFile points to a file containing a SSH certificate template.
  24. TemplateFile string `json:"templateFile,omitempty"`
  25. // TemplateData is a JSON object with variables that can be used in custom
  26. // templates.
  27. TemplateData json.RawMessage `json:"templateData,omitempty"`
  28. }
  29. // HasTemplate returns true if a template is defined in the provisioner options.
  30. func (o *SSHOptions) HasTemplate() bool {
  31. return o != nil && (o.Template != "" || o.TemplateFile != "")
  32. }
  33. // TemplateSSHOptions generates a SSHCertificateOptions with the template and
  34. // data defined in the ProvisionerOptions, the provisioner generated data, and
  35. // the user data provided in the request. If no template has been provided,
  36. // x509util.DefaultLeafTemplate will be used.
  37. func TemplateSSHOptions(o *Options, data sshutil.TemplateData) (SSHCertificateOptions, error) {
  38. return CustomSSHTemplateOptions(o, data, sshutil.DefaultTemplate)
  39. }
  40. // CustomSSHTemplateOptions generates a CertificateOptions with the template, data
  41. // defined in the ProvisionerOptions, the provisioner generated data and the
  42. // user data provided in the request. If no template has been provided in the
  43. // ProvisionerOptions, the given template will be used.
  44. func CustomSSHTemplateOptions(o *Options, data sshutil.TemplateData, defaultTemplate string) (SSHCertificateOptions, error) {
  45. opts := o.GetSSHOptions()
  46. if data == nil {
  47. data = sshutil.NewTemplateData()
  48. }
  49. if opts != nil {
  50. // Add template data if any.
  51. if len(opts.TemplateData) > 0 && string(opts.TemplateData) != "null" {
  52. if err := json.Unmarshal(opts.TemplateData, &data); err != nil {
  53. return nil, errors.Wrap(err, "error unmarshaling template data")
  54. }
  55. }
  56. }
  57. return sshCertificateOptionsFunc(func(so SignSSHOptions) []sshutil.Option {
  58. // We're not provided user data without custom templates.
  59. if !opts.HasTemplate() {
  60. return []sshutil.Option{
  61. sshutil.WithTemplate(defaultTemplate, data),
  62. }
  63. }
  64. // Add user provided data.
  65. if len(so.TemplateData) > 0 {
  66. userObject := make(map[string]interface{})
  67. if err := json.Unmarshal(so.TemplateData, &userObject); err != nil {
  68. data.SetUserData(map[string]interface{}{})
  69. } else {
  70. data.SetUserData(userObject)
  71. }
  72. }
  73. // Load a template from a file if Template is not defined.
  74. if opts.Template == "" && opts.TemplateFile != "" {
  75. return []sshutil.Option{
  76. sshutil.WithTemplateFile(opts.TemplateFile, data),
  77. }
  78. }
  79. // Load a template from the Template fields
  80. // 1. As a JSON in a string.
  81. template := strings.TrimSpace(opts.Template)
  82. if strings.HasPrefix(template, "{") {
  83. return []sshutil.Option{
  84. sshutil.WithTemplate(template, data),
  85. }
  86. }
  87. // 2. As a base64 encoded JSON.
  88. return []sshutil.Option{
  89. sshutil.WithTemplateBase64(template, data),
  90. }
  91. }), nil
  92. }