acme_test.go 11 KB

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