@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 176 lines 4.4 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package httpcaddyfile
16
17 import (
18 "encoding/json"
19 "testing"
20 "time"
21
22 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
23 )
24
25 func TestParsePKIApp_maintenanceIntervalAndRenewalWindowRatio(t *testing.T) {
26 input := `{
27 pki {
28 ca local {
29 maintenance_interval 5m
30 renewal_window_ratio 0.15
31 }
32 }
33 }
34 :8080 {
35 }
36 `
37 adapter := caddyfile.Adapter{ServerType: ServerType{}}
38 out, _, err := adapter.Adapt([]byte(input), nil)
39 if err != nil {
40 t.Fatalf("Adapt failed: %v", err)
41 }
42
43 var cfg struct {
44 Apps struct {
45 PKI struct {
46 CertificateAuthorities map[string]struct {
47 MaintenanceInterval int64 `json:"maintenance_interval,omitempty"`
48 RenewalWindowRatio float64 `json:"renewal_window_ratio,omitempty"`
49 } `json:"certificate_authorities,omitempty"`
50 } `json:"pki,omitempty"`
51 } `json:"apps"`
52 }
53 if err := json.Unmarshal(out, &cfg); err != nil {
54 t.Fatalf("unmarshal config: %v", err)
55 }
56
57 ca, ok := cfg.Apps.PKI.CertificateAuthorities["local"]
58 if !ok {
59 t.Fatal("expected certificate_authorities.local to exist")
60 }
61 wantInterval := 5 * time.Minute.Nanoseconds()
62 if ca.MaintenanceInterval != wantInterval {
63 t.Errorf("maintenance_interval = %d, want %d (5m)", ca.MaintenanceInterval, wantInterval)
64 }
65 if ca.RenewalWindowRatio != 0.15 {
66 t.Errorf("renewal_window_ratio = %v, want 0.15", ca.RenewalWindowRatio)
67 }
68 }
69
70 // adaptedPKIConfig runs the given Caddyfile through the adapter and
71 // returns just the parts of the resulting JSON this test cares about:
72 // which CAs got a certificate_authorities entry, and whether install_trust
73 // was set to false for each.
74 func adaptedPKIConfig(t *testing.T, input string) map[string]struct {
75 InstallTrust *bool `json:"install_trust,omitempty"`
76 } {
77 t.Helper()
78
79 adapter := caddyfile.Adapter{ServerType: ServerType{}}
80 out, _, err := adapter.Adapt([]byte(input), nil)
81 if err != nil {
82 t.Fatalf("Adapt failed: %v", err)
83 }
84
85 var cfg struct {
86 Apps struct {
87 PKI struct {
88 CertificateAuthorities map[string]struct {
89 InstallTrust *bool `json:"install_trust,omitempty"`
90 } `json:"certificate_authorities,omitempty"`
91 } `json:"pki,omitempty"`
92 } `json:"apps"`
93 }
94 if err := json.Unmarshal(out, &cfg); err != nil {
95 t.Fatalf("unmarshal config: %v", err)
96 }
97 return cfg.Apps.PKI.CertificateAuthorities
98 }
99
100 func TestParsePKIApp_skipInstallTrustHonoredForInternalTLSDefaultCA(t *testing.T) {
101 input := `{
102 auto_https off
103 skip_install_trust
104 }
105 internal.example.com {
106 tls internal
107 }
108 `
109 cas := adaptedPKIConfig(t, input)
110
111 ca, ok := cas["local"]
112 if !ok {
113 t.Fatal("expected certificate_authorities.local to exist")
114 }
115 if ca.InstallTrust == nil || *ca.InstallTrust != false {
116 t.Errorf("install_trust = %v, want false", ca.InstallTrust)
117 }
118 }
119
120 func TestParsePKIApp_skipInstallTrustHonoredForInternalTLSCustomCA(t *testing.T) {
121 input := `{
122 auto_https off
123 skip_install_trust
124 }
125 internal.example.com {
126 tls {
127 issuer internal {
128 ca mycustomca
129 }
130 }
131 }
132 `
133 cas := adaptedPKIConfig(t, input)
134
135 ca, ok := cas["mycustomca"]
136 if !ok {
137 t.Fatal("expected certificate_authorities.mycustomca to exist")
138 }
139 if ca.InstallTrust == nil || *ca.InstallTrust != false {
140 t.Errorf("install_trust = %v, want false", ca.InstallTrust)
141 }
142 }
143
144 func TestParsePKIApp_noPKIAppWhenNoInternalIssuerConfigured(t *testing.T) {
145 input := `{
146 auto_https off
147 skip_install_trust
148 }
149 localhost:8080 {
150 respond "no tls internal used anywhere"
151 }
152 `
153 cas := adaptedPKIConfig(t, input)
154
155 if len(cas) != 0 {
156 t.Errorf("expected no certificate_authorities, got %v", cas)
157 }
158 }
159
160 func TestParsePKIApp_renewalWindowRatioInvalid(t *testing.T) {
161 input := `{
162 pki {
163 ca local {
164 renewal_window_ratio 1.5
165 }
166 }
167 }
168 :8080 {
169 }
170 `
171 adapter := caddyfile.Adapter{ServerType: ServerType{}}
172 _, _, err := adapter.Adapt([]byte(input), nil)
173 if err == nil {
174 t.Error("expected error for renewal_window_ratio > 1")
175 }
176 }