Skip to content

Commit c7cdbbf

Browse files
authored
Adding private connection string counterparts. (#344)
Authored-by: Jeremy Hill <[email protected]>
1 parent 587b993 commit c7cdbbf

File tree

6 files changed

+38
-10
lines changed

6 files changed

+38
-10
lines changed

pkg/api/v1/status/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/atlasdatabaseuser/connectionsecrets.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ func CreateOrUpdateConnectionSecrets(ctx *workflow.Context, k8sClient client.Cli
5151
return workflow.Terminate(workflow.DatabaseUserConnectionSecretsNotCreated, err.Error())
5252
}
5353
data := connectionsecret.ConnectionData{
54-
DBUserName: dbUser.Spec.Username,
55-
ConnURL: cluster.ConnectionStrings.Standard,
56-
SrvConnURL: cluster.ConnectionStrings.StandardSrv,
57-
Password: password,
54+
DBUserName: dbUser.Spec.Username,
55+
ConnURL: cluster.ConnectionStrings.Standard,
56+
SrvConnURL: cluster.ConnectionStrings.StandardSrv,
57+
PvtConnURL: cluster.ConnectionStrings.Private,
58+
PvtSrvConnURL: cluster.ConnectionStrings.PrivateSrv,
59+
Password: password,
5860
}
5961
var secretName string
6062
if secretName, err = connectionsecret.Ensure(k8sClient, dbUser.Namespace, project.Spec.Name, project.ID(), cluster.Name, data); err != nil {

pkg/controller/connectionsecret/ensuresecret.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ const (
1919

2020
connectionSecretStdKey string = "connectionStringStandard"
2121
connectionSecretStdSrvKey string = "connectionStringStandardSrv"
22+
connectionSecretPvtKey string = "connectionStringPrivate"
23+
connectionSecretPvtSrvKey string = "connectionStringPrivateSrv"
2224
userNameKey string = "username"
2325
passwordKey string = "password"
2426
)
2527

2628
type ConnectionData struct {
27-
DBUserName, ConnURL, SrvConnURL, Password string
29+
DBUserName, ConnURL, SrvConnURL, PvtConnURL, PvtSrvConnURL, Password string
2830
}
2931

3032
// Ensure creates or updates the connection Secret for the specific cluster and db user. Returns the name of the Secret
@@ -50,20 +52,32 @@ func Ensure(client client.Client, namespace, projectName, projectID, clusterName
5052
}
5153

5254
func fillSecret(secret *corev1.Secret, projectID string, clusterName string, data ConnectionData) error {
53-
var connURL, srvConnURL string
55+
var connURL, srvConnURL, pvtConnURL, pvtSrvConnURL string
5456
var err error
5557
if connURL, err = AddCredentialsToConnectionURL(data.ConnURL, data.DBUserName, data.Password); err != nil {
5658
return err
5759
}
5860
if srvConnURL, err = AddCredentialsToConnectionURL(data.SrvConnURL, data.DBUserName, data.Password); err != nil {
5961
return err
6062
}
63+
if data.PvtConnURL != "" {
64+
if pvtConnURL, err = AddCredentialsToConnectionURL(data.PvtConnURL, data.DBUserName, data.Password); err != nil {
65+
return err
66+
}
67+
}
68+
if data.PvtSrvConnURL != "" {
69+
if pvtSrvConnURL, err = AddCredentialsToConnectionURL(data.PvtSrvConnURL, data.DBUserName, data.Password); err != nil {
70+
return err
71+
}
72+
}
6173

6274
secret.Labels = map[string]string{ProjectLabelKey: projectID, ClusterLabelKey: kube.NormalizeLabelValue(clusterName)}
6375

6476
secret.Data = map[string][]byte{
6577
connectionSecretStdKey: []byte(connURL),
6678
connectionSecretStdSrvKey: []byte(srvConnURL),
79+
connectionSecretPvtKey: []byte(pvtConnURL),
80+
connectionSecretPvtSrvKey: []byte(pvtSrvConnURL),
6781
userNameKey: []byte(data.DBUserName),
6882
passwordKey: []byte(data.Password),
6983
}

pkg/controller/connectionsecret/ensuresecret_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func validateSecret(t *testing.T, fakeClient client.Client, namespace, projectNa
8686
expectedData := map[string][]byte{
8787
"connectionStringStandard": []byte(buildConnectionURL(data.ConnURL, data.DBUserName, data.Password)),
8888
"connectionStringStandardSrv": []byte(buildConnectionURL(data.SrvConnURL, data.DBUserName, data.Password)),
89+
"connectionStringPrivate": []byte(buildConnectionURL(data.PvtConnURL, data.DBUserName, data.Password)),
90+
"connectionStringPrivateSrv": []byte(buildConnectionURL(data.PvtSrvConnURL, data.DBUserName, data.Password)),
8991
"username": []byte(data.DBUserName),
9092
"password": []byte(data.Password),
9193
}
@@ -109,9 +111,11 @@ func buildConnectionURL(connURL, userName, password string) string {
109111

110112
func dataForSecret() ConnectionData {
111113
return ConnectionData{
112-
DBUserName: "admin",
113-
ConnURL: "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?authSource=admin",
114-
SrvConnURL: "mongodb+srv://mongodb.example.com:27017/?authSource=admin",
115-
Password: "m@gick%",
114+
DBUserName: "admin",
115+
ConnURL: "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?authSource=admin",
116+
SrvConnURL: "mongodb+srv://mongodb.example.com:27017/?authSource=admin",
117+
PvtConnURL: "mongodb://mongodb0-pri.example.com:27017,mongodb1-pri.example.com:27017/?authSource=admin",
118+
PvtSrvConnURL: "mongodb+srv://mongodb-pri.example.com:27017/?authSource=admin",
119+
Password: "m@gick%",
116120
}
117121
}

test/int/dbuser_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ func validateSecret(k8sClient client.Client, project mdbv1.AtlasProject, cluster
673673
expectedData := map[string][]byte{
674674
"connectionStringStandard": []byte(buildConnectionURL(c.ConnectionStrings.Standard, username, password)),
675675
"connectionStringStandardSrv": []byte(buildConnectionURL(c.ConnectionStrings.StandardSrv, username, password)),
676+
"connectionStringPrivate": []byte(buildConnectionURL(c.ConnectionStrings.Private, username, password)),
677+
"connectionStringPrivateSrv": []byte(buildConnectionURL(c.ConnectionStrings.PrivateSrv, username, password)),
676678
"username": []byte(username),
677679
"password": []byte(password),
678680
}
@@ -700,6 +702,10 @@ func checkNumberOfConnectionSecrets(k8sClient client.Client, project mdbv1.Atlas
700702
}
701703

702704
func buildConnectionURL(connURL, userName, password string) string {
705+
if connURL == "" {
706+
return ""
707+
}
708+
703709
u, err := connectionsecret.AddCredentialsToConnectionURL(connURL, userName, password)
704710
Expect(err).NotTo(HaveOccurred())
705711
return u

0 commit comments

Comments
 (0)