sign_options_test.go 25 KB

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