-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Keda vault service account token request #6446
Open
BojanZelic
wants to merge
6
commits into
kedacore:main
Choose a base branch
from
BojanZelic:keda-vault-service-account-token-request
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−35
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01af5fd
General: Vault authentication via cross-namespace service accounts
BojanZelic cc92f15
Merge branch 'main' of github.com:kedacore/keda into keda-vault-servi…
BojanZelic 097fec2
General: Vault authentication via cross-namespace service accounts
BojanZelic a141b41
General: Vault authentication via cross-namespace service accounts
BojanZelic 95e7c47
General: Vault authentication via cross-namespace service accounts
BojanZelic bfa2613
add e2e test
BojanZelic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,29 +17,41 @@ limitations under the License. | |
package resolver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
"github.com/go-logr/logr" | ||
vaultapi "github.com/hashicorp/vault/api" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" | ||
authenticationv1 "k8s.io/api/authentication/v1" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// HashicorpVaultHandler is specification of Hashi Corp Vault | ||
type HashicorpVaultHandler struct { | ||
vault *kedav1alpha1.HashiCorpVault | ||
client *vaultapi.Client | ||
stopCh chan struct{} | ||
vault *kedav1alpha1.HashiCorpVault | ||
client *vaultapi.Client | ||
k8sClient client.Client | ||
namespace string | ||
stopCh chan struct{} | ||
} | ||
|
||
// NewHashicorpVaultHandler creates a HashicorpVaultHandler object | ||
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault) *HashicorpVaultHandler { | ||
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault, client client.Client, namespace string) *HashicorpVaultHandler { | ||
return &HashicorpVaultHandler{ | ||
vault: v, | ||
vault: v, | ||
k8sClient: client, | ||
namespace: namespace, | ||
} | ||
} | ||
|
||
|
@@ -88,6 +100,8 @@ func (vh *HashicorpVaultHandler) Initialize(logger logr.Logger) error { | |
// token Extract a vault token from the Authentication method | ||
func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error) { | ||
var token string | ||
var jwt []byte | ||
var err error | ||
|
||
switch vh.vault.Authentication { | ||
case kedav1alpha1.VaultAuthenticationToken: | ||
|
@@ -116,23 +130,68 @@ func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error) | |
vh.vault.Credential = &defaultCred | ||
} | ||
|
||
if len(vh.vault.Credential.ServiceAccount) == 0 { | ||
return token, errors.New("k8s SA file not in config") | ||
if vh.vault.Credential.ServiceAccountName == "" && len(vh.vault.Credential.ServiceAccount) == 0 { | ||
return token, errors.New("k8s SA file not in config or serviceAccountName not supplied") | ||
} | ||
|
||
// Get the JWT from POD | ||
jwt, err := os.ReadFile(vh.vault.Credential.ServiceAccount) | ||
if err != nil { | ||
return token, err | ||
if vh.vault.Credential.ServiceAccountName != "" { | ||
// generate token from namespace | ||
saName := types.NamespacedName{Name: vh.vault.Credential.ServiceAccountName, Namespace: vh.namespace} | ||
sa := &corev1.ServiceAccount{} | ||
secret := &corev1.Secret{} | ||
|
||
if err = vh.k8sClient.Get(context.Background(), saName, sa); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return token, errors.New(fmt.Sprintf("Failed to retreive service account name: %s namespace: %s", saName.Name, saName.Namespace)) | ||
} | ||
} | ||
|
||
if len(sa.Secrets) > 0 { | ||
//using legacy service account secrets | ||
secretName := types.NamespacedName{Name: sa.Secrets[0].Name, Namespace: vh.namespace} | ||
|
||
if err = vh.k8sClient.Get(context.Background(), secretName, secret); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return token, errors.New(fmt.Sprintf("Failed to retreive secret for service account name: %s namespace: %s", secretName.Name, secretName.Namespace)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here should return other errs as well. |
||
} | ||
|
||
jwt = secret.Data["token"] | ||
} | ||
|
||
if len(jwt) == 0 { | ||
tokenTTL := int64(600) // min allowed duration is 10 mins | ||
// this token is only used once for the initial authentication | ||
// renewals happen independently on the vault token | ||
tokenRequest := &authenticationv1.TokenRequest{ | ||
Spec: authenticationv1.TokenRequestSpec{ | ||
Audiences: []string{"https://kubernetes.default.svc"}, | ||
ExpirationSeconds: &tokenTTL, | ||
}, | ||
} | ||
|
||
if err := vh.k8sClient.SubResource("token").Create(context.Background(), sa, tokenRequest); err != nil { | ||
return token, errors.New(fmt.Sprintf("Failed to create token for service account name: %s namespace: %s", saName.Name, saName.Namespace)) | ||
} | ||
|
||
jwt = []byte(tokenRequest.Status.Token) | ||
} | ||
|
||
} else if len(vh.vault.Credential.ServiceAccount) != 0 { | ||
// Get the JWT from POD | ||
jwt, err = os.ReadFile(vh.vault.Credential.ServiceAccount) | ||
if err != nil { | ||
return token, err | ||
} | ||
} | ||
|
||
data := map[string]interface{}{"jwt": string(jwt), "role": vh.vault.Role} | ||
secret, err := client.Logical().Write(fmt.Sprintf("auth/%s/login", vh.vault.Mount), data) | ||
if err != nil { | ||
return token, err | ||
} | ||
|
||
token = secret.Auth.ClientToken | ||
|
||
default: | ||
return token, fmt.Errorf("vault auth method %s is not supported", vh.vault.Authentication) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here should return other errs as well.