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

feat: add Mount Migration support to all secrets/auth backends #1594

Merged
merged 23 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cbf1a21
remove semver package and move semantic versioning logic to providerMeta
vinay-gopalan Sep 2, 2022
22cdf94
add mount migration support for secret engine backends
vinay-gopalan Sep 2, 2022
fc7454e
rename/refactor functions based on feedback
vinay-gopalan Sep 6, 2022
b218339
add helper method to consolidate repeated code
vinay-gopalan Sep 6, 2022
e5a6dd6
fix bug in remount helper
vinay-gopalan Sep 7, 2022
0fda02a
refactor remount helper for auth mounts
vinay-gopalan Sep 8, 2022
84e4022
add mount migration support to auth backends
vinay-gopalan Sep 8, 2022
b57080a
add import steps to tests
vinay-gopalan Sep 8, 2022
8583208
Merge branch 'main' into VAULT-7442/secrets-auth-mount-mig
vinay-gopalan Sep 8, 2022
7cb6aa3
update consul backend with functions that were moved
vinay-gopalan Sep 8, 2022
b0e8ba4
move remount to util package
vinay-gopalan Sep 9, 2022
4a48854
use GetImportTestStep
vinay-gopalan Sep 9, 2022
1465c5e
update minVersion wrapper func to use new version.Version variables
vinay-gopalan Sep 9, 2022
595ad18
add boolean field to oput out of mount migration
vinay-gopalan Sep 9, 2022
6c0ba7d
fix broken import tests
vinay-gopalan Sep 12, 2022
02e766b
fix formatting on imports
vinay-gopalan Sep 12, 2022
77618b3
add getMountMigrationDiff util to reduce extra helper methods
vinay-gopalan Sep 12, 2022
b624495
fix remount test with testStore
vinay-gopalan Sep 12, 2022
145e66f
resolve merge conflicts and move schema util to internal package
vinay-gopalan Sep 12, 2022
cf6f2a6
Update vault/resource_aws_secret_backend.go
vinay-gopalan Sep 12, 2022
69586af
rename mount migration func
vinay-gopalan Sep 12, 2022
77d9d68
Merge branch 'VAULT-7442/secrets-auth-mount-mig' of github.com:hashic…
vinay-gopalan Sep 12, 2022
84a7d98
add 'test' prefix to testutil
vinay-gopalan Sep 13, 2022
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
6 changes: 3 additions & 3 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ const (
/*
Vault version constants
*/
VaultVersion11 = "1.11.0"
VaultVersion10 = "1.10.0"
VaultVersion9 = "1.9.0"
VaultVersion111 = "1.11.0"
VaultVersion110 = "1.10.0"
VaultVersion19 = "1.9.0"
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved

/*
misc. path related constants
Expand Down
61 changes: 61 additions & 0 deletions internal/provider/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/awsutil"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand All @@ -30,6 +31,7 @@ type ProviderMeta struct {
resourceData *schema.ResourceData
clientCache map[string]*api.Client
m sync.RWMutex
vaultVersion *version.Version
}

// GetClient returns the providers default Vault client.
Expand Down Expand Up @@ -76,6 +78,22 @@ func (p *ProviderMeta) GetNSClient(ns string) (*api.Client, error) {
return c, nil
}

// IsAPISupported receives a minimum version
// of type *version.Version.
//
// It returns a boolean describing whether the
// ProviderMeta vaultVersion is above the
// minimum version.
func (p *ProviderMeta) IsAPISupported(minVersion *version.Version) bool {
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
return p.vaultVersion.GreaterThanOrEqual(minVersion)
}

// GetVaultVersion returns the providerMeta
// vaultVersion attribute.
func (p *ProviderMeta) GetVaultVersion() *version.Version {
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
return p.vaultVersion
}

func (p *ProviderMeta) validate() error {
if p.client == nil {
return fmt.Errorf("root api.Client not set, init with NewProviderMeta()")
Expand Down Expand Up @@ -222,6 +240,12 @@ func NewProviderMeta(d *schema.ResourceData) (interface{}, error) {
}
}

// Set the Vault version to *ProviderMeta object
vaultVersion, err := getVaultVersion(client)
if err != nil {
return nil, err
}

// Set the namespace to the requested namespace, if provided
namespace := d.Get(consts.FieldNamespace).(string)
if namespace != "" {
Expand All @@ -231,6 +255,7 @@ func NewProviderMeta(d *schema.ResourceData) (interface{}, error) {
return &ProviderMeta{
resourceData: d,
client: client,
vaultVersion: vaultVersion,
}, nil
}

Expand Down Expand Up @@ -281,6 +306,42 @@ func GetClient(i interface{}, meta interface{}) (*api.Client, error) {
return p.GetClient(), nil
}

// IsAPISupported receives an interface
// and a minimum *version.Version.
//
// It returns a boolean after computing
// whether the API is supported by the
// providerMeta, which is obtained from
// the provided interface.
func IsAPISupported(meta interface{}, minVersion *version.Version) bool {
vinay-gopalan marked this conversation as resolved.
Show resolved Hide resolved
var p *ProviderMeta
switch v := meta.(type) {
case *ProviderMeta:
p = v
default:
panic(fmt.Sprintf("meta argument must be a %T, not %T", p, meta))
}

return p.IsAPISupported(minVersion)
}

func getVaultVersion(client *api.Client) (*version.Version, error) {
resp, err := client.Sys().SealStatus()
if err != nil {
return nil, err
}

if resp == nil {
return nil, fmt.Errorf("expected response data, got nil response")
}

if resp.Version == "" {
return nil, fmt.Errorf("key %q not found in response", consts.FieldVersion)
}

return version.Must(version.NewSemver(resp.Version)), nil
}

func setChildToken(d *schema.ResourceData, c *api.Client) error {
tokenName := d.Get("token_name").(string)
if tokenName == "" {
Expand Down
82 changes: 82 additions & 0 deletions internal/provider/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -358,3 +359,84 @@ func TestGetClient(t *testing.T) {
})
}
}

func TestIsAPISupported(t *testing.T) {
rootClient, err := api.NewClient(api.DefaultConfig())
if err != nil {
t.Fatalf("error initializing root client, err=%s", err)
}

VaultVersion10, err := version.NewVersion("1.10.0")
if err != nil {
t.Fatal(err)
}

VaultVersion11, err := version.NewVersion("1.11.0")
if err != nil {
t.Fatal(err)
}

testCases := []struct {
name string
minVersion string
expected bool
meta interface{}
}{
{
name: "server-greater-than",
minVersion: "1.8.0",
expected: true,
meta: &ProviderMeta{
client: rootClient,
vaultVersion: VaultVersion11,
},
},
{
name: "server-less-than",
minVersion: "1.12.0",
expected: false,
meta: &ProviderMeta{
client: rootClient,
vaultVersion: VaultVersion11,
},
},
{
name: "server-equal",
minVersion: "1.10.0",
expected: true,
meta: &ProviderMeta{
client: rootClient,
vaultVersion: VaultVersion10,
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
if tt.meta != nil {
m := tt.meta.(*ProviderMeta)
m.resourceData = schema.TestResourceDataRaw(t,
map[string]*schema.Schema{
consts.FieldNamespace: {
Type: schema.TypeString,
Required: true,
},
},
map[string]interface{}{},
)
tt.meta = m
}

mv, err := version.NewVersion(tt.minVersion)
if err != nil {
t.Fatal(err)
}

isTFVersionGreater := tt.meta.(*ProviderMeta).IsAPISupported(mv)

if isTFVersionGreater != tt.expected {
t.Errorf("IsAPISupported() got = %v, want %v", isTFVersionGreater, tt.expected)
}
})
}
}
64 changes: 0 additions & 64 deletions internal/semver/semver.go

This file was deleted.

119 changes: 0 additions & 119 deletions internal/semver/semver_test.go

This file was deleted.

Loading