acme_test.go 14 KB

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