Skip to content

Commit fd1cacc

Browse files
committed
Validate policies can contain comments, be JSON
Uncommenting the JSON test fails presently: --- FAIL: TestPolicy_Parse (0.00s) --- FAIL: TestPolicy_Parse/JSON (0.00s) policy_test.go:247: value: &vault.PathRules{Path:"test/types", Policy:"", Permissions:(*vault.ACLPermissions)(0xc000467570), IsPrefix:false, HasSegmentWildcards:false, Capabilities:[]string{"create", "sudo"}, MinWrappingTTLHCL:interface {}(nil), MaxWrappingTTLHCL:interface {}(nil), AllowedParametersHCL:map[string][]interface {}{"int":[]interface {}{1, 2}, "map":[]interface {}{map[string]interface {}{"good":"one"}}}, DeniedParametersHCL:map[string][]interface {}{"bool":[]interface {}{}, "string":[]interface {}{"test"}}, RequiredParametersHCL:[]string(nil), MFAMethodsHCL:[]string(nil), PaginationLimitHCL:0} policy_test.go:494: [slice[8].Permissions.DeniedParameters.map[bool].slice[0]: <no value> != false slice[8].DeniedParametersHCL.map[bool].slice[0]: <no value> != false] FAIL FAIL github.com/openbao/openbao/vault 0.021s FAIL See also: hashicorp/hcl#740 See also: hashicorp/hcl#741 Signed-off-by: Alexander Scheel <[email protected]>
1 parent be127c2 commit fd1cacc

File tree

1 file changed

+133
-4
lines changed

1 file changed

+133
-4
lines changed

vault/policy_test.go

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ path "stage/*" {
2626
# Limited read privilege to production
2727
path "prod/version" {
2828
policy = "read"
29+
comment = "this comment is stored but not parsed"
2930
}
3031
# Read access to foobar
3132
# Also tests stripping of leading slash and parsing of min/max as string and
@@ -120,12 +121,140 @@ path "unpaginated-kv/metadata" {
120121
}
121122
`)
122123

124+
var rawPolicyJSON = strings.TrimSpace(`
125+
{
126+
"name": "dev",
127+
"path": {
128+
"*": {
129+
"policy": "deny"
130+
},
131+
"stage/*": {
132+
"policy": "sudo"
133+
},
134+
"prod/version": {
135+
"policy": "read",
136+
"comment": "this comment is stored but not parsed"
137+
},
138+
"/foo/bar": {
139+
"policy": "read",
140+
"min_wrapping_ttl": 300,
141+
"max_wrapping_ttl": "1h"
142+
},
143+
"foo/bar": {
144+
"capabilities": ["create", "sudo"],
145+
"min_wrapping_ttl": "300s",
146+
"max_wrapping_ttl": 3600
147+
},
148+
"foo/bar": {
149+
"capabilities": ["create", "sudo"],
150+
"allowed_parameters": {
151+
"zip": [],
152+
"zap": []
153+
}
154+
},
155+
"baz/bar": {
156+
"capabilities": ["create", "sudo"],
157+
"denied_parameters": {
158+
"zip": [],
159+
"zap": []
160+
}
161+
},
162+
"biz/bar": {
163+
"capabilities": ["create", "sudo"],
164+
"allowed_parameters": {
165+
"zim": [],
166+
"zam": []
167+
},
168+
"denied_parameters": {
169+
"zip": [],
170+
"zap": []
171+
}
172+
},
173+
"test/types": {
174+
"capabilities": ["create", "sudo"],
175+
"allowed_parameters": {
176+
"map": [{"good": "one"}],
177+
"int": [1, 2]
178+
},
179+
"denied_parameters": {
180+
"string": ["test"],
181+
"bool": [false]
182+
}
183+
},
184+
"test/req": {
185+
"capabilities": ["create", "sudo"],
186+
"required_parameters": ["foo"]
187+
},
188+
"test/patch": {
189+
"capabilities": ["patch"]
190+
},
191+
"test/scan": {
192+
"capabilities": ["scan"]
193+
},
194+
"test/mfa": {
195+
"capabilities": ["create", "sudo"],
196+
"mfa_methods": ["my_totp", "my_totp2"]
197+
},
198+
"test/+/segment": {
199+
"capabilities": ["create", "sudo"]
200+
},
201+
"test/segment/at/end/+": {
202+
"capabilities": ["create", "sudo"]
203+
},
204+
"test/segment/at/end/v2/+/": {
205+
"capabilities": ["create", "sudo"]
206+
},
207+
"test/+/wildcard/+/*": {
208+
"capabilities": ["create", "sudo"]
209+
},
210+
"test/+/wildcard/+/end*": {
211+
"capabilities": ["create", "sudo"]
212+
},
213+
"paginated-kv/metadata": {
214+
"capabilities": ["list"],
215+
"pagination_limit": 12345
216+
},
217+
"unpaginated-kv/metadata": {
218+
"capabilities": ["list"]
219+
}
220+
}
221+
}
222+
`)
223+
123224
func TestPolicy_Parse(t *testing.T) {
124-
p, err := ParseACLPolicy(namespace.RootNamespace, rawPolicy)
125-
if err != nil {
126-
t.Fatalf("err: %v", err)
127-
}
225+
t.Run("HCL", func(t *testing.T) {
226+
pHcl, err := ParseACLPolicy(namespace.RootNamespace, rawPolicy)
227+
if err != nil {
228+
t.Fatalf("err: %v", err)
229+
}
230+
231+
validatePolicy(t, pHcl)
232+
})
233+
234+
/*
235+
TODO(ascheel): When https://github.com/hashicorp/hcl/pull/741 merges, we'll
236+
want to update and uncomment this test.
237+
238+
t.Run("JSON", func(t *testing.T) {
239+
var parsed map[string]interface{}
240+
err := json.Unmarshal([]byte(rawPolicyJSON), &parsed)
241+
if err != nil {
242+
t.Fatalf("failed to parse JSON: %v", err)
243+
}
244+
245+
pJson, err := ParseACLPolicy(namespace.RootNamespace, rawPolicyJSON)
246+
if err != nil {
247+
t.Fatalf("err: %v", err)
248+
}
249+
250+
t.Logf("value: %#v", pJson.Paths[8])
251+
252+
validatePolicy(t, pJson)
253+
})
254+
*/
255+
}
128256

257+
func validatePolicy(t *testing.T, p *Policy) {
129258
if p.Name != "dev" {
130259
t.Fatalf("bad name: %q", p.Name)
131260
}

0 commit comments

Comments
 (0)