Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP [NET-5327] Docs for templated policies #371

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
run: make test
- name: Run OSS acceptance tests
run: |
curl -LO https://releases.hashicorp.com/consul/1.16.2/consul_1.16.2_linux_amd64.zip
sudo unzip consul_1.16.2_linux_amd64.zip consul -d /usr/local/bin
curl -LO https://releases.hashicorp.com/consul/1.17.0-rc1/consul_1.17.0-rc1_linux_amd64.zip
sudo unzip consul_1.17.0-rc1_linux_amd64.zip consul -d /usr/local/bin
SKIP_REMOTE_DATACENTER_TESTS=1 make testacc TESTARGS="-count=1"
- name: Run go vet
run: make vet
Expand Down
48 changes: 48 additions & 0 deletions consul/data_source_consul_acl_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,44 @@ func dataSourceConsulACLRole() *schema.Resource {
},
},
},
"templated_policies": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "The list of templated policies that should be applied to the token.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"template_name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the templated policies.",
},
"template_variables": {
Type: schema.TypeList,
MaxItems: 1,
Description: "The templated policy variables.",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of node, workload identity or service.",
},
},
},
},
"datacenters": {
Type: schema.TypeList,
Optional: true,
Description: "Specifies the datacenters the effective policy is valid within.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -123,13 +161,23 @@ func datasourceConsulACLRoleRead(d *schema.ResourceData, meta interface{}) error
}
}

templatedPolicies := make([]map[string]interface{}, len(role.TemplatedPolicies))
for i, tp := range role.TemplatedPolicies {
templatedPolicies[i] = map[string]interface{}{
"template_name": tp.TemplateName,
"datacenters": tp.Datacenters,
"template_variables": getTemplateVariables(tp),
}
}

d.SetId(role.ID)

sw := newStateWriter(d)
sw.set("description", role.Description)
sw.set("policies", policies)
sw.set("service_identities", identities)
sw.set("node_identities", nodeIdentities)
sw.set("templated_policies", templatedPolicies)

return sw.error()
}
21 changes: 21 additions & 0 deletions consul/data_source_consul_acl_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func TestAccDataACLRole_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.consul_acl_role.test", "service_identities.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "service_identities.0.datacenters.#", "0"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "service_identities.0.service_name", "foo"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.#", "2"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.0.datacenters.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.0.datacenters.0", "world"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.0.template_variables.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.0.template_variables.0.name", "web"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.0.template_name", "builtin/service"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.1.template_variables.#", "0"),
resource.TestCheckResourceAttr("data.consul_acl_role.test", "templated_policies.1.template_name", "builtin/dns"),
),
},
},
Expand Down Expand Up @@ -99,6 +107,19 @@ resource "consul_acl_role" "test" {
node_name = "hello"
datacenter = "world"
}

templated_policies {
template_name = "builtin/service"
datacenters = ["world"]
template_variables {
name = "web"
}
}

templated_policies {
template_name = "builtin/dns"
datacenters = ["world"]
}
}

data "consul_acl_role" "test" {
Expand Down
48 changes: 48 additions & 0 deletions consul/data_source_consul_acl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,44 @@ func dataSourceConsulACLToken() *schema.Resource {
},
},
},
"templated_policies": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "The list of templated policies that should be applied to the token.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"template_name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the templated policies.",
},
"template_variables": {
Type: schema.TypeList,
MaxItems: 1,
Description: "The templated policy variables.",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of node, workload identity or service.",
},
},
},
},
"datacenters": {
Type: schema.TypeList,
Optional: true,
Description: "Specifies the datacenters the effective policy is valid within.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"local": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -162,6 +200,15 @@ func dataSourceConsulACLTokenRead(d *schema.ResourceData, meta interface{}) erro
}
}

templatedPolicies := make([]map[string]interface{}, len(aclToken.TemplatedPolicies))
for i, tp := range aclToken.TemplatedPolicies {
templatedPolicies[i] = map[string]interface{}{
"template_name": tp.TemplateName,
"datacenters": tp.Datacenters,
"template_variables": getTemplateVariables(tp),
}
}

var expirationTime string
if aclToken.ExpirationTime != nil {
expirationTime = aclToken.ExpirationTime.Format(time.RFC3339)
Expand All @@ -176,6 +223,7 @@ func dataSourceConsulACLTokenRead(d *schema.ResourceData, meta interface{}) erro
sw.set("roles", roles)
sw.set("service_identities", serviceIdentities)
sw.set("node_identities", nodeIdentities)
sw.set("templated_policies", templatedPolicies)
sw.set("expiration_time", expirationTime)

return sw.error()
Expand Down
21 changes: 21 additions & 0 deletions consul/data_source_consul_acl_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func TestAccDataACLToken_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.consul_acl_token.read", "service_identities.0.datacenters.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "service_identities.0.datacenters.0", "world"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "service_identities.0.service_name", "hello"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.#", "2"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.0.datacenters.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.0.datacenters.0", "world"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.0.template_variables.#", "1"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.0.template_variables.0.name", "web"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.0.template_name", "builtin/service"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.1.template_variables.#", "0"),
resource.TestCheckResourceAttr("data.consul_acl_token.read", "templated_policies.1.template_name", "builtin/dns"),
),
},
},
Expand Down Expand Up @@ -90,6 +98,19 @@ resource "consul_acl_token" "test" {
node_name = "foo"
datacenter = "bar"
}

templated_policies {
template_name = "builtin/service"
datacenters = ["world"]
template_variables {
name = "web"
}
}

templated_policies {
template_name = "builtin/dns"
datacenters = ["world"]
}
}

data "consul_acl_token" "read" {
Expand Down
40 changes: 39 additions & 1 deletion consul/resource_consul_acl_binding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func resourceConsulACLBindingRule() *schema.Resource {
"bind_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies the way the binding rule affects a token created at login.",
},

Expand All @@ -49,6 +50,23 @@ func resourceConsulACLBindingRule() *schema.Resource {
Description: "The name to bind to a token at login-time.",
},

"bind_vars": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Description: "BindVars is a the variables used when binding rule type is `templated-policy`. Can be lightly templated using HIL ${foo} syntax from available field names.",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of node, workload identity or service.",
},
},
},
},

"namespace": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -102,6 +120,13 @@ func resourceConsulACLBindingRuleRead(d *schema.ResourceData, meta interface{})
sw.set("namespace", rule.Namespace)
sw.set("partition", rule.Partition)

if rule.BindVars != nil {
bindVars := []map[string]interface{}{
{"name": rule.BindVars.Name},
}
sw.set("bind_vars", bindVars)
}

return sw.error()
}

Expand Down Expand Up @@ -134,7 +159,7 @@ func resourceConsulACLBindingRuleDelete(d *schema.ResourceData, meta interface{}

func getBindingRule(d *schema.ResourceData, meta interface{}) *consulapi.ACLBindingRule {
_, _, wOpts := getClient(d, meta)
return &consulapi.ACLBindingRule{
bindingRule := &consulapi.ACLBindingRule{
ID: d.Id(),
Description: d.Get("description").(string),
AuthMethod: d.Get("auth_method").(string),
Expand All @@ -143,4 +168,17 @@ func getBindingRule(d *schema.ResourceData, meta interface{}) *consulapi.ACLBind
BindType: consulapi.BindingRuleBindType(d.Get("bind_type").(string)),
Namespace: wOpts.Namespace,
}

if bindVars, ok := d.GetOk("bind_vars.0"); ok {
tv := bindVars.(map[string]interface{})

processedVars := &consulapi.ACLTemplatedPolicyVariables{}
if tv["name"] != nil {
processedVars.Name = tv["name"].(string)
}

bindingRule.BindVars = processedVars
}

return bindingRule
}
67 changes: 67 additions & 0 deletions consul/resource_consul_acl_binding_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ func TestAccConsulACLBindingRule_basic(t *testing.T) {
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "bind_name", "minikube2"),
),
},
{
Config: testResourceACLBindingRuleConfig_templatedPolicyWithNoVariables,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "auth_method", "minikube2"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "description", ""),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "selector", "serviceaccount.namespace==default2"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "bind_type", "templated-policy"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "bind_name", "builtin/dns"),
),
},
{
Config: testResourceACLBindingRuleConfig_templatedPolicyWithVariables,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "auth_method", "minikube2"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "description", ""),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "selector", "serviceaccount.namespace==default2"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "bind_type", "templated-policy"),
resource.TestCheckResourceAttr("consul_acl_binding_rule.test", "bind_name", "builtin/service"),
),
},
{
Config: testResourceACLBindingRuleConfig_wrongType,
ExpectError: regexp.MustCompile(`Invalid Binding Rule: unknown BindType "foobar"`),
Expand Down Expand Up @@ -225,6 +245,53 @@ resource "consul_acl_binding_rule" "test" {
bind_name = "minikube2"
}`

const testResourceACLBindingRuleConfig_templatedPolicyWithNoVariables = `
resource "consul_acl_auth_method" "test" {
name = "minikube2"
type = "kubernetes"
description = "dev minikube cluster"

config = {
Host = "https://192.0.2.42:8443"
CACert = <<-EOF
` + testCert + `
EOF
ServiceAccountJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
}

resource "consul_acl_binding_rule" "test" {
auth_method = "${consul_acl_auth_method.test.name}"
selector = "serviceaccount.namespace==default2"
bind_type = "templated-policy"
bind_name = "builtin/dns"
}`

const testResourceACLBindingRuleConfig_templatedPolicyWithVariables = `
resource "consul_acl_auth_method" "test" {
name = "minikube2"
type = "kubernetes"
description = "dev minikube cluster"

config = {
Host = "https://192.0.2.42:8443"
CACert = <<-EOF
` + testCert + `
EOF
ServiceAccountJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
}

resource "consul_acl_binding_rule" "test" {
auth_method = "${consul_acl_auth_method.test.name}"
selector = "serviceaccount.namespace==default2"
bind_type = "templated-policy"
bind_name = "builtin/service"
bind_vars {
name = "api"
}
}`

const testResourceACLBindingRuleConfig_wrongType = `
resource "consul_acl_auth_method" "test" {
name = "minikube2"
Expand Down
Loading
Loading