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

Add allowed_serial_numbers support #1119

Closed
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
36 changes: 36 additions & 0 deletions vault/resource_pki_secret_backend_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ func pkiSecretBackendRoleResource() *schema.Resource {
Description: "Specifies the duration by which to backdate the NotBefore property.",
ValidateFunc: validateDuration,
},
"allowed_serial_numbers": {
Type: schema.TypeList,
Required: false,
Optional: true,
Description: "Defines allowed Subject serial numbers.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -350,6 +359,12 @@ func pkiSecretBackendRoleCreate(d *schema.ResourceData, meta interface{}) error
policyIdentifiers = append(policyIdentifiers, iIdentifier.(string))
}

iAllowedSerialNumbers := d.Get("allowed_serial_numbers").([]interface{})
allowedSerialNumbers := make([]string, 0, len(iAllowedSerialNumbers))
for _, iSerialNumber := range iAllowedSerialNumbers {
allowedSerialNumbers = append(allowedSerialNumbers, iSerialNumber.(string))
}

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

if len(allowedSerialNumbers) > 0 {
data["allowed_serial_numbers"] = allowedSerialNumbers
}

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 @@ -473,6 +492,12 @@ func pkiSecretBackendRoleRead(d *schema.ResourceData, meta interface{}) error {

notBeforeDuration := flattenVaultDuration(secret.Data["not_before_duration"])

iAllowedSerialNumbers := secret.Data["allowed_serial_numbers"].([]interface{})
allowedSerialNumbers := make([]string, 0, len(iAllowedSerialNumbers))
for _, iSerialNumber := range iAllowedSerialNumbers {
allowedSerialNumbers = append(allowedSerialNumbers, iSerialNumber.(string))
}

d.Set("backend", backend)
d.Set("name", name)
d.Set("ttl", secret.Data["ttl"])
Expand Down Expand Up @@ -511,6 +536,7 @@ func pkiSecretBackendRoleRead(d *schema.ResourceData, meta interface{}) error {
d.Set("policy_identifiers", policyIdentifiers)
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)

return nil
}
Expand Down Expand Up @@ -545,6 +571,12 @@ func pkiSecretBackendRoleUpdate(d *schema.ResourceData, meta interface{}) error
policyIdentifiers = append(policyIdentifiers, iIdentifier.(string))
}

iAllowedSerialNumbers := d.Get("allowed_serial_numbers").([]interface{})
allowedSerialNumbers := make([]string, 0, len(iAllowedSerialNumbers))
for _, iSerialNumber := range iAllowedSerialNumbers {
allowedSerialNumbers = append(allowedSerialNumbers, iSerialNumber.(string))
}

data := map[string]interface{}{
"ttl": d.Get("ttl"),
"max_ttl": d.Get("max_ttl"),
Expand Down Expand Up @@ -596,6 +628,10 @@ func pkiSecretBackendRoleUpdate(d *schema.ResourceData, meta interface{}) error
data["policy_identifiers"] = policyIdentifiers
}

if len(allowedSerialNumbers) > 0 {
data["allowed_serial_numbers"] = allowedSerialNumbers
}

_, err := client.Logical().Write(path, data)
if err != nil {
return fmt.Errorf("error updating PKI secret backend role %q: %s", path, err)
Expand Down
4 changes: 4 additions & 0 deletions vault/resource_pki_secret_backend_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestPkiSecretBackendRole_basic(t *testing.T) {
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "policy_identifiers.0", "1.2.3.4"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "basic_constraints_valid_for_non_ca", "false"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "not_before_duration", "45m"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "allowed_serial_numbers.0", "*"),
),
},
{
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestPkiSecretBackendRole_basic(t *testing.T) {
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "policy_identifiers.0", "1.2.3.4"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "basic_constraints_valid_for_non_ca", "false"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "not_before_duration", "45m"),
resource.TestCheckResourceAttr("vault_pki_secret_backend_role.test", "allowed_serial_numbers.0", "*"),
),
},
},
Expand Down Expand Up @@ -165,6 +167,7 @@ resource "vault_pki_secret_backend_role" "test" {
policy_identifiers = ["1.2.3.4"]
basic_constraints_valid_for_non_ca = false
not_before_duration = "45m"
allowed_serial_numbers = ["*"]
}`, path, name)
}

Expand Down Expand Up @@ -214,6 +217,7 @@ resource "vault_pki_secret_backend_role" "test" {
policy_identifiers = ["1.2.3.4"]
basic_constraints_valid_for_non_ca = false
not_before_duration = "45m"
allowed_serial_numbers = ["*"]
}`, path, name)
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/pki_secret_backend_role.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ The following arguments are supported:

* `not_before_duration` - (Optional) Specifies the duration by which to backdate the NotBefore property.

* `allowed_serial_numbers` - (Optional) An array of allowed serial numbers to put in Subject

## Attributes Reference

No additional attributes are exported by this resource.
Expand Down