generated from padok-team/yatas-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (166 loc) · 5.39 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
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
package main
import (
"encoding/gob"
"os"
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/padok-team/yatas-gcp/gcp/cloudrun"
"github.com/padok-team/yatas-gcp/gcp/functions"
"github.com/padok-team/yatas-gcp/gcp/gcs"
"github.com/padok-team/yatas-gcp/gcp/gke"
"github.com/padok-team/yatas-gcp/gcp/iam"
"github.com/padok-team/yatas-gcp/gcp/instance"
"github.com/padok-team/yatas-gcp/gcp/loadbalancing"
"github.com/padok-team/yatas-gcp/gcp/network"
"github.com/padok-team/yatas-gcp/gcp/sql"
"github.com/padok-team/yatas-gcp/internal"
"github.com/padok-team/yatas-gcp/logger"
"github.com/padok-team/yatas/plugins/commons"
)
type YatasPlugin struct {
logger hclog.Logger
}
// Don't remove this function
// function of Yatas plugin
func (g *YatasPlugin) Run(c *commons.Config) []commons.Tests {
logger.Logger = g.logger
var err error
var accounts []internal.GCPAccount
accounts, err = UnmarshalGCP(g, c)
if err != nil {
logger.Logger.Error("Error unmarshaling GCP accounts", "error", err)
return nil
}
var checksAll []commons.Tests
checks, err := runPlugins(c, "gcp", accounts)
if err != nil {
logger.Logger.Error("Error running plugins", "error", err)
}
checksAll = append(checksAll, checks...)
return checksAll
}
// Run the plugins that are enabled in the config with a switch based on the name of the plugin
func runPlugins(c *commons.Config, plugin string, accounts []internal.GCPAccount) ([]commons.Tests, error) {
var checksAll []commons.Tests
checksAll, err := Run(c, accounts)
if err != nil {
return nil, err
}
return checksAll, nil
}
func Run(c *commons.Config, accounts []internal.GCPAccount) ([]commons.Tests, error) {
var wg sync.WaitGroup
var queue = make(chan commons.Tests, 10)
var checks []commons.Tests
wg.Add(len(accounts))
for _, account := range accounts {
go runTestsForAccount(account, c, queue)
}
go func() {
for t := range queue {
checks = append(checks, t)
wg.Done()
}
}()
wg.Wait()
return checks, nil
}
func runTestsForAccount(account internal.GCPAccount, c *commons.Config, queue chan commons.Tests) {
checks := initTest(account, c)
queue <- checks
}
func initTest(account internal.GCPAccount, c *commons.Config) commons.Tests {
var checks commons.Tests
checks.Account = account.Project
var wg sync.WaitGroup
queue := make(chan []commons.Check, 100)
go commons.CheckMacroTest(&wg, c, gcs.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, instance.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, sql.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, loadbalancing.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, gke.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, cloudrun.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, functions.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, iam.RunChecks)(&wg, account, c, queue)
go commons.CheckMacroTest(&wg, c, network.RunChecks)(&wg, account, c, queue)
go func() {
for t := range queue {
checks.Checks = append(checks.Checks, t...)
wg.Done()
}
}()
wg.Wait()
return checks
}
// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
// directory. It is a UX feature, not a security feature.
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
func main() {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
})
yatasPlugin := &YatasPlugin{
logger: logger,
}
// pluginMap is the map of plugins we can dispense.
// Name of your plugin
var pluginMap = map[string]plugin.Plugin{
"gcp": &commons.YatasPlugin{Impl: yatasPlugin},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
})
}
func UnmarshalGCP(g *YatasPlugin, c *commons.Config) ([]internal.GCPAccount, error) {
var accounts []internal.GCPAccount
for _, r := range c.PluginConfig {
var tmpAccounts []internal.GCPAccount
gcpFound := false
for key, value := range r {
switch key {
case "pluginName":
if value == "gcp" {
gcpFound = true
}
case "accounts":
for _, v := range value.([]interface{}) {
var account internal.GCPAccount
logger.Logger.Debug("Inspecting account", "account", v)
for keyaccounts, valueaccounts := range v.(map[string]interface{}) {
switch keyaccounts {
case "project":
account.Project = valueaccounts.(string)
case "computeRegions":
// Cannot directly unmarshal []interface{} to []string
computeRegions := valueaccounts.([]interface{})
for _, computeRegion := range computeRegions {
account.ComputeRegions = append(account.ComputeRegions, computeRegion.(string))
}
}
}
tmpAccounts = append(tmpAccounts, account)
}
}
}
if gcpFound {
logger.Logger.Debug("GCP config found ✅")
accounts = tmpAccounts
}
}
logger.Logger.Debug("Unmarshal Done ✅")
logger.Logger.Debug("All accounts", "accounts", accounts)
logger.Logger.Debug("Length of accounts", "len", len(accounts))
return accounts, nil
}