-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_config.go
218 lines (194 loc) · 5.88 KB
/
connection_config.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
package exec
import (
"errors"
"fmt"
"os"
"github.com/mitchellh/go-homedir"
communicator "github.com/turbot/go-exec-communicator"
"github.com/turbot/go-exec-communicator/shared"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
type execConfig struct {
WorkingDir *string `hcl:"working_dir"`
Interpreter []string `hcl:"interpreter,optional"`
Protocol *string `hcl:"protocol"`
User *string `hcl:"user"`
Password *string `hcl:"password"`
PrivateKey *string `hcl:"private_key"`
Certificate *string `hcl:"certificate"`
Host *string `hcl:"host"`
HostKey *string `hcl:"host_key"`
Port *int `hcl:"port"`
Https *bool `hcl:"https"`
Insecure *bool `hcl:"insecure"`
BastionUser *string `hcl:"bastion_user"`
BastionPassword *string `hcl:"bastion_password"`
BastionPrivateKey *string `hcl:"bastion_private_key"`
BastionHost *string `hcl:"bastion_host"`
BastionHostKey *string `hcl:"bastion_host_key"`
BastionPort *int `hcl:"bastion_port"`
ProxyHost *string `hcl:"proxy_host"`
ProxyPort *int `hcl:"proxy_port"`
ProxyUserName *string `hcl:"proxy_user_name"`
ProxyUserPassword *string `hcl:"proxy_user_password"`
}
func ConfigInstance() interface{} {
return &execConfig{}
}
// GetConfig :: retrieve and cast connection config from query data
func GetConfig(connection *plugin.Connection) execConfig {
if connection == nil || connection.Config == nil {
return execConfig{}
}
config, _ := connection.Config.(execConfig)
return config
}
// GetCommunicator :: creates a communicator from config
func GetCommunicator(connection *plugin.Connection) (communicator.Communicator, bool, error) {
conf := GetConfig(connection)
config := shared.ConnectionInfo{
Timeout: "10s",
}
// If no other connection info is provided, assume local connection
localConnection := true
// Bastion settings
if conf.BastionHost != nil {
config.BastionHost = *conf.BastionHost
localConnection = false
if conf.BastionUser != nil {
config.BastionUser = *conf.BastionUser
} else {
return nil, localConnection, errors.New("bastion_user is required when using bastion host")
}
if conf.BastionPassword != nil {
config.BastionPassword = *conf.BastionPassword
} else if conf.BastionPrivateKey != nil {
content, err := PathOrContents(*conf.BastionPrivateKey)
if err != nil {
return nil, localConnection, err
}
config.BastionPrivateKey = content
} else {
return nil, localConnection, errors.New("either bastion_password or bastion_private_key is required when using bastion host")
}
}
if conf.BastionHostKey != nil {
config.BastionHostKey = *conf.BastionHostKey
localConnection = false
}
if conf.BastionPort != nil {
config.BastionPort = uint16(*conf.BastionPort)
localConnection = false
}
// PROXY settings
if conf.ProxyHost != nil {
config.ProxyHost = *conf.ProxyHost
localConnection = false
}
if conf.ProxyPort != nil {
config.ProxyPort = uint16(*conf.ProxyPort)
localConnection = false
}
if conf.ProxyUserName != nil {
config.ProxyUserName = *conf.ProxyUserName
localConnection = false
}
if conf.ProxyUserPassword != nil {
config.ProxyUserPassword = *conf.ProxyUserPassword
localConnection = false
} else {
if conf.ProxyUserName != nil {
return nil, localConnection, errors.New("password is required when proxy username is set")
}
}
// Detects remote connection
if conf.Protocol != nil || conf.Host != nil || conf.Port != nil || conf.Https != nil || conf.Insecure != nil || conf.User != nil || conf.Password != nil || conf.PrivateKey != nil {
localConnection = false
}
if conf.Protocol != nil {
config.Type = *conf.Protocol
} else {
if !localConnection {
return nil, localConnection, errors.New("protocol is required in config")
}
}
if conf.Host != nil {
config.Host = *conf.Host
} else {
if !localConnection {
return nil, localConnection, errors.New("host is required in config")
}
}
if conf.Port != nil {
config.Port = uint16(*conf.Port)
}
if conf.Https != nil {
config.HTTPS = *conf.Https
}
if conf.Insecure != nil {
config.Insecure = *conf.Insecure
}
if config.Type == "ssh" {
if conf.User != nil {
config.User = *conf.User
} else {
return nil, localConnection, errors.New("user is required for SSH connections")
}
if conf.Password != nil {
config.Password = *conf.Password
} else if conf.PrivateKey != nil {
content, err := PathOrContents(*conf.PrivateKey)
if err != nil {
return nil, localConnection, err
}
config.PrivateKey = content
} else {
return nil, localConnection, errors.New("either password or private_key is required for SSH connections")
}
}
if config.Type == "winrm" {
if conf.User != nil {
config.User = *conf.User
} else {
return nil, localConnection, errors.New("user is required for WinRM connections")
}
if conf.Password != nil {
config.Password = *conf.Password
} else {
return nil, localConnection, errors.New("password is required for WinRM connections")
}
}
if localConnection {
return nil, localConnection, nil
}
comm, err := communicator.New(config)
return comm, localConnection, err
}
// PathOrContents :: returns the contents of a file if the parameter is a file path, otherwise returns the parameter itself
func PathOrContents(poc string) (string, error) {
if len(poc) == 0 {
return poc, nil
}
path := poc
if path[0] == '~' {
var err error
path, err = homedir.Expand(path)
if err != nil {
return path, err
}
}
// Check for valid file path
if _, err := os.Stat(path); err == nil {
contents, err := os.ReadFile(path)
if err != nil {
return string(contents), err
}
return string(contents), nil
}
// Return error if content is a file path and the file doesn't exist
if len(path) > 1 && (path[0] == '/' || path[0] == '\\') {
return "", fmt.Errorf("%s: no such file or dir", path)
}
// Return the inline content
return poc, nil
}