|
1 | 1 | package integration
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/elliptic" |
| 7 | + "crypto/rand" |
| 8 | + "strings" |
4 | 9 | "testing"
|
5 | 10 |
|
6 | 11 | "github.com/caddyserver/caddy/v2/caddytest"
|
| 12 | + "github.com/mholt/acmez" |
| 13 | + "github.com/mholt/acmez/acme" |
| 14 | + "go.uber.org/zap" |
7 | 15 | )
|
8 | 16 |
|
9 | 17 | func TestACMEServerDirectory(t *testing.T) {
|
@@ -31,3 +39,171 @@ func TestACMEServerDirectory(t *testing.T) {
|
31 | 39 | `{"newNonce":"https://acme.localhost:9443/acme/local/new-nonce","newAccount":"https://acme.localhost:9443/acme/local/new-account","newOrder":"https://acme.localhost:9443/acme/local/new-order","revokeCert":"https://acme.localhost:9443/acme/local/revoke-cert","keyChange":"https://acme.localhost:9443/acme/local/key-change"}
|
32 | 40 | `)
|
33 | 41 | }
|
| 42 | + |
| 43 | +func TestACMEServerAllowPolicy(t *testing.T) { |
| 44 | + tester := caddytest.NewTester(t) |
| 45 | + tester.InitServer(` |
| 46 | + { |
| 47 | + skip_install_trust |
| 48 | + local_certs |
| 49 | + admin localhost:2999 |
| 50 | + http_port 9080 |
| 51 | + https_port 9443 |
| 52 | + pki { |
| 53 | + ca local { |
| 54 | + name "Caddy Local Authority" |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + acme.localhost { |
| 59 | + acme_server { |
| 60 | + challenges http-01 |
| 61 | + allow { |
| 62 | + domains localhost |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + `, "caddyfile") |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + logger, err := zap.NewDevelopment() |
| 70 | + if err != nil { |
| 71 | + t.Error(err) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + client := acmez.Client{ |
| 76 | + Client: &acme.Client{ |
| 77 | + Directory: "https://acme.localhost:9443/acme/local/directory", |
| 78 | + HTTPClient: tester.Client, |
| 79 | + Logger: logger, |
| 80 | + }, |
| 81 | + ChallengeSolvers: map[string]acmez.Solver{ |
| 82 | + acme.ChallengeTypeHTTP01: &naiveHTTPSolver{logger: logger}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + accountPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 87 | + if err != nil { |
| 88 | + t.Errorf("generating account key: %v", err) |
| 89 | + } |
| 90 | + account := acme.Account{ |
| 91 | + Contact: [] string{ "mailto:[email protected]"}, |
| 92 | + TermsOfServiceAgreed: true, |
| 93 | + PrivateKey: accountPrivateKey, |
| 94 | + } |
| 95 | + account, err = client.NewAccount(ctx, account) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("new account: %v", err) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Every certificate needs a key. |
| 102 | + certPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 103 | + if err != nil { |
| 104 | + t.Errorf("generating certificate key: %v", err) |
| 105 | + return |
| 106 | + } |
| 107 | + { |
| 108 | + certs, err := client.ObtainCertificate( |
| 109 | + ctx, |
| 110 | + account, |
| 111 | + certPrivateKey, |
| 112 | + []string{"localhost"}, |
| 113 | + ) |
| 114 | + if err != nil { |
| 115 | + t.Errorf("obtaining certificate for allowed domain: %v", err) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // ACME servers should usually give you the entire certificate chain |
| 120 | + // in PEM format, and sometimes even alternate chains! It's up to you |
| 121 | + // which one(s) to store and use, but whatever you do, be sure to |
| 122 | + // store the certificate and key somewhere safe and secure, i.e. don't |
| 123 | + // lose them! |
| 124 | + for _, cert := range certs { |
| 125 | + t.Logf("Certificate %q:\n%s\n\n", cert.URL, cert.ChainPEM) |
| 126 | + } |
| 127 | + } |
| 128 | + { |
| 129 | + _, err := client.ObtainCertificate(ctx, account, certPrivateKey, []string{"not-matching.localhost"}) |
| 130 | + if err == nil { |
| 131 | + t.Errorf("obtaining certificate for 'not-matching.localhost' domain") |
| 132 | + } else if err != nil && !strings.Contains(err.Error(), "urn:ietf:params:acme:error:rejectedIdentifier") { |
| 133 | + t.Logf("unexpected error: %v", err) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestACMEServerDenyPolicy(t *testing.T) { |
| 139 | + tester := caddytest.NewTester(t) |
| 140 | + tester.InitServer(` |
| 141 | + { |
| 142 | + skip_install_trust |
| 143 | + local_certs |
| 144 | + admin localhost:2999 |
| 145 | + http_port 9080 |
| 146 | + https_port 9443 |
| 147 | + pki { |
| 148 | + ca local { |
| 149 | + name "Caddy Local Authority" |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + acme.localhost { |
| 154 | + acme_server { |
| 155 | + deny { |
| 156 | + domains deny.localhost |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + `, "caddyfile") |
| 161 | + |
| 162 | + ctx := context.Background() |
| 163 | + logger, err := zap.NewDevelopment() |
| 164 | + if err != nil { |
| 165 | + t.Error(err) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + client := acmez.Client{ |
| 170 | + Client: &acme.Client{ |
| 171 | + Directory: "https://acme.localhost:9443/acme/local/directory", |
| 172 | + HTTPClient: tester.Client, |
| 173 | + Logger: logger, |
| 174 | + }, |
| 175 | + ChallengeSolvers: map[string]acmez.Solver{ |
| 176 | + acme.ChallengeTypeHTTP01: &naiveHTTPSolver{logger: logger}, |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + accountPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 181 | + if err != nil { |
| 182 | + t.Errorf("generating account key: %v", err) |
| 183 | + } |
| 184 | + account := acme.Account{ |
| 185 | + Contact: [] string{ "mailto:[email protected]"}, |
| 186 | + TermsOfServiceAgreed: true, |
| 187 | + PrivateKey: accountPrivateKey, |
| 188 | + } |
| 189 | + account, err = client.NewAccount(ctx, account) |
| 190 | + if err != nil { |
| 191 | + t.Errorf("new account: %v", err) |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + // Every certificate needs a key. |
| 196 | + certPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 197 | + if err != nil { |
| 198 | + t.Errorf("generating certificate key: %v", err) |
| 199 | + return |
| 200 | + } |
| 201 | + { |
| 202 | + _, err := client.ObtainCertificate(ctx, account, certPrivateKey, []string{"deny.localhost"}) |
| 203 | + if err == nil { |
| 204 | + t.Errorf("obtaining certificate for 'deny.localhost' domain") |
| 205 | + } else if err != nil && !strings.Contains(err.Error(), "urn:ietf:params:acme:error:rejectedIdentifier") { |
| 206 | + t.Logf("unexpected error: %v", err) |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments