-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (85 loc) · 2.77 KB
/
main.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
package main
import (
"flag"
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/rs/zerolog"
log "github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.DebugLevel)
configPath := flag.String("config", "config.yaml", "--config=config.yaml")
flag.Parse()
if *configPath == "" {
log.Error().Msg("Config parameter value cannot be empty. Please pass --config parameter value")
return
}
cfg, err := NewSyncConfig(*configPath)
if err != nil {
log.Error().Str("config_file", *configPath).Err(err).Msg("configuration error")
return
}
interval, err := time.ParseDuration(cfg.Interval)
if err != nil {
log.Error().Err(err).Msg("interval cannot be expected format")
return
}
client := NewAWSClient(*cfg)
log.Info().Msg("scheduling started")
log.Info().Str("interval", cfg.Interval).Msg("it has been set interval")
for range time.Tick(interval) {
src, dsts, err := describeAWSSecurityGroups(client)
if err != nil {
log.Error().Err(err).Msg("cannot describe security groups from AWS")
continue
}
err = executeSecurityGroupFunctions(src, dsts, client)
if err != nil {
log.Error().Err(err).Msg("operation error")
}
}
}
func describeAWSSecurityGroups(client *AWSClient) (src *ec2.SecurityGroup, dsts []*ec2.SecurityGroup, err error) {
src, err = client.GetSourceSecurityGroup()
if err != nil {
return nil, nil, err
}
dsts, err = client.GetDestinationSecurityGroups()
if err != nil {
return nil, nil, err
}
return src, dsts, err
}
func executeSecurityGroupFunctions(src *ec2.SecurityGroup, destinations []*ec2.SecurityGroup, client *AWSClient) (err error) {
sync := NewSyncGroup(src, destinations)
ingress := sync.willbeAddedIngress()
egress := sync.willbeAddedEgress()
revokeingress := sync.willbeDeleteIngress()
revokeegress := sync.willbeDeleteEgress()
log.Debug().Str("ingress", fmt.Sprintf("%v", ingress)).Msg("ingress operations executing")
err = client.AuthorizeIngress(ingress)
if err != nil {
log.Debug().Err(err).Msg("ingress operation error")
return err
}
log.Debug().Str("egress", fmt.Sprintf("%v", egress)).Msg("egress operations executing")
err = client.AuthorizeEgress(egress)
if err != nil {
log.Debug().Err(err).Msg("egress operation error")
return err
}
log.Debug().Str("revokeingress", fmt.Sprintf("%v", revokeingress)).Msg("revoke ingress operations executing")
err = client.RevokeIngress(revokeingress)
if err != nil {
log.Debug().Err(err).Msg("revoke ingress operation error")
return err
}
log.Debug().Str("revokeegress", fmt.Sprintf("%v", revokeegress)).Msg("revoke egress operations executing")
err = client.RevokeEgress(revokeegress)
if err != nil {
log.Debug().Err(err).Msg("revoke egress operation error")
}
return err
}