Skip to content

Commit b3e8a95

Browse files
author
George Harley
committed
Update Vault login to use env vars instead of cluster spec annotations
* Vault login namespace now found from OPERATOR_VAULT_NAMESPACE env var in the operator container. If not set then use default Vault namespace * Vault login auth path now found from OPERATOR_VAULT_AUTH_PATH env var in the operator container. If not set then default to using the auth path "auth/kubernetes" * Above means that Vault login step no longer uses any information from the current RabbitMQ cluster spec
1 parent 9860d16 commit b3e8a95

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

internal/cluster_reference.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func ParseRabbitmqClusterReference(ctx context.Context, c client.Client, rmq top
6565
svc := &corev1.Service{}
6666
if cluster.Spec.SecretBackend.Vault != nil && cluster.Spec.SecretBackend.Vault.DefaultUserPath != "" {
6767
// ask the configured secure store for the credentials available at the path retrieved from the cluster resource
68-
secretStoreClient, err := SecretStoreClientProvider(cluster.Spec.SecretBackend.Vault)
68+
secretStoreClient, err := SecretStoreClientProvider()
6969
if err != nil {
7070
return nil, nil, nil, fmt.Errorf("unable to create a client connection to secret store: %w", err)
7171
}

internal/cluster_reference_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ var _ = Describe("ParseRabbitmqClusterReference", func() {
158158
fakeCredentialsProvider.DataReturnsOnCall(1, []byte(existingRabbitMQPassword), true)
159159

160160
fakeSecretStoreClient.ReadCredentialsReturns(fakeCredentialsProvider, nil)
161-
internal.SecretStoreClientProvider = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
161+
internal.SecretStoreClientProvider = func() (internal.SecretStoreClient, error) {
162162
return fakeSecretStoreClient, nil
163163
}
164164
})

internal/vault_reader.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internal
33
import (
44
"errors"
55
"fmt"
6-
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
76
"os"
87
"sync"
98
"time"
@@ -56,12 +55,12 @@ var (
5655
SecretClientCreationError error
5756
)
5857

59-
func GetSecretStoreClient(vaultSpec *rabbitmqv1beta1.VaultSpec) (SecretStoreClient, error) {
60-
createSecretStoreClientOnce.Do(InitializeClient(vaultSpec))
58+
func GetSecretStoreClient() (SecretStoreClient, error) {
59+
createSecretStoreClientOnce.Do(InitializeClient())
6160
return SecretClient, SecretClientCreationError
6261
}
6362

64-
func InitializeClient(vaultSpec *rabbitmqv1beta1.VaultSpec) func() {
63+
func InitializeClient() func() {
6564
return func() {
6665
// VAULT_ADDR environment variable will be the address that pod uses to communicate with Vault.
6766
config := vault.DefaultConfig() // modify for more granular configuration
@@ -71,7 +70,7 @@ func InitializeClient(vaultSpec *rabbitmqv1beta1.VaultSpec) func() {
7170
return
7271
}
7372

74-
go renewToken(vaultClient, vaultSpec, FirstLoginAttemptResultCh)
73+
go renewToken(vaultClient, FirstLoginAttemptResultCh)
7574
err = <-FirstLoginAttemptResultCh
7675
if err != nil {
7776
SecretClientCreationError = fmt.Errorf("unable to login to Vault: %w", err)
@@ -152,33 +151,31 @@ func availableKeys(m map[string]interface{}) []string {
152151
return result
153152
}
154153

155-
func login(vaultClient *vault.Client, vaultSpec *rabbitmqv1beta1.VaultSpec) (*vault.Secret, error) {
154+
func login(vaultClient *vault.Client) (*vault.Secret, error) {
156155
logger := ctrl.LoggerFrom(nil)
157156

158-
var annotations = vaultSpec.Annotations
159-
if annotations["vault.hashicorp.com/namespace"] != "" {
160-
vaultClient.SetNamespace(annotations["vault.hashicorp.com/namespace"])
161-
}
162-
163157
jwt, err := ReadServiceAccountTokenFunc()
164158
if err != nil {
165159
return nil, fmt.Errorf("unable to read file containing service account token: %w", err)
166160
}
167161

168-
loginAuthPath := defaultAuthPath
169-
annotations = vaultSpec.Annotations
170-
if annotations["vault.hashicorp.com/auth-path"] != "" {
171-
loginAuthPath = annotations["vault.hashicorp.com/auth-path"]
162+
vaultNamespace := os.Getenv("OPERATOR_VAULT_NAMESPACE")
163+
if vaultNamespace != "" {
164+
vaultClient.SetNamespace(vaultNamespace)
165+
}
166+
167+
loginAuthPath := os.Getenv("OPERATOR_VAULT_AUTH_PATH")
168+
if loginAuthPath == "" {
169+
loginAuthPath = defaultAuthPath
172170
}
173171

174172
role := os.Getenv("OPERATOR_VAULT_ROLE")
175173
if role == "" {
176174
role = defaultVaultRole
177-
logger.Info("Authenticating to Vault using default role value because OPERATOR_VAULT_ROLE env var is not set", "vault role", role)
178-
} else {
179-
logger.Info("Authenticating to Vault using role set from OPERATOR_VAULT_ROLE env var", "vault role", role)
180175
}
181176

177+
logger.Info("Authenticating to Vault", "vault role", role, "vault namespace", vaultNamespace, "vault auth path", loginAuthPath)
178+
182179
vaultSecret, err := ReadVaultClientSecretFunc(vaultClient, string(jwt), role, loginAuthPath)
183180
if err != nil {
184181
return nil, fmt.Errorf("unable to obtain Vault client secret: %w", err)
@@ -192,12 +189,12 @@ func login(vaultClient *vault.Client, vaultSpec *rabbitmqv1beta1.VaultSpec) (*va
192189
return vaultSecret, nil
193190
}
194191

195-
func renewToken(client *vault.Client, vaultSpec *rabbitmqv1beta1.VaultSpec, initialLoginErrorCh chan<- error) {
192+
func renewToken(client *vault.Client, initialLoginErrorCh chan<- error) {
196193
logger := ctrl.LoggerFrom(nil)
197194
sentFirstLoginAttemptErr := false
198195

199196
for {
200-
vaultLoginResp, err := login(client, vaultSpec)
197+
vaultLoginResp, err := login(client)
201198
if err != nil {
202199
logger.Error(err, "unable to authenticate to Vault server")
203200
}

internal/vault_reader_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ var _ = Describe("VaultReader", func() {
325325
}, nil
326326
}
327327
getSecretStoreClientTester = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
328-
internal.InitializeClient(vaultSpec)()
328+
internal.InitializeClient()()
329329
return internal.SecretClient, internal.SecretClientCreationError
330330
}
331331
})
@@ -381,7 +381,7 @@ var _ = Describe("VaultReader", func() {
381381
}, nil
382382
}
383383
getSecretStoreClientTester = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
384-
internal.InitializeClient(vaultSpec)()
384+
internal.InitializeClient()()
385385
return internal.SecretClient, internal.SecretClientCreationError
386386
}
387387
})
@@ -416,7 +416,7 @@ var _ = Describe("VaultReader", func() {
416416
Role: "cheese-and-ham",
417417
}
418418
getSecretStoreClientTester = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
419-
internal.InitializeClient(vaultSpec)()
419+
internal.InitializeClient()()
420420
return internal.SecretClient, internal.SecretClientCreationError
421421
}
422422
})
@@ -450,7 +450,7 @@ var _ = Describe("VaultReader", func() {
450450
return nil, errors.New("login failed (quickly!)")
451451
}
452452
getSecretStoreClientTester = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
453-
internal.InitializeClient(vaultSpec)()
453+
internal.InitializeClient()()
454454
return internal.SecretClient, internal.SecretClientCreationError
455455
}
456456
})
@@ -500,7 +500,7 @@ var _ = Describe("VaultReader", func() {
500500
}, nil
501501
}
502502
getSecretStoreClientTester = func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error) {
503-
internal.InitializeClient(vaultSpec)()
503+
internal.InitializeClient()()
504504
return internal.SecretClient, internal.SecretClientCreationError
505505
}
506506
})

0 commit comments

Comments
 (0)