-
Notifications
You must be signed in to change notification settings - Fork 540
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 Nomad secret backend #923
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d9e8686
Add Nomad secret backend
jasonodonnell 98b38ee
Update vault/resource_nomad_secret_backend.go
jasonodonnell 8a817a1
Update vault/resource_nomad_secret_role.go
jasonodonnell 2ac09f1
Change PathInventory to config/access
jasonodonnell 496d0d5
Add more paths to inventory
jasonodonnell 247f849
Merge lease config with backend resource
jasonodonnell 3d0c71e
Fix updating mount parameters
jasonodonnell 9a611db
Remove required flag from token/address
jasonodonnell 537b2d5
Remove commented code
jasonodonnell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
func nomadAccessCredentialsDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: readNomadCredsResource, | ||
Schema: map[string]*schema.Schema{ | ||
"backend": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Nomad secret backend to generate tokens from.", | ||
}, | ||
"role": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Name of the role.", | ||
}, | ||
"accessor_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The public identifier for a specific token. It can be used to look up information about a token or to revoke a token.", | ||
}, | ||
"secret_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Used to make requests to Nomad and should be kept private.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func readNomadCredsResource(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
backend := d.Get("backend").(string) | ||
role := d.Get("role").(string) | ||
path := fmt.Sprintf("%s/creds/%s", backend, role) | ||
|
||
secret, err := client.Logical().Read(path) | ||
if err != nil { | ||
return fmt.Errorf("error reading from Vault: %s", err) | ||
} | ||
log.Printf("[DEBUG] Read %q from Vault", path) | ||
|
||
if secret == nil { | ||
return fmt.Errorf("no role found at %q", path) | ||
} | ||
|
||
accessorID := secret.Data["accessor_id"].(string) | ||
if accessorID == "" { | ||
return fmt.Errorf("accessor_id is not set in response") | ||
} | ||
|
||
secretID := secret.Data["secret_id"].(string) | ||
if secretID == "" { | ||
return fmt.Errorf("secret_id is not set in response") | ||
} | ||
|
||
d.SetId(accessorID) | ||
d.Set("accessor_id", accessorID) | ||
d.Set("secret_id", secretID) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-provider-vault/util" | ||
) | ||
|
||
func TestAccDataSourceNomadAccessCredentialsClientBasic(t *testing.T) { | ||
backend := acctest.RandomWithPrefix("tf-test-nomad") | ||
address, token := util.GetTestNomadCreds(t) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testProviders, | ||
PreCheck: func() { util.TestAccPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceNomadAccessCredentialsConfig(backend, address, token, "test"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.vault_nomad_access_token.token", "secret_id"), | ||
resource.TestCheckResourceAttrSet("data.vault_nomad_access_token.token", "accessor_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceNomadAccessCredentialsManagementBasic(t *testing.T) { | ||
backend := acctest.RandomWithPrefix("tf-test-nomad") | ||
address, token := util.GetTestNomadCreds(t) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testProviders, | ||
PreCheck: func() { util.TestAccPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceNomadAccessCredentialsManagementConfig(backend, address, token, "test"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.vault_nomad_access_token.token", "secret_id"), | ||
resource.TestCheckResourceAttrSet("data.vault_nomad_access_token.token", "accessor_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceNomadAccessCredentialsConfig(backend, address, token, role string) string { | ||
return fmt.Sprintf(` | ||
resource "vault_nomad_secret_backend" "config" { | ||
backend = "%s" | ||
description = "test description" | ||
default_lease_ttl_seconds = "3600" | ||
max_lease_ttl_seconds = "7200" | ||
address = "%s" | ||
token = "%s" | ||
} | ||
|
||
resource "vault_nomad_secret_role" "test" { | ||
backend = vault_nomad_secret_backend.config.backend | ||
role = "%s" | ||
policies = ["reaodnly"] | ||
} | ||
|
||
data "vault_nomad_access_token" "token" { | ||
backend = vault_nomad_secret_backend.config.backend | ||
role = vault_nomad_secret_role.test.role | ||
} | ||
`, backend, address, token, role) | ||
} | ||
|
||
func testAccDataSourceNomadAccessCredentialsManagementConfig(backend, address, token, role string) string { | ||
return fmt.Sprintf(` | ||
resource "vault_nomad_secret_backend" "config" { | ||
backend = "%s" | ||
description = "test description" | ||
default_lease_ttl_seconds = "3600" | ||
max_lease_ttl_seconds = "7200" | ||
address = "%s" | ||
token = "%s" | ||
} | ||
|
||
resource "vault_nomad_secret_role" "test" { | ||
backend = vault_nomad_secret_backend.config.backend | ||
role = "%s" | ||
type = "management" | ||
} | ||
|
||
data "vault_nomad_access_token" "token" { | ||
backend = vault_nomad_secret_backend.config.backend | ||
role = vault_nomad_secret_role.test.role | ||
} | ||
`, backend, address, token, role) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
path
here? Granted, we aren't using the ID at all that I can seeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each call to this endpoint generates a new token, so we want the ID to be the unique identifier of that specific generated token. Path would be too generic I think?