-
Notifications
You must be signed in to change notification settings - Fork 88
/
user_store.go
240 lines (199 loc) · 6.97 KB
/
user_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package kotsstore
import (
"context"
"database/sql"
"fmt"
"os"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/persistence"
"github.com/replicatedhq/kots/pkg/util"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
ErrTooManyAttempts = errors.New("too many attempts")
passwordSecretName = "kotsadm-password"
)
// GetSharedPasswordBcrypt will return the hash of the current password
// that can be used to validate an auth request. This is in the store pkg,
// but the data may be in the cluster or the database, depending on the
// configuration.
func (s *KOTSStore) GetSharedPasswordBcrypt() ([]byte, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s clientset")
}
var shaBytes []byte
passwordSecret, err := clientset.CoreV1().Secrets(util.PodNamespace).Get(context.TODO(), passwordSecretName, metav1.GetOptions{})
if err != nil {
// either no existing password secret or unable to get it
// so instead we fallback to the environment variable
shaBytes = []byte(os.Getenv("SHARED_PASSWORD_BCRYPT"))
} else {
if passwordSecret.Labels == nil {
passwordSecret.Labels = map[string]string{}
}
numAttempts, _ := strconv.Atoi(passwordSecret.Labels["numAttempts"])
if numAttempts > 10 {
return nil, ErrTooManyAttempts
}
shaBytes = passwordSecret.Data["passwordBcrypt"]
}
return shaBytes, nil
}
// getSharedPaswordBcryptFromDatabase will return the hash password from the
// database instead of the cluster. this is not an interface method
func (s *KOTSStore) getSharedPaswordBcryptFromDatabase() ([]byte, error) {
db := persistence.MustGetDBSession()
// check for too many attempts / locked out
query := `select value from kotsadm_params where key = $1`
row := db.QueryRow(query, "failed.login.count")
failedLoginCount := "0"
if err := row.Scan(&failedLoginCount); err != nil {
if err != sql.ErrNoRows {
return nil, errors.Wrap(err, "failed to scan invalid attempt count")
}
}
i, err := strconv.Atoi(failedLoginCount)
if err != nil {
return nil, errors.Wrap(err, "failed to parse failed login count")
}
if i > 10 {
return nil, ErrTooManyAttempts
}
// get the pasword now
row = db.QueryRow(query, "password.bcrypt")
var hash []byte
// if it doesn't exist, return the one from the environment
if err := row.Scan(&hash); err != nil {
return []byte(os.Getenv("SHARED_PASSWORD_BCRYPT")), nil
}
return hash, nil
}
func (s *KOTSStore) FlagInvalidPassword() error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get k8s clientset")
}
for i := 0; ; i++ {
secret, err := clientset.CoreV1().Secrets(util.PodNamespace).Get(context.TODO(), passwordSecretName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return nil
}
return errors.Wrap(err, "failed to get password secret")
}
if secret.Labels == nil {
secret.Labels = map[string]string{}
}
secret.Labels["lastFailure"] = fmt.Sprintf("%d", time.Now().Unix())
numAttempts, _ := strconv.Atoi(secret.Labels["numAttempts"])
secret.Labels["numAttempts"] = strconv.Itoa(numAttempts + 1)
if _, err := clientset.CoreV1().Secrets(util.PodNamespace).Update(context.TODO(), secret, metav1.UpdateOptions{}); err != nil {
if kuberneteserrors.IsConflict(err) {
if i > 2 {
return errors.New("failed to update password secret due to conflicts")
}
continue
}
return errors.Wrap(err, "failed to update password secret")
}
return nil
}
}
func (s *KOTSStore) flagInvalidPasswordInDatabase() error {
db := persistence.MustGetDBSession()
query := `select value from kotsadm_params where key = $1`
row := db.QueryRow(query, "failed.login.count")
failedLoginCount := "0"
if err := row.Scan(&failedLoginCount); err != nil {
if err == sql.ErrNoRows {
query = `insert into kotsadm_params (key, value) values ($1, $2)`
if _, err := db.Exec(query, "failed.login.count", "1"); err != nil {
return errors.Wrap(err, "failed to insert failed login count")
}
return nil
} else {
return errors.Wrap(err, "failed to scan invalid attempt count")
}
}
i, err := strconv.Atoi(failedLoginCount)
if err != nil {
return errors.Wrap(err, "failed to parse failed login count")
}
i++
query = `update kotsadm_params set value = $1 where key = $2`
if _, err := db.Exec(query, strconv.Itoa(i), "failed.login.count"); err != nil {
return errors.Wrap(err, "failed to update failed login count")
}
return nil
}
func (s *KOTSStore) FlagSuccessfulLogin() error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get k8s clientset")
}
for i := 0; ; i++ {
secret, err := clientset.CoreV1().Secrets(util.PodNamespace).Get(context.TODO(), passwordSecretName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return nil
}
return errors.Wrap(err, "failed to get password secret")
}
if secret.Labels == nil {
secret.Labels = map[string]string{}
}
secret.Labels["numAttempts"] = "0"
if _, err := clientset.CoreV1().Secrets(util.PodNamespace).Update(context.TODO(), secret, metav1.UpdateOptions{}); err != nil {
if kuberneteserrors.IsConflict(err) {
if i > 2 {
return errors.New("failed to update password secret due to conflicts")
}
continue
}
return errors.Wrap(err, "failed to update password secret")
}
return nil
}
}
func (s *KOTSStore) flagSuccessfulLoginInDatabase() error {
db := persistence.MustGetDBSession()
query := `delete from kotsadm_params where key = $1`
if _, err := db.Exec(query, "failed.login.count"); err != nil {
return errors.Wrap(err, "failed to delete failed login count")
}
query = `insert into kotsadm_params (key, value) values ($1, $2)`
if _, err := db.Exec(query, "failed.login.count", "0"); err != nil {
return errors.Wrap(err, "failed to reset failed login count")
}
return nil
}
// GetPasswordUpdatedAt - returns the time the password was last updated
func (s *KOTSStore) GetPasswordUpdatedAt() (*time.Time, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s clientset")
}
passwordSecret, err := clientset.CoreV1().Secrets(util.PodNamespace).Get(context.TODO(), util.PasswordSecretName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
// similar to fallback case when password secret is not found and uses the default password from environment variable
return nil, nil
}
return nil, errors.Wrap(err, "failed to get password secret")
}
var passwordUpdatedAt *time.Time
updatedAtBytes, ok := passwordSecret.Data["passwordUpdatedAt"]
if ok {
updatedAt, err := time.Parse(time.RFC3339, string(updatedAtBytes))
if err != nil {
return nil, errors.Wrap(err, "failed to parse passwordUpdatedAt")
}
passwordUpdatedAt = &updatedAt
}
return passwordUpdatedAt, nil
}