templates.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package sshutil
  2. const (
  3. TypeKey = "Type"
  4. KeyIDKey = "KeyID"
  5. PrincipalsKey = "Principals"
  6. ExtensionsKey = "Extensions"
  7. CriticalOptionsKey = "CriticalOptions"
  8. TokenKey = "Token"
  9. InsecureKey = "Insecure"
  10. UserKey = "User"
  11. CertificateRequestKey = "CR"
  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. // AddCriticalOption adds one critical option to the templates data.
  66. func (t TemplateData) AddCriticalOption(key, value string) {
  67. if m, ok := t[CriticalOptionsKey].(map[string]interface{}); ok {
  68. m[key] = value
  69. } else {
  70. t[CriticalOptionsKey] = map[string]interface{}{
  71. key: value,
  72. }
  73. }
  74. }
  75. // Set sets a key-value pair in the template data.
  76. func (t TemplateData) Set(key string, v interface{}) {
  77. t[key] = v
  78. }
  79. // SetInsecure sets a key-value pair in the insecure template data.
  80. func (t TemplateData) SetInsecure(key string, v interface{}) {
  81. if m, ok := t[InsecureKey].(TemplateData); ok {
  82. m[key] = v
  83. } else {
  84. t[InsecureKey] = TemplateData{key: v}
  85. }
  86. }
  87. // SetType sets the certificate type in the template data.
  88. func (t TemplateData) SetType(typ CertType) {
  89. t.Set(TypeKey, typ.String())
  90. }
  91. // SetType sets the certificate key id in the template data.
  92. func (t TemplateData) SetKeyID(id string) {
  93. t.Set(KeyIDKey, id)
  94. }
  95. // SetPrincipals sets the certificate principals in the template data.
  96. func (t TemplateData) SetPrincipals(p []string) {
  97. t.Set(PrincipalsKey, p)
  98. }
  99. // SetExtensions sets the certificate extensions in the template data.
  100. func (t TemplateData) SetExtensions(e map[string]interface{}) {
  101. t.Set(ExtensionsKey, e)
  102. }
  103. // SetCriticalOptions sets the certificate critical options in the template
  104. // data.
  105. func (t TemplateData) SetCriticalOptions(o map[string]interface{}) {
  106. t.Set(CriticalOptionsKey, o)
  107. }
  108. // SetToken sets the given token in the template data.
  109. func (t TemplateData) SetToken(v interface{}) {
  110. t.Set(TokenKey, v)
  111. }
  112. // SetUserData sets the given user provided object in the insecure template
  113. // data.
  114. func (t TemplateData) SetUserData(v interface{}) {
  115. t.SetInsecure(UserKey, v)
  116. }
  117. // SetCertificateRequest sets the simulated ssh certificate request the insecure
  118. // template data.
  119. func (t TemplateData) SetCertificateRequest(cr CertificateRequest) {
  120. t.SetInsecure(CertificateRequestKey, cr)
  121. }
  122. // DefaultCertificate is the default template for an SSH certificate.
  123. const DefaultCertificate = `{
  124. "type": "{{ .Type }}",
  125. "keyId": "{{ .KeyID }}",
  126. "principals": {{ toJson .Principals }},
  127. "extensions": {{ toJson .Extensions }},
  128. "criticalOptions": {{ toJson .CriticalOptions }}
  129. }`
  130. const DefaultIIDCertificate = `{
  131. "type": "{{ .Type }}",
  132. {{- if .Insecure.CR.KeyID }}
  133. "keyId": "{{ .Insecure.CR.KeyID }}",
  134. {{- else }}
  135. "keyId": "{{ .KeyID }}",
  136. {{- end}}
  137. {{- if .Insecure.CR.Principals }}
  138. "principals": {{ toJson .Insecure.CR.Principals }},
  139. {{- else }}
  140. "principals": {{ toJson .Principals }},
  141. {{- end }}
  142. "extensions": {{ toJson .Extensions }}
  143. }`
  144. const CertificateRequestTemplate = `{
  145. "type": "{{ .Insecure.CR.Type }}",
  146. "keyId": "{{ .Insecure.CR.KeyID }}",
  147. "principals": {{ toJson .Insecure.CR.Principals }}
  148. {{- if eq .Insecure.CR.Type "user" }}
  149. , "extensions": {
  150. "permit-X11-forwarding": "",
  151. "permit-agent-forwarding": "",
  152. "permit-port-forwarding": "",
  153. "permit-pty": "",
  154. "permit-user-rc": ""
  155. }
  156. {{- end }}
  157. }`