4
0

templates_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. },
  221. }
  222. type fields struct {
  223. Name string
  224. Type TemplateType
  225. TemplatePath string
  226. Path string
  227. Comment string
  228. }
  229. type args struct {
  230. data interface{}
  231. }
  232. tests := []struct {
  233. name string
  234. fields fields
  235. args args
  236. want []byte
  237. wantErr bool
  238. }{
  239. {"snippet", fields{"include.tpl", Snippet, "../authority/testdata/templates/include.tpl", "~/.ssh/config", "#"}, args{data}, []byte("Host *\n\tInclude /tmp/.step/ssh/config"), false},
  240. {"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},
  241. {"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},
  242. {"directory", fields{"dir.tpl", Directory, "", "/tmp/dir", ""}, args{data}, nil, false},
  243. {"error", fields{"error.tpl", File, "../authority/testdata/templates/error.tpl", "/tmp/error", "#"}, args{data}, nil, true},
  244. }
  245. for _, tt := range tests {
  246. t.Run(tt.name, func(t *testing.T) {
  247. tmpl := &Template{
  248. Name: tt.fields.Name,
  249. Type: tt.fields.Type,
  250. TemplatePath: tt.fields.TemplatePath,
  251. Path: tt.fields.Path,
  252. Comment: tt.fields.Comment,
  253. }
  254. got, err := tmpl.Render(tt.args.data)
  255. if (err != nil) != tt.wantErr {
  256. t.Errorf("Template.Render() error = %v, wantErr %v", err, tt.wantErr)
  257. return
  258. }
  259. if !reflect.DeepEqual(got, tt.want) {
  260. t.Errorf("Template.Render() = %v, want %v", got, tt.want)
  261. }
  262. })
  263. }
  264. }
  265. func TestTemplate_Output(t *testing.T) {
  266. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  267. assert.FatalError(t, err)
  268. user, err := ssh.NewPublicKey(key.Public())
  269. assert.FatalError(t, err)
  270. userB64 := base64.StdEncoding.EncodeToString(user.Marshal())
  271. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  272. assert.FatalError(t, err)
  273. host, err := ssh.NewPublicKey(key.Public())
  274. assert.FatalError(t, err)
  275. hostB64 := base64.StdEncoding.EncodeToString(host.Marshal())
  276. data := map[string]interface{}{
  277. "Step": &Step{
  278. SSH: StepSSH{
  279. UserKey: user,
  280. HostKey: host,
  281. },
  282. },
  283. "User": map[string]string{
  284. "StepPath": "/tmp/.step",
  285. },
  286. }
  287. type fields struct {
  288. Name string
  289. Type TemplateType
  290. TemplatePath string
  291. Path string
  292. Comment string
  293. }
  294. type args struct {
  295. data interface{}
  296. }
  297. tests := []struct {
  298. name string
  299. fields fields
  300. args args
  301. want []byte
  302. wantErr bool
  303. }{
  304. {"snippet", fields{"include.tpl", Snippet, "../authority/testdata/templates/include.tpl", "~/.ssh/config", "#"}, args{data}, []byte("Host *\n\tInclude /tmp/.step/ssh/config"), false},
  305. {"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},
  306. {"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},
  307. {"directory", fields{"dir.tpl", Directory, "", "/tmp/dir", ""}, args{data}, nil, false},
  308. {"error", fields{"error.tpl", File, "../authority/testdata/templates/error.tpl", "/tmp/error", "#"}, args{data}, nil, true},
  309. }
  310. for _, tt := range tests {
  311. t.Run(tt.name, func(t *testing.T) {
  312. var want Output
  313. if !tt.wantErr {
  314. want = Output{
  315. Name: tt.fields.Name,
  316. Type: tt.fields.Type,
  317. Path: tt.fields.Path,
  318. Comment: tt.fields.Comment,
  319. Content: tt.want,
  320. }
  321. }
  322. tmpl := &Template{
  323. Name: tt.fields.Name,
  324. Type: tt.fields.Type,
  325. TemplatePath: tt.fields.TemplatePath,
  326. Path: tt.fields.Path,
  327. Comment: tt.fields.Comment,
  328. }
  329. got, err := tmpl.Output(tt.args.data)
  330. if (err != nil) != tt.wantErr {
  331. t.Errorf("Template.Output() error = %v, wantErr %v", err, tt.wantErr)
  332. return
  333. }
  334. if !reflect.DeepEqual(got, want) {
  335. t.Errorf("Template.Output() = %v, want %v", got, want)
  336. }
  337. })
  338. }
  339. }
  340. func TestOutput_Write(t *testing.T) {
  341. dir, err := ioutil.TempDir("", "test-output-write")
  342. assert.FatalError(t, err)
  343. defer os.RemoveAll(dir)
  344. join := func(elem ...string) string {
  345. elems := append([]string{dir}, elem...)
  346. return filepath.Join(elems...)
  347. }
  348. assert.FatalError(t, os.Mkdir(join("bad"), 0644))
  349. type fields struct {
  350. Name string
  351. Type TemplateType
  352. Path string
  353. Comment string
  354. Content []byte
  355. }
  356. tests := []struct {
  357. name string
  358. fields fields
  359. wantErr bool
  360. }{
  361. {"snippet", fields{"snippet", Snippet, join("snippet"), "#", []byte("some content")}, false},
  362. {"file", fields{"file", File, join("file"), "#", []byte("some content")}, false},
  363. {"snippetInDir", fields{"file", Snippet, join("dir", "snippets", "snippet"), "#", []byte("some content")}, false},
  364. {"fileInDir", fields{"file", File, join("dir", "files", "file"), "#", []byte("some content")}, false},
  365. {"directory", fields{"directory", Directory, join("directory"), "", nil}, false},
  366. {"snippetErr", fields{"snippet", Snippet, join("bad", "snippet"), "#", []byte("some content")}, true},
  367. {"fileErr", fields{"file", File, join("bad", "file"), "#", []byte("some content")}, true},
  368. {"directoryErr", fields{"directory", Directory, join("bad", "directory"), "", nil}, true},
  369. }
  370. for _, tt := range tests {
  371. t.Run(tt.name, func(t *testing.T) {
  372. o := &Output{
  373. Name: tt.fields.Name,
  374. Type: tt.fields.Type,
  375. Comment: tt.fields.Comment,
  376. Path: tt.fields.Path,
  377. Content: tt.fields.Content,
  378. }
  379. if err := o.Write(); (err != nil) != tt.wantErr {
  380. t.Errorf("Output.Write() error = %v, wantErr %v", err, tt.wantErr)
  381. }
  382. if !tt.wantErr {
  383. st, err := os.Stat(o.Path)
  384. if err != nil {
  385. t.Errorf("os.Stat(%s) error = %v", o.Path, err)
  386. } else {
  387. if o.Type == Directory {
  388. assert.True(t, st.IsDir())
  389. assert.Equals(t, os.ModeDir|os.FileMode(0700), st.Mode())
  390. } else {
  391. assert.False(t, st.IsDir())
  392. assert.Equals(t, os.FileMode(0600), st.Mode())
  393. }
  394. }
  395. }
  396. })
  397. }
  398. }