4
0

templates.go 3.9 KB

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