-
Notifications
You must be signed in to change notification settings - Fork 47
/
common.go
169 lines (138 loc) · 3.98 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
// 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/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"sync"
"io"
"io/ioutil"
"crypto/md5"
"golang.org/x/crypto/ssh"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
// 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
}
// Lock to store and read private key
var rwMutex sync.RWMutex
// 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)
func AddKey(providerName string, hashString string, keyPairNameId string, privateKey string) error {
rwMutex.Lock()
defer rwMutex.Unlock()
err := insertInfo(providerName, hashString, keyPairNameId, privateKey)
if err != nil {
return err
}
return nil
}
// return: []KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func ListKey(providerName string, hashString string) ([]*irs.KeyValue, error) {
rwMutex.Lock()
defer rwMutex.Unlock()
keyValueList, err := listInfo(providerName, hashString)
if err != nil {
return nil, err
}
return keyValueList, nil
}
// return: KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func GetKey(providerName string, hashString string, keyPairNameId string) (*irs.KeyValue, error) {
rwMutex.Lock()
defer rwMutex.Unlock()
keyValue, err := getInfo(providerName, hashString, keyPairNameId)
if err != nil {
return nil, err
}
return keyValue, nil
}
func DelKey(providerName string, hashString string, keyPairNameId string) error {
rwMutex.Lock()
defer rwMutex.Unlock()
_, err := deleteInfo(providerName, hashString, keyPairNameId)
if err != nil {
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
}
// save a key to a file
func SaveKey(keyBytes []byte, targetFile string) error {
err := ioutil.WriteFile(targetFile, keyBytes, 0600)
if err != nil {
return err
}
return 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
}