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

Data source vault auth backends #1827

Merged
merged 22 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
80 changes: 80 additions & 0 deletions vault/data_source_auth_backends.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package vault

import (
"fmt"
"sort"
"strings"

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

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

func authBackendsDataSource() *schema.Resource {
return &schema.Resource{
Read: ReadWrapper(authBackendsDataSourceRead),
Schema: map[string]*schema.Schema{
"paths": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The auth backend mount points.",
},
"type": {
mengesb marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Description: "The name of the auth backend.",
},
"accessors": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The accessors of the auth backends.",
},
},
}
}

func authBackendsDataSourceRead(d *schema.ResourceData, meta interface{}) error {
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}

targetType := d.Get("type").(string)

auths, err := client.Sys().ListAuth()
if err != nil {
return fmt.Errorf("error reading from Vault: %s", err)
}

var paths, accessors []string

for path, auth := range auths {

path = strings.TrimSuffix(path, "/")

if targetType == "" {
paths = append(paths, path)
accessors = append(accessors, auth.Accessor)
// we do this to make test assertions easier
// this is not required
sort.Strings(paths)
mengesb marked this conversation as resolved.
Show resolved Hide resolved
} else if auth.Type == targetType {
paths = append(paths, path)
accessors = append(accessors, auth.Accessor)
}
}

// Single instance data source - defaulting ID to 'default'
d.SetId("default")
d.Set("paths", paths)
d.Set("type", targetType)
d.Set("accessors", accessors)
mengesb marked this conversation as resolved.
Show resolved Hide resolved

// If we fell out here then we didn't find our Auth in the list.
mengesb marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
96 changes: 96 additions & 0 deletions vault/data_source_auth_backends_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package vault

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/hashicorp/terraform-provider-vault/testutil"
)

func TestDataSourceAuthBackends(t *testing.T) {
userpassPath := acctest.RandomWithPrefix("foo")
approlePath := acctest.RandomWithPrefix("foo")
ds := "data.vault_auth_backends.test"

r.Test(t, r.TestCase{
Providers: testProviders,
PreCheck: func() { testutil.TestAccPreCheck(t) },
Steps: []r.TestStep{
{
Config: testDataSourceAuthBackendsBasic,
// The token auth method is built-in and automatically enabled
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ds, "paths.#", "1"),
resource.TestCheckResourceAttr(ds, "paths.0", "token"),
resource.TestCheckResourceAttr(ds, "accessors.#", "1"),
),
},
{
Config: testDataSourceAuthBackendsBasic_config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ds, "paths.#", "3"),
resource.TestCheckResourceAttr(ds, "paths.0", "approle"),
resource.TestCheckResourceAttr(ds, "paths.1", "token"),
resource.TestCheckResourceAttr(ds, "paths.2", "userpass"),
resource.TestCheckResourceAttr(ds, "accessors.#", "3"),
resource.TestCheckResourceAttr(ds, "type", ""),
),
},
{
Config: testDataSourceAuthBackends_config([]string{userpassPath, approlePath}, "userpass"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ds, "paths.#", "1"),
resource.TestCheckResourceAttr(ds, "paths.0", userpassPath),
resource.TestCheckResourceAttr(ds, "accessors.#", "1"),
resource.TestCheckResourceAttr(ds, "type", "userpass"),
),
},
},
})
}

var testDataSourceAuthBackendsBasic = `
data "vault_auth_backends" "test" {}
`

var testDataSourceAuthBackendsBasic_config = `
resource "vault_auth_backend" "userpass" {
type = "userpass"
}
resource "vault_auth_backend" "approle" {
type = "approle"
}
data "vault_auth_backends" "test" {
depends_on = [
"vault_auth_backend.userpass",
"vault_auth_backend.approle",
mengesb marked this conversation as resolved.
Show resolved Hide resolved
]
}
`

func testDataSourceAuthBackends_config(path []string, typ string) string {
return fmt.Sprintf(`
resource "vault_auth_backend" "test-foo" {
path = "%s"
type = "userpass"
}
resource "vault_auth_backend" "test-bar" {
path = "%s"
type = "approle"
}
data "vault_auth_backends" "test" {
depends_on = [
"vault_auth_backend.test-foo",
"vault_auth_backend.test-bar",
mengesb marked this conversation as resolved.
Show resolved Hide resolved
]
type = "%s"
}
`, path[0], path[1], typ)
}
4 changes: 4 additions & 0 deletions vault/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ var (
Resource: UpdateSchemaResource(authBackendDataSource()),
PathInventory: []string{"/sys/auth"},
},
"vault_auth_backends": {
Resource: UpdateSchemaResource(authBackendsDataSource()),
PathInventory: []string{"/sys/auth"},
},
"vault_transit_encrypt": {
Resource: UpdateSchemaResource(transitEncryptDataSource()),
PathInventory: []string{"/transit/encrypt/{name}"},
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/auth_backends.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: "vault"
page_title: "Vault: vault_auth_backends data source"
sidebar_current: "docs-vault-datasource-auth-backends"
description: |-
List Auth Backends from Vault
---

# vault\_auth\_backends

## Example Usage

```hcl
data "vault_auth_backends" "example" {}
```

```hcl
data "vault_auth_backends" "example-filter" {
type = "kubernetes"
}

## Argument Reference

The following arguments are supported:

* `namespace` - (Optional) The namespace of the target resource.
The value should not contain leading or trailing forward slashes.
The `namespace` is always relative to the provider's configured [namespace](/docs/providers/vault#namespace).
*Available only for Vault Enterprise*.

* `type` - (Optional) The name of the auth method type.
mengesb marked this conversation as resolved.
Show resolved Hide resolved

## Attributes Reference

In addition to the fields above, the following attributes are exported:

* `accessors` - The accessors for this auth methods.

* `paths` - List of auth backend mount points.