Skip to content

Commit bfa9fd2

Browse files
committed
Set mongo user credentials. Close db connections.
1 parent faf7c4a commit bfa9fd2

15 files changed

+260
-109
lines changed

api/v1beta1/mongodb_types.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
)
2222

23+
type MongoDBRootSecretLookup struct {
24+
Name string `json:"name"`
25+
Namespace string `json:"namespace"`
26+
Field string `json:"field"`
27+
}
28+
2329
type MongoDBCredentials []MongoDBCredential
2430
type MongoDBCredential struct {
2531
UserName string `json:"username"`
@@ -29,16 +35,14 @@ type MongoDBCredential struct {
2935
// MongoDBSpec defines the desired state of MongoDB
3036
// IMPORTANT: Run "make" to regenerate code after modifying this file
3137
type MongoDBSpec struct {
32-
// Database name
3338
DatabaseName string `json:"databaseName"`
34-
// Database Server host name and port
35-
HostName string `json:"hostName"`
39+
HostName string `json:"hostName"`
3640
// +optional
3741
RootUsername string `json:"rootUsername"`
3842
// +optional
39-
RootAuthenticationDatabase string `json:"rootAuthDatabase"`
40-
// Database credentials
41-
Credentials MongoDBCredentials `json:"credentials"`
43+
RootAuthenticationDatabase string `json:"rootAuthDatabase"`
44+
RootSecretLookup MongoDBRootSecretLookup `json:"rootSecretLookup"`
45+
Credentials MongoDBCredentials `json:"credentials"`
4246
}
4347

4448
// MongoDBStatus defines the observed state of MongoDB

api/v1beta1/postgresql_types.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,14 @@ type PostgreSQLCredential struct {
4242
// PostgreSQLSpec defines the desired state of PostgreSQL
4343
// IMPORTANT: Run "make" to regenerate code after modifying this file
4444
type PostgreSQLSpec struct {
45-
// Database name
4645
DatabaseName string `json:"databaseName"`
47-
// Database Server host name
48-
Host string `json:"host"`
49-
Port int64 `json:"port"`
46+
HostName string `json:"hostName"`
5047
// +optional
5148
RootUsername string `json:"rootUsername"`
5249
// +optional
5350
RootAuthenticationDatabase string `json:"rootAuthDatabase"`
5451
RootSecretLookup PostgreSQLRootSecretLookup `json:"rootSecretLookup"`
55-
// Database credentials
56-
Credentials PostgreSQLCredentials `json:"credentials"`
52+
Credentials PostgreSQLCredentials `json:"credentials"`
5753
}
5854

5955
// PostgreSQLStatus defines the observed state of PostgreSQL

api/v1beta1/zz_generated.deepcopy.go

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

common/db/mongodb/cache.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package mongodb
2+
3+
type Cache struct {
4+
cache map[string]*MongoDBServer
5+
}
6+
7+
func NewCache() *Cache {
8+
c := make(map[string]*MongoDBServer)
9+
return &Cache{
10+
cache: c,
11+
}
12+
}
13+
14+
func (c *Cache) Get(host string, rootUsername string, rootPassword string, rootAuthenticationDatabase string) (*MongoDBServer, error) {
15+
if _, ok := c.cache[host]; !ok {
16+
if server, err := NewMongoDBServer(host, rootUsername, rootPassword, rootAuthenticationDatabase); err != nil {
17+
return nil, err
18+
} else {
19+
c.cache[host] = server
20+
}
21+
}
22+
return c.cache[host], nil
23+
}
24+
25+
func (c *Cache) Remove(host string) {
26+
server := c.cache[host]
27+
if server != nil {
28+
_ = server.Close()
29+
}
30+
delete(c.cache, host)
31+
}

common/db/mongodb/repository.go

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mongodb
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"go.mongodb.org/mongo-driver/bson"
78
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -17,19 +18,13 @@ type Role struct {
1718
DB string `json:"db" bson:"db"`
1819
}
1920

20-
type Users struct {
21-
Users []User `json:"users" bson:"users"`
22-
}
21+
type Users []User
2322
type User struct {
2423
User string `json:"user" bson:"user"`
2524
DB string `json:"db" bson:"db"`
2625
Roles Roles `json:"roles" bson:"roles"`
2726
}
2827

29-
type UserHolder struct {
30-
User Users `json:"user" bson:"user"`
31-
}
32-
3328
type MongoDBServer struct {
3429
client *mongo.Client
3530
uri string
@@ -61,47 +56,80 @@ func NewMongoDBServer(uri string, rootUser string, rootPassword string, authenti
6156
}, nil
6257
}
6358

64-
func (m *MongoDBServer) SetupUser(database string, username string, password string) (string, error) {
59+
func (m *MongoDBServer) Close() error {
60+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
61+
defer cancel()
62+
return m.client.Disconnect(ctx)
63+
}
64+
65+
func (m *MongoDBServer) SetupUser(database string, username string, password string) error {
6566
doesUserExist, err := m.doesUserExist(database, username)
6667
if err != nil {
67-
return "", err
68+
return err
6869
}
6970
if !doesUserExist {
70-
return m.createUser(database, username, password)
71+
if err := m.createUser(database, username, password); err != nil {
72+
return err
73+
}
74+
if doesUserExistNow, err := m.doesUserExist(database, username); err != nil {
75+
return err
76+
} else if !doesUserExistNow {
77+
return errors.New("user doesn't exist after create")
78+
}
79+
} else {
80+
if err := m.updateUserPasswordAndRoles(database, username, password); err != nil {
81+
return err
82+
}
7183
}
72-
return "user already exists", nil
84+
return nil
7385
}
7486

7587
func (m *MongoDBServer) doesUserExist(database string, username string) (bool, error) {
76-
command := &bson.D{primitive.E{Key: "usersInfo", Value: username}}
77-
r := m.runCommand(database, command)
78-
if err := r.Err(); err != nil {
88+
users, err := m.getAllUsers(database, username)
89+
if err != nil {
7990
return false, err
8091
}
81-
var user Users
82-
if err := r.Decode(&user); err != nil {
83-
return false, err
92+
return users != nil && len(users) > 0, nil
93+
}
94+
95+
func (m *MongoDBServer) getAllUsers(database string, username string) (Users, error) {
96+
users := make(Users, 0)
97+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
98+
defer cancel()
99+
collection := m.client.Database(m.authenticationDatabase).Collection("system.users")
100+
cursor, err := collection.Find(ctx, bson.D{primitive.E{Key: "user", Value: username}, primitive.E{Key: "db", Value: database}})
101+
if err != nil {
102+
return users, err
103+
}
104+
defer cursor.Close(ctx)
105+
for cursor.Next(ctx) {
106+
var user User
107+
if err := cursor.Decode(&user); err != nil {
108+
return users, err
109+
}
110+
users = append(users, user)
84111
}
85-
if user.Users == nil || len(user.Users) == 0 {
86-
return false, nil
112+
return users, nil
113+
}
114+
115+
func (m *MongoDBServer) createUser(database string, username string, password string) error {
116+
command := &bson.D{primitive.E{Key: "createUser", Value: username}, primitive.E{Key: "pwd", Value: password},
117+
primitive.E{Key: "roles", Value: []bson.M{{"role": "readWrite", "db": database}}}}
118+
r := m.runCommand(database, command)
119+
if _, err := r.DecodeBytes(); err != nil {
120+
return err
87121
}
88-
return true, nil
89-
//if br, err := r.DecodeBytes(); err != nil {
90-
// return "", err
91-
//} else {
92-
// return br.String(), nil
93-
//}
122+
return nil
94123
}
95124

96-
func (m *MongoDBServer) createUser(database string, username string, password string) (string, error) {
97-
//command := &bson.D{{"createUser", username}, {"pwd", password}, {"roles", []bson.M{{"role": "readWrite", "db": database}}}}
98-
//r := m.runCommand(m.authenticationDatabase, command)
99-
//if br, err := r.DecodeBytes(); err != nil {
100-
// return "", err
101-
//} else {
102-
// return br.String(), nil
103-
//}
104-
return "", nil
125+
func (m *MongoDBServer) updateUserPasswordAndRoles(database string, username string, password string) error {
126+
command := &bson.D{primitive.E{Key: "updateUser", Value: username}, primitive.E{Key: "pwd", Value: password},
127+
primitive.E{Key: "roles", Value: []bson.M{{"role": "readWrite", "db": database}}}}
128+
r := m.runCommand(database, command)
129+
if _, err := r.DecodeBytes(); err != nil {
130+
return err
131+
}
132+
return nil
105133
}
106134

107135
func (m *MongoDBServer) runCommand(database string, command *bson.D) *mongo.SingleResult {

common/db/postgresql/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ func (c *Cache) Get(host string, rootUsername string, rootPassword string, rootA
2323
}
2424

2525
func (c *Cache) Remove(host string) {
26+
server := c.cache[host]
27+
if server != nil {
28+
server.Close()
29+
}
2630
delete(c.cache, host)
2731
}

common/db/postgresql/repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func NewPostgreSQLServer(host string, rootUser string, rootPassword string, root
3535
}, nil
3636
}
3737

38+
func (s *PostgreSQLServer) Close() {
39+
s.dbpool.Close()
40+
}
41+
3842
// TODO Prepared Statements
3943
func (s *PostgreSQLServer) CreateDatabaseIfNotExists(database string) error {
4044
if databaseExists, err := s.doesDatabaseExist(database); err != nil {

config/crd/bases/infra.doodle.com_mongodbs.yaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ spec:
3838
Run "make" to regenerate code after modifying this file'
3939
properties:
4040
credentials:
41-
description: Database credentials
4241
items:
4342
properties:
4443
username:
@@ -68,19 +67,31 @@ spec:
6867
type: object
6968
type: array
7069
databaseName:
71-
description: Database name
7270
type: string
7371
hostName:
74-
description: Database Server host name and port
7572
type: string
7673
rootAuthDatabase:
7774
type: string
75+
rootSecretLookup:
76+
properties:
77+
field:
78+
type: string
79+
name:
80+
type: string
81+
namespace:
82+
type: string
83+
required:
84+
- field
85+
- name
86+
- namespace
87+
type: object
7888
rootUsername:
7989
type: string
8090
required:
8191
- credentials
8292
- databaseName
8393
- hostName
94+
- rootSecretLookup
8495
type: object
8596
status:
8697
description: 'MongoDBStatus defines the observed state of MongoDB IMPORTANT:

config/crd/bases/infra.doodle.com_postgresqls.yaml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ spec:
3838
Run "make" to regenerate code after modifying this file'
3939
properties:
4040
credentials:
41-
description: Database credentials
4241
items:
4342
properties:
4443
username:
@@ -68,14 +67,9 @@ spec:
6867
type: object
6968
type: array
7069
databaseName:
71-
description: Database name
7270
type: string
73-
host:
74-
description: Database Server host name
71+
hostName:
7572
type: string
76-
port:
77-
format: int64
78-
type: integer
7973
rootAuthDatabase:
8074
type: string
8175
rootSecretLookup:
@@ -96,8 +90,7 @@ spec:
9690
required:
9791
- credentials
9892
- databaseName
99-
- host
100-
- port
93+
- hostName
10194
- rootSecretLookup
10295
type: object
10396
status:

config/samples/infra_v1beta1_mongodb.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ spec:
1313
namespace: devops
1414
field: mongodb-root-password
1515
credentials:
16-
- username: admin
16+
- username: doodle
17+
vault:
18+
useAvailable: true
19+
host: vault.devops
20+
path: secret/doodle/devbox/monolith
21+
userField: USERNAME
22+
secretField: PASSWORD
23+
- username: keycloak
24+
vault:
25+
useAvailable: true
26+
host: vault.devops
27+
path: secret/doodle/devbox/monolith
28+
userField: USERNAME
29+
secretField: PASSWORD
30+
- username: monolith-doodle
1731
vault:
1832
useAvailable: true
1933
host: vault.devops

0 commit comments

Comments
 (0)