sign_ssh_options_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }