-
Notifications
You must be signed in to change notification settings - Fork 90
/
registry_store.go
120 lines (101 loc) · 3.95 KB
/
registry_store.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
package kotsstore
import (
"encoding/base64"
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/persistence"
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/rqlite/gorqlite"
"go.uber.org/zap"
)
func (s *KOTSStore) GetRegistryDetailsForApp(appID string) (registrytypes.RegistrySettings, error) {
db := persistence.MustGetDBSession()
query := `select registry_hostname, registry_username, registry_password_enc, namespace, registry_is_readonly from app where id = ?`
rows, err := db.QueryOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{appID},
})
if err != nil {
return registrytypes.RegistrySettings{}, fmt.Errorf("failed to query: %v: %v", err, rows.Err)
}
if !rows.Next() {
return registrytypes.RegistrySettings{}, ErrNotFound
}
var registryHostname gorqlite.NullString
var registryUsername gorqlite.NullString
var registryPasswordEnc gorqlite.NullString
var registryNamespace gorqlite.NullString
var isReadOnly gorqlite.NullBool
if err := rows.Scan(®istryHostname, ®istryUsername, ®istryPasswordEnc, ®istryNamespace, &isReadOnly); err != nil {
return registrytypes.RegistrySettings{}, errors.Wrap(err, "failed to scan registry")
}
registrySettings := registrytypes.RegistrySettings{
Hostname: registryHostname.String,
Username: registryUsername.String,
PasswordEnc: registryPasswordEnc.String,
Namespace: registryNamespace.String,
IsReadOnly: isReadOnly.Bool,
}
if !registryPasswordEnc.Valid {
return registrySettings, nil
}
decodedPassword, err := base64.StdEncoding.DecodeString(registrySettings.PasswordEnc)
if err != nil {
return registrytypes.RegistrySettings{}, errors.Wrap(err, "failed to decode")
}
decryptedPassword, err := crypto.Decrypt([]byte(decodedPassword))
if err != nil {
return registrytypes.RegistrySettings{}, errors.Wrap(err, "failed to decrypt")
}
registrySettings.Password = string(decryptedPassword)
return registrySettings, nil
}
func (s *KOTSStore) UpdateRegistry(appID string, hostname string, username string, password string, namespace string, isReadOnly bool) error {
logger.Debug("updating app registry",
zap.String("appID", appID))
db := persistence.MustGetDBSession()
if password == registrytypes.PasswordMask {
// password unchanged - don't update it
query := `update app set registry_hostname = ?, registry_username = ?, namespace = ?, registry_is_readonly = ? where id = ?`
wr, err := db.WriteOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{hostname, username, namespace, isReadOnly, appID},
})
if err != nil {
return fmt.Errorf("failed to update registry settings: %v: %v", err, wr.Err)
}
} else {
passwordEnc := base64.StdEncoding.EncodeToString(crypto.Encrypt([]byte(password)))
query := `update app set registry_hostname = ?, registry_username = ?, registry_password_enc = ?, namespace = ?, registry_is_readonly = ? where id = ?`
wr, err := db.WriteOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{hostname, username, passwordEnc, namespace, isReadOnly, appID},
})
if err != nil {
return fmt.Errorf("failed to update registry settings: %v: %v", err, wr.Err)
}
}
return nil
}
func (s *KOTSStore) GetAppIDsFromRegistry(hostname string) ([]string, error) {
db := persistence.MustGetDBSession()
query := `select id from app where registry_hostname = ?`
rows, err := db.QueryOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{hostname},
})
if err != nil {
return nil, fmt.Errorf("failed to query: %v: %v", err, rows.Err)
}
appIDs := []string{}
for rows.Next() {
var appID string
if err := rows.Scan(&appID); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
appIDs = append(appIDs, appID)
}
return appIDs, nil
}