4
0

templates_test.go 14 KB

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