From 7a5f78e380f294ae95fe250fbde408ad7ce0b23a Mon Sep 17 00:00:00 2001 From: Tom Proctor Date: Mon, 4 Mar 2024 17:19:42 +0000 Subject: [PATCH] Reinstate testing token helper but in original command/token package --- command/auth_test.go | 4 +-- command/login_test.go | 4 +-- command/token/helper_testing.go | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 command/token/helper_testing.go diff --git a/command/auth_test.go b/command/auth_test.go index 615bc46aecab..e6c895df8fe1 100644 --- a/command/auth_test.go +++ b/command/auth_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/hashicorp/cli" - "github.com/hashicorp/vault/api/tokenhelper" + "github.com/hashicorp/vault/command/token" ) func testAuthCommand(tb testing.TB) (*cli.MockUi, *AuthCommand) { @@ -19,7 +19,7 @@ func testAuthCommand(tb testing.TB) (*cli.MockUi, *AuthCommand) { UI: ui, // Override to our own token helper - tokenHelper: tokenhelper.NewTestingTokenHelper(), + tokenHelper: token.NewTestingTokenHelper(), }, } } diff --git a/command/login_test.go b/command/login_test.go index 238f93a4e481..3f7c01d3c86e 100644 --- a/command/login_test.go +++ b/command/login_test.go @@ -12,9 +12,9 @@ import ( "github.com/hashicorp/cli" "github.com/hashicorp/vault/api" - "github.com/hashicorp/vault/api/tokenhelper" credToken "github.com/hashicorp/vault/builtin/credential/token" credUserpass "github.com/hashicorp/vault/builtin/credential/userpass" + "github.com/hashicorp/vault/command/token" "github.com/hashicorp/vault/helper/testhelpers" "github.com/hashicorp/vault/vault" ) @@ -33,7 +33,7 @@ func testLoginCommand(tb testing.TB) (*cli.MockUi, *LoginCommand) { UI: ui, // Override to our own token helper - tokenHelper: tokenhelper.NewTestingTokenHelper(), + tokenHelper: token.NewTestingTokenHelper(), }, Handlers: map[string]LoginHandler{ "token": &credToken.CLIHandler{}, diff --git a/command/token/helper_testing.go b/command/token/helper_testing.go new file mode 100644 index 000000000000..a536c3c3cc52 --- /dev/null +++ b/command/token/helper_testing.go @@ -0,0 +1,47 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package token + +import ( + "sync" + + "github.com/hashicorp/vault/api/tokenhelper" +) + +var _ tokenhelper.TokenHelper = (*TestingTokenHelper)(nil) + +// TestingTokenHelper implements token.TokenHelper which runs entirely +// in-memory. This should not be used outside of testing. +type TestingTokenHelper struct { + lock sync.RWMutex + token string +} + +func NewTestingTokenHelper() *TestingTokenHelper { + return &TestingTokenHelper{} +} + +func (t *TestingTokenHelper) Erase() error { + t.lock.Lock() + defer t.lock.Unlock() + t.token = "" + return nil +} + +func (t *TestingTokenHelper) Get() (string, error) { + t.lock.RLock() + defer t.lock.RUnlock() + return t.token, nil +} + +func (t *TestingTokenHelper) Path() string { + return "" +} + +func (t *TestingTokenHelper) Store(token string) error { + t.lock.Lock() + defer t.lock.Unlock() + t.token = token + return nil +}