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

Re-read JWT file for every authentication #491

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion internal/keystore/vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package vault
import (
"context"
"errors"
"os"
"path"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -104,9 +106,18 @@ func (c *client) AuthenticateWithK8S(login *Kubernetes) authFunc {
client = client.WithNamespace(login.Namespace)
}

jwt := login.JWT
if strings.ContainsRune(jwt, '/') || strings.ContainsRune(jwt, os.PathSeparator) {
jwtBytes, err := os.ReadFile(jwt)
if err != nil {
return nil, err
}
jwt = string(jwtBytes)
}

secret, err := client.Logical().WriteWithContext(ctx, path.Join("auth", login.Engine, "login"), map[string]interface{}{
"role": login.Role,
"jwt": login.JWT,
"jwt": jwt,
})
if secret == nil && err == nil {
// The Vault SDK eventually returns no error but also no
Expand Down
4 changes: 2 additions & 2 deletions kesconf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ func ymlToKeyStore(y *ymlFile) (KeyStore, error) {
// We always check for '/' and the OS-specific one make cover cases where
// a path is specified using '/' but the underlying OS is e.g. windows.
if jwt := y.KeyStore.Vault.Kubernetes.JWT.Value; strings.ContainsRune(jwt, '/') || strings.ContainsRune(jwt, os.PathSeparator) {
b, err := os.ReadFile(y.KeyStore.Vault.Kubernetes.JWT.Value)
_, err := os.ReadFile(y.KeyStore.Vault.Kubernetes.JWT.Value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this check to ensure that the file can be read during start-up for verification only.

if err != nil {
return nil, fmt.Errorf("kesconf: failed to read vault kubernetes JWT from '%s': %v", y.KeyStore.Vault.Kubernetes.JWT.Value, err)
}
y.KeyStore.Vault.Kubernetes.JWT.Value = string(b)
// postpone resolving the JWT until actually logging in
}
}
if y.KeyStore.Vault.Transit != nil {
Expand Down
Loading