Skip to content

Commit

Permalink
secret/ssh: add support for allow_empty_principals on roles (#2354)
Browse files Browse the repository at this point in the history
* secret/ssh: add support for allow_empty_principals on roles

* changelog

* ignore import field

* remove comment
  • Loading branch information
fairclothjm authored Nov 7, 2024
1 parent 1351ad1 commit d8d1353
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FEATURES:
* Update `vault_database_secret_backend_connection` to support skip_verification config for Cassandra ([#2346](https://github.com/hashicorp/terraform-provider-vault/pull/2346))
* Update `vault_approle_auth_backend_role_secret_id` to support `num_uses` and `ttl` fields ([#2345](https://github.com/hashicorp/terraform-provider-vault/pull/2345))
* Add support for `use_annotations_as_alias_metadata` field for the `vault_kubernetes_auth_backend_config` resource ([#2206](https://github.com/hashicorp/terraform-provider-vault/pull/2206))
* Add support for `allow_empty_principals` field for the `vault_ssh_secret_backend_role` resource ([#2354](https://github.com/hashicorp/terraform-provider-vault/pull/2354))

## 4.4.0 (Aug 7, 2024)

Expand Down
16 changes: 14 additions & 2 deletions vault/resource_ssh_secret_backend_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ func sshSecretBackendRoleResource() *schema.Resource {
Optional: true,
Computed: true,
},
"allow_empty_principals": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
}

return &schema.Resource{
Expand Down Expand Up @@ -261,6 +266,9 @@ func sshSecretBackendRoleWrite(d *schema.ResourceData, meta interface{}) error {

data["allowed_domains_template"] = d.Get("allowed_domains_template")
}
if provider.IsAPISupported(meta, provider.VaultVersion117) {
data["allow_empty_principals"] = d.Get("allow_empty_principals").(bool)
}

if v, ok := d.GetOk("key_id_format"); ok {
data["key_id_format"] = v.(string)
Expand Down Expand Up @@ -359,9 +367,13 @@ func sshSecretBackendRoleRead(d *schema.ResourceData, meta interface{}) error {
if provider.IsAPISupported(meta, provider.VaultVersion112) {
fields = append(fields, []string{"default_user_template", "allowed_domains_template"}...)
}
if provider.IsAPISupported(meta, provider.VaultVersion117) {
fields = append(fields, []string{"allow_empty_principals"}...)
}

// cidr_list cannot be read from the API
// potential for drift here
// cannot be read from the API, potential for drift here:
// - cidr_list
// - allow_empty_principals
for _, k := range fields {
if err := d.Set(k, role.Data[k]); err != nil {
return err
Expand Down
52 changes: 12 additions & 40 deletions vault/resource_ssh_secret_backend_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestAccSSHSecretBackendRole(t *testing.T) {
// 30s is the default value vault uese.
// https://developer.hashicorp.com/vault/api-docs/secret/ssh#not_before_duration
resource.TestCheckResourceAttr(resourceName, "not_before_duration", "30"),
resource.TestCheckResourceAttr(resourceName, "allowed_domains_template", "false"),
)

updateCheckFuncs := append(commonCheckFuncs,
Expand All @@ -73,6 +74,7 @@ func TestAccSSHSecretBackendRole(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "ttl", "43200"),
// 50m (3000 seconds)
resource.TestCheckResourceAttr(resourceName, "not_before_duration", "3000"),
resource.TestCheckResourceAttr(resourceName, "allowed_domains_template", "true"),
)

getCheckFuncs := func(isUpdate bool) resource.TestCheckFunc {
Expand All @@ -84,19 +86,6 @@ func TestAccSSHSecretBackendRole(t *testing.T) {
checks = append(checks, initialCheckFuncs...)
}

meta := testProvider.Meta().(*provider.ProviderMeta)
isVaultVersion112 := meta.IsAPISupported(provider.VaultVersion112)
if isVaultVersion112 {
if isUpdate {
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "allowed_domains_template", "true"),
)
} else {
checks = append(checks,
resource.TestCheckResourceAttr(resourceName, "allowed_domains_template", "false"),
)
}
}
return resource.ComposeAggregateTestCheckFunc(checks...)(state)
}
}
Expand Down Expand Up @@ -129,35 +118,17 @@ func TestAccSSHSecretBackendRole(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "allowed_user_key_config.1.lengths.0", "256"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
testutil.GetImportTestStep(resourceName, false, nil, "allow_empty_principals"),
}
}

t.Run("vault-1.11-and-below", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
PreCheck: func() {
testutil.TestAccPreCheck(t)
SkipIfAPIVersionGTE(t, testProvider.Meta(), provider.VaultVersion112)
},
CheckDestroy: testAccSSHSecretBackendRoleCheckDestroy,
Steps: getSteps(""),
})
})
t.Run("vault-1.12-and-up", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
PreCheck: func() {
testutil.TestAccPreCheck(t)
SkipIfAPIVersionLT(t, testProvider.Meta(), provider.VaultVersion112)
},
CheckDestroy: testAccSSHSecretBackendRoleCheckDestroy,
Steps: getSteps("allowed_domains_template = true"),
})
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
PreCheck: func() {
testutil.TestAccPreCheck(t)
},
CheckDestroy: testAccSSHSecretBackendRoleCheckDestroy,
Steps: getSteps(""),
})
}

Expand Down Expand Up @@ -205,7 +176,7 @@ func TestAccSSHSecretBackendRole_template(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "default_user_template", "true"),
),
},
testutil.GetImportTestStep(resourceName, false, nil),
testutil.GetImportTestStep(resourceName, false, nil, "allow_empty_principals"),
},
})
}
Expand Down Expand Up @@ -289,6 +260,7 @@ resource "vault_ssh_secret_backend_role" "test_role" {
allow_user_key_ids = true
allowed_critical_options = "foo,bar"
allowed_domains = "example.com,foo.com"
allowed_domains_template = true
allowed_extensions = "ext1,ext2"
default_extensions = { "ext1" = "" }
default_critical_options = { "opt1" = "" }
Expand Down

0 comments on commit d8d1353

Please sign in to comment.