templates.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package sshutil
  2. import "golang.org/x/crypto/ssh"
  3. const (
  4. TypeKey = "Type"
  5. KeyIDKey = "KeyID"
  6. PrincipalsKey = "Principals"
  7. ExtensionsKey = "Extensions"
  8. TokenKey = "Token"
  9. InsecureKey = "Insecure"
  10. UserKey = "User"
  11. PublicKey = "PublicKey"
  12. )
  13. // TemplateError represents an error in a template produced by the fail
  14. // function.
  15. type TemplateError struct {
  16. Message string
  17. }
  18. // Error implements the error interface and returns the error string when a
  19. // template executes the `fail "message"` function.
  20. func (e *TemplateError) Error() string {
  21. return e.Message
  22. }
  23. // TemplateData is an alias for map[string]interface{}. It represents the data
  24. // passed to the templates.
  25. type TemplateData map[string]interface{}
  26. // CreateTemplateData returns a TemplateData with the given certificate type,
  27. // key id, principals, and the default extensions.
  28. func CreateTemplateData(ct CertType, keyID string, principals []string) TemplateData {
  29. return TemplateData{
  30. TypeKey: ct.String(),
  31. KeyIDKey: keyID,
  32. PrincipalsKey: principals,
  33. ExtensionsKey: DefaultExtensions(ct),
  34. }
  35. }
  36. // DefaultExtensions returns the default extensions set in an SSH certificate.
  37. func DefaultExtensions(ct CertType) map[string]interface{} {
  38. switch ct {
  39. case UserCert:
  40. return map[string]interface{}{
  41. "permit-X11-forwarding": "",
  42. "permit-agent-forwarding": "",
  43. "permit-port-forwarding": "",
  44. "permit-pty": "",
  45. "permit-user-rc": "",
  46. }
  47. default:
  48. return nil
  49. }
  50. }
  51. // NewTemplateData creates a new map for templates data.
  52. func NewTemplateData() TemplateData {
  53. return TemplateData{}
  54. }
  55. // AddExtension adds one extension to the templates data.
  56. func (t TemplateData) AddExtension(key, value string) {
  57. if m, ok := t[ExtensionsKey].(map[string]interface{}); ok {
  58. m[key] = value
  59. } else {
  60. t[ExtensionsKey] = map[string]interface{}{
  61. key: value,
  62. }
  63. }
  64. }
  65. // Set sets a key-value pair in the template data.
  66. func (t TemplateData) Set(key string, v interface{}) {
  67. t[key] = v
  68. }
  69. // SetInsecure sets a key-value pair in the insecure template data.
  70. func (t TemplateData) SetInsecure(key string, v interface{}) {
  71. if m, ok := t[InsecureKey].(TemplateData); ok {
  72. m[key] = v
  73. } else {
  74. t[InsecureKey] = TemplateData{key: v}
  75. }
  76. }
  77. // SetType sets the certificate type in the template data.
  78. func (t TemplateData) SetType(typ CertType) {
  79. t.Set(TypeKey, typ.String())
  80. }
  81. // SetType sets the certificate key id in the template data.
  82. func (t TemplateData) SetKeyID(id string) {
  83. t.Set(KeyIDKey, id)
  84. }
  85. // SetPrincipals sets the certificate principals in the template data.
  86. func (t TemplateData) SetPrincipals(p []string) {
  87. t.Set(PrincipalsKey, p)
  88. }
  89. // SetExtensions sets the certificate extensions in the template data.
  90. func (t TemplateData) SetExtensions(e map[string]interface{}) {
  91. t.Set(ExtensionsKey, e)
  92. }
  93. // SetToken sets the given token in the template data.
  94. func (t TemplateData) SetToken(v interface{}) {
  95. t.Set(TokenKey, v)
  96. }
  97. // SetUserData sets the given user provided object in the insecure template
  98. // data.
  99. func (t TemplateData) SetUserData(v interface{}) {
  100. t.SetInsecure(UserKey, v)
  101. }
  102. // SetUserData sets the given user provided object in the insecure template
  103. // data.
  104. func (t TemplateData) SetPublicKey(v ssh.PublicKey) {
  105. t.Set(PublicKey, v)
  106. }
  107. // DefaultCertificate is the default template for an SSH certificate.
  108. const DefaultCertificate = `{
  109. "type": "{{ .Type }}",
  110. "keyId": "{{ .KeyID }}",
  111. "principals": {{ toJson .Principals }},
  112. "extensions": {{ toJson .Extensions }}
  113. }`