forked from cloudquery/cloudquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
272 lines (246 loc) · 5.98 KB
/
client.go
File metadata and controls
272 lines (246 loc) · 5.98 KB
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
package cloudqueryclient
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/cloudquery/cloudquery/database"
"github.com/cloudquery/cloudquery/providers/aws"
"github.com/cloudquery/cloudquery/providers/azure"
"github.com/cloudquery/cloudquery/providers/gcp"
"github.com/cloudquery/cloudquery/providers/k8s"
"github.com/cloudquery/cloudquery/providers/okta"
"github.com/cloudquery/cloudquery/providers/provider"
"github.com/olekukonko/tablewriter"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"strings"
)
var ProviderMap = map[string]func(*database.Database, *zap.Logger) (provider.Interface, error){
"aws": aws.NewProvider,
"gcp": gcp.NewProvider,
"okta": okta.NewProvider,
"azure": azure.NewProvider,
"k8s": k8s.NewProvider,
}
type Config struct {
Providers []struct {
Name string
Rest map[string]interface{} `yaml:",inline"`
}
}
type PolicyConfig struct {
Views []struct {
Name string
Query string
}
Queries []struct {
Name string
Invert bool
Query string
}
}
type Client struct {
db *database.Database
config Config
log *zap.Logger
}
func NewLogger(verbose bool, options ...zap.Option) (*zap.Logger, error) {
level := zap.NewAtomicLevelAt(zap.InfoLevel)
disableCaller := true
if verbose {
level = zap.NewAtomicLevelAt(zap.DebugLevel)
disableCaller = false
}
return zap.Config{
Sampling: nil,
Level: level,
Development: true,
DisableCaller: disableCaller,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}.Build(options...)
}
func New(driver string, dsn string, verbose bool) (*Client, error) {
client := Client{}
var err error
client.db, err = database.Open(driver, dsn)
if err != nil {
return nil, err
}
zapLogger, err := NewLogger(verbose)
client.log = zapLogger
if err != nil {
return nil, err
}
return &client, nil
}
func (c *Client) Run(path string) error {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s doesn't exist. you can create one via 'gen config' command", path)
}
return err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
config := Config{}
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
for _, provider := range config.Providers {
if provider.Name == "" {
return fmt.Errorf("provider must contain key: name")
}
if ProviderMap[provider.Name] == nil {
return fmt.Errorf("provider %s is not supported\n", provider.Name)
}
log := c.log.With(zap.String("provider", provider.Name))
p, err := ProviderMap[provider.Name](c.db, log)
if err != nil {
return err
}
err = p.Run(provider.Rest)
if err != nil {
return err
}
}
return nil
}
func (c *Client) RunQuery(path string, outputPath string) error {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s doesn't exist. you can create one via 'gen policy' command", path)
}
return err
}
if c.db.Driver == "neo4j" {
return fmt.Errorf("query command doesn't support neo4j driver yet")
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
config := PolicyConfig{}
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
err = c.createViews(&config)
if err != nil {
return err
}
err = c.runQueries(&config, outputPath)
if err != nil {
return err
}
return nil
}
type QueryResult struct {
Name string
CheckPassed bool
ResultHeaders []string
ResultRows [][]string
}
func (c *Client) runQueries(config *PolicyConfig, outputPath string) error {
var f *os.File
var err error
if outputPath != "" {
f, err = os.Create(outputPath)
if err != nil {
return err
}
defer f.Close()
}
c.log.Info("Executing queries", zap.Int("count", len(config.Queries)))
for _, query := range config.Queries {
queryResult := QueryResult{
Name: query.Name,
CheckPassed: true,
}
c.log.Info("Executing query", zap.String("name", query.Name))
rows, err := c.db.GormDB.Raw(query.Query).Rows()
if err != nil {
return err
}
columns, err := rows.Columns()
if err != nil {
return err
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetHeader(columns)
nc := len(columns)
queryResult.ResultHeaders = columns
prettyRow := make([]string, nc)
res := make([]sql.NullString, nc)
resPtrs := make([]interface{}, nc)
for i := 0; i < nc; i++ {
resPtrs[i] = &res[i]
}
resultsCount := 0
for rows.Next() {
err := rows.Scan(resPtrs...)
if err != nil {
return err
}
resultsCount += 1
for i, v := range res {
prettyRow[i] = v.String
}
table.Append(prettyRow)
queryResult.ResultRows = append(queryResult.ResultRows, prettyRow)
}
err = rows.Close()
if err != nil {
return err
}
if resultsCount > 0 && !query.Invert{
c.log.Info("Check failed. Query returned results.", zap.String("name", query.Name), zap.Int("count", resultsCount))
table.Render()
queryResult.CheckPassed = false
} else {
if query.Invert {
c.log.Info("Check failed. Query returned no results.", zap.String("name", query.Name))
queryResult.CheckPassed = false
} else {
c.log.Info("Check passed. Query returned no results.", zap.String("name", query.Name))
}
}
if outputPath != "" {
b, err := json.Marshal(&queryResult)
if err != nil {
return err
}
_, err = f.WriteString(string(b) + "\n")
if err != nil {
return err
}
}
}
return nil
}
func (c *Client) createViews(config *PolicyConfig) error {
c.log.Info("Creating views", zap.Int("count", len(config.Views)))
for _, view := range config.Views {
c.log.Info("Creating view", zap.String("name", view.Name))
fmt.Println(view.Query)
err := c.db.GormDB.Exec(view.Query).Error
if err != nil {
if strings.HasPrefix(err.Error(), "table") {
c.log.Info("table already exist. skipping.", zap.String("name", view.Name))
continue
}
return err
}
}
return nil
}