sign_ssh_options_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package provisioner
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/assert"
  7. "github.com/smallstep/cli/crypto/keys"
  8. "golang.org/x/crypto/ssh"
  9. )
  10. func Test_sshCertificateDefaultValidator_Valid(t *testing.T) {
  11. pub, _, err := keys.GenerateDefaultKeyPair()
  12. assert.FatalError(t, err)
  13. sshPub, err := ssh.NewPublicKey(pub)
  14. assert.FatalError(t, err)
  15. v := sshCertificateDefaultValidator{}
  16. tests := []struct {
  17. name string
  18. cert *ssh.Certificate
  19. err error
  20. }{
  21. {
  22. "fail/zero-nonce",
  23. &ssh.Certificate{},
  24. errors.New("ssh certificate nonce cannot be empty"),
  25. },
  26. {
  27. "fail/nil-key",
  28. &ssh.Certificate{Nonce: []byte("foo")},
  29. errors.New("ssh certificate key cannot be nil"),
  30. },
  31. {
  32. "fail/zero-serial",
  33. &ssh.Certificate{Nonce: []byte("foo"), Key: sshPub},
  34. errors.New("ssh certificate serial cannot be 0"),
  35. },
  36. {
  37. "fail/unexpected-cert-type",
  38. // UserCert = 1, HostCert = 2
  39. &ssh.Certificate{Nonce: []byte("foo"), Key: sshPub, CertType: 3, Serial: 1},
  40. errors.New("ssh certificate has an unknown type: 3"),
  41. },
  42. {
  43. "fail/empty-cert-key-id",
  44. &ssh.Certificate{Nonce: []byte("foo"), Key: sshPub, Serial: 1, CertType: 1},
  45. errors.New("ssh certificate key id cannot be empty"),
  46. },
  47. {
  48. "fail/empty-valid-principals",
  49. &ssh.Certificate{Nonce: []byte("foo"), Key: sshPub, Serial: 1, CertType: 1, KeyId: "foo"},
  50. errors.New("ssh certificate valid principals cannot be empty"),
  51. },
  52. {
  53. "fail/zero-validAfter",
  54. &ssh.Certificate{
  55. Nonce: []byte("foo"),
  56. Key: sshPub,
  57. Serial: 1,
  58. CertType: 1,
  59. KeyId: "foo",
  60. ValidPrincipals: []string{"foo"},
  61. ValidAfter: 0,
  62. },
  63. errors.New("ssh certificate validAfter cannot be 0"),
  64. },
  65. {
  66. "fail/validBefore-past",
  67. &ssh.Certificate{
  68. Nonce: []byte("foo"),
  69. Key: sshPub,
  70. Serial: 1,
  71. CertType: 1,
  72. KeyId: "foo",
  73. ValidPrincipals: []string{"foo"},
  74. ValidAfter: uint64(time.Now().Add(-10 * time.Minute).Unix()),
  75. ValidBefore: uint64(time.Now().Add(-5 * time.Minute).Unix()),
  76. },
  77. errors.New("ssh certificate validBefore cannot be in the past"),
  78. },
  79. {
  80. "fail/validAfter-after-validBefore",
  81. &ssh.Certificate{
  82. Nonce: []byte("foo"),
  83. Key: sshPub,
  84. Serial: 1,
  85. CertType: 1,
  86. KeyId: "foo",
  87. ValidPrincipals: []string{"foo"},
  88. ValidAfter: uint64(time.Now().Add(15 * time.Minute).Unix()),
  89. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  90. },
  91. errors.New("ssh certificate validBefore cannot be before validAfter"),
  92. },
  93. {
  94. "fail/empty-extensions",
  95. &ssh.Certificate{
  96. Nonce: []byte("foo"),
  97. Key: sshPub,
  98. Serial: 1,
  99. CertType: 1,
  100. KeyId: "foo",
  101. ValidPrincipals: []string{"foo"},
  102. ValidAfter: uint64(time.Now().Unix()),
  103. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  104. },
  105. errors.New("ssh certificate extensions cannot be empty"),
  106. },
  107. {
  108. "fail/nil-signature-key",
  109. &ssh.Certificate{
  110. Nonce: []byte("foo"),
  111. Key: sshPub,
  112. Serial: 1,
  113. CertType: 1,
  114. KeyId: "foo",
  115. ValidPrincipals: []string{"foo"},
  116. ValidAfter: uint64(time.Now().Unix()),
  117. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  118. Permissions: ssh.Permissions{
  119. Extensions: map[string]string{"foo": "bar"},
  120. },
  121. },
  122. errors.New("ssh certificate signature key cannot be nil"),
  123. },
  124. {
  125. "fail/nil-signature",
  126. &ssh.Certificate{
  127. Nonce: []byte("foo"),
  128. Key: sshPub,
  129. Serial: 1,
  130. CertType: 1,
  131. KeyId: "foo",
  132. ValidPrincipals: []string{"foo"},
  133. ValidAfter: uint64(time.Now().Unix()),
  134. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  135. Permissions: ssh.Permissions{
  136. Extensions: map[string]string{"foo": "bar"},
  137. },
  138. SignatureKey: sshPub,
  139. },
  140. errors.New("ssh certificate signature cannot be nil"),
  141. },
  142. {
  143. "ok/userCert",
  144. &ssh.Certificate{
  145. Nonce: []byte("foo"),
  146. Key: sshPub,
  147. Serial: 1,
  148. CertType: 1,
  149. KeyId: "foo",
  150. ValidPrincipals: []string{"foo"},
  151. ValidAfter: uint64(time.Now().Unix()),
  152. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  153. Permissions: ssh.Permissions{
  154. Extensions: map[string]string{"foo": "bar"},
  155. },
  156. SignatureKey: sshPub,
  157. Signature: &ssh.Signature{},
  158. },
  159. nil,
  160. },
  161. {
  162. "ok/hostCert",
  163. &ssh.Certificate{
  164. Nonce: []byte("foo"),
  165. Key: sshPub,
  166. Serial: 1,
  167. CertType: 2,
  168. KeyId: "foo",
  169. ValidPrincipals: []string{"foo"},
  170. ValidAfter: uint64(time.Now().Unix()),
  171. ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
  172. SignatureKey: sshPub,
  173. Signature: &ssh.Signature{},
  174. },
  175. nil,
  176. },
  177. }
  178. for _, tt := range tests {
  179. t.Run(tt.name, func(t *testing.T) {
  180. if err := v.Valid(tt.cert); err != nil {
  181. if assert.NotNil(t, tt.err) {
  182. assert.HasPrefix(t, err.Error(), tt.err.Error())
  183. }
  184. } else {
  185. assert.Nil(t, tt.err)
  186. }
  187. })
  188. }
  189. }
  190. func Test_sshCertificateValidityValidator(t *testing.T) {
  191. p, err := generateX5C(nil)
  192. assert.FatalError(t, err)
  193. v := sshCertificateValidityValidator{p.claimer}
  194. n := now()
  195. tests := []struct {
  196. name string
  197. cert *ssh.Certificate
  198. err error
  199. }{
  200. {
  201. "fail/validAfter-0",
  202. &ssh.Certificate{CertType: ssh.UserCert},
  203. errors.New("ssh certificate validAfter cannot be 0"),
  204. },
  205. {
  206. "fail/validBefore-in-past",
  207. &ssh.Certificate{CertType: ssh.UserCert, ValidAfter: uint64(now().Unix()), ValidBefore: uint64(now().Add(-time.Minute).Unix())},
  208. errors.New("ssh certificate validBefore cannot be in the past"),
  209. },
  210. {
  211. "fail/validBefore-before-validAfter",
  212. &ssh.Certificate{CertType: ssh.UserCert, ValidAfter: uint64(now().Add(5 * time.Minute).Unix()), ValidBefore: uint64(now().Add(3 * time.Minute).Unix())},
  213. errors.New("ssh certificate validBefore cannot be before validAfter"),
  214. },
  215. {
  216. "fail/cert-type-not-set",
  217. &ssh.Certificate{ValidAfter: uint64(now().Unix()), ValidBefore: uint64(now().Add(10 * time.Minute).Unix())},
  218. errors.New("ssh certificate type has not been set"),
  219. },
  220. {
  221. "fail/unexpected-cert-type",
  222. &ssh.Certificate{
  223. CertType: 3,
  224. ValidAfter: uint64(now().Unix()),
  225. ValidBefore: uint64(now().Add(10 * time.Minute).Unix()),
  226. },
  227. errors.New("unknown ssh certificate type 3"),
  228. },
  229. {
  230. "fail/duration<min",
  231. &ssh.Certificate{
  232. CertType: 1,
  233. ValidAfter: uint64(n.Unix()),
  234. ValidBefore: uint64(n.Add(4 * time.Minute).Unix()),
  235. },
  236. errors.New("requested duration of 4m0s is less than minimum accepted duration for selected provisioner of 5m0s"),
  237. },
  238. {
  239. "fail/duration>max",
  240. &ssh.Certificate{
  241. CertType: 1,
  242. ValidAfter: uint64(n.Unix()),
  243. ValidBefore: uint64(n.Add(48 * time.Hour).Unix()),
  244. },
  245. errors.New("requested duration of 48h0m0s is greater than maximum accepted duration for selected provisioner of 24h0m0s"),
  246. },
  247. {
  248. "ok",
  249. &ssh.Certificate{
  250. CertType: 1,
  251. ValidAfter: uint64(now().Unix()),
  252. ValidBefore: uint64(now().Add(8 * time.Hour).Unix()),
  253. },
  254. nil,
  255. },
  256. }
  257. for _, tt := range tests {
  258. t.Run(tt.name, func(t *testing.T) {
  259. if err := v.Valid(tt.cert); err != nil {
  260. if assert.NotNil(t, tt.err) {
  261. assert.HasPrefix(t, err.Error(), tt.err.Error())
  262. }
  263. } else {
  264. assert.Nil(t, tt.err)
  265. }
  266. })
  267. }
  268. }
  269. func Test_sshValidityModifier(t *testing.T) {
  270. n := now()
  271. p, err := generateX5C(nil)
  272. assert.FatalError(t, err)
  273. type test struct {
  274. svm *sshValidityModifier
  275. cert *ssh.Certificate
  276. valid func(*ssh.Certificate)
  277. err error
  278. }
  279. tests := map[string]func() test{
  280. "fail/type-not-set": func() test {
  281. return test{
  282. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(6 * time.Hour)},
  283. cert: &ssh.Certificate{
  284. ValidAfter: uint64(n.Unix()),
  285. ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
  286. },
  287. err: errors.New("ssh certificate type has not been set"),
  288. }
  289. },
  290. "fail/type-not-recognized": func() test {
  291. return test{
  292. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(6 * time.Hour)},
  293. cert: &ssh.Certificate{
  294. CertType: 4,
  295. ValidAfter: uint64(n.Unix()),
  296. ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
  297. },
  298. err: errors.New("unknown ssh certificate type 4"),
  299. }
  300. },
  301. "fail/requested-validAfter-after-limit": func() test {
  302. return test{
  303. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(1 * time.Hour)},
  304. cert: &ssh.Certificate{
  305. CertType: 1,
  306. ValidAfter: uint64(n.Add(2 * time.Hour).Unix()),
  307. ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
  308. },
  309. err: errors.Errorf("provisioning credential expiration ("),
  310. }
  311. },
  312. "fail/requested-validBefore-after-limit": func() test {
  313. return test{
  314. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(1 * time.Hour)},
  315. cert: &ssh.Certificate{
  316. CertType: 1,
  317. ValidAfter: uint64(n.Unix()),
  318. ValidBefore: uint64(n.Add(2 * time.Hour).Unix()),
  319. },
  320. err: errors.New("provisioning credential expiration ("),
  321. }
  322. },
  323. "ok/valid-requested-validBefore": func() test {
  324. va, vb := uint64(n.Unix()), uint64(n.Add(2*time.Hour).Unix())
  325. return test{
  326. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(3 * time.Hour)},
  327. cert: &ssh.Certificate{
  328. CertType: 1,
  329. ValidAfter: va,
  330. ValidBefore: vb,
  331. },
  332. valid: func(cert *ssh.Certificate) {
  333. assert.Equals(t, cert.ValidAfter, va)
  334. assert.Equals(t, cert.ValidBefore, vb)
  335. },
  336. }
  337. },
  338. "ok/empty-requested-validBefore-limit-after-default": func() test {
  339. va := uint64(n.Unix())
  340. return test{
  341. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(5 * time.Hour)},
  342. cert: &ssh.Certificate{
  343. CertType: 1,
  344. ValidAfter: va,
  345. },
  346. valid: func(cert *ssh.Certificate) {
  347. assert.Equals(t, cert.ValidAfter, va)
  348. assert.Equals(t, cert.ValidBefore, uint64(n.Add(4*time.Hour).Unix()))
  349. },
  350. }
  351. },
  352. "ok/empty-requested-validBefore-limit-before-default": func() test {
  353. va := uint64(n.Unix())
  354. return test{
  355. svm: &sshValidityModifier{Claimer: p.claimer, validBefore: n.Add(3 * time.Hour)},
  356. cert: &ssh.Certificate{
  357. CertType: 1,
  358. ValidAfter: va,
  359. },
  360. valid: func(cert *ssh.Certificate) {
  361. assert.Equals(t, cert.ValidAfter, va)
  362. assert.Equals(t, cert.ValidBefore, uint64(n.Add(3*time.Hour).Unix()))
  363. },
  364. }
  365. },
  366. }
  367. for name, run := range tests {
  368. t.Run(name, func(t *testing.T) {
  369. tt := run()
  370. if err := tt.svm.Modify(tt.cert); err != nil {
  371. if assert.NotNil(t, tt.err) {
  372. assert.HasPrefix(t, err.Error(), tt.err.Error())
  373. }
  374. } else {
  375. if assert.Nil(t, tt.err) {
  376. tt.valid(tt.cert)
  377. }
  378. }
  379. })
  380. }
  381. }