-
Notifications
You must be signed in to change notification settings - Fork 44
/
common.go
251 lines (208 loc) · 6.01 KB
/
common.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
241
242
243
244
245
246
247
248
249
250
251
// common package of CB-Spider's Cloud Drivers
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.12.
// by CB-Spider Team, 2021.08.
package common
import (
"bytes"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"errors"
"regexp"
cblogger "github.com/cloud-barista/cb-log"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
enc "github.com/cloud-barista/cb-spider/cloud-info-manager/credential-info-manager"
infostore "github.com/cloud-barista/cb-spider/info-store"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
// generate a KeyPair with 4KB length
// returns: privateKeyBytes, publicKeyBytes, error
func GenKeyPair() ([]byte, []byte, error) {
// (1) Generate a private Key
keyLength := 4096
privateKey, err := rsa.GenerateKey(rand.Reader, keyLength)
if err != nil {
return nil, nil, err
}
err = privateKey.Validate()
if err != nil {
return nil, nil, err
}
// for ASN.1 DER format
DERKey := x509.MarshalPKCS1PrivateKey(privateKey)
keyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: DERKey,
}
// for PEM format
privateKeyBytes := pem.EncodeToMemory(&keyBlock)
// (2) Generate a public key
publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
publicKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
return privateKeyBytes, publicKeyBytes, nil
}
// ex)
// privateKey, publicKey, err := GenKeyPair()
//
// srcList[0] = credentialInfo.IdentityEndpoint
// srcList[1] = credentialInfo.AuthToken
// srcList[2] = credentialInfo.TenantId
// strHash, err := GenHash(srcList)
//
// AddKey("CLOUDIT", strHash, keyPairReqInfo.IId.NameId, privateKey)
type LocalKeyInfo struct {
ProviderName string `gorm:"primaryKey"`
HashString string `gorm:"primaryKey"`
NameId string `gorm:"primaryKey"`
PrivateKey string
}
func (LocalKeyInfo) TableName() string {
return "local_key_infos"
}
//====================================================================
var cblog *logrus.Logger
func init() {
cblog = cblogger.GetLogger("CLOUD-BARISTA")
db, err := infostore.Open()
if err != nil {
cblog.Error(err)
return
}
db.AutoMigrate(&LocalKeyInfo{})
infostore.Close(db)
}
func AddKey(providerName string, hashString string, keyPairNameId string, privateKey string) error {
encPrivateKey, err := enc.Encrypt(enc.SPIDER_KEY, []byte(privateKey))
if err != nil {
return err
}
err = infostore.Insert(&LocalKeyInfo{ProviderName: providerName, HashString: hashString, NameId: keyPairNameId, PrivateKey: encPrivateKey})
if err != nil {
cblog.Error(err)
return err
}
return nil
}
// return: []KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func ListKey(providerName string, hashString string) ([]*irs.KeyValue, error) {
var iidInfoList []*LocalKeyInfo
err := infostore.ListByConditions(&iidInfoList, "provider_name", providerName, "hash_string", hashString)
if err != nil {
cblog.Error(err)
return nil, err
}
var keyValueList []*irs.KeyValue
for _, iidInfo := range iidInfoList {
decPrivateKey, err := enc.Decrypt(enc.SPIDER_KEY, []byte(iidInfo.PrivateKey))
if err != nil {
return nil, err
}
keyValue := &irs.KeyValue{
Key: iidInfo.NameId,
Value: decPrivateKey,
}
keyValueList = append(keyValueList, keyValue)
}
return keyValueList, nil
}
// return: KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func GetKey(providerName string, hashString string, keyPairNameId string) (*irs.KeyValue, error) {
var localKeyInfo LocalKeyInfo
err := infostore.GetBy3Conditions(&localKeyInfo, "provider_name", providerName, "hash_string", hashString, "name_id", keyPairNameId)
if err != nil {
cblog.Error(err)
return nil, err
}
decPrivateKey, err := enc.Decrypt(enc.SPIDER_KEY, []byte(localKeyInfo.PrivateKey))
if err != nil {
return nil, err
}
keyValue := &irs.KeyValue{
Key: localKeyInfo.NameId,
Value: decPrivateKey,
}
return keyValue, nil
}
func DelKey(providerName string, hashString string, keyPairNameId string) error {
_, err := infostore.DeleteBy3Conditions(&LocalKeyInfo{}, "provider_name", providerName, "hash_string", hashString, "name_id", keyPairNameId)
if err != nil {
cblog.Error(err)
return err
}
return nil
}
func GenHash(sourceList []string) (string, error) {
var keyString string
for _, str := range sourceList {
keyString += str
}
hasher := md5.New()
_, err := io.WriteString(hasher, keyString)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
// ParseKey reads the given RSA private key and create a public one for it.
func MakePublicKeyFromPrivateKey(pem string) (string, error) {
key, err := ssh.ParseRawPrivateKey([]byte(pem))
if err != nil {
return "", err
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return "", fmt.Errorf("%q is not a RSA key", pem)
}
pub, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return "", err
}
return string(bytes.TrimRight(ssh.MarshalAuthorizedKey(pub), "\n")), nil
}
//-------------------
func ValidateWindowsPassword(pw string) error {
invalidMSG := `Password must be between 12 and 123 characters long and must have 3 of the following:
1 lower case character,
1 upper case character,
1 number,
1 special character`
if len(pw) < 12 || len(pw) > 123 {
return errors.New(invalidMSG)
}
checkNum := 0
matchCase, err := regexp.MatchString(".*[a-z]+", pw)
if matchCase && err == nil {
checkNum++
}
matchCase, _ = regexp.MatchString(".*[A-Z]+", pw)
if matchCase && err == nil {
checkNum++
}
matchCase, _ = regexp.MatchString(".*[0-9]+", pw)
if matchCase && err == nil {
checkNum++
}
matchCase, _ = regexp.MatchString(`[\{\}\[\]\/?.,;:|\)*~!^\-_+<>@\#$%&\\\=\(\'\"\n\r]+`, pw)
if matchCase && err == nil {
checkNum++
}
if checkNum >= 3 {
return nil
} else {
return errors.New(invalidMSG)
}
}