-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
307 lines (255 loc) · 10.2 KB
/
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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package config
import (
"encoding/json"
"errors"
"fmt"
"log"
"path"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/spf13/viper"
)
var ErrInvalidConnectorName = errors.New("invalid connector name")
type SocketConfig struct {
Host string
Port int
Name string
Type string
Description string
AllowedEmailAddresses []string `mapstructure:"allowed_email_addresses"`
AllowedEmailDomains []string `mapstructure:"allowed_email_domains"`
UpstreamUserDeprecated string `mapstructure:"upstream_user"`
UpstreamUser string `mapstructure:"upstream_username"`
UpstreamPassword string `mapstructure:"upstream_password"`
UpstreamType string `mapstructure:"upstream_type"`
DatabaseCredentials string `mapstructure:"database_credentials"`
UpstreamHttpHostname string `mapstructure:"upstream_http_hostname"`
ConnectorAuthenticationEnabled bool `mapstructure:"connector_authentication"`
Policies []string `mapstructure:"policies"`
UpstreamCertFile string `mapstructure:"upstream_certificate_file"`
UpstreamKeyFile string `mapstructure:"upstream_key_file"`
UpstreamCACertFile string `mapstructure:"upstream_ca_file"`
UpstreamIdentifyFile string `mapstructure:"upstream_identity_file"`
UpstreamTLS *bool `mapstructure:"upstream_tls,omitempty"`
RdsIAMAuth bool `mapstructure:"rds_iam_auth"`
AWSRegion string `mapstructure:"aws_region"`
AWSEC2InstanceConnectEnabled bool `mapstructure:"aws_ec2_instance_connect"`
AWSEC2InstanceId string `mapstructure:"aws_ec2_instance_id"`
CloudSQLConnector bool `mapstructure:"cloudsql_connector"`
CloudSQLIAMAuth bool `mapstructure:"cloudsql_iam_auth"`
CloudSQLInstance string `mapstructure:"cloudsql_instance"`
GoogleCredentialsFile string `mapstructure:"google_credentials_file"`
SSHServer bool `mapstructure:"sshserver"`
AWSECSCluster string `mapstructure:"aws_ecs_cluster"`
TaskFilter []string `mapstructure:"aws_ecs_tasks"`
ServiceFilter []string `mapstructure:"aws_ecs_services"`
ContainerFilter []string `mapstructure:"aws_ecs_containers"`
}
type Credentials struct {
Username string
User string
Password string
Token string
}
func (c Credentials) GetUsername() string {
if c.Username != "" {
return c.Username
}
if c.User != "" {
return c.User
}
return ""
}
type ConnectorGroups struct {
Group string
AllowedEmailAddresses []string `mapstructure:"allowed_email_addresses"`
AllowedEmailDomains []string `mapstructure:"allowed_email_domains"`
ConnectorAuthenticationEnabled bool `mapstructure:"connector_authentication"`
Policies []string `mapstructure:"policies"`
AwsSsmEnabled bool `mapstructure:"aws_ssm_enabled"`
AwsEC2InstanceConnectEnabled bool `mapstructure:"aws_ec2_instance_connect"`
UpstreamUsername string `mapstructure:"upstream_username"`
UpstreamPassword string `mapstructure:"upstream_password"`
UpstreamIdentifyFile string `mapstructure:"upstream_identity_file"`
}
type EcsPlugin struct {
Group string
ConnectorAuthenticationEnabled bool `mapstructure:"connector_authentication"`
Policies []string `mapstructure:"policies"`
TaskFilter []string `mapstructure:"aws_ecs_tasks"`
ServiceFilter []string `mapstructure:"aws_ecs_services"`
ContainerFilter []string `mapstructure:"aws_ecs_containers"`
}
type K8Plugin struct {
Group string
Namespace string
AllowedEmailAddresses []string `mapstructure:"allowed_email_addresses"`
AllowedEmailDomains []string `mapstructure:"allowed_email_domains"`
ConnectorAuthenticationEnabled bool `mapstructure:"connector_authentication"`
Policies []string `mapstructure:"policies"`
UpstreamUsername string `mapstructure:"upstream_username"`
UpstreamPassword string `mapstructure:"upstream_password"`
UpstreamIdentifyFile string `mapstructure:"upstream_identity_file"`
}
type NetworkPlugin struct {
Scan_interval int64 `mapstructure:"scan_interval"`
Group string `mapstructure:"group"`
AllowedEmailAddresses []string `mapstructure:"allowed_email_addresses"`
AllowedEmailDomains []string `mapstructure:"allowed_email_domains"`
Networks map[string]NetworkPluginNetwork `mapstructure:"networks"`
Policies []string `mapstructure:"policies"`
ConnectorAuthenticationEnabled bool `mapstructure:"connector_authentication"`
}
type NetworkPluginNetwork struct {
Interfaces []string `mapstructure:"interfaces"`
Subnets []string `mapstructure:"subnets"`
Ports []uint16 `mapstructure:"ports"`
}
type Connector struct {
Name string
AwsRegion string `mapstructure:"aws-region"`
SSMAwsRegion string `mapstructure:"ssm-aws-region"`
AwsProfile string `mapstructure:"aws-profile"`
ProviderRegion string `mapstructure:"provider-region"`
ProviderEnv string `mapstructure:"provider-enviroment"`
ProviderEnvironment string `mapstructure:"provider-environment"`
ProviderType string `mapstructure:"provider-type"`
}
func (c *Connector) GetProviderEnvironment() string {
if c.ProviderEnvironment != "" {
return c.ProviderEnvironment
}
return c.ProviderEnv
}
type SocketParams []map[string]SocketConfig
type Config struct {
Credentials Credentials
Sockets SocketParams
Connector Connector
AwsGroups []ConnectorGroups `mapstructure:"aws_groups"`
EcsPlugin []EcsPlugin `mapstructure:"aws_ecs_ssm"`
DockerPlugin []ConnectorGroups `mapstructure:"docker_plugin"`
NetworkPlugin []NetworkPlugin `mapstructure:"network_plugin"`
K8Plugin []K8Plugin `mapstructure:"k8_plugin"`
}
func (c *Config) Validate() error {
if c.Connector.Name == "" {
return fmt.Errorf("connector.name is required")
} else if validateName(c.Connector.Name) != nil {
return ErrInvalidConnectorName
}
return nil
}
func validateName(name string) error {
re := regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,198}[a-zA-Z0-9])?$`)
if !re.Match([]byte(name)) {
return errors.New("invalid name")
}
return nil
}
func NewConfig() *Config {
return &Config{}
}
type ConfigParser struct {
}
func NewConfigParser() *ConfigParser {
return &ConfigParser{}
}
func (c *ConfigParser) Parse(configPath string) (*Config, error) {
viper.SetConfigType("yaml")
viper.AddConfigPath(path.Dir(configPath))
viper.SetConfigFile(configPath)
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
var cfg Config
err = viper.Unmarshal(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
func (c *ConfigParser) LoadSSMInConfig(ssmAPI ssmiface.SSMAPI, cfg *Config) error {
for _, socketMap := range cfg.Sockets {
for k, v := range socketMap {
if strings.HasPrefix(v.DatabaseCredentials, "aws:ssm:") {
jsonCreds := SetupSSMField(ssmAPI, v.DatabaseCredentials)
var mapCreds map[string]interface{}
err := json.Unmarshal([]byte(jsonCreds), &mapCreds)
if err != nil {
return err
}
v.UpstreamUser = fmt.Sprint(mapCreds["username"])
v.UpstreamPassword = fmt.Sprint(mapCreds["password"])
if value, ok := mapCreds["engine"]; ok {
v.UpstreamType = fmt.Sprint(value)
}
if value, ok := mapCreds["type"]; ok {
v.UpstreamType = fmt.Sprint(value)
}
v.Host = fmt.Sprint(mapCreds["host"])
}
if strings.HasPrefix(v.UpstreamPassword, "aws:ssm:") {
v.UpstreamPassword = SetupSSMField(ssmAPI, v.UpstreamPassword)
}
if strings.HasPrefix(v.UpstreamType, "aws:ssm:") {
v.UpstreamType = SetupSSMField(ssmAPI, v.UpstreamType)
}
if strings.HasPrefix(v.UpstreamUser, "aws:ssm:") {
v.UpstreamUser = SetupSSMField(ssmAPI, v.UpstreamUser)
}
if strings.HasPrefix(v.Host, "aws:ssm:") {
v.Host = SetupSSMField(ssmAPI, v.Host)
}
socketMap[k] = v
}
}
if strings.HasPrefix(cfg.Credentials.Username, "aws:ssm:") {
cfg.Credentials.Username = SetupSSMField(ssmAPI, cfg.Credentials.Username)
}
if strings.HasPrefix(cfg.Credentials.Token, "aws:ssm:") {
cfg.Credentials.Token = SetupSSMField(ssmAPI, cfg.Credentials.Token)
}
if strings.HasPrefix(cfg.Credentials.Password, "aws:ssm:") {
cfg.Credentials.Password = SetupSSMField(ssmAPI, cfg.Credentials.Password)
}
return nil
}
func FetchFromSSM(svc ssmiface.SSMAPI, param string) (*ssm.GetParameterOutput, error) {
results, err := svc.GetParameter(&ssm.GetParameterInput{
Name: aws.String(param),
WithDecryption: aws.Bool(true),
})
return results, err
}
func StartSSMSession(cfg *Config) (ssmiface.SSMAPI, error) {
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: cfg.Connector.AwsProfile,
Config: aws.Config{
Region: &cfg.Connector.SSMAwsRegion,
},
})
if err != nil {
log.Printf("failed to create aws session: %v", err)
return nil, err
}
return ssm.New(sess), nil
}
func SetupSSMField(svc ssmiface.SSMAPI, key string) string {
spllitedKey := strings.Split(key, "aws:ssm:")
output, err := FetchFromSSM(svc, spllitedKey[1])
if err != nil {
log.Printf("we couldn't fetch the key %v from ssm: %v\n", spllitedKey, err)
return ""
}
if output != nil && output.Parameter != nil && output.Parameter.Value != nil {
return *output.Parameter.Value
}
return ""
}