-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
provider/vault: vault_mount resource #14456
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,147 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/vault/api" | ||
"log" | ||
) | ||
|
||
func mountResource() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: mountWrite, | ||
Update: mountUpdate, | ||
Delete: mountDelete, | ||
Read: mountRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
Description: "Where the secret backend will be mounted", | ||
}, | ||
|
||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Type of the backend, such as 'aws'", | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Required: false, | ||
ForceNew: true, | ||
Description: "Human-friendly description of the mount", | ||
}, | ||
|
||
"default_lease_ttl_seconds": { | ||
Type: schema.TypeInt, | ||
Required: false, | ||
Optional: true, | ||
ForceNew: false, | ||
Description: "Default lease duration for tokens and secrets in seconds", | ||
}, | ||
|
||
"max_lease_ttl_seconds": { | ||
Type: schema.TypeInt, | ||
Required: false, | ||
Optional: true, | ||
ForceNew: false, | ||
Description: "Maximum possible lease duration for tokens and secrets in seconds", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func mountWrite(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
|
||
info := &api.MountInput{ | ||
Type: d.Get("type").(string), | ||
Description: d.Get("description").(string), | ||
Config: api.MountConfigInput{ | ||
DefaultLeaseTTL: fmt.Sprintf("%ds", d.Get("default_lease_ttl_seconds")), | ||
MaxLeaseTTL: fmt.Sprintf("%ds", d.Get("max_lease_ttl_seconds")), | ||
}, | ||
} | ||
|
||
path := d.Get("path").(string) | ||
|
||
log.Printf("[DEBUG] Creating mount %s in Vault", path) | ||
|
||
if err := client.Sys().Mount(path, info); err != nil { | ||
return fmt.Errorf("error writing to Vault: %s", err) | ||
} | ||
|
||
d.SetId(path) | ||
|
||
return nil | ||
} | ||
|
||
func mountUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
|
||
config := api.MountConfigInput{ | ||
DefaultLeaseTTL: fmt.Sprintf("%ds", d.Get("default_lease_ttl_seconds")), | ||
MaxLeaseTTL: fmt.Sprintf("%ds", d.Get("max_lease_ttl_seconds")), | ||
} | ||
|
||
path := d.Id() | ||
|
||
if d.HasChange("path") { | ||
newPath := d.Get("path").(string) | ||
|
||
log.Printf("[DEBUG] Remount %s to %s in Vault", path, newPath) | ||
|
||
err := client.Sys().Remount(d.Id(), newPath) | ||
if err != nil { | ||
return fmt.Errorf("error remounting in Vault: %s", err) | ||
} | ||
|
||
d.SetId(newPath) | ||
path = newPath | ||
} | ||
|
||
log.Printf("[DEBUG] Updating mount %s in Vault", path) | ||
|
||
if err := client.Sys().TuneMount(path, config); err != nil { | ||
return fmt.Errorf("error updating Vault: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func mountDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
|
||
path := d.Id() | ||
|
||
log.Printf("[DEBUG] Unmounting %s from Vault", path) | ||
|
||
if err := client.Sys().Unmount(path); err != nil { | ||
return fmt.Errorf("error deleting from Vault: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func mountRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*api.Client) | ||
|
||
path := d.Id() | ||
|
||
log.Printf("[DEBUG] Reading mount %s from Vault", path) | ||
|
||
mount, err := client.Sys().MountConfig(path) | ||
if err != nil { | ||
return fmt.Errorf("error reading from Vault: %s", err) | ||
} | ||
|
||
d.Set("default_lease_ttl_seconds", mount.DefaultLeaseTTL) | ||
d.Set("max_lease_ttl_seconds", mount.MaxLeaseTTL) | ||
|
||
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,151 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
r "github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
func TestResourceMount(t *testing.T) { | ||
r.Test(t, r.TestCase{ | ||
Providers: testProviders, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Steps: []r.TestStep{ | ||
{ | ||
Config: testResourceMount_initialConfig, | ||
Check: testResourceMount_initialCheck, | ||
}, | ||
{ | ||
Config: testResourceMount_updateConfig, | ||
Check: testResourceMount_updateCheck, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
var testResourceMount_initialConfig = ` | ||
|
||
resource "vault_mount" "test" { | ||
path = "example" | ||
type = "generic" | ||
description = "Example mount for testing" | ||
default_lease_ttl_seconds = 3600 | ||
max_lease_ttl_seconds = 36000 | ||
} | ||
|
||
` | ||
|
||
func testResourceMount_initialCheck(s *terraform.State) error { | ||
resourceState := s.Modules[0].Resources["vault_mount.test"] | ||
if resourceState == nil { | ||
return fmt.Errorf("resource not found in state") | ||
} | ||
|
||
instanceState := resourceState.Primary | ||
if instanceState == nil { | ||
return fmt.Errorf("resource has no primary instance") | ||
} | ||
|
||
path := instanceState.ID | ||
|
||
if path != instanceState.Attributes["path"] { | ||
return fmt.Errorf("id doesn't match path") | ||
} | ||
|
||
if path != "example" { | ||
return fmt.Errorf("unexpected path value") | ||
} | ||
|
||
mount, err := findMount(path) | ||
if err != nil { | ||
return fmt.Errorf("error reading back mount: %s", err) | ||
} | ||
|
||
if wanted := "Example mount for testing"; mount.Description != wanted { | ||
return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := "generic"; mount.Type != wanted { | ||
return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := 3600; mount.Config.DefaultLeaseTTL != wanted { | ||
return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := 36000; mount.Config.MaxLeaseTTL != wanted { | ||
return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var testResourceMount_updateConfig = ` | ||
|
||
resource "vault_mount" "test" { | ||
path = "remountingExample" | ||
type = "generic" | ||
description = "Example mount for testing" | ||
default_lease_ttl_seconds = 7200 | ||
max_lease_ttl_seconds = 72000 | ||
} | ||
|
||
` | ||
|
||
func testResourceMount_updateCheck(s *terraform.State) error { | ||
resourceState := s.Modules[0].Resources["vault_mount.test"] | ||
instanceState := resourceState.Primary | ||
|
||
path := instanceState.ID | ||
|
||
if path != instanceState.Attributes["path"] { | ||
return fmt.Errorf("id doesn't match path") | ||
} | ||
|
||
if path != "remountingExample" { | ||
return fmt.Errorf("unexpected path value") | ||
} | ||
|
||
mount, err := findMount(path) | ||
if err != nil { | ||
return fmt.Errorf("error reading back mount: %s", err) | ||
} | ||
|
||
if wanted := "Example mount for testing"; mount.Description != wanted { | ||
return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := "generic"; mount.Type != wanted { | ||
return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := 7200; mount.Config.DefaultLeaseTTL != wanted { | ||
return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
if wanted := 72000; mount.Config.MaxLeaseTTL != wanted { | ||
return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func findMount(path string) (*api.MountOutput, error) { | ||
client := testProvider.Meta().(*api.Client) | ||
|
||
path = path + "/" | ||
|
||
mounts, err := client.Sys().ListMounts() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if mounts[path] != nil { | ||
return mounts[path], nil | ||
} | ||
|
||
return nil, fmt.Errorf("Unable to find mount %s in Vault; current list: %v", path, mounts) | ||
} |
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,38 @@ | ||
--- | ||
layout: "vault" | ||
page_title: "Vault: vault_mount resource" | ||
sidebar_current: "docs-vault-resource-mount" | ||
description: |- | ||
Managing the mounting of secret backends in Vault | ||
--- | ||
|
||
# vault\_mount | ||
|
||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "vault_mount" "example" { | ||
path = "dummy" | ||
type = "generic" | ||
description = "This is an example mount" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `path` - (Required) Where the secret backend will be mounted | ||
|
||
* `type` - (Required) Type of the backend, such as "aws" | ||
|
||
* `description` - (Optional) Human-friendly description of the mount | ||
|
||
* `default_lease_ttl_seconds` - (Optional) Default lease duration for tokens and secrets in seconds | ||
|
||
* `max_lease_ttl_seconds` - (Optional) Maximum possible lease duration for tokens and secrets in seconds | ||
|
||
## Attributes Reference | ||
|
||
No additional attributes are exported by this resource. |
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
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.
There is an existing
vault_auth_backend
; maybevault_secret_backend
is more consistent with the lingo. Avault_audit_backend
would be nice too.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.
This is a good thought, @bmoylan! I think though that since the term "mount" is what appears in Vault's UI it makes sense to follow that here so that it's easier to understand how the Terraform resources map onto the Vault CLI operations.