1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
package acmeserver
import (
"reflect"
"testing"
"github.com/smallstep/certificates/authority/policy"
"github.com/smallstep/certificates/authority/provisioner"
)
func TestPolicyNormalizeAllowRules(t *testing.T) {
type fields struct {
Allow *RuleSet
Deny *RuleSet
AllowWildcardNames bool
}
tests := []struct {
name string
fields fields
want *policy.X509NameOptions
}{
{
name: "providing no rules results in 'nil'",
fields: fields{},
want: nil,
},
{
name: "providing 'nil' Allow rules results in 'nil', regardless of Deny rules",
fields: fields{
Allow: nil,
Deny: &RuleSet{},
AllowWildcardNames: true,
},
want: nil,
},
{
name: "providing empty Allow rules results in 'nil', regardless of Deny rules",
fields: fields{
Allow: &RuleSet{
Domains: []string{},
IPRanges: []string{},
},
},
want: nil,
},
{
name: "rules configured in Allow are returned in X509NameOptions",
fields: fields{
Allow: &RuleSet{
Domains: []string{"example.com"},
IPRanges: []string{"127.0.0.1/32"},
},
},
want: &policy.X509NameOptions{
DNSDomains: []string{"example.com"},
IPRanges: []string{"127.0.0.1/32"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Policy{
Allow: tt.fields.Allow,
Deny: tt.fields.Deny,
AllowWildcardNames: tt.fields.AllowWildcardNames,
}
if got := p.normalizeAllowRules(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Policy.normalizeAllowRules() = %v, want %v", got, tt.want)
}
})
}
}
func TestPolicy_normalizeDenyRules(t *testing.T) {
type fields struct {
Allow *RuleSet
Deny *RuleSet
AllowWildcardNames bool
}
tests := []struct {
name string
fields fields
want *policy.X509NameOptions
}{
{
name: "providing no rules results in 'nil'",
fields: fields{},
want: nil,
},
{
name: "providing 'nil' Deny rules results in 'nil', regardless of Allow rules",
fields: fields{
Deny: nil,
Allow: &RuleSet{},
AllowWildcardNames: true,
},
want: nil,
},
{
name: "providing empty Deny rules results in 'nil', regardless of Allow rules",
fields: fields{
Deny: &RuleSet{
Domains: []string{},
IPRanges: []string{},
},
},
want: nil,
},
{
name: "rules configured in Deny are returned in X509NameOptions",
fields: fields{
Deny: &RuleSet{
Domains: []string{"example.com"},
IPRanges: []string{"127.0.0.1/32"},
},
},
want: &policy.X509NameOptions{
DNSDomains: []string{"example.com"},
IPRanges: []string{"127.0.0.1/32"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Policy{
Allow: tt.fields.Allow,
Deny: tt.fields.Deny,
AllowWildcardNames: tt.fields.AllowWildcardNames,
}
if got := p.normalizeDenyRules(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Policy.normalizeDenyRules() = %v, want %v", got, tt.want)
}
})
}
}
func TestPolicy_normalizeRules(t *testing.T) {
tests := []struct {
name string
policy *Policy
want *provisioner.X509Options
}{
{
name: "'nil' policy results in 'nil' options",
policy: nil,
want: nil,
},
{
name: "'nil' Allow/Deny rules and disallowing wildcard names result in 'nil' X509Options",
policy: &Policy{
Allow: nil,
Deny: nil,
AllowWildcardNames: false,
},
want: nil,
},
{
name: "'nil' Allow/Deny rules and allowing wildcard names result in 'nil' Allow/Deny rules in X509Options but allowing wildcard names in X509Options",
policy: &Policy{
Allow: nil,
Deny: nil,
AllowWildcardNames: true,
},
want: &provisioner.X509Options{
AllowWildcardNames: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.policy.normalizeRules(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Policy.normalizeRules() = %v, want %v", got, tt.want)
}
})
}
}
|