-
Notifications
You must be signed in to change notification settings - Fork 88
/
postgres.go
132 lines (112 loc) · 4.34 KB
/
postgres.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
package deploy
import (
"bytes"
"context"
"github.com/pkg/errors"
kotsv1beta "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/segmentio/ksuid"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
func EnsurePostgresSecret(ctx context.Context, clientset kubernetes.Interface, namespace, namePrefix string, cipher *crypto.AESCipher, config kotsv1beta.IdentityPostgresConfig, additionalLabels map[string]string) error {
secret, err := postgresSecretResource(namePrefix, cipher, config, additionalLabels)
if err != nil {
return err
}
existingSecret, err := GetPostgresSecret(ctx, clientset, namespace, namePrefix)
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing secret")
}
_, err = clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create secret")
}
return nil
}
existingSecret = updatePostgresSecret(existingSecret, secret)
_, err = clientset.CoreV1().Secrets(namespace).Update(ctx, existingSecret, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update secret")
}
return nil
}
func RenderPostgresSecret(ctx context.Context, namePrefix string, cipher *crypto.AESCipher, config kotsv1beta.IdentityPostgresConfig, additionalLabels map[string]string) ([]byte, error) {
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
secret, err := postgresSecretResource(namePrefix, cipher, config, additionalLabels)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
if err := s.Encode(secret, buf); err != nil {
return nil, errors.Wrap(err, "failed to encode secret")
}
return buf.Bytes(), nil
}
func GetPostgresSecret(ctx context.Context, clientset kubernetes.Interface, namespace, namePrefix string) (*corev1.Secret, error) {
return clientset.CoreV1().Secrets(namespace).Get(ctx, prefixName(namePrefix, "dex-postgres"), metav1.GetOptions{})
}
func postgresSecretResource(namePrefix string, cipher *crypto.AESCipher, config kotsv1beta.IdentityPostgresConfig, additionalLabels map[string]string) (*corev1.Secret, error) {
password := ""
if config.Password != nil {
// NOTE: we do not encrypt kotsadm config
if cipher == nil && config.Password.ValueEncrypted != "" {
return nil, errors.New("cannot decrypt password without cipher")
}
p, err := config.Password.GetValue(*cipher)
if err != nil {
return nil, errors.Wrap(err, "failed to decrypt password")
}
password = p
}
if password == "" {
password = ksuid.New().String()
}
data := map[string][]byte{
"PGHOST": []byte(config.Host),
"PGDATABASE": []byte(config.Database),
"PGUSER": []byte(config.User),
"PGPASSWORD": []byte(password),
}
if config.Port != "" {
data["PGPORT"] = []byte(config.Port)
}
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: prefixName(namePrefix, "dex-postgres"),
Labels: kotsadmtypes.GetKotsadmLabels(AdditionalLabels(namePrefix, additionalLabels)),
},
Data: data,
}, nil
}
func updatePostgresSecret(existingSecret, desiredSecret *corev1.Secret) *corev1.Secret {
if len(existingSecret.Data["PGHOST"]) == 0 {
existingSecret.Data["PGHOST"] = desiredSecret.Data["PGHOST"]
if len(existingSecret.Data["PGPORT"]) == 0 && len(desiredSecret.Data["PGPORT"]) > 0 {
existingSecret.Data["PGPORT"] = desiredSecret.Data["PGPORT"]
}
}
if len(existingSecret.Data["PGDATABASE"]) == 0 {
existingSecret.Data["PGDATABASE"] = desiredSecret.Data["PGDATABASE"]
}
if len(existingSecret.Data["PGUSER"]) == 0 {
existingSecret.Data["PGUSER"] = desiredSecret.Data["PGUSER"]
}
if len(existingSecret.Data["password"]) > 0 { // migrate to PGPASSWORD
existingSecret.Data["PGPASSWORD"] = existingSecret.Data["password"]
delete(existingSecret.Data, "password")
} else if len(existingSecret.Data["PGPASSWORD"]) == 0 {
existingSecret.Data["PGPASSWORD"] = desiredSecret.Data["PGPASSWORD"]
}
return existingSecret
}