templates_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package templates
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "testing"
  13. "github.com/smallstep/assert"
  14. "golang.org/x/crypto/ssh"
  15. )
  16. func TestTemplates_Validate(t *testing.T) {
  17. sshTemplates := &SSHTemplates{
  18. User: []Template{
  19. {Name: "known_host.tpl", Type: File, TemplatePath: "../authority/testdata/templates/known_hosts.tpl", Path: "ssh/known_host", Comment: "#"},
  20. },
  21. Host: []Template{
  22. {Name: "ca.tpl", Type: File, TemplatePath: "../authority/testdata/templates/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
  23. },
  24. }
  25. type fields struct {
  26. SSH *SSHTemplates
  27. Data map[string]interface{}
  28. }
  29. tests := []struct {
  30. name string
  31. fields fields
  32. wantErr bool
  33. }{
  34. {"ok", fields{sshTemplates, nil}, false},
  35. {"okWithData", fields{sshTemplates, map[string]interface{}{"Foo": "Bar"}}, false},
  36. {"badSSH", fields{&SSHTemplates{User: []Template{{}}}, nil}, true},
  37. {"badDataUser", fields{sshTemplates, map[string]interface{}{"User": "Bar"}}, true},
  38. {"badDataStep", fields{sshTemplates, map[string]interface{}{"Step": "Bar"}}, true},
  39. }
  40. var nilValue *Templates
  41. assert.NoError(t, nilValue.Validate())
  42. for _, tt := range tests {
  43. t.Run(tt.name, func(t *testing.T) {
  44. tmpl := &Templates{
  45. SSH: tt.fields.SSH,
  46. Data: tt.fields.Data,
  47. }
  48. if err := tmpl.Validate(); (err != nil) != tt.wantErr {
  49. t.Errorf("Templates.Validate() error = %v, wantErr %v", err, tt.wantErr)
  50. }
  51. })
  52. }
  53. }
  54. func TestSSHTemplates_Validate(t *testing.T) {
  55. user := []Template{
  56. {Name: "include.tpl", Type: Snippet, TemplatePath: "../authority/testdata/templates/include.tpl", Path: "~/.ssh/config", Comment: "#"},
  57. }
  58. host := []Template{
  59. {Name: "ca.tpl", Type: File, TemplatePath: "../authority/testdata/templates/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
  60. }
  61. type fields struct {
  62. User []Template
  63. Host []Template
  64. }
  65. tests := []struct {
  66. name string
  67. fields fields
  68. wantErr bool
  69. }{
  70. {"ok", fields{user, host}, false},
  71. {"user", fields{user, nil}, false},
  72. {"host", fields{nil, host}, false},
  73. {"badUser", fields{[]Template{{}}, nil}, true},
  74. {"badHost", fields{nil, []Template{{}}}, true},
  75. }
  76. var nilValue *SSHTemplates
  77. assert.NoError(t, nilValue.Validate())
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. tmpl := &SSHTemplates{
  81. User: tt.fields.User,
  82. Host: tt.fields.Host,
  83. }
  84. if err := tmpl.Validate(); (err != nil) != tt.wantErr {
  85. t.Errorf("SSHTemplates.Validate() error = %v, wantErr %v", err, tt.wantErr)
  86. }
  87. })
  88. }
  89. }
  90. func TestTemplate_Validate(t *testing.T) {
  91. okPath := "~/.ssh/config"
  92. okTmplPath := "../authority/testdata/templates/include.tpl"
  93. type fields struct {
  94. Name string
  95. Type TemplateType
  96. TemplatePath string
  97. Path string
  98. Comment string
  99. }
  100. tests := []struct {
  101. name string
  102. fields fields
  103. wantErr bool
  104. }{
  105. {"okSnippet", fields{"include.tpl", Snippet, okTmplPath, okPath, "#"}, false},
  106. {"okFile", fields{"file.tpl", File, okTmplPath, okPath, "#"}, false},
  107. {"okDirectory", fields{"dir.tpl", Directory, "", "/tmp/dir", "#"}, false},
  108. {"badName", fields{"", Snippet, okTmplPath, okPath, "#"}, true},
  109. {"badType", fields{"include.tpl", "", okTmplPath, okPath, "#"}, true},
  110. {"badType", fields{"include.tpl", "foo", okTmplPath, okPath, "#"}, true},
  111. {"badTemplatePath", fields{"include.tpl", Snippet, "", okPath, "#"}, true},
  112. {"badTemplatePath", fields{"include.tpl", File, "", okPath, "#"}, true},
  113. {"badTemplatePath", fields{"include.tpl", Directory, okTmplPath, okPath, "#"}, true},
  114. {"badPath", fields{"include.tpl", Snippet, okTmplPath, "", "#"}, true},
  115. {"missingTemplate", fields{"include.tpl", Snippet, "./testdata/include.tpl", okTmplPath, "#"}, true},
  116. {"directoryTemplate", fields{"include.tpl", File, "../authority/testdata", okTmplPath, "#"}, true},
  117. }
  118. var nilValue *Template
  119. assert.NoError(t, nilValue.Validate())
  120. for _, tt := range tests {
  121. t.Run(tt.name, func(t *testing.T) {
  122. tmpl := &Template{
  123. Name: tt.fields.Name,
  124. Type: tt.fields.Type,
  125. TemplatePath: tt.fields.TemplatePath,
  126. Path: tt.fields.Path,
  127. Comment: tt.fields.Comment,
  128. }
  129. if err := tmpl.Validate(); (err != nil) != tt.wantErr {
  130. t.Errorf("Template.Validate() error = %v, wantErr %v", err, tt.wantErr)
  131. }
  132. })
  133. }
  134. }
  135. func TestLoadAll(t *testing.T) {
  136. tmpl := &Templates{
  137. SSH: &SSHTemplates{
  138. User: []Template{
  139. {Name: "include.tpl", Type: Snippet, TemplatePath: "../authority/testdata/templates/include.tpl", Path: "~/.ssh/config", Comment: "#"},
  140. },
  141. Host: []Template{
  142. {Name: "ca.tpl", Type: File, TemplatePath: "../authority/testdata/templates/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
  143. },
  144. },
  145. }
  146. type args struct {
  147. t *Templates
  148. }
  149. tests := []struct {
  150. name string
  151. args args
  152. wantErr bool
  153. }{
  154. {"ok", args{tmpl}, false},
  155. {"empty", args{&Templates{}}, false},
  156. {"nil", args{nil}, false},
  157. {"badUser", args{&Templates{SSH: &SSHTemplates{User: []Template{{}}}}}, true},
  158. {"badHost", args{&Templates{SSH: &SSHTemplates{Host: []Template{{}}}}}, true},
  159. }
  160. for _, tt := range tests {
  161. t.Run(tt.name, func(t *testing.T) {
  162. if err := LoadAll(tt.args.t); (err != nil) != tt.wantErr {
  163. t.Errorf("LoadAll() error = %v, wantErr %v", err, tt.wantErr)
  164. }
  165. })
  166. }
  167. }
  168. func TestTemplate_Load(t *testing.T) {
  169. type fields struct {
  170. Name string
  171. Type TemplateType
  172. TemplatePath string
  173. Path string
  174. Comment string
  175. }
  176. tests := []struct {
  177. name string
  178. fields fields
  179. wantErr bool
  180. }{
  181. {"ok", fields{"include.tpl", Snippet, "../authority/testdata/templates/include.tpl", "~/.ssh/config", "#"}, false},
  182. {"error", fields{"error.tpl", Snippet, "../authority/testdata/templates/error.tpl", "/tmp/error", "#"}, true},
  183. {"missing", fields{"include.tpl", Snippet, "./testdata/include.tpl", "~/.ssh/config", "#"}, true},
  184. }
  185. for _, tt := range tests {
  186. t.Run(tt.name, func(t *testing.T) {
  187. tmpl := &Template{
  188. Name: tt.fields.Name,
  189. Type: tt.fields.Type,
  190. TemplatePath: tt.fields.TemplatePath,
  191. Path: tt.fields.Path,
  192. Comment: tt.fields.Comment,
  193. }
  194. if err := tmpl.Load(); (err != nil) != tt.wantErr {
  195. t.Errorf("Template.Load() error = %v, wantErr %v", err, tt.wantErr)
  196. }
  197. })
  198. }
  199. }
  200. func TestTemplate_Render(t *testing.T) {
  201. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  202. assert.FatalError(t, err)
  203. user, err := ssh.NewPublicKey(key.Public())
  204. assert.FatalError(t, err)
  205. userB64 := base64.StdEncoding.EncodeToString(user.Marshal())
  206. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  207. assert.FatalError(t, err)
  208. host, err := ssh.NewPublicKey(key.Public())
  209. assert.FatalError(t, err)
  210. hostB64 := base64.StdEncoding.EncodeToString(host.Marshal())
  211. data := map[string]interface{}{
  212. "Step": &Step{
  213. SSH: StepSSH{
  214. UserKey: user,
  215. HostKey: host,
  216. },
  217. },
  218. "User": map[string]string{
  219. "StepPath": "/tmp/.step",
  220. "User": "john",
  221. "GOOS": "linux",
  222. },
  223. }
  224. type fields struct {
  225. Name string
  226. Type TemplateType
  227. TemplatePath string
  228. Path string
  229. Comment string
  230. }
  231. type args struct {
  232. data interface{}
  233. }
  234. tests := []struct {
  235. name string
  236. fields fields
  237. args args
  238. want []byte
  239. wantErr bool
  240. }{
  241. {"snippet", fields{"include.tpl", Snippet, "../authority/testdata/templates/include.tpl", "~/.ssh/config", "#"}, args{data}, []byte("Host *\n\tInclude /tmp/.step/ssh/config"), false},
  242. {"file", fields{"known_hosts.tpl", File, "../authority/testdata/templates/known_hosts.tpl", "ssh/known_hosts", "#"}, args{data}, []byte(fmt.Sprintf("@cert-authority * %s %s", host.Type(), hostB64)), false},
  243. {"file", fields{"ca.tpl", File, "../authority/testdata/templates/ca.tpl", "/etc/ssh/ca.pub", "#"}, args{data}, []byte(fmt.Sprintf("%s %s", user.Type(), userB64)), false},
  244. {"directory", fields{"dir.tpl", Directory, "", "/tmp/dir", ""}, args{data}, nil, false},
  245. {"error", fields{"error.tpl", File, "../authority/testdata/templates/error.tpl", "/tmp/error", "#"}, args{data}, nil, true},
  246. }
  247. for _, tt := range tests {
  248. t.Run(tt.name, func(t *testing.T) {
  249. tmpl := &Template{
  250. Name: tt.fields.Name,
  251. Type: tt.fields.Type,
  252. TemplatePath: tt.fields.TemplatePath,
  253. Path: tt.fields.Path,
  254. Comment: tt.fields.Comment,
  255. }
  256. got, err := tmpl.Render(tt.args.data)
  257. if (err != nil) != tt.wantErr {
  258. t.Errorf("Template.Render() error = %v, wantErr %v", err, tt.wantErr)
  259. return
  260. }
  261. if !reflect.DeepEqual(got, tt.want) {
  262. t.Errorf("Template.Render() = %v, want %v", string(got), string(tt.want))
  263. }
  264. })
  265. }
  266. }
  267. func TestTemplate_Output(t *testing.T) {
  268. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  269. assert.FatalError(t, err)
  270. user, err := ssh.NewPublicKey(key.Public())
  271. assert.FatalError(t, err)
  272. userB64 := base64.StdEncoding.EncodeToString(user.Marshal())
  273. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  274. assert.FatalError(t, err)
  275. host, err := ssh.NewPublicKey(key.Public())
  276. assert.FatalError(t, err)
  277. hostB64 := base64.StdEncoding.EncodeToString(host.Marshal())
  278. data := map[string]interface{}{
  279. "Step": &Step{
  280. SSH: StepSSH{
  281. UserKey: user,
  282. HostKey: host,
  283. },
  284. },
  285. "User": map[string]string{
  286. "StepPath": "/tmp/.step",
  287. },
  288. }
  289. type fields struct {
  290. Name string
  291. Type TemplateType
  292. TemplatePath string
  293. Path string
  294. Comment string
  295. }
  296. type args struct {
  297. data interface{}
  298. }
  299. tests := []struct {
  300. name string
  301. fields fields
  302. args args
  303. want []byte
  304. wantErr bool
  305. }{
  306. {"snippet", fields{"include.tpl", Snippet, "../authority/testdata/templates/include.tpl", "~/.ssh/config", "#"}, args{data}, []byte("Host *\n\tInclude /tmp/.step/ssh/config"), false},
  307. {"file", fields{"known_hosts.tpl", File, "../authority/testdata/templates/known_hosts.tpl", "ssh/known_hosts", "#"}, args{data}, []byte(fmt.Sprintf("@cert-authority * %s %s", host.Type(), hostB64)), false},
  308. {"file", fields{"ca.tpl", File, "../authority/testdata/templates/ca.tpl", "/etc/ssh/ca.pub", "#"}, args{data}, []byte(fmt.Sprintf("%s %s", user.Type(), userB64)), false},
  309. {"directory", fields{"dir.tpl", Directory, "", "/tmp/dir", ""}, args{data}, nil, false},
  310. {"error", fields{"error.tpl", File, "../authority/testdata/templates/error.tpl", "/tmp/error", "#"}, args{data}, nil, true},
  311. }
  312. for _, tt := range tests {
  313. t.Run(tt.name, func(t *testing.T) {
  314. var want Output
  315. if !tt.wantErr {
  316. want = Output{
  317. Name: tt.fields.Name,
  318. Type: tt.fields.Type,
  319. Path: tt.fields.Path,
  320. Comment: tt.fields.Comment,
  321. Content: tt.want,
  322. }
  323. }
  324. tmpl := &Template{
  325. Name: tt.fields.Name,
  326. Type: tt.fields.Type,
  327. TemplatePath: tt.fields.TemplatePath,
  328. Path: tt.fields.Path,
  329. Comment: tt.fields.Comment,
  330. }
  331. got, err := tmpl.Output(tt.args.data)
  332. if (err != nil) != tt.wantErr {
  333. t.Errorf("Template.Output() error = %v, wantErr %v", err, tt.wantErr)
  334. return
  335. }
  336. if !reflect.DeepEqual(got, want) {
  337. t.Errorf("Template.Output() = %v, want %v", got, want)
  338. }
  339. })
  340. }
  341. }
  342. func TestOutput_Write(t *testing.T) {
  343. dir, err := ioutil.TempDir("", "test-output-write")
  344. assert.FatalError(t, err)
  345. defer os.RemoveAll(dir)
  346. join := func(elem ...string) string {
  347. elems := append([]string{dir}, elem...)
  348. return filepath.Join(elems...)
  349. }
  350. assert.FatalError(t, os.Mkdir(join("bad"), 0644))
  351. type fields struct {
  352. Name string
  353. Type TemplateType
  354. Path string
  355. Comment string
  356. Content []byte
  357. }
  358. tests := []struct {
  359. name string
  360. fields fields
  361. wantErr bool
  362. }{
  363. {"snippet", fields{"snippet", Snippet, join("snippet"), "#", []byte("some content")}, false},
  364. {"file", fields{"file", File, join("file"), "#", []byte("some content")}, false},
  365. {"snippetInDir", fields{"file", Snippet, join("dir", "snippets", "snippet"), "#", []byte("some content")}, false},
  366. {"fileInDir", fields{"file", File, join("dir", "files", "file"), "#", []byte("some content")}, false},
  367. {"directory", fields{"directory", Directory, join("directory"), "", nil}, false},
  368. {"snippetErr", fields{"snippet", Snippet, join("bad", "snippet"), "#", []byte("some content")}, true},
  369. {"fileErr", fields{"file", File, join("bad", "file"), "#", []byte("some content")}, true},
  370. {"directoryErr", fields{"directory", Directory, join("bad", "directory"), "", nil}, true},
  371. }
  372. for _, tt := range tests {
  373. t.Run(tt.name, func(t *testing.T) {
  374. o := &Output{
  375. Name: tt.fields.Name,
  376. Type: tt.fields.Type,
  377. Comment: tt.fields.Comment,
  378. Path: tt.fields.Path,
  379. Content: tt.fields.Content,
  380. }
  381. if err := o.Write(); (err != nil) != tt.wantErr {
  382. t.Errorf("Output.Write() error = %v, wantErr %v", err, tt.wantErr)
  383. }
  384. if !tt.wantErr {
  385. st, err := os.Stat(o.Path)
  386. if err != nil {
  387. t.Errorf("os.Stat(%s) error = %v", o.Path, err)
  388. } else {
  389. if o.Type == Directory {
  390. assert.True(t, st.IsDir())
  391. assert.Equals(t, os.ModeDir|os.FileMode(0700), st.Mode())
  392. } else {
  393. assert.False(t, st.IsDir())
  394. assert.Equals(t, os.FileMode(0600), st.Mode())
  395. }
  396. }
  397. }
  398. })
  399. }
  400. }