-
Notifications
You must be signed in to change notification settings - Fork 88
/
identityconfig.go
100 lines (86 loc) · 2.82 KB
/
identityconfig.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
package identity
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/kotsadmidentity/store"
"github.com/replicatedhq/kots/pkg/util"
"github.com/segmentio/ksuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
func AppIdentityNeedsBootstrap(appSlug string) (bool, error) {
user := fmt.Sprintf("%s-dex", appSlug)
exists, err := postgresUserExists(user)
if err != nil {
return false, errors.Wrapf(err, "failed to check %s user exists", user)
}
if exists {
return false, nil
}
return true, nil
}
func InitAppIdentityConfig(appSlug string, storage kotsv1beta1.Storage) (string, error) {
// support for the dev environment where app is in "test" namespace
host := "kotsadm-postgres"
if kotsadmNamespace := util.PodNamespace; kotsadmNamespace != "" {
host = fmt.Sprintf("%s.%s", host, kotsadmNamespace)
}
var postgresPassword string
if storage.PostgresConfig != nil {
var err error
postgresPassword, err = storage.PostgresConfig.Password.GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get password value")
}
}
if postgresPassword == "" {
postgresPassword = ksuid.New().String()
}
database := fmt.Sprintf("%s-dex", appSlug)
user := fmt.Sprintf("%s-dex", appSlug)
err := store.GetStore().CreateDexDatabase(database, user, postgresPassword)
if err != nil {
return "", errors.Wrap(err, "failed to create dex database")
}
identityConfig := &kotsv1beta1.IdentityConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "IdentityConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: "identity",
},
Spec: kotsv1beta1.IdentityConfigSpec{
Storage: kotsv1beta1.Storage{
PostgresConfig: &kotsv1beta1.IdentityPostgresConfig{
Host: host,
Database: database,
User: user,
Password: &kotsv1beta1.StringValueOrEncrypted{Value: postgresPassword},
},
},
ClientID: appSlug,
ClientSecret: &kotsv1beta1.StringValueOrEncrypted{Value: ksuid.New().String()},
},
}
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
buf := bytes.NewBuffer(nil)
if err := s.Encode(identityConfig, buf); err != nil {
return "", errors.Wrap(err, "failed to encode config")
}
identityConfigTmpFile, err := ioutil.TempFile("", "kots")
if err != nil {
return "", errors.Wrap(err, "failed to create temp file")
}
_ = identityConfigTmpFile.Close()
if err := ioutil.WriteFile(identityConfigTmpFile.Name(), buf.Bytes(), 0644); err != nil {
os.Remove(identityConfigTmpFile.Name())
return "", errors.Wrap(err, "failed to write config to temp file")
}
return identityConfigTmpFile.Name(), nil
}