templates.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package sshutil
  2. // Variables used to hold template data.
  3. const (
  4. TypeKey = "Type"
  5. KeyIDKey = "KeyID"
  6. PrincipalsKey = "Principals"
  7. ExtensionsKey = "Extensions"
  8. CriticalOptionsKey = "CriticalOptions"
  9. TokenKey = "Token"
  10. InsecureKey = "Insecure"
  11. UserKey = "User"
  12. CertificateRequestKey = "CR"
  13. )
  14. // TemplateError represents an error in a template produced by the fail
  15. // function.
  16. type TemplateError struct {
  17. Message string
  18. }
  19. // Error implements the error interface and returns the error string when a
  20. // template executes the `fail "message"` function.
  21. func (e *TemplateError) Error() string {
  22. return e.Message
  23. }
  24. // TemplateData is an alias for map[string]interface{}. It represents the data
  25. // passed to the templates.
  26. type TemplateData map[string]interface{}
  27. // CreateTemplateData returns a TemplateData with the given certificate type,
  28. // key id, principals, and the default extensions.
  29. func CreateTemplateData(ct CertType, keyID string, principals []string) TemplateData {
  30. return TemplateData{
  31. TypeKey: ct.String(),
  32. KeyIDKey: keyID,
  33. PrincipalsKey: principals,
  34. ExtensionsKey: DefaultExtensions(ct),
  35. }
  36. }
  37. // DefaultExtensions returns the default extensions set in an SSH certificate.
  38. func DefaultExtensions(ct CertType) map[string]interface{} {
  39. switch ct {
  40. case UserCert:
  41. return map[string]interface{}{
  42. "permit-X11-forwarding": "",
  43. "permit-agent-forwarding": "",
  44. "permit-port-forwarding": "",
  45. "permit-pty": "",
  46. "permit-user-rc": "",
  47. }
  48. default:
  49. return nil
  50. }
  51. }
  52. // NewTemplateData creates a new map for templates data.
  53. func NewTemplateData() TemplateData {
  54. return TemplateData{}
  55. }
  56. // AddExtension adds one extension to the templates data.
  57. func (t TemplateData) AddExtension(key, value string) {
  58. if m, ok := t[ExtensionsKey].(map[string]interface{}); ok {
  59. m[key] = value
  60. } else {
  61. t[ExtensionsKey] = map[string]interface{}{
  62. key: value,
  63. }
  64. }
  65. }
  66. // AddCriticalOption adds one critical option to the templates data.
  67. func (t TemplateData) AddCriticalOption(key, value string) {
  68. if m, ok := t[CriticalOptionsKey].(map[string]interface{}); ok {
  69. m[key] = value
  70. } else {
  71. t[CriticalOptionsKey] = map[string]interface{}{
  72. key: value,
  73. }
  74. }
  75. }
  76. // Set sets a key-value pair in the template data.
  77. func (t TemplateData) Set(key string, v interface{}) {
  78. t[key] = v
  79. }
  80. // SetInsecure sets a key-value pair in the insecure template data.
  81. func (t TemplateData) SetInsecure(key string, v interface{}) {
  82. if m, ok := t[InsecureKey].(TemplateData); ok {
  83. m[key] = v
  84. } else {
  85. t[InsecureKey] = TemplateData{key: v}
  86. }
  87. }
  88. // SetType sets the certificate type in the template data.
  89. func (t TemplateData) SetType(typ CertType) {
  90. t.Set(TypeKey, typ.String())
  91. }
  92. // SetKeyID sets the certificate key id in the template data.
  93. func (t TemplateData) SetKeyID(id string) {
  94. t.Set(KeyIDKey, id)
  95. }
  96. // SetPrincipals sets the certificate principals in the template data.
  97. func (t TemplateData) SetPrincipals(p []string) {
  98. t.Set(PrincipalsKey, p)
  99. }
  100. // SetExtensions sets the certificate extensions in the template data.
  101. func (t TemplateData) SetExtensions(e map[string]interface{}) {
  102. t.Set(ExtensionsKey, e)
  103. }
  104. // SetCriticalOptions sets the certificate critical options in the template
  105. // data.
  106. func (t TemplateData) SetCriticalOptions(o map[string]interface{}) {
  107. t.Set(CriticalOptionsKey, o)
  108. }
  109. // SetToken sets the given token in the template data.
  110. func (t TemplateData) SetToken(v interface{}) {
  111. t.Set(TokenKey, v)
  112. }
  113. // SetUserData sets the given user provided object in the insecure template
  114. // data.
  115. func (t TemplateData) SetUserData(v interface{}) {
  116. t.SetInsecure(UserKey, v)
  117. }
  118. // SetCertificateRequest sets the simulated ssh certificate request the insecure
  119. // template data.
  120. func (t TemplateData) SetCertificateRequest(cr CertificateRequest) {
  121. t.SetInsecure(CertificateRequestKey, cr)
  122. }
  123. // DefaultCertificate is the default template for an SSH certificate.
  124. const DefaultCertificate = `{
  125. "type": "{{ .Type }}",
  126. "keyId": "{{ .KeyID }}",
  127. "principals": {{ toJson .Principals }},
  128. "extensions": {{ toJson .Extensions }},
  129. "criticalOptions": {{ toJson .CriticalOptions }}
  130. }`
  131. // DefaultAdminCertificate is the template used by an admin user in a OIDC
  132. // provisioner.
  133. const DefaultAdminCertificate = `{
  134. "type": "{{ .Insecure.CR.Type }}",
  135. "keyId": "{{ .Insecure.CR.KeyID }}",
  136. "principals": {{ toJson .Insecure.CR.Principals }}
  137. {{- if eq .Insecure.CR.Type "user" }}
  138. , "extensions": {{ toJson .Extensions }},
  139. "criticalOptions": {{ toJson .CriticalOptions }}
  140. {{- end }}
  141. }`
  142. // DefaultIIDCertificate is the default template for IID provisioners. By
  143. // default certificate type will be set always to host, key id to the instance
  144. // id. Principals will be only enforced by the provisioner if disableCustomSANs
  145. // is set to true.
  146. const DefaultIIDCertificate = `{
  147. "type": "{{ .Type }}",
  148. "keyId": "{{ .KeyID }}",
  149. {{- if .Insecure.CR.Principals }}
  150. "principals": {{ toJson .Insecure.CR.Principals }},
  151. {{- else }}
  152. "principals": {{ toJson .Principals }},
  153. {{- end }}
  154. "extensions": {{ toJson .Extensions }}
  155. }`
  156. // CertificateRequestTemplate is the template used for provisioners that accepts
  157. // any certificate request. The provisioner must validate that type, keyId and
  158. // principals are passed in the request.
  159. const CertificateRequestTemplate = `{
  160. "type": "{{ .Insecure.CR.Type }}",
  161. "keyId": "{{ .Insecure.CR.KeyID }}",
  162. "principals": {{ toJson .Insecure.CR.Principals }}
  163. {{- if eq .Insecure.CR.Type "user" }}
  164. , "extensions": {
  165. "permit-X11-forwarding": "",
  166. "permit-agent-forwarding": "",
  167. "permit-port-forwarding": "",
  168. "permit-pty": "",
  169. "permit-user-rc": ""
  170. }
  171. {{- end }}
  172. }`