Skip to content

Commit e5397c8

Browse files
BojanZelicwozniakjanzroubalik
authored
KEDA Hashicorp vault service account token request (#6446)
* General: Vault authentication via cross-namespace service accounts Signed-off-by: Bojan Zelic <[email protected]> * General: Vault authentication via cross-namespace service accounts Signed-off-by: Bojan Zelic <[email protected]> * General: Vault authentication via cross-namespace service accounts Signed-off-by: Bojan Zelic <[email protected]> * General: Vault authentication via cross-namespace service accounts Signed-off-by: Bojan Zelic <[email protected]> * add e2e test Signed-off-by: Bojan Zelic <[email protected]> * combine logic to retreive service account tokens Signed-off-by: Bojan Zelic <[email protected]> * combine logic to retreive service account tokens Signed-off-by: Bojan Zelic <[email protected]> * combine logic to retreive service account tokens Signed-off-by: Bojan Zelic <[email protected]> * Update CHANGELOG.md Signed-off-by: Bojan Zelic <[email protected]> * Update pkg/scaling/resolver/hashicorpvault_handler.go Co-authored-by: Jan Wozniak <[email protected]> Signed-off-by: Bojan Zelic <[email protected]> * Rename patch_operator.yaml to patch_operator.yml Signed-off-by: Bojan Zelic <[email protected]> * fix order of changelog Signed-off-by: Bojan Zelic <[email protected]> * Update CHANGELOG.md Signed-off-by: Bojan Zelic <[email protected]> --------- Signed-off-by: Bojan Zelic <[email protected]> Co-authored-by: Jan Wozniak <[email protected]> Co-authored-by: Zbynek Roubalik <[email protected]>
1 parent e438e93 commit e5397c8

File tree

8 files changed

+306
-76
lines changed

8 files changed

+306
-76
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
6969
- **General**: Enable support on s390x for KEDA ([#6543](https://github.com/kedacore/keda/issues/6543))
7070
- **General**: Introduce new Solace Direct Messaging scaler ([#6545](https://github.com/kedacore/keda/issues/6545))
7171
- **General**: Introduce new Sumo Logic Scaler ([#6734](https://github.com/kedacore/keda/issues/6734))
72+
- **General**: Vault authentication via cross-namespace service accounts ([#6153](https://github.com/kedacore/keda/issues/6153))
7273

7374
#### Experimental
7475

apis/keda/v1alpha1/triggerauthentication_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ type Credential struct {
239239

240240
// +optional
241241
ServiceAccount string `json:"serviceAccount,omitempty"`
242+
243+
// +optional
244+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
242245
}
243246

244247
// VaultAuthentication contains the list of Hashicorp Vault authentication methods

config/crd/bases/keda.sh_clustertriggerauthentications.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ spec:
451451
properties:
452452
serviceAccount:
453453
type: string
454+
serviceAccountName:
455+
type: string
454456
token:
455457
type: string
456458
type: object

config/crd/bases/keda.sh_triggerauthentications.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ spec:
450450
properties:
451451
serviceAccount:
452452
type: string
453+
serviceAccountName:
454+
type: string
453455
token:
454456
type: string
455457
type: object

pkg/scaling/resolver/hashicorpvault_handler.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package resolver
1818

1919
import (
20+
"context"
2021
"errors"
2122
"fmt"
2223
"os"
@@ -26,19 +27,24 @@ import (
2627
vaultapi "github.com/hashicorp/vault/api"
2728

2829
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
30+
"github.com/kedacore/keda/v2/pkg/scalers/authentication"
2931
)
3032

3133
// HashicorpVaultHandler is a specification of HashiCorp Vault
3234
type HashicorpVaultHandler struct {
33-
vault *kedav1alpha1.HashiCorpVault
34-
client *vaultapi.Client
35-
stopCh chan struct{}
35+
vault *kedav1alpha1.HashiCorpVault
36+
client *vaultapi.Client
37+
acs *authentication.AuthClientSet
38+
namespace string
39+
stopCh chan struct{}
3640
}
3741

3842
// NewHashicorpVaultHandler creates a HashicorpVaultHandler object
39-
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault) *HashicorpVaultHandler {
43+
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault, acs *authentication.AuthClientSet, namespace string) *HashicorpVaultHandler {
4044
return &HashicorpVaultHandler{
41-
vault: v,
45+
vault: v,
46+
acs: acs,
47+
namespace: namespace,
4248
}
4349
}
4450

@@ -87,6 +93,8 @@ func (vh *HashicorpVaultHandler) Initialize(logger logr.Logger) error {
8793
// token Extract a vault token from the Authentication method
8894
func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error) {
8995
var token string
96+
var jwt []byte
97+
var err error
9098

9199
switch vh.vault.Authentication {
92100
case kedav1alpha1.VaultAuthenticationToken:
@@ -115,23 +123,27 @@ func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error)
115123
vh.vault.Credential = &defaultCred
116124
}
117125

118-
if len(vh.vault.Credential.ServiceAccount) == 0 {
119-
return token, errors.New("k8s SA file not in config")
126+
if vh.vault.Credential.ServiceAccountName == "" && vh.vault.Credential.ServiceAccount == "" {
127+
return token, errors.New("k8s SA file not in config or serviceAccountName not supplied")
120128
}
121129

122-
// Get the JWT from POD
123-
jwt, err := os.ReadFile(vh.vault.Credential.ServiceAccount)
124-
if err != nil {
125-
return token, err
130+
if vh.vault.Credential.ServiceAccountName != "" {
131+
jwt = []byte(GenerateBoundServiceAccountToken(context.Background(), vh.vault.Credential.ServiceAccountName, vh.namespace, vh.acs))
132+
} else if len(vh.vault.Credential.ServiceAccount) != 0 {
133+
// Get the JWT from POD
134+
jwt, err = os.ReadFile(vh.vault.Credential.ServiceAccount)
135+
if err != nil {
136+
return token, err
137+
}
126138
}
127139

128140
data := map[string]interface{}{"jwt": string(jwt), "role": vh.vault.Role}
129141
secret, err := client.Logical().Write(fmt.Sprintf("auth/%s/login", vh.vault.Mount), data)
130142
if err != nil {
131143
return token, err
132144
}
133-
134145
token = secret.Auth.ClientToken
146+
135147
default:
136148
return token, fmt.Errorf("vault auth method %s is not supported", vh.vault.Authentication)
137149
}

0 commit comments

Comments
 (0)