/
common.go
149 lines (120 loc) · 3.76 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package controllers
import (
"context"
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1beta1 "github.com/doodlescheduling/k8sdb-controller/api/v1beta1"
"github.com/doodlescheduling/k8sdb-controller/common/database"
)
// Index keys
const (
secretIndexKey string = ".metadata.secret"
credentialsIndexKey string = ".metadata.credentials"
dbIndexKey string = ".metadata.database"
)
type userDropper interface {
DropUser(ctx context.Context, db, username string) error
}
// objectKey returns c.ObjectKey for the object.
func objectKey(object metav1.Object) client.ObjectKey {
return client.ObjectKey{
Namespace: object.GetNamespace(),
Name: object.GetName(),
}
}
func extractMongoDBUserRoles(roles []infrav1beta1.MongoDBUserRole) database.MongoDBRoles {
list := make(database.MongoDBRoles, 0)
for _, r := range roles {
list = append(list, database.MongoDBRole{
Name: r.Name,
DB: r.DB,
})
}
return list
}
func extractCredentials(credentials *infrav1beta1.SecretReference, secret *corev1.Secret) (string, string, error) {
var (
user string
pw string
)
userField := credentials.UserField
if userField == "" {
userField = "username"
}
pwField := credentials.PasswordField
if pwField == "" {
pwField = "password"
}
if val, ok := secret.Data[userField]; !ok {
return "", "", errors.New("defined username field not found in secret")
} else {
user = string(val)
}
if val, ok := secret.Data[pwField]; !ok {
return "", "", errors.New("defined password field not found in secret")
} else {
pw = string(val)
}
return user, pw, nil
}
func setupAtlas(ctx context.Context, db infrav1beta1.MongoDBDatabase, pubKey, privKey string) (*database.AtlasRepository, error) {
handler, err := database.NewAtlasRepository(context.TODO(), database.AtlasOptions{
GroupID: db.Spec.AtlasGroupId,
PrivateKey: privKey,
PublicKey: pubKey,
})
if err != nil {
return handler, fmt.Errorf("failed to setup connection to mongodb atlas: %w", err)
}
return handler, nil
}
func setupPostgreSQL(ctx context.Context, db infrav1beta1.PostgreSQLDatabase, usr, pw string, switchDB bool) (*database.PostgreSQLRepository, error) {
opts := database.PostgreSQLOptions{
URI: db.Spec.Address,
Username: usr,
Password: pw,
}
if switchDB {
opts.DatabaseName = db.GetDatabaseName()
}
handler, err := database.NewPostgreSQLRepository(context.TODO(), opts)
if err != nil {
return handler, fmt.Errorf("failed to setup connection to postgres server: %w", err)
}
return handler, nil
}
func setupMongoDB(ctx context.Context, db infrav1beta1.MongoDBDatabase, usr, pw string) (*database.MongoDBRepository, error) {
handler, err := database.NewMongoDBRepository(context.TODO(), database.MongoDBOptions{
URI: db.Spec.Address,
AuthDatabaseName: db.GetRootDatabaseName(),
DatabaseName: db.GetDatabaseName(),
Username: usr,
Password: pw,
})
if err != nil {
return handler, fmt.Errorf("failed to setup connection to mongodb: %w", err)
}
return handler, nil
}
func getSecret(ctx context.Context, c client.Client, sec *infrav1beta1.SecretReference) (string, string, error) {
// Fetch referencing root secret
secret := &corev1.Secret{}
secretName := types.NamespacedName{
Namespace: sec.Namespace,
Name: sec.Name,
}
err := c.Get(ctx, secretName, secret)
// Failed to fetch referenced secret, requeue immediately
if err != nil {
return "", "", fmt.Errorf("referencing secret was not found: %w", err)
}
usr, pw, err := extractCredentials(sec, secret)
if err != nil {
return usr, pw, fmt.Errorf("credentials field not found in referenced secret: %w", err)
}
return usr, pw, err
}