/
pw.go
240 lines (196 loc) · 6.21 KB
/
pw.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
// Copyright © 2020 Ulrich Anhalt <ulrich.anhalt@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
crypt "crypto/rand"
"fmt"
"io"
"math/rand"
"strings"
"syscall"
"time"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ulranh/hana_sql_exporter/internal"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/ssh/terminal"
)
// pwCmd represents the pw command
var pwCmd = &cobra.Command{
Use: "pw",
Short: "Set passwords for the tenants in the config file",
Long: `With the command pw you can set the passwords for the tenants you want to monitor. You can set the password for one tenant or several tenants separated by comma. For example:
hana_sql_exporter pw --tenant d01
hana_sql_exporter pw -t d01,d02 --config ./.hana_sql_exporter.toml`,
Run: func(cmd *cobra.Command, args []string) {
config, err := getConfig()
if err != nil {
exit("Can't handle config file: ", err)
}
// set timeout for pw tenant connection test
config.Timeout = 5
err = config.SetPw(cmd)
if err != nil {
exit("Can't set password: ", err)
}
},
}
func init() {
RootCmd.AddCommand(pwCmd)
pwCmd.PersistentFlags().StringP("tenant", "t", "", "name(s) of tenant(s) separated by comma")
pwCmd.MarkPersistentFlagRequired("tenant")
}
// SetPw - save password(s) of tenant(s) database user to the config file
func (config *Config) SetPw(cmd *cobra.Command) error {
fmt.Print("Password: ")
// syscall.Stdin is not 0 on windows
pw, err := terminal.ReadPassword(int(syscall.Stdin))
// pw, err := terminal.ReadPassword(0)
if err != nil {
return errors.Wrap(err, "setPw(ReadPassword)")
}
tenants, err := cmd.Flags().GetString("tenant")
if err != nil {
return errors.Wrap(err, "setPw(GetString)")
}
config.Secret, err = config.AddSecret(tenants, pw)
if err != nil {
return errors.Wrap(err, "setPw(newSecret)")
}
viper.Set("secret", config.Secret)
err = viper.WriteConfig()
if err != nil {
return errors.Wrap(err, "setPw(WriteConfig)")
}
// connection test for all tenants
secretMap, err := config.GetSecretMap()
if err != nil {
return errors.Wrap(err, "prepare(getSecretMap)")
}
for i := range config.Tenants {
db := config.getConnection(i, secretMap)
if db == nil {
continue
}
db.Close()
}
return nil
}
// AddSecret - create encrypted secret for tenant(s)
func (config *Config) AddSecret(tenants string, pw []byte) ([]byte, error) {
var err error
secret, err := config.GetSecretMap()
if err != nil {
return nil, errors.Wrap(err, "AddSecret(GetSecretMap)")
}
// create secret key once if it doesn't exist
if _, ok := secret.Name["secretkey"]; !ok {
secret.Name = make(map[string][]byte)
secret.Name["secretkey"], err = GetSecretKey()
if err != nil {
return nil, errors.Wrap(err, "AddSecret(GetSecretKey)")
}
}
// encrypt password
encPw, err := PwEncrypt(pw, secret.Name["secretkey"])
if err != nil {
return nil, errors.Wrap(err, "AddSecret(PwEncrypt)")
}
for _, tenant := range strings.Split(tenants, ",") {
// check, if cmd line tenant exists in configfile
tInfo := config.FindTenant(low(tenant))
if "" == tInfo.Name {
log.WithFields(log.Fields{
"tenant": low(tenant),
}).Error("missing tenant")
return nil, errors.New("Did not find tenant in configfile tenants slice.")
}
// add password to secret map
secret.Name[low(tenant)] = encPw
}
// write pw information back to the config file
newSecret, err := proto.Marshal(&secret)
if err != nil {
return nil, errors.Wrap(err, "AddSecret(Marshal)")
}
return newSecret, nil
}
// FindTenant - check if cmpTenant already exists in configfile
func (config *Config) FindTenant(cmpTenant string) TenantInfo {
for _, tenant := range config.Tenants {
if low(tenant.Name) == low(cmpTenant) {
return tenant
}
}
return TenantInfo{}
}
// GetSecretKey - create secret key once
func GetSecretKey() ([]byte, error) {
key := make([]byte, 32)
rand.Seed(time.Now().UnixNano())
if _, err := rand.Read(key); err != nil {
return nil, errors.Wrap(err, "GetSecretKey(rand.Read)")
}
return key, nil
}
// PwEncrypt - encrypt tenant password
func PwEncrypt(bytePw, byteSecret []byte) ([]byte, error) {
var secretKey [32]byte
copy(secretKey[:], byteSecret)
var nonce [24]byte
if _, err := io.ReadFull(crypt.Reader, nonce[:]); err != nil {
return nil, errors.Wrap(err, "PwEncrypt(ReadFull)")
}
return secretbox.Seal(nonce[:], bytePw, &nonce, &secretKey), nil
}
// PwDecrypt - decrypt tenant password
func PwDecrypt(encrypted, byteSecret []byte) (string, error) {
var secretKey [32]byte
copy(secretKey[:], byteSecret)
var decryptNonce [24]byte
copy(decryptNonce[:], encrypted[:24])
decrypted, ok := secretbox.Open(nil, encrypted[24:], &decryptNonce, &secretKey)
if !ok {
return "", errors.New("PwDecrypt(secretbox.Open)")
}
return string(decrypted), nil
}
// GetSecretMap - unmarshal secret bytes
func (config *Config) GetSecretMap() (internal.Secret, error) {
if config.Secret == nil {
return internal.Secret{}, nil
}
// unmarshal secret byte array
var secret internal.Secret
if err := proto.Unmarshal(config.Secret, &secret); err != nil {
return internal.Secret{}, errors.Wrap(err, "GetSecretMap(Unmarshal)")
}
return secret, nil
}
// GetPassword - decrypt password
func GetPassword(secret internal.Secret, tenant string) (string, error) {
// get encrypted tenant pw
if _, ok := secret.Name[low(tenant)]; !ok {
return "", errors.New("GetPassword(encrypted tenant pw info does not exist)")
}
// decrypt tenant password
pw, err := PwDecrypt(secret.Name[low(tenant)], secret.Name["secretkey"])
if err != nil {
return "", errors.Wrap(err, "GetPassword(PwDecrypt)")
}
return pw, nil
}