acme_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package provisioner
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "os"
  10. "testing"
  11. "time"
  12. "github.com/smallstep/certificates/api/render"
  13. "github.com/smallstep/certificates/authority/provisioner/wire"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func TestACMEChallenge_Validate(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. c ACMEChallenge
  21. wantErr bool
  22. }{
  23. {"http-01", HTTP_01, false},
  24. {"dns-01", DNS_01, false},
  25. {"tls-alpn-01", TLS_ALPN_01, false},
  26. {"device-attest-01", DEVICE_ATTEST_01, false},
  27. {"wire-oidc-01", DEVICE_ATTEST_01, false},
  28. {"wire-dpop-01", DEVICE_ATTEST_01, false},
  29. {"uppercase", "HTTP-01", false},
  30. {"fail", "http-02", true},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. err := tt.c.Validate()
  35. if tt.wantErr {
  36. assert.Error(t, err)
  37. return
  38. }
  39. assert.NoError(t, err)
  40. })
  41. }
  42. }
  43. func TestACMEAttestationFormat_Validate(t *testing.T) {
  44. tests := []struct {
  45. name string
  46. f ACMEAttestationFormat
  47. wantErr bool
  48. }{
  49. {"apple", APPLE, false},
  50. {"step", STEP, false},
  51. {"tpm", TPM, false},
  52. {"uppercase", "APPLE", false},
  53. {"fail", "FOO", true},
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. err := tt.f.Validate()
  58. if tt.wantErr {
  59. assert.Error(t, err)
  60. return
  61. }
  62. assert.NoError(t, err)
  63. })
  64. }
  65. }
  66. func TestACME_Getters(t *testing.T) {
  67. p, err := generateACME()
  68. require.NoError(t, err)
  69. id := "acme/test@acme-provisioner.com"
  70. assert.Equal(t, id, p.GetID())
  71. assert.Equal(t, "test@acme-provisioner.com", p.GetName())
  72. assert.Equal(t, TypeACME, p.GetType())
  73. kid, key, ok := p.GetEncryptedKey()
  74. if kid != "" || key != "" || ok == true {
  75. t.Errorf("ACME.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
  76. kid, key, ok, "", "", false)
  77. }
  78. }
  79. func TestACME_Init(t *testing.T) {
  80. appleCA, err := os.ReadFile("testdata/certs/apple-att-ca.crt")
  81. require.NoError(t, err)
  82. yubicoCA, err := os.ReadFile("testdata/certs/yubico-piv-ca.crt")
  83. require.NoError(t, err)
  84. fakeWireDPoPKey := []byte(`-----BEGIN PUBLIC KEY-----
  85. MCowBQYDK2VwAyEA5c+4NKZSNQcR1T8qN6SjwgdPZQ0Ge12Ylx/YeGAJ35k=
  86. -----END PUBLIC KEY-----`)
  87. type ProvisionerValidateTest struct {
  88. p *ACME
  89. err error
  90. }
  91. tests := map[string]func(*testing.T) ProvisionerValidateTest{
  92. "fail/empty": func(t *testing.T) ProvisionerValidateTest {
  93. return ProvisionerValidateTest{
  94. p: &ACME{},
  95. err: errors.New("provisioner type cannot be empty"),
  96. }
  97. },
  98. "fail/empty-name": func(t *testing.T) ProvisionerValidateTest {
  99. return ProvisionerValidateTest{
  100. p: &ACME{
  101. Type: "ACME",
  102. },
  103. err: errors.New("provisioner name cannot be empty"),
  104. }
  105. },
  106. "fail/empty-type": func(t *testing.T) ProvisionerValidateTest {
  107. return ProvisionerValidateTest{
  108. p: &ACME{Name: "foo"},
  109. err: errors.New("provisioner type cannot be empty"),
  110. }
  111. },
  112. "fail/bad-claims": func(t *testing.T) ProvisionerValidateTest {
  113. return ProvisionerValidateTest{
  114. p: &ACME{Name: "foo", Type: "ACME", Claims: &Claims{DefaultTLSDur: &Duration{0}}},
  115. err: errors.New("claims: MinTLSCertDuration must be greater than 0"),
  116. }
  117. },
  118. "fail/bad-challenge": func(t *testing.T) ProvisionerValidateTest {
  119. return ProvisionerValidateTest{
  120. p: &ACME{Name: "foo", Type: "ACME", Challenges: []ACMEChallenge{HTTP_01, "zar"}},
  121. err: errors.New("acme challenge \"zar\" is not supported"),
  122. }
  123. },
  124. "fail/bad-attestation-format": func(t *testing.T) ProvisionerValidateTest {
  125. return ProvisionerValidateTest{
  126. p: &ACME{Name: "foo", Type: "ACME", AttestationFormats: []ACMEAttestationFormat{APPLE, "zar"}},
  127. err: errors.New("acme attestation format \"zar\" is not supported"),
  128. }
  129. },
  130. "fail/parse-attestation-roots": func(t *testing.T) ProvisionerValidateTest {
  131. return ProvisionerValidateTest{
  132. p: &ACME{Name: "foo", Type: "ACME", AttestationRoots: []byte("-----BEGIN CERTIFICATE-----\nZm9v\n-----END CERTIFICATE-----")},
  133. err: errors.New("error parsing attestationRoots: malformed certificate"),
  134. }
  135. },
  136. "fail/empty-attestation-roots": func(t *testing.T) ProvisionerValidateTest {
  137. return ProvisionerValidateTest{
  138. p: &ACME{Name: "foo", Type: "ACME", AttestationRoots: []byte("\n")},
  139. err: errors.New("error parsing attestationRoots: no certificates found"),
  140. }
  141. },
  142. "fail/wire-missing-options": func(t *testing.T) ProvisionerValidateTest {
  143. return ProvisionerValidateTest{
  144. p: &ACME{
  145. Name: "foo",
  146. Type: "ACME",
  147. Challenges: []ACMEChallenge{WIREOIDC_01, WIREDPOP_01},
  148. },
  149. err: errors.New("failed initializing Wire options: failed getting Wire options: no options available"),
  150. }
  151. },
  152. "fail/wire-missing-wire-options": func(t *testing.T) ProvisionerValidateTest {
  153. return ProvisionerValidateTest{
  154. p: &ACME{
  155. Name: "foo",
  156. Type: "ACME",
  157. Challenges: []ACMEChallenge{WIREOIDC_01, WIREDPOP_01},
  158. Options: &Options{},
  159. },
  160. err: errors.New("failed initializing Wire options: failed getting Wire options: no Wire options available"),
  161. }
  162. },
  163. "fail/wire-validate-options": func(t *testing.T) ProvisionerValidateTest {
  164. return ProvisionerValidateTest{
  165. p: &ACME{
  166. Name: "foo",
  167. Type: "ACME",
  168. Challenges: []ACMEChallenge{WIREOIDC_01, WIREDPOP_01},
  169. Options: &Options{
  170. Wire: &wire.Options{
  171. OIDC: &wire.OIDCOptions{},
  172. DPOP: &wire.DPOPOptions{
  173. SigningKey: fakeWireDPoPKey,
  174. },
  175. },
  176. },
  177. },
  178. err: errors.New("failed initializing Wire options: failed validating Wire options: failed initializing OIDC options: provider not set"),
  179. }
  180. },
  181. "ok": func(t *testing.T) ProvisionerValidateTest {
  182. return ProvisionerValidateTest{
  183. p: &ACME{Name: "foo", Type: "ACME"},
  184. }
  185. },
  186. "ok/attestation": func(t *testing.T) ProvisionerValidateTest {
  187. return ProvisionerValidateTest{
  188. p: &ACME{
  189. Name: "foo",
  190. Type: "ACME",
  191. Challenges: []ACMEChallenge{DNS_01, DEVICE_ATTEST_01},
  192. AttestationFormats: []ACMEAttestationFormat{APPLE, STEP},
  193. AttestationRoots: bytes.Join([][]byte{appleCA, yubicoCA}, []byte("\n")),
  194. },
  195. }
  196. },
  197. "ok/wire": func(t *testing.T) ProvisionerValidateTest {
  198. return ProvisionerValidateTest{
  199. p: &ACME{
  200. Name: "foo",
  201. Type: "ACME",
  202. Challenges: []ACMEChallenge{WIREOIDC_01, WIREDPOP_01},
  203. Options: &Options{
  204. Wire: &wire.Options{
  205. OIDC: &wire.OIDCOptions{
  206. Provider: &wire.Provider{
  207. IssuerURL: "https://issuer.example.com",
  208. },
  209. },
  210. DPOP: &wire.DPOPOptions{
  211. SigningKey: fakeWireDPoPKey,
  212. },
  213. },
  214. },
  215. },
  216. }
  217. },
  218. }
  219. config := Config{
  220. Claims: globalProvisionerClaims,
  221. Audiences: testAudiences,
  222. }
  223. for name, get := range tests {
  224. t.Run(name, func(t *testing.T) {
  225. tc := get(t)
  226. t.Log(string(tc.p.AttestationRoots))
  227. err := tc.p.Init(config)
  228. if tc.err != nil {
  229. assert.EqualError(t, err, tc.err.Error())
  230. return
  231. }
  232. assert.NoError(t, err)
  233. })
  234. }
  235. }
  236. func TestACME_AuthorizeRenew(t *testing.T) {
  237. now := time.Now().Truncate(time.Second)
  238. type test struct {
  239. p *ACME
  240. cert *x509.Certificate
  241. err error
  242. code int
  243. }
  244. tests := map[string]func(*testing.T) test{
  245. "fail/renew-disabled": func(t *testing.T) test {
  246. p, err := generateACME()
  247. require.NoError(t, err)
  248. // disable renewal
  249. disable := true
  250. p.Claims = &Claims{DisableRenewal: &disable}
  251. p.ctl.Claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
  252. require.NoError(t, err)
  253. return test{
  254. p: p,
  255. cert: &x509.Certificate{
  256. NotBefore: now,
  257. NotAfter: now.Add(time.Hour),
  258. },
  259. code: http.StatusUnauthorized,
  260. err: fmt.Errorf("renew is disabled for provisioner '%s'", p.GetName()),
  261. }
  262. },
  263. "ok": func(t *testing.T) test {
  264. p, err := generateACME()
  265. require.NoError(t, err)
  266. return test{
  267. p: p,
  268. cert: &x509.Certificate{
  269. NotBefore: now,
  270. NotAfter: now.Add(time.Hour),
  271. },
  272. }
  273. },
  274. }
  275. for name, tt := range tests {
  276. t.Run(name, func(t *testing.T) {
  277. tc := tt(t)
  278. err := tc.p.AuthorizeRenew(context.Background(), tc.cert)
  279. if tc.err != nil {
  280. if assert.Implements(t, (*render.StatusCodedError)(nil), err) {
  281. var sc render.StatusCodedError
  282. if errors.As(err, &sc) {
  283. assert.Equal(t, tc.code, sc.StatusCode())
  284. }
  285. }
  286. assert.EqualError(t, err, tc.err.Error())
  287. return
  288. }
  289. assert.NoError(t, err)
  290. })
  291. }
  292. }
  293. func TestACME_AuthorizeSign(t *testing.T) {
  294. type test struct {
  295. p *ACME
  296. token string
  297. code int
  298. err error
  299. }
  300. tests := map[string]func(*testing.T) test{
  301. "ok": func(t *testing.T) test {
  302. p, err := generateACME()
  303. require.NoError(t, err)
  304. return test{
  305. p: p,
  306. token: "foo",
  307. }
  308. },
  309. }
  310. for name, tt := range tests {
  311. t.Run(name, func(t *testing.T) {
  312. tc := tt(t)
  313. opts, err := tc.p.AuthorizeSign(context.Background(), tc.token)
  314. if tc.err != nil {
  315. if assert.Implements(t, (*render.StatusCodedError)(nil), err) {
  316. var sc render.StatusCodedError
  317. if errors.As(err, &sc) {
  318. assert.Equal(t, tc.code, sc.StatusCode())
  319. }
  320. }
  321. assert.EqualError(t, err, tc.err.Error())
  322. return
  323. }
  324. assert.NoError(t, err)
  325. if assert.NotNil(t, opts) {
  326. assert.Len(t, opts, 8) // number of SignOptions returned
  327. for _, o := range opts {
  328. switch v := o.(type) {
  329. case *ACME:
  330. case *provisionerExtensionOption:
  331. assert.Equal(t, v.Type, TypeACME)
  332. assert.Equal(t, v.Name, tc.p.GetName())
  333. assert.Equal(t, v.CredentialID, "")
  334. assert.Len(t, v.KeyValuePairs, 0)
  335. case *forceCNOption:
  336. assert.Equal(t, v.ForceCN, tc.p.ForceCN)
  337. case profileDefaultDuration:
  338. assert.Equal(t, time.Duration(v), tc.p.ctl.Claimer.DefaultTLSCertDuration())
  339. case defaultPublicKeyValidator:
  340. case *validityValidator:
  341. assert.Equal(t, v.min, tc.p.ctl.Claimer.MinTLSCertDuration())
  342. assert.Equal(t, v.max, tc.p.ctl.Claimer.MaxTLSCertDuration())
  343. case *x509NamePolicyValidator:
  344. assert.Equal(t, nil, v.policyEngine)
  345. case *WebhookController:
  346. assert.Len(t, v.webhooks, 0)
  347. default:
  348. require.NoError(t, fmt.Errorf("unexpected sign option of type %T", v))
  349. }
  350. }
  351. }
  352. })
  353. }
  354. }
  355. func TestACME_IsChallengeEnabled(t *testing.T) {
  356. ctx := context.Background()
  357. type fields struct {
  358. Challenges []ACMEChallenge
  359. }
  360. type args struct {
  361. ctx context.Context
  362. challenge ACMEChallenge
  363. }
  364. tests := []struct {
  365. name string
  366. fields fields
  367. args args
  368. want bool
  369. }{
  370. {"ok http-01", fields{nil}, args{ctx, HTTP_01}, true},
  371. {"ok dns-01", fields{nil}, args{ctx, DNS_01}, true},
  372. {"ok tls-alpn-01", fields{[]ACMEChallenge{}}, args{ctx, TLS_ALPN_01}, true},
  373. {"fail device-attest-01", fields{[]ACMEChallenge{}}, args{ctx, "device-attest-01"}, false},
  374. {"ok http-01 enabled", fields{[]ACMEChallenge{"http-01"}}, args{ctx, "HTTP-01"}, true},
  375. {"ok dns-01 enabled", fields{[]ACMEChallenge{"http-01", "dns-01"}}, args{ctx, DNS_01}, true},
  376. {"ok tls-alpn-01 enabled", fields{[]ACMEChallenge{"http-01", "dns-01", "tls-alpn-01"}}, args{ctx, TLS_ALPN_01}, true},
  377. {"ok device-attest-01 enabled", fields{[]ACMEChallenge{"device-attest-01", "dns-01"}}, args{ctx, DEVICE_ATTEST_01}, true},
  378. {"ok wire-oidc-01 enabled", fields{[]ACMEChallenge{"wire-oidc-01"}}, args{ctx, WIREOIDC_01}, true},
  379. {"ok wire-dpop-01 enabled", fields{[]ACMEChallenge{"wire-dpop-01"}}, args{ctx, WIREDPOP_01}, true},
  380. {"fail http-01", fields{[]ACMEChallenge{"dns-01"}}, args{ctx, "http-01"}, false},
  381. {"fail dns-01", fields{[]ACMEChallenge{"http-01", "tls-alpn-01"}}, args{ctx, "dns-01"}, false},
  382. {"fail tls-alpn-01", fields{[]ACMEChallenge{"http-01", "dns-01", "device-attest-01"}}, args{ctx, "tls-alpn-01"}, false},
  383. {"fail device-attest-01", fields{[]ACMEChallenge{"http-01", "dns-01"}}, args{ctx, "device-attest-01"}, false},
  384. {"fail wire-oidc-01", fields{[]ACMEChallenge{"http-01", "dns-01"}}, args{ctx, "wire-oidc-01"}, false},
  385. {"fail wire-dpop-01", fields{[]ACMEChallenge{"http-01", "dns-01"}}, args{ctx, "wire-dpop-01"}, false},
  386. {"fail unknown", fields{[]ACMEChallenge{"http-01", "dns-01", "tls-alpn-01", "device-attest-01"}}, args{ctx, "unknown"}, false},
  387. }
  388. for _, tt := range tests {
  389. t.Run(tt.name, func(t *testing.T) {
  390. p := &ACME{
  391. Challenges: tt.fields.Challenges,
  392. }
  393. got := p.IsChallengeEnabled(tt.args.ctx, tt.args.challenge)
  394. assert.Equal(t, tt.want, got)
  395. })
  396. }
  397. }
  398. func TestACME_IsAttestationFormatEnabled(t *testing.T) {
  399. ctx := context.Background()
  400. type fields struct {
  401. AttestationFormats []ACMEAttestationFormat
  402. }
  403. type args struct {
  404. ctx context.Context
  405. format ACMEAttestationFormat
  406. }
  407. tests := []struct {
  408. name string
  409. fields fields
  410. args args
  411. want bool
  412. }{
  413. {"ok", fields{[]ACMEAttestationFormat{APPLE, STEP, TPM}}, args{ctx, TPM}, true},
  414. {"ok empty apple", fields{nil}, args{ctx, APPLE}, true},
  415. {"ok empty step", fields{nil}, args{ctx, STEP}, true},
  416. {"ok empty tpm", fields{[]ACMEAttestationFormat{}}, args{ctx, "tpm"}, true},
  417. {"ok uppercase", fields{[]ACMEAttestationFormat{APPLE, STEP, TPM}}, args{ctx, "STEP"}, true},
  418. {"fail apple", fields{[]ACMEAttestationFormat{STEP, TPM}}, args{ctx, APPLE}, false},
  419. {"fail step", fields{[]ACMEAttestationFormat{APPLE, TPM}}, args{ctx, STEP}, false},
  420. {"fail step", fields{[]ACMEAttestationFormat{APPLE, STEP}}, args{ctx, TPM}, false},
  421. }
  422. for _, tt := range tests {
  423. t.Run(tt.name, func(t *testing.T) {
  424. p := &ACME{
  425. AttestationFormats: tt.fields.AttestationFormats,
  426. }
  427. got := p.IsAttestationFormatEnabled(tt.args.ctx, tt.args.format)
  428. assert.Equal(t, tt.want, got)
  429. })
  430. }
  431. }