templates.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package pki
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/cli-utils/errs"
  7. "github.com/smallstep/cli-utils/fileutil"
  8. "github.com/smallstep/cli-utils/step"
  9. "github.com/smallstep/certificates/templates"
  10. )
  11. // getTemplates returns all the templates enabled
  12. func (p *PKI) getTemplates() *templates.Templates {
  13. if !p.options.enableSSH {
  14. return nil
  15. }
  16. return &templates.Templates{
  17. SSH: &templates.DefaultSSHTemplates,
  18. Data: map[string]any{},
  19. }
  20. }
  21. // generateTemplates generates given templates.
  22. func generateTemplates(t *templates.Templates) error {
  23. if t == nil {
  24. return nil
  25. }
  26. base := GetTemplatesPath()
  27. // Generate SSH templates
  28. if t.SSH != nil {
  29. // all ssh templates are under ssh:
  30. sshDir := filepath.Join(base, "ssh")
  31. if _, err := os.Stat(sshDir); os.IsNotExist(err) {
  32. if err = os.MkdirAll(sshDir, 0700); err != nil {
  33. return errs.FileError(err, sshDir)
  34. }
  35. }
  36. // Create all templates
  37. for _, t := range t.SSH.User {
  38. data, ok := templates.DefaultSSHTemplateData[t.Name]
  39. if !ok {
  40. return errors.Errorf("template %s does not exists", t.Name)
  41. }
  42. if err := fileutil.WriteFile(step.Abs(t.TemplatePath), []byte(data), 0644); err != nil {
  43. return err
  44. }
  45. }
  46. for _, t := range t.SSH.Host {
  47. data, ok := templates.DefaultSSHTemplateData[t.Name]
  48. if !ok {
  49. return errors.Errorf("template %s does not exists", t.Name)
  50. }
  51. if err := fileutil.WriteFile(step.Abs(t.TemplatePath), []byte(data), 0644); err != nil {
  52. return err
  53. }
  54. }
  55. }
  56. return nil
  57. }