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

auth_login: enforce empty client token #2029

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build: go-version-check fmtcheck
go install

test: go-version-check fmtcheck
TF_ACC= go test $(TESTARGS) -timeout 10m $(TEST_PATH)
TF_ACC= VAULT_TOKEN= go test $(TESTARGS) -timeout 10m $(TEST_PATH)

testacc: fmtcheck
TF_ACC=1 go test $(TESTARGS) -timeout 30m $(TEST_PATH)
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (l *AuthLoginCommon) copyParamsExcluding(excludes ...string) (map[string]in
}

func (l *AuthLoginCommon) login(client *api.Client, path string, params map[string]interface{}) (*api.Secret, error) {
if client.Token() != "" {
return nil, fmt.Errorf("vault login client has a token set")
}

return client.Logical().Write(path, params)
}

Expand Down
21 changes: 21 additions & 0 deletions internal/provider/auth_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -286,9 +287,29 @@ func TestAuthLoginAWS_Login(t *testing.T) {
wantErr: true,
expectErr: authLoginInitCheckError,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginAWS{
AuthLoginCommon{
authField: "baz",
mount: "foo",
params: map[string]interface{}{
consts.FieldRole: "bob",
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/auth_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -240,6 +241,27 @@ func TestAuthLoginAzure_Login(t *testing.T) {
},
wantErr: false,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginAzure{
AuthLoginCommon: AuthLoginCommon{
authField: consts.FieldAuthLoginAzure,
params: map[string]interface{}{
consts.FieldRole: "alice",
consts.FieldJWT: "jwt1",
consts.FieldSubscriptionID: "sub1",
consts.FieldResourceGroupName: "res1",
consts.FieldVMSSName: "vmss1",
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "error-uninitialized",
authLogin: &AuthLoginAzure{
Expand All @@ -258,6 +280,7 @@ func TestAuthLoginAzure_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/auth_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -294,6 +295,28 @@ func TestAuthLoginCert_Login(t *testing.T) {
tls: true,
wantErr: false,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginCert{
AuthLoginCommon{
authField: "baz",
mount: "qux",
params: map[string]interface{}{
consts.FieldName: "bob",
consts.FieldCertFile: certFile,
consts.FieldKeyFile: keyFile,
consts.FieldSkipTLSVerify: true,
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "error-uninitialized",
authLogin: &AuthLoginCert{
Expand All @@ -312,6 +335,7 @@ func TestAuthLoginCert_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
38 changes: 38 additions & 0 deletions internal/provider/auth_gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -168,6 +169,42 @@ func TestAuthLoginGCP_Login(t *testing.T) {
testutil.SkipTestEnvUnset(t, consts.EnvVarGoogleApplicationCreds, envVarGCPServiceAccount)
},
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginGCP{
AuthLoginCommon{
authField: "baz",
mount: "qux",
params: map[string]interface{}{
consts.FieldRole: "bob",
consts.FieldCredentials: os.Getenv(consts.EnvVarGoogleApplicationCreds),
consts.FieldServiceAccount: os.Getenv(envVarGCPServiceAccount),
},
initialized: true,
},
},
handler: &testLoginHandler{
excludeParams: []string{consts.FieldJWT},
handlerFunc: handlerFunc,
},
expectReqCount: 1,
expectReqPaths: []string{
"/v1/auth/qux/login",
},
expectReqParams: []map[string]interface{}{{
consts.FieldRole: "bob",
}},
want: &api.Secret{
Auth: &api.SecretAuth{
Metadata: map[string]string{
consts.FieldRole: "bob",
},
},
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "no-jwt",
authLogin: &AuthLoginGCP{
Expand Down Expand Up @@ -201,6 +238,7 @@ func TestAuthLoginGCP_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -157,6 +158,25 @@ func TestAuthLoginJWT_Login(t *testing.T) {
},
wantErr: false,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginJWT{
AuthLoginCommon: AuthLoginCommon{
authField: consts.FieldAuthLoginJWT,
params: map[string]interface{}{
consts.FieldRole: "alice",
consts.FieldJWT: "jwt1",
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "error-uninitialized",
authLogin: &AuthLoginJWT{
Expand All @@ -175,6 +195,7 @@ func TestAuthLoginJWT_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/auth_kerberos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -221,9 +222,43 @@ func TestAuthLoginKerberos_Login(t *testing.T) {
},
wantErr: false,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginKerberos{
authHeaderFunc: getTestAuthHeaderFunc(&krbauth.LoginCfg{
Username: "alice",
Service: "service1",
Realm: "realm1",
KeytabPath: "/etc/kerberos/keytab",
Krb5ConfPath: "/etc/kerberos/krb5.conf",
DisableFASTNegotiation: true,
RemoveInstanceName: false,
}),
AuthLoginCommon: AuthLoginCommon{
authField: consts.FieldAuthLoginKerberos,
params: map[string]interface{}{
consts.FieldUsername: "alice",
consts.FieldService: "service1",
consts.FieldRealm: "realm1",
consts.FieldKeytabPath: "/etc/kerberos/keytab",
consts.FieldKRB5ConfPath: "/etc/kerberos/krb5.conf",
consts.FieldDisableFastNegotiation: true,
consts.FieldRemoveInstanceName: false,
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/auth_oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -187,6 +188,25 @@ func TestAuthLoginOCI_Login(t *testing.T) {
},
wantErr: false,
},
{
name: "error-vault-token-set",
authLogin: &AuthLoginOCI{
AuthLoginCommon: AuthLoginCommon{
authField: consts.FieldAuthLoginOCI,
params: map[string]interface{}{
consts.FieldRole: "alice",
consts.FieldAuthType: ociAuthTypeAPIKeys,
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "error-uninitialized",
authLogin: &AuthLoginOCI{
Expand All @@ -205,6 +225,7 @@ func TestAuthLoginOCI_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/auth_oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -229,6 +230,25 @@ func TestAuthLoginOIDC_Login(t *testing.T) {
}

tests := []authLoginTest{
{
name: "error-vault-token-set",
authLogin: &AuthLoginOIDC{
AuthLoginCommon{
authField: "baz",
mount: "foo",
params: map[string]interface{}{
consts.FieldRole: "bob",
},
initialized: true,
},
},
handler: &testLoginHandler{
handlerFunc: handlerFunc,
},
token: "foo",
wantErr: true,
expectErr: errors.New("vault login client has a token set"),
},
{
name: "error-uninitialized",
authLogin: &AuthLoginOIDC{
Expand All @@ -246,6 +266,7 @@ func TestAuthLoginOIDC_Login(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testAuthLogin(t, tt)
})
}
Expand Down
Loading