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

Namespace by resource #1305

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6de8419
Replace api.Client with a higher level object ProviderMeta
benashz Jan 13, 2022
ae1a52b
Test out new ProviderMeta NS client code
benashz Jan 13, 2022
7bf9178
Add namespace field to all resources
benashz Jan 15, 2022
b8fa56c
Drop usaged of CallWithClient()
benashz Jan 17, 2022
d18ac0f
Document the namespace field.
benashz Jan 17, 2022
d4dc380
Add namespace field validator function
benashz Jan 17, 2022
91b76d9
Move namespace tests to dedicated Tests.
benashz Jan 17, 2022
668190d
Fix-up validator tests
benashz Jan 18, 2022
5a95244
Fix generic_secret import test.
benashz Jan 18, 2022
19abf73
Use unique mount name for all generic_secret tests
benashz Jan 18, 2022
b677d50
Update invalid path tests
benashz Jan 18, 2022
a5bca4d
Update generated code to use vault.GetClient()
benashz Jan 18, 2022
f9fd818
Move ProviderMeta to provider_meta.go
benashz Jan 21, 2022
c914284
Add ProviderMeta tests.
benashz Jan 21, 2022
32a9988
Move ProviderMeta specific functions to provider_meta
benashz Jan 21, 2022
7d8ed9d
Merge branch 'main' into VAULT-4254/namespace-poc-count
benashz Apr 12, 2022
2e4f9e3
Update all to use ProviderMeta
benashz Apr 12, 2022
81532cd
Fix up semantic merge breakage
benashz Apr 12, 2022
5f73422
Generalize validateNamespace()
benashz Apr 12, 2022
3e7080e
Add namespace support to new resources
benashz Apr 14, 2022
7f12127
Add basic evaluation example and related docs
benashz Apr 19, 2022
68c099c
Merge branch 'VAULT-5858/release-ns-enhancements' into VAULT-4254/nam…
benashz May 11, 2022
1ff0eb7
Cleanup merge stragglers
benashz May 11, 2022
5d46482
Get namespace from schema.ResourceDiff if provided.
benashz May 12, 2022
d466d76
Merge branch 'VAULT-5858/release-ns-enhancements' into VAULT-4254/nam…
benashz May 24, 2022
4aa95b7
Move ProviderMeta to its own package
benashz May 25, 2022
b7570b0
Move the consts package under internal
benashz May 25, 2022
92d2bff
Update straggler docs
benashz May 25, 2022
5262759
Add support for importing namespaced resources via env var
benashz Jun 1, 2022
d3e8694
Add import test to generic_secret NS test
benashz Jun 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ $ cd $GOPATH/src/github.com/hashicorp/terraform-provider-vault
$ make build
```

Using the provider
----------------------

Developing the Provider
---------------------------

Expand Down Expand Up @@ -94,3 +91,35 @@ If you wish to run specific tests, use the `TESTARGS` environment variable:
```sh
TESTARGS="--run DataSourceAWSAccessCredentials" make testacc
```

Using a local development build
----------------------

It's possible to use a local build of the Vault provider with Terraform directly.
This is useful when testing the provider outside the acceptance test framework.

Configure Terraform to use the development build of the provider.

> **warning**: backup your `~/.terraformrc` before running this command:

```shell
cat > ~/.terraformrc <<HERE
provider_installation {
dev_overrides {
"hashicorp/vault" = "$HOME/.terraform.d/plugins"
}

# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
HERE
```

Then execute the `dev` make target from the project root.
```shell
make dev
```
Now Terraform is set up to use the `dev` provider build instead of the provider
from the HashiCorp registry.
60 changes: 60 additions & 0 deletions eval/namespace-enhancements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
### Public Evaluation: Enhanced Vault namespace support

This directory contains sample Terraform code which demonstrates an enhanced way
of provisioning resources under Vault namespaces. It assumes the following:

- Terraform is installed
- The provider development requirements are satisfied *see the top level README.md for more info*
- Root access to a Vault Enterprise server

#### Setup Terraform to use a local build of the Vault provider

> **warning**: backup your `~/.terraformrc` before running this command:

```shell
cat > ~/.terraformrc <<HERE
provider_installation {
dev_overrides {
"hashicorp/vault" = "$HOME/.terraform.d/plugins"
}

# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
HERE
```

Then execute the `dev` make target from the project root.
```shell
make dev
```

Now Terraform is set up to use the `dev` provider build instead of the provider
from the HashiCorp registry.

#### The basic example

Provision a generic KV secret in multiple namespaces using a single `provider{}` block.

Ensure that the `VAULT_TOKEN` and `VAULT_ADDR` environment variables are properly set,
or an alternative auth method is configured.

*from the repo root*:

Apply the example
```shell
pushd eval/namespace-enhancements/examples/basic/.
terraform init
terraform apply
terraform output -json
popd
```

Destroy the example
```shell
pushd eval/namespace-enhancements/examples/basic/.
terraform destroy
popd
```
35 changes: 35 additions & 0 deletions eval/namespace-enhancements/examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# single provider block
provider "vault" {}

locals {
# provide namespaces as a set
namespaces = toset(var.namespaces)
}

resource "vault_namespace" "demo" {
# leverage the for_each meta-argument
for_each = local.namespaces
path = each.key
}

resource "vault_mount" "demo" {
for_each = local.namespaces
namespace = vault_namespace.demo[each.key].path
path = "secretsv1"
type = "kv"
options = {
version = "1"
}
}

resource "vault_generic_secret" "demo" {
for_each = local.namespaces
# Support namespace at the level of the resource and data source
namespace = vault_mount.demo[each.key].namespace
path = "${vault_mount.demo[each.key].path}/secret"
data_json = jsonencode(
{
"baz" = "qux"
}
)
}
8 changes: 8 additions & 0 deletions eval/namespace-enhancements/examples/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "mount_path" {
value = values(vault_mount.demo)[*].path
}

output "secret_data" {
sensitive = true
value = values(vault_generic_secret.demo)[*].data
}
7 changes: 7 additions & 0 deletions eval/namespace-enhancements/examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "namespaces" {
default = [
"ns-1",
"ns-2",
"ns-3",
]
}
8 changes: 6 additions & 2 deletions generated/datasources/transform/decode/role_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/util"
"github.com/hashicorp/vault/api"
)

const roleNameEndpoint = "/transform/decode/{role_name}"
Expand Down Expand Up @@ -73,7 +74,10 @@ func RoleNameDataSource() *schema.Resource {
}

func readRoleNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Get("path").(string)
vaultPath := util.ParsePath(path, roleNameEndpoint, d)
log.Printf("[DEBUG] Writing %q", vaultPath)
Expand Down
8 changes: 6 additions & 2 deletions generated/datasources/transform/encode/role_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/util"
"github.com/hashicorp/vault/api"
)

const roleNameEndpoint = "/transform/encode/{role_name}"
Expand Down Expand Up @@ -73,7 +74,10 @@ func RoleNameDataSource() *schema.Resource {
}

func readRoleNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Get("path").(string)
vaultPath := util.ParsePath(path, roleNameEndpoint, d)
log.Printf("[DEBUG] Writing %q", vaultPath)
Expand Down
29 changes: 23 additions & 6 deletions generated/resources/transform/alphabet/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/util"
"github.com/hashicorp/vault/api"
)

const nameEndpoint = "/transform/alphabet/{name}"
Expand Down Expand Up @@ -50,8 +51,12 @@ func NameResource() *schema.Resource {
Schema: fields,
}
}

func createNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Get("path").(string)
vaultPath := util.ParsePath(path, nameEndpoint, d)
log.Printf("[DEBUG] Creating %q", vaultPath)
Expand All @@ -72,7 +77,10 @@ func createNameResource(d *schema.ResourceData, meta interface{}) error {
}

func readNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Reading %q", vaultPath)

Expand Down Expand Up @@ -104,7 +112,10 @@ func readNameResource(d *schema.ResourceData, meta interface{}) error {
}

func updateNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Updating %q", vaultPath)

Expand All @@ -120,7 +131,10 @@ func updateNameResource(d *schema.ResourceData, meta interface{}) error {
}

func deleteNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Deleting %q", vaultPath)

Expand All @@ -136,7 +150,10 @@ func deleteNameResource(d *schema.ResourceData, meta interface{}) error {
}

func resourceNameExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return false, e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Checking if %q exists", vaultPath)

Expand Down
4 changes: 2 additions & 2 deletions generated/resources/transform/alphabet/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
sdk_schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/vault/api"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/schema"
"github.com/hashicorp/terraform-provider-vault/testutil"
"github.com/hashicorp/terraform-provider-vault/vault"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAlphabetName(t *testing.T) {
}

func destroy(s *terraform.State) error {
client := nameTestProvider.SchemaProvider().Meta().(*api.Client)
client := nameTestProvider.SchemaProvider().Meta().(*provider.ProviderMeta).GetClient()

for _, rs := range s.RootModule().Resources {
if rs.Type != "vault_transform_alphabet_name" {
Expand Down
29 changes: 23 additions & 6 deletions generated/resources/transform/role/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/util"
"github.com/hashicorp/vault/api"
)

const nameEndpoint = "/transform/role/{name}"
Expand Down Expand Up @@ -51,8 +52,12 @@ func NameResource() *schema.Resource {
Schema: fields,
}
}

func createNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Get("path").(string)
vaultPath := util.ParsePath(path, nameEndpoint, d)
log.Printf("[DEBUG] Creating %q", vaultPath)
Expand All @@ -73,7 +78,10 @@ func createNameResource(d *schema.ResourceData, meta interface{}) error {
}

func readNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Reading %q", vaultPath)

Expand Down Expand Up @@ -105,7 +113,10 @@ func readNameResource(d *schema.ResourceData, meta interface{}) error {
}

func updateNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Updating %q", vaultPath)

Expand All @@ -121,7 +132,10 @@ func updateNameResource(d *schema.ResourceData, meta interface{}) error {
}

func deleteNameResource(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Deleting %q", vaultPath)

Expand All @@ -137,7 +151,10 @@ func deleteNameResource(d *schema.ResourceData, meta interface{}) error {
}

func resourceNameExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*api.Client)
client, e := provider.GetClient(d, meta)
if e != nil {
return false, e
}
vaultPath := d.Id()
log.Printf("[DEBUG] Checking if %q exists", vaultPath)

Expand Down
4 changes: 2 additions & 2 deletions generated/resources/transform/role/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
sdk_schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/vault/api"

"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/schema"
"github.com/hashicorp/terraform-provider-vault/testutil"
"github.com/hashicorp/terraform-provider-vault/vault"
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestRoleName(t *testing.T) {
}

func destroy(s *terraform.State) error {
client := nameTestProvider.SchemaProvider().Meta().(*api.Client)
client := nameTestProvider.SchemaProvider().Meta().(*provider.ProviderMeta).GetClient()

for _, rs := range s.RootModule().Resources {
if rs.Type != "vault_transform_role_name" {
Expand Down
Loading