-
Notifications
You must be signed in to change notification settings - Fork 45
/
AnyCallHandler.go
234 lines (194 loc) · 7.65 KB
/
AnyCallHandler.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
// Cloud Driver Interface of CB-Spider.
// 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
//
// This is AWS Driver.
//
// by CB-Spider Team, 2022.09.
package resources
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
type AwsAnyCallHandler struct {
Region idrv.RegionInfo
CredentialInfo idrv.CredentialInfo
Client *ec2.EC2
}
/********************************************************
// call example
curl -sX POST http://localhost:1024/spider/anycall -H 'Content-Type: application/json' -d \
'{
"ConnectionName" : "aws-ohio-config",
"ReqInfo" : {
"FID" : "createTags",
"IKeyValueList" : [{"Key":"key1", "Value":"value1"}, {"Key":"key2", "Value":"value2"}]
}
}' | json_pp
********************************************************/
func (anyCallHandler *AwsAnyCallHandler) AnyCall(callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger.Info("AWS Driver: called AnyCall()!")
switch callInfo.FID {
case "createTags":
return createTags(anyCallHandler, callInfo)
case "associateIamInstanceProfile":
return associateIamInstanceProfile(anyCallHandler, callInfo)
case "getRegionInfo":
return getRegionInfo(anyCallHandler, callInfo)
// add more ...
default:
return irs.AnyCallInfo{}, errors.New("AWS Driver: " + callInfo.FID + " Function is not implemented!")
}
}
///////////////////////////////////////////////////////////////////////
// implemented by developer user, like 'createTags(kv []KeyVale) bool'
///////////////////////////////////////////////////////////////////////
const (
CreateTagsResourceId = "ResourceId"
CreateTagsTag = "Tag"
)
func createTags(anyCallHandler *AwsAnyCallHandler, callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger.Info("AWS Driver: called AnyCall()/createTags()!")
if anyCallHandler.Client == nil {
return irs.AnyCallInfo{}, errors.New("AWS Driver: " + callInfo.FID + " has no session")
}
// make results
if callInfo.OKeyValueList == nil {
callInfo.OKeyValueList = []irs.KeyValue{}
}
// Input Arg Validation
if callInfo.IKeyValueList == nil {
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "false"})
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Reason", fmt.Sprintf("Input Argument is empty!")})
return callInfo, nil
}
var resourceId string
var tags []*ec2.Tag
// run
for _, kv := range callInfo.IKeyValueList {
if kv.Key == CreateTagsResourceId {
resourceId = kv.Value
} else if kv.Key == CreateTagsTag {
var kvTag irs.KeyValue
if err := json.Unmarshal([]byte(kv.Value), &kvTag); err != nil {
//return irs.AnyCallInfo{}, errors.New("AWS Driver: "+callInfo.FID+"'s argument(key=%s, value=%s) is invalid", kv.Key, kv.Value)
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "false"})
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Reason", fmt.Sprintf("Input Argument(key=%s,value=%s) is invalid!(err=%v)", kv.Key, kv.Value, err)})
return callInfo, nil
}
tags = append(tags, &ec2.Tag{Key: &kvTag.Key, Value: &kvTag.Value})
}
}
input := &ec2.CreateTagsInput{
Resources: []*string{aws.String(resourceId)},
Tags: tags,
}
cblogger.Info("input:", input)
output, err := anyCallHandler.Client.CreateTags(input)
if err != nil {
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "false"})
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Reason", fmt.Sprintf("got error: %v", err)})
return callInfo, nil
}
cblogger.Info("output:", output)
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "true"})
return callInfo, nil
}
const (
AssociateIamInstanceProfileInstanceId = "InstanceId"
AssociateIamInstanceProfileRole = "Role"
)
///////////////////////////////////////////////////////////////////
// implemented by developer user, like 'associateIamInstanceProfile(kv []KeyValue) bool'
///////////////////////////////////////////////////////////////////
func associateIamInstanceProfile(anyCallHandler *AwsAnyCallHandler, callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger.Info("AWS Driver: called AnyCall()/associateIamInstanceProfile()!")
if anyCallHandler.Client == nil {
return irs.AnyCallInfo{}, errors.New("AWS Driver: " + callInfo.FID + " has no session")
}
// make results
if callInfo.OKeyValueList == nil {
callInfo.OKeyValueList = []irs.KeyValue{}
}
// Input Arg Validation
if callInfo.IKeyValueList == nil {
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "false"})
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Reason", fmt.Sprintf("Input Argument is empty!")})
return callInfo, nil
}
var instanceId string
var role string
// run
for _, kv := range callInfo.IKeyValueList {
if kv.Key == AssociateIamInstanceProfileInstanceId {
instanceId = kv.Value
} else if kv.Key == AssociateIamInstanceProfileRole {
role = kv.Value
}
}
input := &ec2.AssociateIamInstanceProfileInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: &role,
},
InstanceId: &instanceId,
}
cblogger.Info("input:", input)
output, err := anyCallHandler.Client.AssociateIamInstanceProfile(input)
if err != nil {
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "false"})
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Reason", fmt.Sprintf("got error: %v", err)})
return callInfo, nil
}
cblogger.Info("output:", output)
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Result", "true"})
return callInfo, nil
}
func getRegionInfo(anyCallHandler *AwsAnyCallHandler, callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger.Info("AWS Driver: called AnyCall()/getRegionInfo()!")
// encryption and make results
if callInfo.OKeyValueList == nil {
callInfo.OKeyValueList = []irs.KeyValue{}
}
callInfo.OKeyValueList = append(callInfo.OKeyValueList,
irs.KeyValue{"Region", tmpEncryptAndEncode(anyCallHandler.Region.Region)})
callInfo.OKeyValueList = append(callInfo.OKeyValueList,
irs.KeyValue{"Zone", tmpEncryptAndEncode(anyCallHandler.Region.Zone)})
return callInfo, nil
}
// exmaples
func tmpEncryptAndEncode(i string) string {
// Implement to encrypt secure info
// ref) encryptKeyValueList() and decryptKeyValueList() in cloud-info-manager/credential-info-manager/CredentialInfoManager.go
// this is example codes
encb, _ := encrypt([]byte(i))
sEnc := base64.StdEncoding.EncodeToString(encb)
return sEnc
}
// examples: encryption with spider key
func encrypt(contents []byte) ([]byte, error) {
var spider_key = []byte("cloud-barista-cb-spider-cloud-ba") // 32 bytes
encryptData := make([]byte, aes.BlockSize+len(contents))
initVector := encryptData[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, initVector); err != nil {
return nil, err
}
cipherBlock, err := aes.NewCipher(spider_key)
if err != nil {
return nil, err
}
cipherTextFB := cipher.NewCFBEncrypter(cipherBlock, initVector)
cipherTextFB.XORKeyStream(encryptData[aes.BlockSize:], []byte(contents))
return encryptData, nil
}