-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VAULT-12564 Add new token_file auto-auth method (#18740)
* VAULT-12564 Work so far on token file auto-auth * VAULT-12564 remove lifetime watcher struct modifications * VAULT-12564 add other config items, and clean up * VAULT-12564 clean-up and more tests * VAULT-12564 clean-up * VAULT-12564 lookup-self and some clean-up * VAULT-12564 safer client usage * VAULT-12564 some clean-up * VAULT-12564 changelog * VAULT-12564 some clean-ups * VAULT-12564 batch token warning * VAULT-12564 remove follow_symlink reference * VAULT-12564 Remove redundant stat, change temp file creation * VAULT-12564 Remove ability to delete token after auth
- Loading branch information
1 parent
2ffe49a
commit 17be102
Showing
6 changed files
with
423 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
agent: Added `token_file` auto-auth configuration to allow using a pre-existing token for Vault Agent. | ||
``` |
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
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,83 @@ | ||
package token_file | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/api" | ||
"github.com/hashicorp/vault/command/agent/auth" | ||
) | ||
|
||
type tokenFileMethod struct { | ||
logger hclog.Logger | ||
mountPath string | ||
|
||
cachedToken string | ||
tokenFilePath string | ||
} | ||
|
||
func NewTokenFileAuthMethod(conf *auth.AuthConfig) (auth.AuthMethod, error) { | ||
if conf == nil { | ||
return nil, errors.New("empty config") | ||
} | ||
if conf.Config == nil { | ||
return nil, errors.New("empty config data") | ||
} | ||
|
||
a := &tokenFileMethod{ | ||
logger: conf.Logger, | ||
mountPath: "auth/token", | ||
} | ||
|
||
tokenFilePathRaw, ok := conf.Config["token_file_path"] | ||
if !ok { | ||
return nil, errors.New("missing 'token_file_path' value") | ||
} | ||
a.tokenFilePath, ok = tokenFilePathRaw.(string) | ||
if !ok { | ||
return nil, errors.New("could not convert 'token_file_path' config value to string") | ||
} | ||
if a.tokenFilePath == "" { | ||
return nil, errors.New("'token_file_path' value is empty") | ||
} | ||
|
||
return a, nil | ||
} | ||
|
||
func (a *tokenFileMethod) Authenticate(ctx context.Context, client *api.Client) (string, http.Header, map[string]interface{}, error) { | ||
token, err := os.ReadFile(a.tokenFilePath) | ||
if err != nil { | ||
if a.cachedToken == "" { | ||
return "", nil, nil, fmt.Errorf("error reading token file and no cached token known: %w", err) | ||
} | ||
a.logger.Warn("error reading token file", "error", err) | ||
} | ||
if len(token) == 0 { | ||
if a.cachedToken == "" { | ||
return "", nil, nil, errors.New("token file empty and no cached token known") | ||
} | ||
a.logger.Warn("token file exists but read empty value, re-using cached value") | ||
} else { | ||
a.cachedToken = strings.TrimSpace(string(token)) | ||
} | ||
|
||
// i.e. auth/token/lookup-self | ||
return fmt.Sprintf("%s/lookup-self", a.mountPath), nil, map[string]interface{}{ | ||
"token": a.cachedToken, | ||
}, nil | ||
} | ||
|
||
func (a *tokenFileMethod) NewCreds() chan struct{} { | ||
return nil | ||
} | ||
|
||
func (a *tokenFileMethod) CredSuccess() { | ||
} | ||
|
||
func (a *tokenFileMethod) Shutdown() { | ||
} |
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,81 @@ | ||
package token_file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/command/agent/auth" | ||
"github.com/hashicorp/vault/sdk/helper/logging" | ||
) | ||
|
||
func TestNewTokenFileAuthMethodEmptyConfig(t *testing.T) { | ||
logger := logging.NewVaultLogger(log.Trace) | ||
_, err := NewTokenFileAuthMethod(&auth.AuthConfig{ | ||
Logger: logger.Named("auth.method"), | ||
Config: map[string]interface{}{}, | ||
}) | ||
if err == nil { | ||
t.Fatal("Expected error due to empty config") | ||
} | ||
} | ||
|
||
func TestNewTokenFileEmptyFilePath(t *testing.T) { | ||
logger := logging.NewVaultLogger(log.Trace) | ||
_, err := NewTokenFileAuthMethod(&auth.AuthConfig{ | ||
Logger: logger.Named("auth.method"), | ||
Config: map[string]interface{}{ | ||
"token_file_path": "", | ||
}, | ||
}) | ||
if err == nil { | ||
t.Fatalf("Expected error when giving empty file path") | ||
} | ||
} | ||
|
||
func TestNewTokenFileAuthenticate(t *testing.T) { | ||
tokenFile, err := os.Create(filepath.Join(t.TempDir(), "token_file")) | ||
tokenFileContents := "super-secret-token" | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tokenFileName := tokenFile.Name() | ||
tokenFile.Close() // WriteFile doesn't need it open | ||
os.WriteFile(tokenFileName, []byte(tokenFileContents), 0o666) | ||
defer os.Remove(tokenFileName) | ||
|
||
logger := logging.NewVaultLogger(log.Trace) | ||
am, err := NewTokenFileAuthMethod(&auth.AuthConfig{ | ||
Logger: logger.Named("auth.method"), | ||
Config: map[string]interface{}{ | ||
"token_file_path": tokenFileName, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
path, headers, data, err := am.Authenticate(nil, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if path != "auth/token/lookup-self" { | ||
t.Fatalf("Incorrect path, was %s", path) | ||
} | ||
if headers != nil { | ||
t.Fatalf("Expected no headers, instead got %v", headers) | ||
} | ||
if data == nil { | ||
t.Fatal("Data was nil") | ||
} | ||
tokenDataFromAuthMethod := data["token"].(string) | ||
if tokenDataFromAuthMethod != tokenFileContents { | ||
t.Fatalf("Incorrect token file contents return by auth method, expected %s, got %s", tokenFileContents, tokenDataFromAuthMethod) | ||
} | ||
|
||
_, err = os.Stat(tokenFileName) | ||
if err != nil { | ||
t.Fatal("Token file removed") | ||
} | ||
} |
Oops, something went wrong.