/
util.go
229 lines (209 loc) · 5.01 KB
/
util.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
package proxy
import (
"net/url"
"strings"
"github.com/jumpserver/koko/pkg/jms-sdk-go/model"
"github.com/jumpserver/koko/pkg/jms-sdk-go/service"
storage "github.com/jumpserver/koko/pkg/proxy/recorderstorage"
)
type StorageType interface {
TypeName() string
}
type Storage interface {
Upload(gZipFile, target string) error
StorageType
}
type ReplayStorage interface {
Storage
}
type FTPFileStorage interface {
Storage
}
type CommandStorage interface {
BulkSave(commands []*model.Command) error
StorageType
}
func GetStorage(conf *model.TerminalConfig) Storage {
cfg := conf.ReplayStorage
switch cfg.TypeName {
case "azure":
var (
accountName string
accountKey string
containerName string
endpointSuffix string
)
endpointSuffix = cfg.EndpointSuffix
accountName = cfg.AccountName
accountKey = cfg.AccountKey
containerName = cfg.ContainerName
if endpointSuffix == "" {
endpointSuffix = "core.chinacloudapi.cn"
}
return storage.AzureReplayStorage{
AccountName: accountName,
AccountKey: accountKey,
ContainerName: containerName,
EndpointSuffix: endpointSuffix,
}
case "oss":
var (
endpoint string
bucket string
accessKey string
secretKey string
)
endpoint = cfg.Endpoint
bucket = cfg.Bucket
accessKey = cfg.AccessKey
secretKey = cfg.SecretKey
return storage.OSSReplayStorage{
Endpoint: endpoint,
Bucket: bucket,
AccessKey: accessKey,
SecretKey: secretKey,
}
case "s3", "swift", "cos":
var (
region string
endpoint string
bucket string
accessKey string
secretKey string
)
bucket = cfg.Bucket
endpoint = cfg.Endpoint
region = cfg.Region
accessKey = cfg.AccessKey
secretKey = cfg.SecretKey
if region == "" && endpoint != "" {
region = ParseEndpointRegion(endpoint)
}
if bucket == "" {
bucket = "jumpserver"
}
return storage.S3ReplayStorage{
Bucket: bucket,
Region: region,
AccessKey: accessKey,
SecretKey: secretKey,
Endpoint: endpoint,
}
case "obs":
var (
endpoint string
bucket string
accessKey string
secretKey string
)
endpoint = cfg.Endpoint
bucket = cfg.Bucket
accessKey = cfg.AccessKey
secretKey = cfg.SecretKey
return storage.OBSReplayStorage{
Endpoint: endpoint,
Bucket: bucket,
AccessKey: accessKey,
SecretKey: secretKey,
}
case "null":
return storage.NewNullStorage()
default:
return nil
}
}
func NewReplayStorage(jmsService *service.JMService, conf *model.TerminalConfig) ReplayStorage {
replayStorage := GetStorage(conf)
if replayStorage == nil {
replayStorage = storage.ServerStorage{StorageType: "server", JmsService: jmsService}
}
return replayStorage
}
func NewFTPFileStorage(jmsService *service.JMService, conf *model.TerminalConfig) FTPFileStorage {
ftpStorage := GetStorage(conf)
if ftpStorage == nil {
ftpStorage = storage.FTPServerStorage{StorageType: "server", JmsService: jmsService}
}
return ftpStorage
}
func NewCommandStorage(jmsService *service.JMService, conf *model.TerminalConfig) CommandStorage {
cf := conf.CommandStorage
tp, ok := cf["TYPE"]
if !ok {
tp = "server"
}
/*
{
'DOC_TYPE': 'command',
'HOSTS': ['http://172.16.10.122:9200'],
'INDEX': 'jumpserver',
'OTHER': {'IGNORE_VERIFY_CERTS': True, 'IS_INDEX_DATASTREAM': True},
'TYPE': 'es'
}
*/
switch tp {
case "es", "elasticsearch":
var hosts = make([]string, len(cf["HOSTS"].([]interface{})))
for i, item := range cf["HOSTS"].([]interface{}) {
hosts[i] = item.(string)
}
var skipVerify bool
var isDataStream bool
index := cf["INDEX"].(string)
docType := cf["DOC_TYPE"].(string)
if otherMap, ok := cf["OTHER"].(map[string]interface{}); ok {
if insecureSkipVerify, ok := otherMap["IGNORE_VERIFY_CERTS"]; ok {
skipVerify = insecureSkipVerify.(bool)
}
if isIndexDataStream, ok := otherMap["IS_INDEX_DATASTREAM"]; ok {
isDataStream = isIndexDataStream.(bool)
}
}
if index == "" {
index = "jumpserver"
}
if docType == "" {
docType = "_doc"
}
return storage.ESCommandStorage{
Hosts: hosts,
Index: index,
DocType: docType,
IsDataStream: isDataStream,
InsecureSkipVerify: skipVerify,
}
case "null":
return storage.NewNullStorage()
default:
return storage.ServerStorage{StorageType: "server", JmsService: jmsService}
}
}
func ParseEndpointRegion(s string) string {
if strings.Contains(s, amazonawsSuffix) {
return ParseAWSURLRegion(s)
}
endpoint, err := url.Parse(s)
if err != nil {
return ""
}
endpoints := strings.Split(endpoint.Hostname(), ".")
if len(endpoints) >= 3 {
return endpoints[len(endpoints)-3]
}
return ""
}
func ParseAWSURLRegion(s string) string {
endpoint, err := url.Parse(s)
if err != nil {
return ""
}
s = endpoint.Hostname()
s = strings.TrimSuffix(s, amazonawsCNSuffix)
s = strings.TrimSuffix(s, amazonawsSuffix)
regions := strings.Split(s, ".")
return regions[len(regions)-1]
}
const (
amazonawsCNSuffix = ".amazonaws.com.cn"
amazonawsSuffix = ".amazonaws.com"
)