sign_options_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "net"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/assert"
  11. "github.com/smallstep/cli/crypto/pemutil"
  12. "github.com/smallstep/cli/crypto/x509util"
  13. stepx509 "github.com/smallstep/cli/pkg/x509"
  14. )
  15. func Test_emailOnlyIdentity_Valid(t *testing.T) {
  16. uri, err := url.Parse("https://example.com/1.0/getUser")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. type args struct {
  21. req *x509.CertificateRequest
  22. }
  23. tests := []struct {
  24. name string
  25. e emailOnlyIdentity
  26. args args
  27. wantErr bool
  28. }{
  29. {"ok", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com"}}}, false},
  30. {"DNSNames", "name@smallstep.com", args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, true},
  31. {"IPAddresses", "name@smallstep.com", args{&x509.CertificateRequest{IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}}}, true},
  32. {"URIs", "name@smallstep.com", args{&x509.CertificateRequest{URIs: []*url.URL{uri}}}, true},
  33. {"no-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{}}}, true},
  34. {"empty-email", "", args{&x509.CertificateRequest{EmailAddresses: []string{""}}}, true},
  35. {"multiple-emails", "name@smallstep.com", args{&x509.CertificateRequest{EmailAddresses: []string{"name@smallstep.com", "foo@smallstep.com"}}}, true},
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. if err := tt.e.Valid(tt.args.req); (err != nil) != tt.wantErr {
  40. t.Errorf("emailOnlyIdentity.Valid() error = %v, wantErr %v", err, tt.wantErr)
  41. }
  42. })
  43. }
  44. }
  45. func Test_defaultPublicKeyValidator_Valid(t *testing.T) {
  46. _shortRSA, err := pemutil.Read("./testdata/short-rsa.csr")
  47. assert.FatalError(t, err)
  48. shortRSA, ok := _shortRSA.(*x509.CertificateRequest)
  49. assert.Fatal(t, ok)
  50. _rsa, err := pemutil.Read("./testdata/rsa.csr")
  51. assert.FatalError(t, err)
  52. rsaCSR, ok := _rsa.(*x509.CertificateRequest)
  53. assert.Fatal(t, ok)
  54. _ecdsa, err := pemutil.Read("./testdata/ecdsa.csr")
  55. assert.FatalError(t, err)
  56. ecdsaCSR, ok := _ecdsa.(*x509.CertificateRequest)
  57. assert.Fatal(t, ok)
  58. _ed25519, err := pemutil.Read("./testdata/ed25519.csr", pemutil.WithStepCrypto())
  59. assert.FatalError(t, err)
  60. ed25519CSR, ok := _ed25519.(*stepx509.CertificateRequest)
  61. assert.Fatal(t, ok)
  62. v := defaultPublicKeyValidator{}
  63. tests := []struct {
  64. name string
  65. csr *x509.CertificateRequest
  66. err error
  67. }{
  68. {
  69. "fail/unrecognized-key-type",
  70. &x509.CertificateRequest{PublicKey: "foo"},
  71. errors.New("unrecognized public key of type 'string' in CSR"),
  72. },
  73. {
  74. "fail/rsa/too-short",
  75. shortRSA,
  76. errors.New("rsa key in CSR must be at least 2048 bits (256 bytes)"),
  77. },
  78. {
  79. "ok/rsa",
  80. rsaCSR,
  81. nil,
  82. },
  83. {
  84. "ok/ecdsa",
  85. ecdsaCSR,
  86. nil,
  87. },
  88. {
  89. "ok/ed25519",
  90. x509util.ToX509CertificateRequest(ed25519CSR),
  91. nil,
  92. },
  93. }
  94. for _, tt := range tests {
  95. t.Run(tt.name, func(t *testing.T) {
  96. if err := v.Valid(tt.csr); err != nil {
  97. if assert.NotNil(t, tt.err) {
  98. assert.HasPrefix(t, err.Error(), tt.err.Error())
  99. }
  100. } else {
  101. assert.Nil(t, tt.err)
  102. }
  103. })
  104. }
  105. }
  106. func Test_commonNameValidator_Valid(t *testing.T) {
  107. type args struct {
  108. req *x509.CertificateRequest
  109. }
  110. tests := []struct {
  111. name string
  112. v commonNameValidator
  113. args args
  114. wantErr bool
  115. }{
  116. {"ok", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  117. {"empty", "", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, true},
  118. {"wrong", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  119. }
  120. for _, tt := range tests {
  121. t.Run(tt.name, func(t *testing.T) {
  122. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  123. t.Errorf("commonNameValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  124. }
  125. })
  126. }
  127. }
  128. func Test_commonNameSliceValidator_Valid(t *testing.T) {
  129. type args struct {
  130. req *x509.CertificateRequest
  131. }
  132. tests := []struct {
  133. name string
  134. v commonNameSliceValidator
  135. args args
  136. wantErr bool
  137. }{
  138. {"ok", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  139. {"ok", []string{"example.com", "foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  140. {"empty", []string{""}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, true},
  141. {"wrong", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  142. }
  143. for _, tt := range tests {
  144. t.Run(tt.name, func(t *testing.T) {
  145. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  146. t.Errorf("commonNameSliceValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  147. }
  148. })
  149. }
  150. }
  151. func Test_emailAddressesValidator_Valid(t *testing.T) {
  152. type args struct {
  153. req *x509.CertificateRequest
  154. }
  155. tests := []struct {
  156. name string
  157. v emailAddressesValidator
  158. args args
  159. wantErr bool
  160. }{
  161. {"ok0", []string{}, args{&x509.CertificateRequest{EmailAddresses: []string{}}}, false},
  162. {"ok1", []string{"max@smallstep.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@smallstep.com"}}}, false},
  163. {"ok2", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, false},
  164. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "max@step.com"}}}, false},
  165. {"fail1", []string{"max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com"}}}, true},
  166. {"fail2", []string{"mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, true},
  167. {"fail3", []string{"mike@step.com", "max@step.com"}, args{&x509.CertificateRequest{DNSNames: []string{"mike@step.com", "mex@step.com"}}}, true},
  168. }
  169. for _, tt := range tests {
  170. t.Run(tt.name, func(t *testing.T) {
  171. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  172. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  173. }
  174. })
  175. }
  176. }
  177. func Test_dnsNamesValidator_Valid(t *testing.T) {
  178. type args struct {
  179. req *x509.CertificateRequest
  180. }
  181. tests := []struct {
  182. name string
  183. v dnsNamesValidator
  184. args args
  185. wantErr bool
  186. }{
  187. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  188. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  189. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  190. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  191. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  192. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  193. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  194. }
  195. for _, tt := range tests {
  196. t.Run(tt.name, func(t *testing.T) {
  197. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  198. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  199. }
  200. })
  201. }
  202. }
  203. func Test_ipAddressesValidator_Valid(t *testing.T) {
  204. ip1 := net.IPv4(10, 3, 2, 1)
  205. ip2 := net.IPv4(10, 3, 2, 2)
  206. ip3 := net.IPv4(10, 3, 2, 3)
  207. type args struct {
  208. req *x509.CertificateRequest
  209. }
  210. tests := []struct {
  211. name string
  212. v ipAddressesValidator
  213. args args
  214. wantErr bool
  215. }{
  216. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  217. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  218. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  219. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  220. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  221. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  222. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  223. }
  224. for _, tt := range tests {
  225. t.Run(tt.name, func(t *testing.T) {
  226. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  227. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  228. }
  229. })
  230. }
  231. }
  232. func Test_validityValidator_Valid(t *testing.T) {
  233. type fields struct {
  234. min time.Duration
  235. max time.Duration
  236. }
  237. type args struct {
  238. crt *x509.Certificate
  239. }
  240. tests := []struct {
  241. name string
  242. fields fields
  243. args args
  244. wantErr bool
  245. }{
  246. // TODO: Add test cases.
  247. }
  248. for _, tt := range tests {
  249. t.Run(tt.name, func(t *testing.T) {
  250. v := &validityValidator{
  251. min: tt.fields.min,
  252. max: tt.fields.max,
  253. }
  254. if err := v.Valid(tt.args.crt); (err != nil) != tt.wantErr {
  255. t.Errorf("validityValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  256. }
  257. })
  258. }
  259. }
  260. func Test_profileLimitDuration_Option(t *testing.T) {
  261. n := now()
  262. type test struct {
  263. pld profileLimitDuration
  264. so Options
  265. cert *x509.Certificate
  266. valid func(*x509.Certificate)
  267. err error
  268. }
  269. tests := map[string]func() test{
  270. "fail/notBefore-after-limit": func() test {
  271. d, err := ParseTimeDuration("8h")
  272. assert.FatalError(t, err)
  273. return test{
  274. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  275. so: Options{NotBefore: d},
  276. cert: new(x509.Certificate),
  277. err: errors.New("provisioning credential expiration ("),
  278. }
  279. },
  280. "fail/requested-notAfter-after-limit": func() test {
  281. d, err := ParseTimeDuration("4h")
  282. assert.FatalError(t, err)
  283. return test{
  284. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  285. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  286. cert: new(x509.Certificate),
  287. err: errors.New("provisioning credential expiration ("),
  288. }
  289. },
  290. "ok/valid-notAfter-requested": func() test {
  291. d, err := ParseTimeDuration("2h")
  292. assert.FatalError(t, err)
  293. return test{
  294. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  295. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  296. cert: new(x509.Certificate),
  297. valid: func(cert *x509.Certificate) {
  298. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  299. assert.Equals(t, cert.NotAfter, n.Add(5*time.Hour))
  300. },
  301. }
  302. },
  303. "ok/valid-notAfter-nil-limit-over-default": func() test {
  304. return test{
  305. pld: profileLimitDuration{def: 1 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  306. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour))},
  307. cert: new(x509.Certificate),
  308. valid: func(cert *x509.Certificate) {
  309. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  310. assert.Equals(t, cert.NotAfter, n.Add(4*time.Hour))
  311. },
  312. }
  313. },
  314. "ok/valid-notAfter-nil-limit-under-default": func() test {
  315. return test{
  316. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  317. so: Options{NotBefore: NewTimeDuration(n.Add(3 * time.Hour))},
  318. cert: new(x509.Certificate),
  319. valid: func(cert *x509.Certificate) {
  320. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  321. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  322. },
  323. }
  324. },
  325. }
  326. for name, run := range tests {
  327. t.Run(name, func(t *testing.T) {
  328. tt := run()
  329. prof := &x509util.Leaf{}
  330. prof.SetSubject(tt.cert)
  331. if err := tt.pld.Option(tt.so)(prof); err != nil {
  332. if assert.NotNil(t, tt.err) {
  333. assert.HasPrefix(t, err.Error(), tt.err.Error())
  334. }
  335. } else {
  336. if assert.Nil(t, tt.err) {
  337. tt.valid(prof.Subject())
  338. }
  339. }
  340. })
  341. }
  342. }