ssh_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. package authority
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/x509"
  8. "encoding/base64"
  9. "fmt"
  10. "net/http"
  11. "reflect"
  12. "testing"
  13. "time"
  14. "github.com/pkg/errors"
  15. "github.com/smallstep/assert"
  16. "github.com/smallstep/certificates/authority/provisioner"
  17. "github.com/smallstep/certificates/db"
  18. "github.com/smallstep/certificates/errs"
  19. "github.com/smallstep/certificates/sshutil"
  20. "github.com/smallstep/certificates/templates"
  21. "github.com/smallstep/cli/jose"
  22. "golang.org/x/crypto/ssh"
  23. )
  24. type sshTestModifier ssh.Certificate
  25. func (m sshTestModifier) Modify(cert *ssh.Certificate, _ provisioner.SignSSHOptions) error {
  26. if m.CertType != 0 {
  27. cert.CertType = m.CertType
  28. }
  29. if m.KeyId != "" {
  30. cert.KeyId = m.KeyId
  31. }
  32. if m.ValidAfter != 0 {
  33. cert.ValidAfter = m.ValidAfter
  34. }
  35. if m.ValidBefore != 0 {
  36. cert.ValidBefore = m.ValidBefore
  37. }
  38. if len(m.ValidPrincipals) != 0 {
  39. cert.ValidPrincipals = m.ValidPrincipals
  40. }
  41. if m.Permissions.CriticalOptions != nil {
  42. cert.Permissions.CriticalOptions = m.Permissions.CriticalOptions
  43. }
  44. if m.Permissions.Extensions != nil {
  45. cert.Permissions.Extensions = m.Permissions.Extensions
  46. }
  47. return nil
  48. }
  49. type sshTestCertModifier string
  50. func (m sshTestCertModifier) Modify(cert *ssh.Certificate, opts provisioner.SignSSHOptions) error {
  51. if m == "" {
  52. return nil
  53. }
  54. return fmt.Errorf(string(m))
  55. }
  56. type sshTestCertValidator string
  57. func (v sshTestCertValidator) Valid(crt *ssh.Certificate, opts provisioner.SignSSHOptions) error {
  58. if v == "" {
  59. return nil
  60. }
  61. return fmt.Errorf(string(v))
  62. }
  63. type sshTestOptionsValidator string
  64. func (v sshTestOptionsValidator) Valid(opts provisioner.SignSSHOptions) error {
  65. if v == "" {
  66. return nil
  67. }
  68. return fmt.Errorf(string(v))
  69. }
  70. type sshTestOptionsModifier string
  71. func (m sshTestOptionsModifier) Modify(cert *ssh.Certificate, opts provisioner.SignSSHOptions) error {
  72. if m == "" {
  73. return nil
  74. }
  75. return fmt.Errorf(string(m))
  76. }
  77. func TestAuthority_SignSSH(t *testing.T) {
  78. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  79. assert.FatalError(t, err)
  80. pub, err := ssh.NewPublicKey(key.Public())
  81. assert.FatalError(t, err)
  82. signKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  83. assert.FatalError(t, err)
  84. signer, err := ssh.NewSignerFromKey(signKey)
  85. assert.FatalError(t, err)
  86. userOptions := sshTestModifier{
  87. CertType: ssh.UserCert,
  88. }
  89. hostOptions := sshTestModifier{
  90. CertType: ssh.HostCert,
  91. }
  92. userTemplate, err := provisioner.TemplateSSHOptions(nil, sshutil.CreateTemplateData(sshutil.UserCert, "key-id", nil))
  93. assert.FatalError(t, err)
  94. hostTemplate, err := provisioner.TemplateSSHOptions(nil, sshutil.CreateTemplateData(sshutil.HostCert, "key-id", nil))
  95. assert.FatalError(t, err)
  96. userTemplateWithUser, err := provisioner.TemplateSSHOptions(nil, sshutil.CreateTemplateData(sshutil.UserCert, "key-id", []string{"user"}))
  97. assert.FatalError(t, err)
  98. hostTemplateWithHosts, err := provisioner.TemplateSSHOptions(nil, sshutil.CreateTemplateData(sshutil.HostCert, "key-id", []string{"foo.test.com", "bar.test.com"}))
  99. assert.FatalError(t, err)
  100. now := time.Now()
  101. type fields struct {
  102. sshCAUserCertSignKey ssh.Signer
  103. sshCAHostCertSignKey ssh.Signer
  104. }
  105. type args struct {
  106. key ssh.PublicKey
  107. opts provisioner.SignSSHOptions
  108. signOpts []provisioner.SignOption
  109. }
  110. type want struct {
  111. CertType uint32
  112. Principals []string
  113. ValidAfter uint64
  114. ValidBefore uint64
  115. }
  116. tests := []struct {
  117. name string
  118. fields fields
  119. args args
  120. want want
  121. wantErr bool
  122. }{
  123. {"ok-user", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions}}, want{CertType: ssh.UserCert}, false},
  124. {"ok-host", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{hostTemplate, hostOptions}}, want{CertType: ssh.HostCert}, false},
  125. {"ok-opts-type-user", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "user"}, []provisioner.SignOption{userTemplate}}, want{CertType: ssh.UserCert}, false},
  126. {"ok-opts-type-host", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "host"}, []provisioner.SignOption{hostTemplate}}, want{CertType: ssh.HostCert}, false},
  127. {"ok-opts-principals", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "user", Principals: []string{"user"}}, []provisioner.SignOption{userTemplateWithUser}}, want{CertType: ssh.UserCert, Principals: []string{"user"}}, false},
  128. {"ok-opts-principals", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "host", Principals: []string{"foo.test.com", "bar.test.com"}}, []provisioner.SignOption{hostTemplateWithHosts}}, want{CertType: ssh.HostCert, Principals: []string{"foo.test.com", "bar.test.com"}}, false},
  129. {"ok-opts-valid-after", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "user", ValidAfter: provisioner.NewTimeDuration(now)}, []provisioner.SignOption{userTemplate}}, want{CertType: ssh.UserCert, ValidAfter: uint64(now.Unix())}, false},
  130. {"ok-opts-valid-before", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "host", ValidBefore: provisioner.NewTimeDuration(now)}, []provisioner.SignOption{hostTemplate}}, want{CertType: ssh.HostCert, ValidBefore: uint64(now.Unix())}, false},
  131. {"ok-cert-validator", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestCertValidator("")}}, want{CertType: ssh.UserCert}, false},
  132. {"ok-cert-modifier", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestCertModifier("")}}, want{CertType: ssh.UserCert}, false},
  133. {"ok-opts-validator", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestOptionsValidator("")}}, want{CertType: ssh.UserCert}, false},
  134. {"ok-opts-modifier", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestOptionsModifier("")}}, want{CertType: ssh.UserCert}, false},
  135. {"fail-opts-type", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{CertType: "foo"}, []provisioner.SignOption{userTemplate}}, want{}, true},
  136. {"fail-cert-validator", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestCertValidator("an error")}}, want{}, true},
  137. {"fail-cert-modifier", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestCertModifier("an error")}}, want{}, true},
  138. {"fail-opts-validator", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestOptionsValidator("an error")}}, want{}, true},
  139. {"fail-opts-modifier", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, sshTestOptionsModifier("an error")}}, want{}, true},
  140. {"fail-bad-sign-options", fields{signer, signer}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, userOptions, "wrong type"}}, want{}, true},
  141. {"fail-no-user-key", fields{nil, signer}, args{pub, provisioner.SignSSHOptions{CertType: "user"}, []provisioner.SignOption{userTemplate}}, want{}, true},
  142. {"fail-no-host-key", fields{signer, nil}, args{pub, provisioner.SignSSHOptions{CertType: "host"}, []provisioner.SignOption{hostTemplate}}, want{}, true},
  143. {"fail-bad-type", fields{signer, nil}, args{pub, provisioner.SignSSHOptions{}, []provisioner.SignOption{userTemplate, sshTestModifier{CertType: 100}}}, want{}, true},
  144. }
  145. for _, tt := range tests {
  146. t.Run(tt.name, func(t *testing.T) {
  147. a := testAuthority(t)
  148. a.sshCAUserCertSignKey = tt.fields.sshCAUserCertSignKey
  149. a.sshCAHostCertSignKey = tt.fields.sshCAHostCertSignKey
  150. got, err := a.SignSSH(context.Background(), tt.args.key, tt.args.opts, tt.args.signOpts...)
  151. if (err != nil) != tt.wantErr {
  152. t.Errorf("Authority.SignSSH() error = %v, wantErr %v", err, tt.wantErr)
  153. return
  154. }
  155. if err == nil && assert.NotNil(t, got) {
  156. assert.Equals(t, tt.want.CertType, got.CertType)
  157. assert.Equals(t, tt.want.Principals, got.ValidPrincipals)
  158. assert.Equals(t, tt.want.ValidAfter, got.ValidAfter)
  159. assert.Equals(t, tt.want.ValidBefore, got.ValidBefore)
  160. assert.NotNil(t, got.Key)
  161. assert.NotNil(t, got.Nonce)
  162. assert.NotEquals(t, 0, got.Serial)
  163. assert.NotNil(t, got.Signature)
  164. assert.NotNil(t, got.SignatureKey)
  165. }
  166. })
  167. }
  168. }
  169. func TestAuthority_SignSSHAddUser(t *testing.T) {
  170. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  171. assert.FatalError(t, err)
  172. pub, err := ssh.NewPublicKey(key.Public())
  173. assert.FatalError(t, err)
  174. signKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  175. assert.FatalError(t, err)
  176. signer, err := ssh.NewSignerFromKey(signKey)
  177. assert.FatalError(t, err)
  178. type fields struct {
  179. sshCAUserCertSignKey ssh.Signer
  180. sshCAHostCertSignKey ssh.Signer
  181. addUserPrincipal string
  182. addUserCommand string
  183. }
  184. type args struct {
  185. key ssh.PublicKey
  186. subject *ssh.Certificate
  187. }
  188. type want struct {
  189. CertType uint32
  190. Principals []string
  191. ValidAfter uint64
  192. ValidBefore uint64
  193. ForceCommand string
  194. }
  195. now := time.Now()
  196. validCert := &ssh.Certificate{
  197. CertType: ssh.UserCert,
  198. ValidPrincipals: []string{"user"},
  199. ValidAfter: uint64(now.Unix()),
  200. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  201. }
  202. validWant := want{
  203. CertType: ssh.UserCert,
  204. Principals: []string{"provisioner"},
  205. ValidAfter: uint64(now.Unix()),
  206. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  207. ForceCommand: "sudo useradd -m user; nc -q0 localhost 22",
  208. }
  209. tests := []struct {
  210. name string
  211. fields fields
  212. args args
  213. want want
  214. wantErr bool
  215. }{
  216. {"ok", fields{signer, signer, "", ""}, args{pub, validCert}, validWant, false},
  217. {"ok-no-host-key", fields{signer, nil, "", ""}, args{pub, validCert}, validWant, false},
  218. {"ok-custom-principal", fields{signer, signer, "my-principal", ""}, args{pub, &ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"user"}}}, want{CertType: ssh.UserCert, Principals: []string{"my-principal"}, ForceCommand: "sudo useradd -m user; nc -q0 localhost 22"}, false},
  219. {"ok-custom-command", fields{signer, signer, "", "foo <principal> <principal>"}, args{pub, &ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"user"}}}, want{CertType: ssh.UserCert, Principals: []string{"provisioner"}, ForceCommand: "foo user user"}, false},
  220. {"ok-custom-principal-and-command", fields{signer, signer, "my-principal", "foo <principal> <principal>"}, args{pub, &ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"user"}}}, want{CertType: ssh.UserCert, Principals: []string{"my-principal"}, ForceCommand: "foo user user"}, false},
  221. {"fail-no-user-key", fields{nil, signer, "", ""}, args{pub, validCert}, want{}, true},
  222. {"fail-no-user-cert", fields{signer, signer, "", ""}, args{pub, &ssh.Certificate{CertType: ssh.HostCert, ValidPrincipals: []string{"foo"}}}, want{}, true},
  223. {"fail-no-principals", fields{signer, signer, "", ""}, args{pub, &ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{}}}, want{}, true},
  224. {"fail-many-principals", fields{signer, signer, "", ""}, args{pub, &ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"foo", "bar"}}}, want{}, true},
  225. }
  226. for _, tt := range tests {
  227. t.Run(tt.name, func(t *testing.T) {
  228. a := testAuthority(t)
  229. a.sshCAUserCertSignKey = tt.fields.sshCAUserCertSignKey
  230. a.sshCAHostCertSignKey = tt.fields.sshCAHostCertSignKey
  231. a.config.SSH = &SSHConfig{
  232. AddUserPrincipal: tt.fields.addUserPrincipal,
  233. AddUserCommand: tt.fields.addUserCommand,
  234. }
  235. got, err := a.SignSSHAddUser(context.Background(), tt.args.key, tt.args.subject)
  236. if (err != nil) != tt.wantErr {
  237. t.Errorf("Authority.SignSSHAddUser() error = %v, wantErr %v", err, tt.wantErr)
  238. return
  239. }
  240. if err == nil && assert.NotNil(t, got) {
  241. assert.Equals(t, tt.want.CertType, got.CertType)
  242. assert.Equals(t, tt.want.Principals, got.ValidPrincipals)
  243. assert.Equals(t, tt.args.subject.ValidPrincipals[0]+"-"+tt.want.Principals[0], got.KeyId)
  244. assert.Equals(t, tt.want.ValidAfter, got.ValidAfter)
  245. assert.Equals(t, tt.want.ValidBefore, got.ValidBefore)
  246. assert.Equals(t, map[string]string{"force-command": tt.want.ForceCommand}, got.CriticalOptions)
  247. assert.Equals(t, nil, got.Extensions)
  248. assert.NotNil(t, got.Key)
  249. assert.NotNil(t, got.Nonce)
  250. assert.NotEquals(t, 0, got.Serial)
  251. assert.NotNil(t, got.Signature)
  252. assert.NotNil(t, got.SignatureKey)
  253. }
  254. })
  255. }
  256. }
  257. func TestAuthority_GetSSHRoots(t *testing.T) {
  258. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  259. assert.FatalError(t, err)
  260. user, err := ssh.NewPublicKey(key.Public())
  261. assert.FatalError(t, err)
  262. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  263. assert.FatalError(t, err)
  264. host, err := ssh.NewPublicKey(key.Public())
  265. assert.FatalError(t, err)
  266. type fields struct {
  267. sshCAUserCerts []ssh.PublicKey
  268. sshCAHostCerts []ssh.PublicKey
  269. }
  270. tests := []struct {
  271. name string
  272. fields fields
  273. want *SSHKeys
  274. wantErr bool
  275. }{
  276. {"ok", fields{[]ssh.PublicKey{user}, []ssh.PublicKey{host}}, &SSHKeys{UserKeys: []ssh.PublicKey{user}, HostKeys: []ssh.PublicKey{host}}, false},
  277. {"nil", fields{}, &SSHKeys{}, false},
  278. }
  279. for _, tt := range tests {
  280. t.Run(tt.name, func(t *testing.T) {
  281. a := testAuthority(t)
  282. a.sshCAUserCerts = tt.fields.sshCAUserCerts
  283. a.sshCAHostCerts = tt.fields.sshCAHostCerts
  284. got, err := a.GetSSHRoots(context.Background())
  285. if (err != nil) != tt.wantErr {
  286. t.Errorf("Authority.GetSSHRoots() error = %v, wantErr %v", err, tt.wantErr)
  287. return
  288. }
  289. if !reflect.DeepEqual(got, tt.want) {
  290. t.Errorf("Authority.GetSSHRoots() = %v, want %v", got, tt.want)
  291. }
  292. })
  293. }
  294. }
  295. func TestAuthority_GetSSHFederation(t *testing.T) {
  296. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  297. assert.FatalError(t, err)
  298. user, err := ssh.NewPublicKey(key.Public())
  299. assert.FatalError(t, err)
  300. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  301. assert.FatalError(t, err)
  302. host, err := ssh.NewPublicKey(key.Public())
  303. assert.FatalError(t, err)
  304. type fields struct {
  305. sshCAUserFederatedCerts []ssh.PublicKey
  306. sshCAHostFederatedCerts []ssh.PublicKey
  307. }
  308. tests := []struct {
  309. name string
  310. fields fields
  311. want *SSHKeys
  312. wantErr bool
  313. }{
  314. {"ok", fields{[]ssh.PublicKey{user}, []ssh.PublicKey{host}}, &SSHKeys{UserKeys: []ssh.PublicKey{user}, HostKeys: []ssh.PublicKey{host}}, false},
  315. {"nil", fields{}, &SSHKeys{}, false},
  316. }
  317. for _, tt := range tests {
  318. t.Run(tt.name, func(t *testing.T) {
  319. a := testAuthority(t)
  320. a.sshCAUserFederatedCerts = tt.fields.sshCAUserFederatedCerts
  321. a.sshCAHostFederatedCerts = tt.fields.sshCAHostFederatedCerts
  322. got, err := a.GetSSHFederation(context.Background())
  323. if (err != nil) != tt.wantErr {
  324. t.Errorf("Authority.GetSSHFederation() error = %v, wantErr %v", err, tt.wantErr)
  325. return
  326. }
  327. if !reflect.DeepEqual(got, tt.want) {
  328. t.Errorf("Authority.GetSSHFederation() = %v, want %v", got, tt.want)
  329. }
  330. })
  331. }
  332. }
  333. func TestAuthority_GetSSHConfig(t *testing.T) {
  334. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  335. assert.FatalError(t, err)
  336. user, err := ssh.NewPublicKey(key.Public())
  337. assert.FatalError(t, err)
  338. userSigner, err := ssh.NewSignerFromSigner(key)
  339. assert.FatalError(t, err)
  340. userB64 := base64.StdEncoding.EncodeToString(user.Marshal())
  341. key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  342. assert.FatalError(t, err)
  343. host, err := ssh.NewPublicKey(key.Public())
  344. assert.FatalError(t, err)
  345. hostSigner, err := ssh.NewSignerFromSigner(key)
  346. assert.FatalError(t, err)
  347. hostB64 := base64.StdEncoding.EncodeToString(host.Marshal())
  348. tmplConfig := &templates.Templates{
  349. SSH: &templates.SSHTemplates{
  350. User: []templates.Template{
  351. {Name: "known_host.tpl", Type: templates.File, TemplatePath: "./testdata/templates/known_hosts.tpl", Path: "ssh/known_host", Comment: "#"},
  352. },
  353. Host: []templates.Template{
  354. {Name: "ca.tpl", Type: templates.File, TemplatePath: "./testdata/templates/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
  355. },
  356. },
  357. Data: map[string]interface{}{
  358. "Step": &templates.Step{
  359. SSH: templates.StepSSH{
  360. UserKey: user,
  361. HostKey: host,
  362. },
  363. },
  364. },
  365. }
  366. userOutput := []templates.Output{
  367. {Name: "known_host.tpl", Type: templates.File, Comment: "#", Path: "ssh/known_host", Content: []byte(fmt.Sprintf("@cert-authority * %s %s", host.Type(), hostB64))},
  368. }
  369. hostOutput := []templates.Output{
  370. {Name: "ca.tpl", Type: templates.File, Comment: "#", Path: "/etc/ssh/ca.pub", Content: []byte(user.Type() + " " + userB64)},
  371. }
  372. tmplConfigWithUserData := &templates.Templates{
  373. SSH: &templates.SSHTemplates{
  374. User: []templates.Template{
  375. {Name: "include.tpl", Type: templates.File, TemplatePath: "./testdata/templates/include.tpl", Path: "ssh/include", Comment: "#"},
  376. {Name: "config.tpl", Type: templates.File, TemplatePath: "./testdata/templates/config.tpl", Path: "ssh/config", Comment: "#"},
  377. },
  378. Host: []templates.Template{
  379. {
  380. Name: "sshd_config.tpl",
  381. Type: templates.File,
  382. TemplatePath: "./testdata/templates/sshd_config.tpl",
  383. Path: "/etc/ssh/sshd_config",
  384. Comment: "#",
  385. RequiredData: []string{"Certificate", "Key"},
  386. },
  387. },
  388. },
  389. Data: map[string]interface{}{
  390. "Step": &templates.Step{
  391. SSH: templates.StepSSH{
  392. UserKey: user,
  393. HostKey: host,
  394. },
  395. },
  396. },
  397. }
  398. userOutputWithUserData := []templates.Output{
  399. {Name: "include.tpl", Type: templates.File, Comment: "#", Path: "ssh/include", Content: []byte("Host *\n\tInclude /home/user/.step/ssh/config")},
  400. {Name: "config.tpl", Type: templates.File, Comment: "#", Path: "ssh/config", Content: []byte("Match exec \"step ssh check-host %h\"\n\tUserKnownHostsFile /home/user/.step/ssh/known_hosts\n\tProxyCommand step ssh proxycommand %r %h %p\n")},
  401. }
  402. hostOutputWithUserData := []templates.Output{
  403. {Name: "sshd_config.tpl", Type: templates.File, Comment: "#", Path: "/etc/ssh/sshd_config", Content: []byte("TrustedUserCAKeys /etc/ssh/ca.pub\nHostCertificate /etc/ssh/ssh_host_ecdsa_key-cert.pub\nHostKey /etc/ssh/ssh_host_ecdsa_key")},
  404. }
  405. tmplConfigErr := &templates.Templates{
  406. SSH: &templates.SSHTemplates{
  407. User: []templates.Template{
  408. {Name: "error.tpl", Type: templates.File, TemplatePath: "./testdata/templates/error.tpl", Path: "ssh/error", Comment: "#"},
  409. },
  410. Host: []templates.Template{
  411. {Name: "error.tpl", Type: templates.File, TemplatePath: "./testdata/templates/error.tpl", Path: "ssh/error", Comment: "#"},
  412. },
  413. },
  414. }
  415. tmplConfigFail := &templates.Templates{
  416. SSH: &templates.SSHTemplates{
  417. User: []templates.Template{
  418. {Name: "fail.tpl", Type: templates.File, TemplatePath: "./testdata/templates/fail.tpl", Path: "ssh/fail", Comment: "#"},
  419. },
  420. },
  421. }
  422. type fields struct {
  423. templates *templates.Templates
  424. userSigner ssh.Signer
  425. hostSigner ssh.Signer
  426. }
  427. type args struct {
  428. typ string
  429. data map[string]string
  430. }
  431. tests := []struct {
  432. name string
  433. fields fields
  434. args args
  435. want []templates.Output
  436. wantErr bool
  437. }{
  438. {"user", fields{tmplConfig, userSigner, hostSigner}, args{"user", nil}, userOutput, false},
  439. {"user", fields{tmplConfig, userSigner, nil}, args{"user", nil}, userOutput, false},
  440. {"host", fields{tmplConfig, userSigner, hostSigner}, args{"host", nil}, hostOutput, false},
  441. {"host", fields{tmplConfig, nil, hostSigner}, args{"host", nil}, hostOutput, false},
  442. {"userWithData", fields{tmplConfigWithUserData, userSigner, hostSigner}, args{"user", map[string]string{"StepPath": "/home/user/.step"}}, userOutputWithUserData, false},
  443. {"hostWithData", fields{tmplConfigWithUserData, userSigner, hostSigner}, args{"host", map[string]string{"Certificate": "ssh_host_ecdsa_key-cert.pub", "Key": "ssh_host_ecdsa_key"}}, hostOutputWithUserData, false},
  444. {"disabled", fields{tmplConfig, nil, nil}, args{"host", nil}, nil, true},
  445. {"badType", fields{tmplConfig, userSigner, hostSigner}, args{"bad", nil}, nil, true},
  446. {"userError", fields{tmplConfigErr, userSigner, hostSigner}, args{"user", nil}, nil, true},
  447. {"hostError", fields{tmplConfigErr, userSigner, hostSigner}, args{"host", map[string]string{"Function": "foo"}}, nil, true},
  448. {"noTemplates", fields{nil, userSigner, hostSigner}, args{"user", nil}, nil, true},
  449. {"missingData", fields{tmplConfigWithUserData, userSigner, hostSigner}, args{"host", map[string]string{"Certificate": "ssh_host_ecdsa_key-cert.pub"}}, nil, true},
  450. {"failError", fields{tmplConfigFail, userSigner, hostSigner}, args{"user", nil}, nil, true},
  451. }
  452. for _, tt := range tests {
  453. t.Run(tt.name, func(t *testing.T) {
  454. a := testAuthority(t)
  455. a.templates = tt.fields.templates
  456. a.sshCAUserCertSignKey = tt.fields.userSigner
  457. a.sshCAHostCertSignKey = tt.fields.hostSigner
  458. got, err := a.GetSSHConfig(context.Background(), tt.args.typ, tt.args.data)
  459. if (err != nil) != tt.wantErr {
  460. t.Errorf("Authority.GetSSHConfig() error = %v, wantErr %v", err, tt.wantErr)
  461. return
  462. }
  463. if !reflect.DeepEqual(got, tt.want) {
  464. t.Errorf("Authority.GetSSHConfig() = %v, want %v", got, tt.want)
  465. }
  466. })
  467. }
  468. }
  469. func TestAuthority_CheckSSHHost(t *testing.T) {
  470. type fields struct {
  471. exists bool
  472. err error
  473. }
  474. type args struct {
  475. ctx context.Context
  476. principal string
  477. token string
  478. }
  479. tests := []struct {
  480. name string
  481. fields fields
  482. args args
  483. want bool
  484. wantErr bool
  485. }{
  486. {"true", fields{true, nil}, args{context.Background(), "foo.internal.com", ""}, true, false},
  487. {"false", fields{false, nil}, args{context.Background(), "foo.internal.com", ""}, false, false},
  488. {"notImplemented", fields{false, db.ErrNotImplemented}, args{context.Background(), "foo.internal.com", ""}, false, true},
  489. {"notImplemented", fields{true, db.ErrNotImplemented}, args{context.Background(), "foo.internal.com", ""}, false, true},
  490. {"internal", fields{false, fmt.Errorf("an error")}, args{context.Background(), "foo.internal.com", ""}, false, true},
  491. {"internal", fields{true, fmt.Errorf("an error")}, args{context.Background(), "foo.internal.com", ""}, false, true},
  492. }
  493. for _, tt := range tests {
  494. t.Run(tt.name, func(t *testing.T) {
  495. a := testAuthority(t)
  496. a.db = &db.MockAuthDB{
  497. MIsSSHHost: func(_ string) (bool, error) {
  498. return tt.fields.exists, tt.fields.err
  499. },
  500. }
  501. got, err := a.CheckSSHHost(tt.args.ctx, tt.args.principal, tt.args.token)
  502. if (err != nil) != tt.wantErr {
  503. t.Errorf("Authority.CheckSSHHost() error = %v, wantErr %v", err, tt.wantErr)
  504. return
  505. }
  506. if got != tt.want {
  507. t.Errorf("Authority.CheckSSHHost() = %v, want %v", got, tt.want)
  508. }
  509. })
  510. }
  511. }
  512. func TestSSHConfig_Validate(t *testing.T) {
  513. key, err := jose.GenerateJWK("EC", "P-256", "", "sig", "", 0)
  514. assert.FatalError(t, err)
  515. tests := []struct {
  516. name string
  517. sshConfig *SSHConfig
  518. wantErr bool
  519. }{
  520. {"nil", nil, false},
  521. {"ok", &SSHConfig{Keys: []*SSHPublicKey{{Type: "user", Key: key.Public()}}}, false},
  522. {"ok", &SSHConfig{Keys: []*SSHPublicKey{{Type: "host", Key: key.Public()}}}, false},
  523. {"badType", &SSHConfig{Keys: []*SSHPublicKey{{Type: "bad", Key: key.Public()}}}, true},
  524. {"badKey", &SSHConfig{Keys: []*SSHPublicKey{{Type: "user", Key: *key}}}, true},
  525. }
  526. for _, tt := range tests {
  527. t.Run(tt.name, func(t *testing.T) {
  528. if err := tt.sshConfig.Validate(); (err != nil) != tt.wantErr {
  529. t.Errorf("SSHConfig.Validate() error = %v, wantErr %v", err, tt.wantErr)
  530. }
  531. })
  532. }
  533. }
  534. func TestSSHPublicKey_Validate(t *testing.T) {
  535. key, err := jose.GenerateJWK("EC", "P-256", "", "sig", "", 0)
  536. assert.FatalError(t, err)
  537. type fields struct {
  538. Type string
  539. Federated bool
  540. Key jose.JSONWebKey
  541. }
  542. tests := []struct {
  543. name string
  544. fields fields
  545. wantErr bool
  546. }{
  547. {"user", fields{"user", true, key.Public()}, false},
  548. {"host", fields{"host", false, key.Public()}, false},
  549. {"empty", fields{"", true, key.Public()}, true},
  550. {"badType", fields{"bad", false, key.Public()}, true},
  551. {"badKey", fields{"user", false, *key}, true},
  552. }
  553. for _, tt := range tests {
  554. t.Run(tt.name, func(t *testing.T) {
  555. k := &SSHPublicKey{
  556. Type: tt.fields.Type,
  557. Federated: tt.fields.Federated,
  558. Key: tt.fields.Key,
  559. }
  560. if err := k.Validate(); (err != nil) != tt.wantErr {
  561. t.Errorf("SSHPublicKey.Validate() error = %v, wantErr %v", err, tt.wantErr)
  562. }
  563. })
  564. }
  565. }
  566. func TestSSHPublicKey_PublicKey(t *testing.T) {
  567. key, err := jose.GenerateJWK("EC", "P-256", "", "sig", "", 0)
  568. assert.FatalError(t, err)
  569. pub, err := ssh.NewPublicKey(key.Public().Key)
  570. assert.FatalError(t, err)
  571. type fields struct {
  572. publicKey ssh.PublicKey
  573. }
  574. tests := []struct {
  575. name string
  576. fields fields
  577. want ssh.PublicKey
  578. }{
  579. {"ok", fields{pub}, pub},
  580. {"nil", fields{nil}, nil},
  581. }
  582. for _, tt := range tests {
  583. t.Run(tt.name, func(t *testing.T) {
  584. k := &SSHPublicKey{
  585. publicKey: tt.fields.publicKey,
  586. }
  587. if got := k.PublicKey(); !reflect.DeepEqual(got, tt.want) {
  588. t.Errorf("SSHPublicKey.PublicKey() = %v, want %v", got, tt.want)
  589. }
  590. })
  591. }
  592. }
  593. func TestAuthority_GetSSHBastion(t *testing.T) {
  594. bastion := &Bastion{
  595. Hostname: "bastion.local",
  596. Port: "2222",
  597. }
  598. type fields struct {
  599. config *Config
  600. sshBastionFunc func(ctx context.Context, user, hostname string) (*Bastion, error)
  601. }
  602. type args struct {
  603. user string
  604. hostname string
  605. }
  606. tests := []struct {
  607. name string
  608. fields fields
  609. args args
  610. want *Bastion
  611. wantErr bool
  612. }{
  613. {"config", fields{&Config{SSH: &SSHConfig{Bastion: bastion}}, nil}, args{"user", "host.local"}, bastion, false},
  614. {"bastion", fields{&Config{SSH: &SSHConfig{Bastion: bastion}}, nil}, args{"user", "bastion.local"}, nil, false},
  615. {"nil", fields{&Config{SSH: &SSHConfig{Bastion: nil}}, nil}, args{"user", "host.local"}, nil, false},
  616. {"empty", fields{&Config{SSH: &SSHConfig{Bastion: &Bastion{}}}, nil}, args{"user", "host.local"}, nil, false},
  617. {"func", fields{&Config{}, func(_ context.Context, _, _ string) (*Bastion, error) { return bastion, nil }}, args{"user", "host.local"}, bastion, false},
  618. {"func err", fields{&Config{}, func(_ context.Context, _, _ string) (*Bastion, error) { return nil, errors.New("foo") }}, args{"user", "host.local"}, nil, true},
  619. {"error", fields{&Config{SSH: nil}, nil}, args{"user", "host.local"}, nil, true},
  620. }
  621. for _, tt := range tests {
  622. t.Run(tt.name, func(t *testing.T) {
  623. a := &Authority{
  624. config: tt.fields.config,
  625. sshBastionFunc: tt.fields.sshBastionFunc,
  626. }
  627. got, err := a.GetSSHBastion(context.Background(), tt.args.user, tt.args.hostname)
  628. if (err != nil) != tt.wantErr {
  629. t.Errorf("Authority.GetSSHBastion() error = %v, wantErr %v", err, tt.wantErr)
  630. return
  631. } else if err != nil {
  632. _, ok := err.(errs.StatusCoder)
  633. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  634. }
  635. if !reflect.DeepEqual(got, tt.want) {
  636. t.Errorf("Authority.GetSSHBastion() = %v, want %v", got, tt.want)
  637. }
  638. })
  639. }
  640. }
  641. func TestAuthority_GetSSHHosts(t *testing.T) {
  642. a := testAuthority(t)
  643. type test struct {
  644. getHostsFunc func(context.Context, *x509.Certificate) ([]sshutil.Host, error)
  645. auth *Authority
  646. cert *x509.Certificate
  647. cmp func(got []sshutil.Host)
  648. err error
  649. code int
  650. }
  651. tests := map[string]func(t *testing.T) *test{
  652. "fail/getHostsFunc-fail": func(t *testing.T) *test {
  653. return &test{
  654. getHostsFunc: func(ctx context.Context, cert *x509.Certificate) ([]sshutil.Host, error) {
  655. return nil, errors.New("force")
  656. },
  657. cert: &x509.Certificate{},
  658. err: errors.New("getSSHHosts: force"),
  659. code: http.StatusInternalServerError,
  660. }
  661. },
  662. "ok/getHostsFunc-defined": func(t *testing.T) *test {
  663. hosts := []sshutil.Host{
  664. {HostID: "1", Hostname: "foo"},
  665. {HostID: "2", Hostname: "bar"},
  666. }
  667. return &test{
  668. getHostsFunc: func(ctx context.Context, cert *x509.Certificate) ([]sshutil.Host, error) {
  669. return hosts, nil
  670. },
  671. cert: &x509.Certificate{},
  672. cmp: func(got []sshutil.Host) {
  673. assert.Equals(t, got, hosts)
  674. },
  675. }
  676. },
  677. "fail/db-get-fail": func(t *testing.T) *test {
  678. return &test{
  679. auth: testAuthority(t, WithDatabase(&db.MockAuthDB{
  680. MGetSSHHostPrincipals: func() ([]string, error) {
  681. return nil, errors.New("force")
  682. },
  683. })),
  684. cert: &x509.Certificate{},
  685. err: errors.New("getSSHHosts: force"),
  686. code: http.StatusInternalServerError,
  687. }
  688. },
  689. "ok": func(t *testing.T) *test {
  690. return &test{
  691. auth: testAuthority(t, WithDatabase(&db.MockAuthDB{
  692. MGetSSHHostPrincipals: func() ([]string, error) {
  693. return []string{"foo", "bar"}, nil
  694. },
  695. })),
  696. cert: &x509.Certificate{},
  697. cmp: func(got []sshutil.Host) {
  698. assert.Equals(t, got, []sshutil.Host{
  699. {Hostname: "foo"},
  700. {Hostname: "bar"},
  701. })
  702. },
  703. }
  704. },
  705. }
  706. for name, genTestCase := range tests {
  707. t.Run(name, func(t *testing.T) {
  708. tc := genTestCase(t)
  709. auth := tc.auth
  710. if auth == nil {
  711. auth = a
  712. }
  713. auth.sshGetHostsFunc = tc.getHostsFunc
  714. hosts, err := auth.GetSSHHosts(context.Background(), tc.cert)
  715. if err != nil {
  716. if assert.NotNil(t, tc.err) {
  717. sc, ok := err.(errs.StatusCoder)
  718. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  719. assert.Equals(t, sc.StatusCode(), tc.code)
  720. assert.HasPrefix(t, err.Error(), tc.err.Error())
  721. }
  722. } else {
  723. if assert.Nil(t, tc.err) {
  724. tc.cmp(hosts)
  725. }
  726. }
  727. })
  728. }
  729. }
  730. func TestAuthority_RekeySSH(t *testing.T) {
  731. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  732. assert.FatalError(t, err)
  733. pub, err := ssh.NewPublicKey(key.Public())
  734. assert.FatalError(t, err)
  735. signKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  736. assert.FatalError(t, err)
  737. signer, err := ssh.NewSignerFromKey(signKey)
  738. assert.FatalError(t, err)
  739. userOptions := sshTestModifier{
  740. CertType: ssh.UserCert,
  741. }
  742. now := time.Now().UTC()
  743. a := testAuthority(t)
  744. type test struct {
  745. auth *Authority
  746. userSigner ssh.Signer
  747. hostSigner ssh.Signer
  748. cert *ssh.Certificate
  749. key ssh.PublicKey
  750. signOpts []provisioner.SignOption
  751. cmpResult func(old, n *ssh.Certificate)
  752. err error
  753. code int
  754. }
  755. tests := map[string]func(t *testing.T) *test{
  756. "fail/opts-type": func(t *testing.T) *test {
  757. return &test{
  758. userSigner: signer,
  759. hostSigner: signer,
  760. key: pub,
  761. signOpts: []provisioner.SignOption{userOptions},
  762. err: errors.New("rekeySSH; invalid extra option type"),
  763. code: http.StatusInternalServerError,
  764. }
  765. },
  766. "fail/old-cert-validAfter": func(t *testing.T) *test {
  767. return &test{
  768. userSigner: signer,
  769. hostSigner: signer,
  770. cert: &ssh.Certificate{},
  771. key: pub,
  772. signOpts: []provisioner.SignOption{},
  773. err: errors.New("rekeySSH; cannot rekey certificate without validity period"),
  774. code: http.StatusBadRequest,
  775. }
  776. },
  777. "fail/old-cert-validBefore": func(t *testing.T) *test {
  778. return &test{
  779. userSigner: signer,
  780. hostSigner: signer,
  781. cert: &ssh.Certificate{ValidAfter: uint64(now.Unix())},
  782. key: pub,
  783. signOpts: []provisioner.SignOption{},
  784. err: errors.New("rekeySSH; cannot rekey certificate without validity period"),
  785. code: http.StatusBadRequest,
  786. }
  787. },
  788. "fail/old-cert-no-user-key": func(t *testing.T) *test {
  789. return &test{
  790. userSigner: nil,
  791. hostSigner: signer,
  792. cert: &ssh.Certificate{ValidAfter: uint64(now.Unix()), ValidBefore: uint64(now.Add(10 * time.Minute).Unix()), CertType: ssh.UserCert},
  793. key: pub,
  794. signOpts: []provisioner.SignOption{},
  795. err: errors.New("rekeySSH; user certificate signing is not enabled"),
  796. code: http.StatusNotImplemented,
  797. }
  798. },
  799. "fail/old-cert-no-host-key": func(t *testing.T) *test {
  800. return &test{
  801. userSigner: signer,
  802. hostSigner: nil,
  803. cert: &ssh.Certificate{ValidAfter: uint64(now.Unix()), ValidBefore: uint64(now.Add(10 * time.Minute).Unix()), CertType: ssh.HostCert},
  804. key: pub,
  805. signOpts: []provisioner.SignOption{},
  806. err: errors.New("rekeySSH; host certificate signing is not enabled"),
  807. code: http.StatusNotImplemented,
  808. }
  809. },
  810. "fail/unexpected-old-cert-type": func(t *testing.T) *test {
  811. return &test{
  812. userSigner: signer,
  813. hostSigner: signer,
  814. cert: &ssh.Certificate{ValidAfter: uint64(now.Unix()), ValidBefore: uint64(now.Add(10 * time.Minute).Unix()), CertType: 0},
  815. key: pub,
  816. signOpts: []provisioner.SignOption{},
  817. err: errors.New("rekeySSH; unexpected ssh certificate type: 0"),
  818. code: http.StatusBadRequest,
  819. }
  820. },
  821. "fail/db-store": func(t *testing.T) *test {
  822. return &test{
  823. auth: testAuthority(t, WithDatabase(&db.MockAuthDB{
  824. MStoreSSHCertificate: func(cert *ssh.Certificate) error {
  825. return errors.New("force")
  826. },
  827. })),
  828. userSigner: signer,
  829. hostSigner: nil,
  830. cert: &ssh.Certificate{ValidAfter: uint64(now.Unix()), ValidBefore: uint64(now.Add(10 * time.Minute).Unix()), CertType: ssh.UserCert},
  831. key: pub,
  832. signOpts: []provisioner.SignOption{},
  833. err: errors.New("rekeySSH; error storing certificate in db: force"),
  834. code: http.StatusInternalServerError,
  835. }
  836. },
  837. "ok": func(t *testing.T) *test {
  838. va1 := now.Add(-24 * time.Hour)
  839. vb1 := now.Add(-23 * time.Hour)
  840. return &test{
  841. userSigner: signer,
  842. hostSigner: nil,
  843. cert: &ssh.Certificate{
  844. ValidAfter: uint64(va1.Unix()),
  845. ValidBefore: uint64(vb1.Unix()),
  846. CertType: ssh.UserCert,
  847. ValidPrincipals: []string{"foo", "bar"},
  848. KeyId: "foo",
  849. },
  850. key: pub,
  851. signOpts: []provisioner.SignOption{},
  852. cmpResult: func(old, n *ssh.Certificate) {
  853. assert.Equals(t, n.CertType, old.CertType)
  854. assert.Equals(t, n.ValidPrincipals, old.ValidPrincipals)
  855. assert.Equals(t, n.KeyId, old.KeyId)
  856. assert.True(t, n.ValidAfter > uint64(now.Add(-5*time.Minute).Unix()))
  857. assert.True(t, n.ValidAfter < uint64(now.Add(5*time.Minute).Unix()))
  858. l8r := now.Add(1 * time.Hour)
  859. assert.True(t, n.ValidBefore > uint64(l8r.Add(-5*time.Minute).Unix()))
  860. assert.True(t, n.ValidBefore < uint64(l8r.Add(5*time.Minute).Unix()))
  861. },
  862. }
  863. },
  864. }
  865. for name, genTestCase := range tests {
  866. t.Run(name, func(t *testing.T) {
  867. tc := genTestCase(t)
  868. auth := tc.auth
  869. if auth == nil {
  870. auth = a
  871. }
  872. a.sshCAUserCertSignKey = tc.userSigner
  873. a.sshCAHostCertSignKey = tc.hostSigner
  874. cert, err := auth.RekeySSH(context.Background(), tc.cert, tc.key, tc.signOpts...)
  875. if err != nil {
  876. if assert.NotNil(t, tc.err) {
  877. sc, ok := err.(errs.StatusCoder)
  878. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  879. assert.Equals(t, sc.StatusCode(), tc.code)
  880. assert.HasPrefix(t, err.Error(), tc.err.Error())
  881. }
  882. } else {
  883. if assert.Nil(t, tc.err) {
  884. tc.cmpResult(tc.cert, cert)
  885. }
  886. }
  887. })
  888. }
  889. }
  890. func TestIsValidForAddUser(t *testing.T) {
  891. type args struct {
  892. cert *ssh.Certificate
  893. }
  894. tests := []struct {
  895. name string
  896. args args
  897. wantErr bool
  898. }{
  899. {"ok", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"john"}}}, false},
  900. {"ok oidc", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"jane", "jane@smallstep.com"}}}, false},
  901. {"fail at", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"jane", "@smallstep.com"}}}, true},
  902. {"fail host", args{&ssh.Certificate{CertType: ssh.HostCert, ValidPrincipals: []string{"john"}}}, true},
  903. {"fail principals", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"john", "jane"}}}, true},
  904. {"fail no principals", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{}}}, true},
  905. {"fail extra principals", args{&ssh.Certificate{CertType: ssh.UserCert, ValidPrincipals: []string{"john", "jane", "doe"}}}, true},
  906. }
  907. for _, tt := range tests {
  908. t.Run(tt.name, func(t *testing.T) {
  909. if err := IsValidForAddUser(tt.args.cert); (err != nil) != tt.wantErr {
  910. t.Errorf("IsValidForAddUser() error = %v, wantErr %v", err, tt.wantErr)
  911. }
  912. })
  913. }
  914. }