Skip to content

Commit

Permalink
Fix up some bits
Browse files Browse the repository at this point in the history
  • Loading branch information
benashz committed Oct 25, 2023
1 parent d92391b commit 974658c
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 126 deletions.
2 changes: 1 addition & 1 deletion internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
FieldParameters = "parameters"
FieldMethod = "method"
FieldNamespace = "namespace"
FieldIsRootNamespace = "is_root_namespace"
FieldUseRootNamespace = "use_root_namespace"
FieldNamespaceID = "namespace_id"
FieldNamespacePath = "namespace_path"
FieldPathFQ = "path_fq"
Expand Down
35 changes: 26 additions & 9 deletions internal/provider/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (l *AuthLoginCommon) Init(d *schema.ResourceData, authField string, validat

func (l *AuthLoginCommon) Namespace() (string, bool) {
if l.params != nil {
if v, ok := l.params[consts.FieldIsRootNamespace]; ok && v.(bool) {
if v, ok := l.params[consts.FieldUseRootNamespace]; ok && v.(bool) {
return "", true
}

Expand Down Expand Up @@ -250,6 +250,12 @@ func (l *AuthLoginCommon) init(d *schema.ResourceData) (string, map[string]inter
var params map[string]interface{}
if v, ok := l.getOk(d, consts.FieldParameters); ok {
params = v.(map[string]interface{})
ns, _ := l.getOk(d, consts.FieldNamespace)
params[consts.FieldNamespace] = ns

if v := l.get(d, consts.FieldUseRootNamespace); v != nil {
params[consts.FieldUseRootNamespace] = v
}
} else {
v := config[0]
if v == nil {
Expand All @@ -259,10 +265,6 @@ func (l *AuthLoginCommon) init(d *schema.ResourceData) (string, map[string]inter
}
}

if v, ok := params[consts.FieldIsRootNamespace]; ok && !v.(bool) {
delete(params, consts.FieldIsRootNamespace)
}

l.initialized = true

return path, params, nil
Expand Down Expand Up @@ -302,6 +304,10 @@ func (l *AuthLoginCommon) getOk(d *schema.ResourceData, field string) (interface
return d.GetOk(l.fieldPath(d, field))
}

func (l *AuthLoginCommon) get(d *schema.ResourceData, field string) interface{} {
return d.Get(l.fieldPath(d, field))
}

func (l *AuthLoginCommon) fieldPath(d *schema.ResourceData, field string) string {
return fmt.Sprintf("%s.0.%s", l.authField, field)
}
Expand Down Expand Up @@ -332,24 +338,35 @@ func GetAuthLogin(r *schema.ResourceData) (AuthLogin, error) {
return nil, nil
}

func mustAddLoginSchema(r *schema.Resource, defaultMount string) *schema.Resource {
func mustAddLoginSchema(r *schema.Resource, authField string, defaultMount string) *schema.Resource {
m := map[string]*schema.Schema{
consts.FieldNamespace: {
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf(
"The authentication engine's namespace. Conflicts with %s",
consts.FieldIsRootNamespace,
consts.FieldUseRootNamespace,
),
ConflictsWith: []string{
fmt.Sprintf("%s.0.%s",
authField,
consts.FieldUseRootNamespace,
),
},
},
consts.FieldIsRootNamespace: {
consts.FieldUseRootNamespace: {
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf(
"Authenticate to the root Vault namespace. Conflicts with %s",
consts.FieldNamespace,
),
ConflictsWith: []string{consts.FieldNamespace},
ConflictsWith: []string{
fmt.Sprintf("%s.0.%s",
authField,
consts.FieldNamespace,
),
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/auth_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GetAWSLoginSchemaResource(authField string) *schema.Resource {
Description: `The Vault header value to include in the STS signing request.`,
},
},
}, consts.MountTypeAWS)
}, authField, consts.MountTypeAWS)
}

var _ AuthLogin = (*AuthLoginAWS)(nil)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/auth_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestAuthLoginAWS_Init(t *testing.T) {
},
expectParams: map[string]interface{}{
consts.FieldNamespace: "ns1",
consts.FieldUseRootNamespace: false,
consts.FieldRole: "alice",
consts.FieldMount: consts.MountTypeAWS,
consts.FieldAWSAccessKeyID: "key-id",
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/auth_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func GetAzureLoginSchemaResource(authField string) *schema.Resource {
ConflictsWith: []string{fmt.Sprintf("%s.0.%s", authField, consts.FieldJWT)},
},
},
}, consts.MountTypeAzure)
}, authField, consts.MountTypeAzure)
}

var _ AuthLogin = (*AuthLoginAzure)(nil)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/auth_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestAuthLoginAzure_Init(t *testing.T) {
},
expectParams: map[string]interface{}{
consts.FieldNamespace: "ns1",
consts.FieldUseRootNamespace: false,
consts.FieldMount: consts.MountTypeAzure,
consts.FieldRole: "alice",
consts.FieldJWT: "jwt1",
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/auth_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetCertLoginSchemaResource(authField string) *schema.Resource {
Description: "Path to a file containing the private key that the certificate was issued for.",
},
},
}, consts.MountTypeCert)
}, authField, consts.MountTypeCert)
}

var _ AuthLogin = (*AuthLoginCert)(nil)
Expand Down
56 changes: 30 additions & 26 deletions internal/provider/auth_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ func TestAuthLoginCert_Init(t *testing.T) {
},
authField: consts.FieldAuthLoginCert,
expectParams: map[string]interface{}{
consts.FieldNamespace: "",
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
consts.FieldNamespace: "",
consts.FieldUseRootNamespace: false,
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
},
wantErr: false,
},
Expand All @@ -75,11 +76,12 @@ func TestAuthLoginCert_Init(t *testing.T) {
},
authField: consts.FieldAuthLoginCert,
expectParams: map[string]interface{}{
consts.FieldNamespace: "",
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "bob",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
consts.FieldNamespace: "",
consts.FieldUseRootNamespace: false,
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "bob",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
},
wantErr: false,
},
Expand All @@ -97,12 +99,13 @@ func TestAuthLoginCert_Init(t *testing.T) {
},
authField: consts.FieldAuthLoginCert,
expectParams: map[string]interface{}{
consts.FieldNamespace: "ns1",
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
consts.FieldNamespace: "ns1",
consts.FieldUseRootNamespace: false,
consts.FieldMount: consts.MountTypeCert,
consts.FieldName: "",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
},
wantErr: false,
},
Expand All @@ -125,15 +128,16 @@ func TestAuthLoginCert_Init(t *testing.T) {
},
authField: consts.FieldAuthLoginCert,
expectParams: map[string]interface{}{
consts.FieldCACertDir: "/foo/baz",
consts.FieldSkipTLSVerify: true,
consts.FieldTLSServerName: "baz.biff",
consts.FieldNamespace: "ns1",
consts.FieldMount: "cert1",
consts.FieldName: "bob",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
consts.FieldNamespace: "ns1",
consts.FieldUseRootNamespace: false,
consts.FieldCACertDir: "/foo/baz",
consts.FieldSkipTLSVerify: true,
consts.FieldTLSServerName: "baz.biff",
consts.FieldMount: "cert1",
consts.FieldName: "bob",
consts.FieldCACertFile: "ca.crt",
consts.FieldCertFile: "cert.crt",
consts.FieldKeyFile: "cert.key",
},
wantErr: false,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/auth_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GetGCPLoginSchemaResource(authField string) *schema.Resource {
ConflictsWith: []string{fmt.Sprintf("%s.0.%s", authField, consts.FieldJWT)},
},
},
}, consts.MountTypeGCP)
}, authField, consts.MountTypeGCP)
}

var _ AuthLogin = (*AuthLoginGCP)(nil)
Expand Down Expand Up @@ -120,7 +120,7 @@ func (l *AuthLoginGCP) Login(client *api.Client) (*api.Secret, error) {
}

params, err := l.copyParamsExcluding(
consts.FieldIsRootNamespace,
consts.FieldUseRootNamespace,
consts.FieldNamespace,
consts.FieldMount,
consts.FieldJWT,
Expand Down
38 changes: 12 additions & 26 deletions internal/provider/auth_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"fmt"
"log"

Check failure on line 8 in internal/provider/auth_generic.go

View workflow job for this annotation

GitHub Actions / build

"log" imported and not used

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -33,32 +34,16 @@ func GetGenericLoginSchema(authField string) *schema.Schema {
}

func GetGenericLoginSchemaResource(_ string) *schema.Resource {
return &schema.Resource{
return mustAddLoginSchema(&schema.Resource{
Schema: map[string]*schema.Schema{
consts.FieldPath: {
Type: schema.TypeString,
Required: true,
},
consts.FieldNamespace: {
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf(
"The authentication engine's namespace. Conflicts with %s",
consts.FieldIsRootNamespace,
),
},
consts.FieldIsRootNamespace: {
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf(
"Authenticate to the root Vault namespace. Conflicts with %s",
consts.FieldNamespace,
),
ConflictsWith: []string{consts.FieldNamespace},
},
consts.FieldParameters: {
Type: schema.TypeMap,
Optional: true,
Type: schema.TypeMap,
Optional: true,
Sensitive: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand All @@ -68,7 +53,7 @@ func GetGenericLoginSchemaResource(_ string) *schema.Resource {
Optional: true,
},
},
}
}, consts.FieldAuthLoginGeneric, consts.MountTypeNone)
}

var _ AuthLogin = (*AuthLoginGeneric)(nil)
Expand All @@ -78,10 +63,8 @@ var _ AuthLogin = (*AuthLoginGeneric)(nil)
// Requires configuration provided by SchemaLoginGeneric.
type AuthLoginGeneric struct {
AuthLoginCommon
path string
namespace string
namespaceExists bool
method string
path string
method string
}

func (l *AuthLoginGeneric) Init(d *schema.ResourceData, authField string) (AuthLogin, error) {
Expand Down Expand Up @@ -115,7 +98,10 @@ func (l *AuthLoginGeneric) Login(client *api.Client) (*api.Secret, error) {
return nil, err
}

params, err := l.copyParams()
params, err := l.copyParamsExcluding(
consts.FieldNamespace,
consts.FieldUseRootNamespace,
)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/auth_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAuthLoginGeneric_Namespace(t *testing.T) {
{
name: "root-ns",
params: map[string]interface{}{
consts.FieldIsRootNamespace: true,
consts.FieldUseRootNamespace: true,
},
want: "",
exists: true,
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetJWTLoginSchema(authField string) *schema.Schema {
}

// GetJWTLoginSchemaResource for the jwt authentication engine.
func GetJWTLoginSchemaResource(_ string) *schema.Resource {
func GetJWTLoginSchemaResource(authField string) *schema.Resource {
return mustAddLoginSchema(&schema.Resource{
Schema: map[string]*schema.Schema{
consts.FieldRole: {
Expand All @@ -48,7 +48,7 @@ func GetJWTLoginSchemaResource(_ string) *schema.Resource {
DefaultFunc: schema.EnvDefaultFunc(consts.EnvVarVaultAuthJWT, nil),
},
},
}, consts.MountTypeJWT)
}, authField, consts.MountTypeJWT)
}

var _ AuthLogin = (*AuthLoginJWT)(nil)
Expand Down Expand Up @@ -94,7 +94,7 @@ func (l *AuthLoginJWT) Login(client *api.Client) (*api.Secret, error) {
}

params, err := l.copyParamsExcluding(
consts.FieldIsRootNamespace,
consts.FieldUseRootNamespace,
consts.FieldNamespace,
consts.FieldMount,
)
Expand Down
9 changes: 5 additions & 4 deletions internal/provider/auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func TestAuthLoginJWT_Init(t *testing.T) {
},
},
expectParams: map[string]interface{}{
consts.FieldNamespace: "ns1",
consts.FieldMount: consts.MountTypeJWT,
consts.FieldRole: "alice",
consts.FieldJWT: "jwt1",
consts.FieldNamespace: "ns1",
consts.FieldUseRootNamespace: false,
consts.FieldMount: consts.MountTypeJWT,
consts.FieldRole: "alice",
consts.FieldJWT: "jwt1",
},
wantErr: false,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/auth_kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func GetKerberosLoginSchemaResource(authField string) *schema.Resource {
Description: "Strip the host from the username found in the keytab.",
},
},
}, consts.MountTypeKerberos)
}, authField, consts.MountTypeKerberos)

return s
}
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/auth_kerberos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func TestAuthLoginKerberos_Init(t *testing.T) {
},
authField: consts.FieldAuthLoginKerberos,
expectParams: map[string]interface{}{
consts.FieldToken: testNegTokenInit,
consts.FieldNamespace: "",
consts.FieldUseRootNamespace: false,
consts.FieldToken: testNegTokenInit,
consts.FieldMount: consts.MountTypeKerberos,
consts.FieldUsername: "",
consts.FieldService: "",
Expand Down
Loading

0 comments on commit 974658c

Please sign in to comment.