sign_options_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "encoding/asn1"
  6. "fmt"
  7. "net"
  8. "net/url"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/assert"
  14. "go.step.sm/crypto/pemutil"
  15. )
  16. func Test_defaultPublicKeyValidator_Valid(t *testing.T) {
  17. _shortRSA, err := pemutil.Read("./testdata/certs/short-rsa.csr")
  18. assert.FatalError(t, err)
  19. shortRSA, ok := _shortRSA.(*x509.CertificateRequest)
  20. assert.Fatal(t, ok)
  21. _rsa, err := pemutil.Read("./testdata/certs/rsa.csr")
  22. assert.FatalError(t, err)
  23. rsaCSR, ok := _rsa.(*x509.CertificateRequest)
  24. assert.Fatal(t, ok)
  25. _ecdsa, err := pemutil.Read("./testdata/certs/ecdsa.csr")
  26. assert.FatalError(t, err)
  27. ecdsaCSR, ok := _ecdsa.(*x509.CertificateRequest)
  28. assert.Fatal(t, ok)
  29. _ed25519, err := pemutil.Read("./testdata/certs/ed25519.csr")
  30. assert.FatalError(t, err)
  31. ed25519CSR, ok := _ed25519.(*x509.CertificateRequest)
  32. assert.Fatal(t, ok)
  33. v := defaultPublicKeyValidator{}
  34. tests := []struct {
  35. name string
  36. csr *x509.CertificateRequest
  37. err error
  38. }{
  39. {
  40. "fail/unrecognized-key-type",
  41. &x509.CertificateRequest{PublicKey: "foo"},
  42. errors.New("certificate request key of type 'string' is not supported"),
  43. },
  44. {
  45. "fail/rsa/too-short",
  46. shortRSA,
  47. errors.New("certificate request RSA key must be at least 2048 bits (256 bytes)"),
  48. },
  49. {
  50. "ok/rsa",
  51. rsaCSR,
  52. nil,
  53. },
  54. {
  55. "ok/ecdsa",
  56. ecdsaCSR,
  57. nil,
  58. },
  59. {
  60. "ok/ed25519",
  61. ed25519CSR,
  62. nil,
  63. },
  64. }
  65. for _, tt := range tests {
  66. t.Run(tt.name, func(t *testing.T) {
  67. if err := v.Valid(tt.csr); err != nil {
  68. if assert.NotNil(t, tt.err) {
  69. assert.HasPrefix(t, err.Error(), tt.err.Error())
  70. }
  71. } else {
  72. assert.Nil(t, tt.err)
  73. }
  74. })
  75. }
  76. }
  77. func Test_commonNameValidator_Valid(t *testing.T) {
  78. type args struct {
  79. req *x509.CertificateRequest
  80. }
  81. tests := []struct {
  82. name string
  83. v commonNameValidator
  84. args args
  85. wantErr bool
  86. }{
  87. {"ok", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  88. {"empty", "", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, false},
  89. {"wrong", "foo.bar.zar", args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  90. }
  91. for _, tt := range tests {
  92. t.Run(tt.name, func(t *testing.T) {
  93. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  94. t.Errorf("commonNameValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  95. }
  96. })
  97. }
  98. }
  99. func Test_commonNameSliceValidator_Valid(t *testing.T) {
  100. type args struct {
  101. req *x509.CertificateRequest
  102. }
  103. tests := []struct {
  104. name string
  105. v commonNameSliceValidator
  106. args args
  107. wantErr bool
  108. }{
  109. {"ok", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  110. {"ok", []string{"example.com", "foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "foo.bar.zar"}}}, false},
  111. {"empty", []string{""}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: ""}}}, false},
  112. {"wrong", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{Subject: pkix.Name{CommonName: "example.com"}}}, true},
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  117. t.Errorf("commonNameSliceValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  118. }
  119. })
  120. }
  121. }
  122. func Test_emailAddressesValidator_Valid(t *testing.T) {
  123. type args struct {
  124. req *x509.CertificateRequest
  125. }
  126. tests := []struct {
  127. name string
  128. v emailAddressesValidator
  129. args args
  130. wantErr bool
  131. }{
  132. {"ok0", []string{}, args{&x509.CertificateRequest{EmailAddresses: []string{}}}, false},
  133. {"ok1", []string{"max@smallstep.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@smallstep.com"}}}, false},
  134. {"ok2", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, false},
  135. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "max@step.com"}}}, false},
  136. {"ok3", []string{"max@step.com", "mike@step.com"}, args{&x509.CertificateRequest{}}, false},
  137. {"fail1", []string{"max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com"}}}, true},
  138. {"fail2", []string{"mike@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"max@step.com", "mike@step.com"}}}, true},
  139. {"fail3", []string{"mike@step.com", "max@step.com"}, args{&x509.CertificateRequest{EmailAddresses: []string{"mike@step.com", "mex@step.com"}}}, true},
  140. }
  141. for _, tt := range tests {
  142. t.Run(tt.name, func(t *testing.T) {
  143. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  144. t.Errorf("emailAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  145. }
  146. })
  147. }
  148. }
  149. func Test_dnsNamesValidator_Valid(t *testing.T) {
  150. type args struct {
  151. req *x509.CertificateRequest
  152. }
  153. tests := []struct {
  154. name string
  155. v dnsNamesValidator
  156. args args
  157. wantErr bool
  158. }{
  159. {"ok0", []string{}, args{&x509.CertificateRequest{DNSNames: []string{}}}, false},
  160. {"ok1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar"}}}, false},
  161. {"ok2", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "bar.zar"}}}, false},
  162. {"ok3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, false},
  163. {"ok4", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{}}, false},
  164. {"fail1", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar"}}}, true},
  165. {"fail2", []string{"foo.bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"bar.zar", "foo.bar.zar"}}}, true},
  166. {"fail3", []string{"foo.bar.zar", "bar.zar"}, args{&x509.CertificateRequest{DNSNames: []string{"foo.bar.zar", "zar.bar"}}}, true},
  167. }
  168. for _, tt := range tests {
  169. t.Run(tt.name, func(t *testing.T) {
  170. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  171. t.Errorf("dnsNamesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  172. }
  173. })
  174. }
  175. }
  176. func Test_ipAddressesValidator_Valid(t *testing.T) {
  177. ip1 := net.IPv4(10, 3, 2, 1)
  178. ip2 := net.IPv4(10, 3, 2, 2)
  179. ip3 := net.IPv4(10, 3, 2, 3)
  180. type args struct {
  181. req *x509.CertificateRequest
  182. }
  183. tests := []struct {
  184. name string
  185. v ipAddressesValidator
  186. args args
  187. wantErr bool
  188. }{
  189. {"ok0", []net.IP{}, args{&x509.CertificateRequest{IPAddresses: []net.IP{}}}, false},
  190. {"ok1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1}}}, false},
  191. {"ok2", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip2}}}, false},
  192. {"ok3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, false},
  193. {"ok4", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{}}, false},
  194. {"fail1", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2}}}, true},
  195. {"fail2", []net.IP{ip1}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip2, ip1}}}, true},
  196. {"fail3", []net.IP{ip1, ip2}, args{&x509.CertificateRequest{IPAddresses: []net.IP{ip1, ip3}}}, true},
  197. }
  198. for _, tt := range tests {
  199. t.Run(tt.name, func(t *testing.T) {
  200. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  201. t.Errorf("ipAddressesValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  202. }
  203. })
  204. }
  205. }
  206. func Test_urisValidator_Valid(t *testing.T) {
  207. u1, err := url.Parse("https://ca.smallstep.com")
  208. assert.FatalError(t, err)
  209. u2, err := url.Parse("https://google.com/index.html")
  210. assert.FatalError(t, err)
  211. u3, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  212. assert.FatalError(t, err)
  213. fu, err := url.Parse("https://unexpected.com")
  214. assert.FatalError(t, err)
  215. type args struct {
  216. req *x509.CertificateRequest
  217. }
  218. tests := []struct {
  219. name string
  220. v urisValidator
  221. args args
  222. wantErr bool
  223. }{
  224. {"ok0", []*url.URL{}, args{&x509.CertificateRequest{URIs: []*url.URL{}}}, false},
  225. {"ok1", []*url.URL{u1}, args{&x509.CertificateRequest{URIs: []*url.URL{u1}}}, false},
  226. {"ok2", []*url.URL{u1, u2}, args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, false},
  227. {"ok3", []*url.URL{u2, u1, u3}, args{&x509.CertificateRequest{URIs: []*url.URL{u3, u2, u1}}}, false},
  228. {"ok3", []*url.URL{u2, u1, u3}, args{&x509.CertificateRequest{}}, false},
  229. {"fail1", []*url.URL{u1}, args{&x509.CertificateRequest{URIs: []*url.URL{u2}}}, true},
  230. {"fail2", []*url.URL{u1}, args{&x509.CertificateRequest{URIs: []*url.URL{u2, u1}}}, true},
  231. {"fail3", []*url.URL{u1, u2}, args{&x509.CertificateRequest{URIs: []*url.URL{u1, fu}}}, true},
  232. }
  233. for _, tt := range tests {
  234. t.Run(tt.name, func(t *testing.T) {
  235. if err := tt.v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  236. t.Errorf("urisValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  237. }
  238. })
  239. }
  240. }
  241. func Test_defaultSANsValidator_Valid(t *testing.T) {
  242. type test struct {
  243. csr *x509.CertificateRequest
  244. expectedSANs []string
  245. err error
  246. }
  247. tests := map[string]func() test{
  248. "fail/dnsNamesValidator": func() test {
  249. return test{
  250. csr: &x509.CertificateRequest{DNSNames: []string{"foo", "bar"}},
  251. expectedSANs: []string{"foo"},
  252. err: errors.New("certificate request does not contain the valid DNS names"),
  253. }
  254. },
  255. "fail/emailAddressesValidator": func() test {
  256. return test{
  257. csr: &x509.CertificateRequest{EmailAddresses: []string{"max@fx.com", "mariano@fx.com"}},
  258. expectedSANs: []string{"dcow@fx.com"},
  259. err: errors.New("certificate request does not contain the valid email addresses"),
  260. }
  261. },
  262. "fail/ipAddressesValidator": func() test {
  263. return test{
  264. csr: &x509.CertificateRequest{IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")}},
  265. expectedSANs: []string{"127.0.0.1"},
  266. err: errors.New("certificate request does not contain the valid IP addresses"),
  267. }
  268. },
  269. "fail/urisValidator": func() test {
  270. u1, err := url.Parse("https://google.com")
  271. assert.FatalError(t, err)
  272. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  273. assert.FatalError(t, err)
  274. return test{
  275. csr: &x509.CertificateRequest{URIs: []*url.URL{u1, u2}},
  276. expectedSANs: []string{"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  277. err: errors.New("certificate request does not contain the valid URIs"),
  278. }
  279. },
  280. "ok": func() test {
  281. u1, err := url.Parse("https://google.com")
  282. assert.FatalError(t, err)
  283. u2, err := url.Parse("urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959")
  284. assert.FatalError(t, err)
  285. return test{
  286. csr: &x509.CertificateRequest{
  287. DNSNames: []string{"foo", "bar"},
  288. EmailAddresses: []string{"max@fx.com", "mariano@fx.com"},
  289. IPAddresses: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("127.0.0.1")},
  290. URIs: []*url.URL{u1, u2},
  291. },
  292. expectedSANs: []string{"foo", "127.0.0.1", "max@fx.com", "mariano@fx.com", "https://google.com", "1.1.1.1", "bar", "urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
  293. }
  294. },
  295. }
  296. for name, run := range tests {
  297. t.Run(name, func(t *testing.T) {
  298. tt := run()
  299. if err := defaultSANsValidator(tt.expectedSANs).Valid(tt.csr); err != nil {
  300. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  301. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  302. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  303. }
  304. } else {
  305. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  306. }
  307. })
  308. }
  309. }
  310. func Test_validityValidator_Valid(t *testing.T) {
  311. type test struct {
  312. cert *x509.Certificate
  313. opts SignOptions
  314. vv *validityValidator
  315. err error
  316. }
  317. tests := map[string]func() test{
  318. "fail/notAfter-past": func() test {
  319. return test{
  320. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  321. cert: &x509.Certificate{NotAfter: time.Now().Add(-5 * time.Minute)},
  322. opts: SignOptions{},
  323. err: errors.New("notAfter cannot be in the past"),
  324. }
  325. },
  326. "fail/notBefore-after-notAfter": func() test {
  327. return test{
  328. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  329. cert: &x509.Certificate{NotBefore: time.Now().Add(10 * time.Minute),
  330. NotAfter: time.Now().Add(5 * time.Minute)},
  331. opts: SignOptions{},
  332. err: errors.New("notAfter cannot be before notBefore"),
  333. }
  334. },
  335. "fail/duration-too-short": func() test {
  336. n := now()
  337. return test{
  338. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  339. cert: &x509.Certificate{NotBefore: n,
  340. NotAfter: n.Add(3 * time.Minute)},
  341. opts: SignOptions{},
  342. err: errors.New("is less than the authorized minimum certificate duration of "),
  343. }
  344. },
  345. "ok/duration-exactly-min": func() test {
  346. n := now()
  347. return test{
  348. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  349. cert: &x509.Certificate{NotBefore: n,
  350. NotAfter: n.Add(5 * time.Minute)},
  351. opts: SignOptions{},
  352. }
  353. },
  354. "fail/duration-too-great": func() test {
  355. n := now()
  356. return test{
  357. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  358. cert: &x509.Certificate{NotBefore: n,
  359. NotAfter: n.Add(24*time.Hour + time.Second)},
  360. err: errors.New("is more than the authorized maximum certificate duration of "),
  361. }
  362. },
  363. "ok/duration-exactly-max": func() test {
  364. n := time.Now()
  365. return test{
  366. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  367. cert: &x509.Certificate{NotBefore: n,
  368. NotAfter: n.Add(24 * time.Hour)},
  369. }
  370. },
  371. "ok/duration-exact-min-with-backdate": func() test {
  372. now := time.Now()
  373. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(5 * time.Minute)}
  374. time.Sleep(time.Second)
  375. return test{
  376. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  377. cert: cert,
  378. opts: SignOptions{Backdate: time.Second},
  379. }
  380. },
  381. "ok/duration-exact-max-with-backdate": func() test {
  382. backdate := time.Second
  383. now := time.Now()
  384. cert := &x509.Certificate{NotBefore: now, NotAfter: now.Add(24*time.Hour + backdate)}
  385. time.Sleep(backdate)
  386. return test{
  387. vv: &validityValidator{5 * time.Minute, 24 * time.Hour},
  388. cert: cert,
  389. opts: SignOptions{Backdate: backdate},
  390. }
  391. },
  392. }
  393. for name, run := range tests {
  394. t.Run(name, func(t *testing.T) {
  395. tt := run()
  396. if err := tt.vv.Valid(tt.cert, tt.opts); err != nil {
  397. if assert.NotNil(t, tt.err, fmt.Sprintf("expected no error, but got err = %s", err.Error())) {
  398. assert.True(t, strings.Contains(err.Error(), tt.err.Error()),
  399. fmt.Sprintf("want err = %s, but got err = %s", tt.err.Error(), err.Error()))
  400. }
  401. } else {
  402. assert.Nil(t, tt.err, fmt.Sprintf("expected err = %s, but not <nil>", tt.err))
  403. }
  404. })
  405. }
  406. }
  407. func Test_forceCN_Option(t *testing.T) {
  408. type test struct {
  409. so SignOptions
  410. fcn forceCNOption
  411. cert *x509.Certificate
  412. valid func(*x509.Certificate)
  413. err error
  414. }
  415. tests := map[string]func() test{
  416. "ok/CN-not-forced": func() test {
  417. return test{
  418. fcn: forceCNOption{false},
  419. so: SignOptions{},
  420. cert: &x509.Certificate{
  421. Subject: pkix.Name{},
  422. DNSNames: []string{"acme.example.com", "step.example.com"},
  423. },
  424. valid: func(cert *x509.Certificate) {
  425. assert.Equals(t, cert.Subject.CommonName, "")
  426. },
  427. }
  428. },
  429. "ok/CN-forced-and-set": func() test {
  430. return test{
  431. fcn: forceCNOption{true},
  432. so: SignOptions{},
  433. cert: &x509.Certificate{
  434. Subject: pkix.Name{
  435. CommonName: "Some Common Name",
  436. },
  437. DNSNames: []string{"acme.example.com", "step.example.com"},
  438. },
  439. valid: func(cert *x509.Certificate) {
  440. assert.Equals(t, cert.Subject.CommonName, "Some Common Name")
  441. },
  442. }
  443. },
  444. "ok/CN-forced-and-not-set": func() test {
  445. return test{
  446. fcn: forceCNOption{true},
  447. so: SignOptions{},
  448. cert: &x509.Certificate{
  449. Subject: pkix.Name{},
  450. DNSNames: []string{"acme.example.com", "step.example.com"},
  451. },
  452. valid: func(cert *x509.Certificate) {
  453. assert.Equals(t, cert.Subject.CommonName, "acme.example.com")
  454. },
  455. }
  456. },
  457. "fail/CN-forced-and-empty-DNSNames": func() test {
  458. return test{
  459. fcn: forceCNOption{true},
  460. so: SignOptions{},
  461. cert: &x509.Certificate{
  462. Subject: pkix.Name{},
  463. DNSNames: []string{},
  464. },
  465. err: errors.New("cannot force common name, DNS names is empty"),
  466. }
  467. },
  468. }
  469. for name, run := range tests {
  470. t.Run(name, func(t *testing.T) {
  471. tt := run()
  472. if err := tt.fcn.Modify(tt.cert, tt.so); err != nil {
  473. if assert.NotNil(t, tt.err) {
  474. assert.HasPrefix(t, err.Error(), tt.err.Error())
  475. }
  476. } else {
  477. if assert.Nil(t, tt.err) {
  478. tt.valid(tt.cert)
  479. }
  480. }
  481. })
  482. }
  483. }
  484. func Test_profileDefaultDuration_Option(t *testing.T) {
  485. type test struct {
  486. so SignOptions
  487. pdd profileDefaultDuration
  488. cert *x509.Certificate
  489. valid func(*x509.Certificate)
  490. }
  491. tests := map[string]func() test{
  492. "ok/notBefore-notAfter-duration-empty": func() test {
  493. return test{
  494. pdd: profileDefaultDuration(0),
  495. so: SignOptions{},
  496. cert: new(x509.Certificate),
  497. valid: func(cert *x509.Certificate) {
  498. n := now()
  499. assert.True(t, n.After(cert.NotBefore.Add(-time.Second)))
  500. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  501. assert.True(t, n.Add(24*time.Hour).After(cert.NotAfter.Add(-time.Second)))
  502. assert.True(t, n.Add(24*time.Hour).Add(-1*time.Minute).Before(cert.NotAfter))
  503. },
  504. }
  505. },
  506. "ok/notBefore-set": func() test {
  507. nb := time.Now().Add(5 * time.Minute).UTC()
  508. return test{
  509. pdd: profileDefaultDuration(0),
  510. so: SignOptions{NotBefore: NewTimeDuration(nb)},
  511. cert: new(x509.Certificate),
  512. valid: func(cert *x509.Certificate) {
  513. assert.Equals(t, cert.NotBefore, nb)
  514. assert.Equals(t, cert.NotAfter, nb.Add(24*time.Hour))
  515. },
  516. }
  517. },
  518. "ok/duration-set": func() test {
  519. d := 4 * time.Hour
  520. return test{
  521. pdd: profileDefaultDuration(d),
  522. so: SignOptions{Backdate: time.Second},
  523. cert: new(x509.Certificate),
  524. valid: func(cert *x509.Certificate) {
  525. n := now()
  526. assert.True(t, n.After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n, cert.NotBefore))
  527. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  528. assert.True(t, n.Add(d).After(cert.NotAfter))
  529. assert.True(t, n.Add(d).Add(-1*time.Minute).Before(cert.NotAfter))
  530. },
  531. }
  532. },
  533. "ok/notAfter-set": func() test {
  534. na := now().Add(10 * time.Minute).UTC()
  535. return test{
  536. pdd: profileDefaultDuration(0),
  537. so: SignOptions{NotAfter: NewTimeDuration(na)},
  538. cert: new(x509.Certificate),
  539. valid: func(cert *x509.Certificate) {
  540. n := now()
  541. assert.True(t, n.Add(3*time.Second).After(cert.NotBefore), fmt.Sprintf("expected now = %s to be after cert.NotBefore = %s", n.Add(3*time.Second), cert.NotBefore))
  542. assert.True(t, n.Add(-1*time.Minute).Before(cert.NotBefore))
  543. assert.Equals(t, cert.NotAfter, na)
  544. },
  545. }
  546. },
  547. "ok/notBefore-and-notAfter-set": func() test {
  548. nb := time.Now().Add(5 * time.Minute).UTC()
  549. na := time.Now().Add(10 * time.Minute).UTC()
  550. d := 4 * time.Hour
  551. return test{
  552. pdd: profileDefaultDuration(d),
  553. so: SignOptions{NotBefore: NewTimeDuration(nb), NotAfter: NewTimeDuration(na)},
  554. cert: new(x509.Certificate),
  555. valid: func(cert *x509.Certificate) {
  556. assert.Equals(t, cert.NotBefore, nb)
  557. assert.Equals(t, cert.NotAfter, na)
  558. },
  559. }
  560. },
  561. }
  562. for name, run := range tests {
  563. t.Run(name, func(t *testing.T) {
  564. tt := run()
  565. assert.FatalError(t, tt.pdd.Modify(tt.cert, tt.so), "unexpected error")
  566. time.Sleep(100 * time.Millisecond)
  567. tt.valid(tt.cert)
  568. })
  569. }
  570. }
  571. func Test_newProvisionerExtension_Option(t *testing.T) {
  572. expectedValue, err := asn1.Marshal(extensionASN1{
  573. Type: int(TypeJWK),
  574. Name: []byte("name"),
  575. CredentialID: []byte("credentialId"),
  576. KeyValuePairs: []string{"key", "value"},
  577. })
  578. if err != nil {
  579. t.Fatal(err)
  580. }
  581. // Claims with smallstep extensions disabled.
  582. claimer, err := NewClaimer(&Claims{
  583. DisableSmallstepExtensions: &trueValue,
  584. }, globalProvisionerClaims)
  585. if err != nil {
  586. t.Fatal(err)
  587. }
  588. type test struct {
  589. modifier *provisionerExtensionOption
  590. cert *x509.Certificate
  591. valid func(*x509.Certificate)
  592. }
  593. tests := map[string]func() test{
  594. "ok/one-element": func() test {
  595. return test{
  596. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  597. cert: new(x509.Certificate),
  598. valid: func(cert *x509.Certificate) {
  599. if assert.Len(t, 1, cert.ExtraExtensions) {
  600. ext := cert.ExtraExtensions[0]
  601. assert.Equals(t, StepOIDProvisioner, ext.Id)
  602. assert.Equals(t, expectedValue, ext.Value)
  603. assert.False(t, ext.Critical)
  604. }
  605. },
  606. }
  607. },
  608. "ok/replace": func() test {
  609. return test{
  610. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value"),
  611. cert: &x509.Certificate{ExtraExtensions: []pkix.Extension{{Id: StepOIDProvisioner, Critical: true}, {Id: []int{1, 2, 3}}}},
  612. valid: func(cert *x509.Certificate) {
  613. if assert.Len(t, 2, cert.ExtraExtensions) {
  614. ext := cert.ExtraExtensions[0]
  615. assert.Equals(t, StepOIDProvisioner, ext.Id)
  616. assert.Equals(t, expectedValue, ext.Value)
  617. assert.False(t, ext.Critical)
  618. }
  619. },
  620. }
  621. },
  622. "ok/disabled": func() test {
  623. return test{
  624. modifier: newProvisionerExtensionOption(TypeJWK, "name", "credentialId", "key", "value").WithControllerOptions(&Controller{
  625. Claimer: claimer,
  626. }),
  627. cert: new(x509.Certificate),
  628. valid: func(cert *x509.Certificate) {
  629. assert.Len(t, 0, cert.ExtraExtensions)
  630. },
  631. }
  632. },
  633. }
  634. for name, run := range tests {
  635. t.Run(name, func(t *testing.T) {
  636. tt := run()
  637. assert.FatalError(t, tt.modifier.Modify(tt.cert, SignOptions{}))
  638. tt.valid(tt.cert)
  639. })
  640. }
  641. }
  642. func Test_profileLimitDuration_Option(t *testing.T) {
  643. n, fn := mockNow()
  644. defer fn()
  645. type test struct {
  646. pld profileLimitDuration
  647. so SignOptions
  648. cert *x509.Certificate
  649. valid func(*x509.Certificate)
  650. err error
  651. }
  652. tests := map[string]func() test{
  653. "fail/notBefore-before-active-window": func() test {
  654. d, err := ParseTimeDuration("6h")
  655. assert.FatalError(t, err)
  656. return test{
  657. pld: profileLimitDuration{def: 4 * time.Hour, notBefore: n.Add(8 * time.Hour)},
  658. so: SignOptions{NotBefore: d},
  659. cert: new(x509.Certificate),
  660. err: errors.New("requested certificate notBefore ("),
  661. }
  662. },
  663. "fail/requested-notAfter-after-limit": func() test {
  664. d, err := ParseTimeDuration("4h")
  665. assert.FatalError(t, err)
  666. return test{
  667. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  668. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d},
  669. cert: new(x509.Certificate),
  670. err: errors.New("requested certificate notAfter ("),
  671. }
  672. },
  673. "ok/valid-notAfter-requested": func() test {
  674. d, err := ParseTimeDuration("2h")
  675. assert.FatalError(t, err)
  676. return test{
  677. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  678. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), NotAfter: d, Backdate: 1 * time.Minute},
  679. cert: new(x509.Certificate),
  680. valid: func(cert *x509.Certificate) {
  681. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  682. assert.Equals(t, cert.NotAfter, n.Add(5*time.Hour))
  683. },
  684. }
  685. },
  686. "ok/valid-notAfter-nil-limit-over-default": func() test {
  687. return test{
  688. pld: profileLimitDuration{def: 1 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  689. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  690. cert: new(x509.Certificate),
  691. valid: func(cert *x509.Certificate) {
  692. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  693. assert.Equals(t, cert.NotAfter, n.Add(4*time.Hour))
  694. },
  695. }
  696. },
  697. "ok/valid-notAfter-nil-limit-under-default": func() test {
  698. return test{
  699. pld: profileLimitDuration{def: 4 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  700. so: SignOptions{NotBefore: NewTimeDuration(n.Add(3 * time.Hour)), Backdate: 1 * time.Minute},
  701. cert: new(x509.Certificate),
  702. valid: func(cert *x509.Certificate) {
  703. assert.Equals(t, cert.NotBefore, n.Add(3*time.Hour))
  704. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  705. },
  706. }
  707. },
  708. "ok/over-limit-with-backdate": func() test {
  709. return test{
  710. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(6 * time.Hour)},
  711. so: SignOptions{Backdate: 1 * time.Minute},
  712. cert: new(x509.Certificate),
  713. valid: func(cert *x509.Certificate) {
  714. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  715. assert.Equals(t, cert.NotAfter, n.Add(6*time.Hour))
  716. },
  717. }
  718. },
  719. "ok/under-limit-with-backdate": func() test {
  720. return test{
  721. pld: profileLimitDuration{def: 24 * time.Hour, notAfter: n.Add(30 * time.Hour)},
  722. so: SignOptions{Backdate: 1 * time.Minute},
  723. cert: new(x509.Certificate),
  724. valid: func(cert *x509.Certificate) {
  725. assert.Equals(t, cert.NotBefore, n.Add(-time.Minute))
  726. assert.Equals(t, cert.NotAfter, n.Add(24*time.Hour))
  727. },
  728. }
  729. },
  730. }
  731. for name, run := range tests {
  732. t.Run(name, func(t *testing.T) {
  733. tt := run()
  734. if err := tt.pld.Modify(tt.cert, tt.so); err != nil {
  735. if assert.NotNil(t, tt.err) {
  736. assert.HasPrefix(t, err.Error(), tt.err.Error())
  737. }
  738. } else {
  739. if assert.Nil(t, tt.err) {
  740. tt.valid(tt.cert)
  741. }
  742. }
  743. })
  744. }
  745. }