templates_test.go 15 KB

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