Skip to content

Commit 968ca94

Browse files
authored
Merge pull request #366 from rabbitmq/vault-tls
Support connecting Vault with TLS and check rabbitmqcluster status
2 parents a2e72f1 + 5a03f26 commit 968ca94

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

internal/cluster_reference.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ var SecretStoreClientProvider = GetSecretStoreClient
3232

3333
var (
3434
NoSuchRabbitmqClusterError = errors.New("RabbitmqCluster object does not exist")
35-
ResourceNotAllowedError = errors.New("Resource is not allowed to reference defined cluster reference. Check the namespace of the resource is allowed as part of the cluster's `rabbitmq.com/topology-allowed-namespaces` annotation")
35+
ResourceNotAllowedError = errors.New("resource is not allowed to reference defined cluster reference. Check the namespace of the resource is allowed as part of the cluster's `rabbitmq.com/topology-allowed-namespaces` annotation")
36+
NoServiceReferenceSetError = errors.New("RabbitmqCluster has no ServiceReference set in status.defaultUser")
3637
)
3738

3839
func ParseRabbitmqClusterReference(ctx context.Context, c client.Client, rmq topology.RabbitmqClusterReference, requestNamespace string, clusterDomain string) (ConnectionCredentials, bool, error) {
@@ -60,6 +61,10 @@ func ParseRabbitmqClusterReference(ctx context.Context, c client.Client, rmq top
6061
return nil, false, ResourceNotAllowedError
6162
}
6263

64+
if cluster.Status.DefaultUser == nil || cluster.Status.DefaultUser.ServiceReference == nil {
65+
return nil, false, NoServiceReferenceSetError
66+
}
67+
6368
var user, pass string
6469
if cluster.Spec.SecretBackend.Vault != nil && cluster.Spec.SecretBackend.Vault.DefaultUserPath != "" {
6570
// ask the configured secure store for the credentials available at the path retrieved from the cluster resource
@@ -78,10 +83,6 @@ func ParseRabbitmqClusterReference(ctx context.Context, c client.Client, rmq top
7883
return nil, false, errors.New("no status.binding set")
7984
}
8085

81-
if cluster.Status.DefaultUser == nil {
82-
return nil, false, errors.New("no status.defaultUser set")
83-
}
84-
8586
secret := &corev1.Secret{}
8687
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: cluster.Status.Binding.Name}, secret); err != nil {
8788
return nil, false, err

internal/cluster_reference_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var _ = Describe("ParseRabbitmqClusterReference", func() {
114114

115115
It("errors", func() {
116116
_, _, err := internal.ParseRabbitmqClusterReference(ctx, fakeClient, topology.RabbitmqClusterReference{Name: existingRabbitMQCluster.Name}, existingRabbitMQCluster.Namespace, "")
117-
Expect(err).To(MatchError("no status.defaultUser set"))
117+
Expect(err).To(MatchError(internal.NoServiceReferenceSetError))
118118
})
119119
})
120120

@@ -181,6 +181,35 @@ var _ = Describe("ParseRabbitmqClusterReference", func() {
181181
Expect(passwordBytes).To(Equal([]byte(existingRabbitMQPassword)))
182182
Expect(uriBytes).To(Equal([]byte("http://rmq.rabbitmq-system.svc:15672")))
183183
})
184+
185+
When("RabbitmqCluster does not have status.defaultUser set", func() {
186+
BeforeEach(func() {
187+
*existingRabbitMQCluster = rabbitmqv1beta1.RabbitmqCluster{
188+
ObjectMeta: metav1.ObjectMeta{
189+
Name: "rmq-vault-incomplete-status",
190+
Namespace: namespace,
191+
},
192+
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
193+
SecretBackend: rabbitmqv1beta1.SecretBackend{
194+
Vault: &rabbitmqv1beta1.VaultSpec{
195+
Role: "sausage",
196+
DefaultUserPath: "/some/path",
197+
},
198+
},
199+
},
200+
}
201+
fakeSecretStoreClient = &internalfakes.FakeSecretStoreClient{}
202+
fakeSecretStoreClient.ReadCredentialsReturns(existingRabbitMQUsername, existingRabbitMQPassword, nil)
203+
internal.SecretStoreClientProvider = func() (internal.SecretStoreClient, error) {
204+
return fakeSecretStoreClient, nil
205+
}
206+
})
207+
208+
It("errors", func() {
209+
_, _, err := internal.ParseRabbitmqClusterReference(ctx, fakeClient, topology.RabbitmqClusterReference{Name: existingRabbitMQCluster.Name}, existingRabbitMQCluster.Namespace, "")
210+
Expect(err).To(MatchError(internal.NoServiceReferenceSetError))
211+
})
212+
})
184213
})
185214
})
186215

internal/vault_reader.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package internal
22

33
import (
4+
"crypto/x509"
45
"errors"
56
"fmt"
7+
"net/http"
68
"os"
9+
"strings"
710
"sync"
811
"time"
912

@@ -63,7 +66,24 @@ func GetSecretStoreClient() (SecretStoreClient, error) {
6366
func InitializeClient() func() {
6467
return func() {
6568
// VAULT_ADDR environment variable will be the address that pod uses to communicate with Vault.
69+
// returns error when not set
70+
vaultURL := os.Getenv("VAULT_ADDR")
71+
if vaultURL == "" {
72+
SecretClientCreationError = fmt.Errorf("VAULT_ADDR environment variable not set; cannot initialize vault client")
73+
return
74+
}
75+
6676
config := vault.DefaultConfig() // modify for more granular configuration
77+
78+
if strings.HasPrefix(vaultURL, "https") {
79+
systemCertPool, err := x509.SystemCertPool()
80+
if err != nil {
81+
SecretClientCreationError = fmt.Errorf("failed to retrieve system trusted certs: %w", err)
82+
return
83+
}
84+
config.HttpClient.Transport.(*http.Transport).TLSClientConfig.RootCAs = systemCertPool
85+
}
86+
6787
vaultClient, err := vault.NewClient(config)
6888
if err != nil {
6989
SecretClientCreationError = fmt.Errorf("unable to initialize Vault client: %w", err)

internal/vault_reader_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,12 @@ var _ = Describe("VaultReader", func() {
300300
vaultSpec *rabbitmqv1beta1.VaultSpec
301301
getSecretStoreClientTester func(vaultSpec *rabbitmqv1beta1.VaultSpec) (internal.SecretStoreClient, error)
302302
)
303+
BeforeEach(func() {
304+
os.Setenv("VAULT_ADDR", "vault-address")
305+
})
303306

304307
When("vault role is not set in the environment", func() {
305-
var (
306-
vaultRoleUsedForLogin string
307-
)
308+
var vaultRoleUsedForLogin string
308309

309310
BeforeEach(func() {
310311
internal.FirstLoginAttemptResultCh = make(chan error, 1)
@@ -530,5 +531,13 @@ var _ = Describe("VaultReader", func() {
530531
Expect(secretStoreClient).ToNot(BeNil())
531532
})
532533
})
534+
535+
When("VAULT_ADDR is not set", func() {
536+
It("returns an error", func() {
537+
os.Unsetenv("VAULT_ADDR")
538+
secretStoreClient, err = getSecretStoreClientTester(vaultSpec)
539+
Expect(err).To(MatchError("VAULT_ADDR environment variable not set; cannot initialize vault client"))
540+
})
541+
})
533542
})
534543
})

0 commit comments

Comments
 (0)