Skip to content

Commit

Permalink
Add cn_validations property to pki_secret_backend_role
Browse files Browse the repository at this point in the history
  • Loading branch information
bmhughes committed Apr 18, 2023
1 parent 4346ded commit 0d4f2e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 31 additions & 3 deletions vault/resource_pki_secret_backend_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,15 @@ func pkiSecretBackendRoleResource() *schema.Resource {
Required: false,
Optional: true,
Description: "Defines allowed Subject serial numbers.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Elem: &schema.Schema{Type: schema.TypeString},
},
"cn_validations": {
Type: schema.TypeList,
Required: false,
Optional: true,
Computed: true,
Description: "Specify validations to run on the Common Name field of the certificate.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
Expand Down Expand Up @@ -393,6 +399,12 @@ func pkiSecretBackendRoleCreate(d *schema.ResourceData, meta interface{}) error
allowedSerialNumbers = append(allowedSerialNumbers, iSerialNumber.(string))
}

iCnValidations := d.Get("cn_validations").([]interface{})
cnValidations := make([]string, 0, len(iCnValidations))
for _, iCnValidation := range iCnValidations {
cnValidations = append(cnValidations, iCnValidation.(string))
}

data := map[string]interface{}{
"ttl": d.Get("ttl"),
"max_ttl": d.Get("max_ttl"),
Expand Down Expand Up @@ -450,6 +462,10 @@ func pkiSecretBackendRoleCreate(d *schema.ResourceData, meta interface{}) error
data["allowed_serial_numbers"] = allowedSerialNumbers
}

if len(cnValidations) > 0 {
data["cn_validations"] = cnValidations
}

log.Printf("[DEBUG] Creating role %s on PKI secret backend %q", name, backend)
_, err := client.Logical().Write(path, data)
if err != nil {
Expand Down Expand Up @@ -580,6 +596,7 @@ func pkiSecretBackendRoleRead(d *schema.ResourceData, meta interface{}) error {
d.Set("basic_constraints_valid_for_non_ca", secret.Data["basic_constraints_valid_for_non_ca"])
d.Set("not_before_duration", notBeforeDuration)
d.Set("allowed_serial_numbers", allowedSerialNumbers)
d.Set("cn_validations", secret.Data["cn_validations"])

return nil
}
Expand Down Expand Up @@ -617,6 +634,12 @@ func pkiSecretBackendRoleUpdate(d *schema.ResourceData, meta interface{}) error
allowedSerialNumbers = append(allowedSerialNumbers, iSerialNumber.(string))
}

iCnValidations := d.Get("cn_validations").([]interface{})
cnValidations := make([]string, 0, len(iCnValidations))
for _, iCnValidation := range iCnValidations {
cnValidations = append(cnValidations, iCnValidation.(string))
}

data := map[string]interface{}{
"ttl": d.Get("ttl"),
"max_ttl": d.Get("max_ttl"),
Expand Down Expand Up @@ -650,6 +673,7 @@ func pkiSecretBackendRoleUpdate(d *schema.ResourceData, meta interface{}) error
"require_cn": d.Get("require_cn"),
"basic_constraints_valid_for_non_ca": d.Get("basic_constraints_valid_for_non_ca"),
"not_before_duration": d.Get("not_before_duration"),
"cn_validations": d.Get("cn_validations"),
}

if len(allowedDomains) > 0 {
Expand All @@ -674,6 +698,10 @@ func pkiSecretBackendRoleUpdate(d *schema.ResourceData, meta interface{}) error
data["allowed_serial_numbers"] = allowedSerialNumbers
}

if len(cnValidations) > 0 {
data["cn_validations"] = cnValidations
}

_, err := client.Logical().Write(path, data)
if err != nil {
return fmt.Errorf("error updating PKI secret backend role %q: %s", path, err)
Expand Down
2 changes: 2 additions & 0 deletions vault/resource_pki_secret_backend_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ resource "vault_pki_secret_backend_role" "test" {
basic_constraints_valid_for_non_ca = false
not_before_duration = "45m"
allowed_serial_numbers = ["*"]
cn_validations = ["email", "hostname"]
}
`, path, name, roleTTL, maxTTL, policyIdentifiers)
}
Expand Down Expand Up @@ -378,6 +379,7 @@ resource "vault_pki_secret_backend_role" "test" {
basic_constraints_valid_for_non_ca = false
not_before_duration = "45m"
allowed_serial_numbers = ["*"]
cn_validations = ["disabled"]
}`, path, name, policyIdentifiers)
}

Expand Down

0 comments on commit 0d4f2e2

Please sign in to comment.