Skip to content

Commit

Permalink
Move ACL templated policies to hcl files (#18853)
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodingenthusiast authored Sep 18, 2023
1 parent 087539f commit 49cb842
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .copywrite.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ project {
"agent/grpc-middleware/rate_limit_mappings.gen.go",
"agent/uiserver/dist/**",

# ignoring policy embedded files
"agent/structs/acltemplatedpolicy/policies/ce/**",

# licensed under MPL - ignoring for now until the copywrite tool can support
# multiple licenses per repo.
"sdk/**",
Expand Down
2 changes: 1 addition & 1 deletion agent/acl_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ func TestACL_HTTP(t *testing.T) {

var templatedPolicy api.ACLTemplatedPolicyResponse
require.NoError(t, json.NewDecoder(resp.Body).Decode(&templatedPolicy))
require.Equal(t, structs.ACLTemplatedPolicyDNSSchema, templatedPolicy.Schema)
require.Equal(t, structs.ACLTemplatedPolicyNoRequiredVariablesSchema, templatedPolicy.Schema)
require.Equal(t, api.ACLTemplatedPolicyDNSName, templatedPolicy.TemplateName)
require.Equal(t, structs.ACLTemplatedPolicyDNS, templatedPolicy.Template)
})
Expand Down
5 changes: 3 additions & 2 deletions agent/structs/acl_templated_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const (
ACLTemplatedPolicyServiceID = "00000000-0000-0000-0000-000000000003"
ACLTemplatedPolicyNodeID = "00000000-0000-0000-0000-000000000004"
ACLTemplatedPolicyDNSID = "00000000-0000-0000-0000-000000000005"
ACLTemplatedPolicyDNSSchema = "" // empty schema as it does not require variables

ACLTemplatedPolicyNoRequiredVariablesSchema = "" // catch-all schema for all templated policy that don't require a schema
)

// ACLTemplatedPolicyBase contains basic information about builtin templated policies
Expand Down Expand Up @@ -63,7 +64,7 @@ var (
api.ACLTemplatedPolicyDNSName: {
TemplateID: ACLTemplatedPolicyDNSID,
TemplateName: api.ACLTemplatedPolicyDNSName,
Schema: ACLTemplatedPolicyDNSSchema,
Schema: ACLTemplatedPolicyNoRequiredVariablesSchema,
Template: ACLTemplatedPolicyDNS,
},
}
Expand Down
40 changes: 8 additions & 32 deletions agent/structs/acl_templated_policy_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,16 @@

package structs

const (
ACLTemplatedPolicyService = `
service "{{.Name}}" {
policy = "write"
}
service "{{.Name}}-sidecar-proxy" {
policy = "write"
}
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}`
import _ "embed"

ACLTemplatedPolicyNode = `
node "{{.Name}}" {
policy = "write"
}
service_prefix "" {
policy = "read"
}`
//go:embed acltemplatedpolicy/policies/ce/service.hcl
var ACLTemplatedPolicyService string

ACLTemplatedPolicyDNS = `
node_prefix "" {
policy = "read"
}
service_prefix "" {
policy = "read"
}
query_prefix "" {
policy = "read"
}`
)
//go:embed acltemplatedpolicy/policies/ce/node.hcl
var ACLTemplatedPolicyNode string

//go:embed acltemplatedpolicy/policies/ce/dns.hcl
var ACLTemplatedPolicyDNS string

func (t *ACLToken) TemplatedPolicyList() []*ACLTemplatedPolicy {
if len(t.TemplatedPolicies) == 0 {
Expand Down
10 changes: 10 additions & 0 deletions agent/structs/acltemplatedpolicy/policies/ce/dns.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

node_prefix "" {
policy = "read"
}
service_prefix "" {
policy = "read"
}
query_prefix "" {
policy = "read"
}
7 changes: 7 additions & 0 deletions agent/structs/acltemplatedpolicy/policies/ce/node.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

node "{{.Name}}" {
policy = "write"
}
service_prefix "" {
policy = "read"
}
13 changes: 13 additions & 0 deletions agent/structs/acltemplatedpolicy/policies/ce/service.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

service "{{.Name}}" {
policy = "write"
}
service "{{.Name}}-sidecar-proxy" {
policy = "write"
}
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}
4 changes: 2 additions & 2 deletions command/acl/templatedpolicy/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func testFormatTemplatedPolicy(t *testing.T, dirPath string) {
"dns-templated-policy": {
templatedPolicy: api.ACLTemplatedPolicyResponse{
TemplateName: api.ACLTemplatedPolicyDNSName,
Schema: structs.ACLTemplatedPolicyDNSSchema,
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
Template: structs.ACLTemplatedPolicyDNS,
},
},
Expand Down Expand Up @@ -94,7 +94,7 @@ func testFormatTemplatedPolicyList(t *testing.T, dirPath string) {
},
"builtin/dns": {
TemplateName: api.ACLTemplatedPolicyDNSName,
Schema: structs.ACLTemplatedPolicyDNSSchema,
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
Template: structs.ACLTemplatedPolicyDNS,
},
"builtin/service": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func TestTemplatedPolicyListCommand_JSON(t *testing.T) {
err := json.Unmarshal([]byte(output), &jsonOutput)
assert.NoError(t, err)
outputTemplate := jsonOutput[api.ACLTemplatedPolicyDNSName]
assert.Equal(t, structs.ACLTemplatedPolicyDNSSchema, outputTemplate.Schema)
assert.Equal(t, structs.ACLTemplatedPolicyNoRequiredVariablesSchema, outputTemplate.Schema)
}

0 comments on commit 49cb842

Please sign in to comment.