-
Notifications
You must be signed in to change notification settings - Fork 500
/
policy.go
233 lines (206 loc) · 7.61 KB
/
policy.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
package cmd
import (
"fmt"
"github.com/cloudquery/cloudquery/pkg/errors"
"github.com/cloudquery/cloudquery/pkg/ui/console"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
policyHelpMsg = `Download and run CloudQuery policy`
policyDescribeHelpMsg = `Describe CloudQuery policy`
policyDownloadHelpMsg = "Download a policy from the CloudQuery Policy Hub"
policyRunHelpMsg = "Executes a policy on CloudQuery database"
policySnapshotHelpMsg = `Take database snapshot of all tables included in a CloudQuery policy`
policyTestHelpMsg = "Tests policy against a precompiled set of database snapshots"
policyValidateHelpMsg = "Validate policy for any issues and diagnostics"
policyPruneHelpMsg = "Prune policy executions from the database which are older than the relative time specified"
)
var (
policyCmd = &cobra.Command{
Use: "policy SUBCOMMAND",
Short: policyHelpMsg,
Long: policyHelpMsg,
}
describePolicyCmd = &cobra.Command{
Use: "describe",
Short: policyDescribeHelpMsg,
Long: policyDescribeHelpMsg,
Example: `
# Describe official policy
cloudquery policy describe aws
# The following will be the same as above
# Official policies are hosted here: https://github.com/cloudquery-policies
cloudquery policy describe aws//cis-1.2.0
# Describe community policy
cloudquery policy describe github.com/COMMUNITY_GITHUB_ORG/aws
# See https://hub.cloudquery.io for additional policies.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), true, nil, instanceId)
if err != nil {
return err
}
return c.DescribePolicies(cmd.Context(), args[0])
},
}
policyDownloadCmd = &cobra.Command{
Use: "download GITHUB_REPO",
Short: policyDownloadHelpMsg,
Long: policyDownloadHelpMsg,
Example: `
# Download official policy
cloudquery policy download aws
# The following will be the same as above
# Official policies are hosted here: https://github.com/cloudquery-policies
cloudquery policy download aws//cis-1.2.0
# Download community policy
cloudquery policy download github.com/COMMUNITY_GITHUB_ORG/aws
# See https://hub.cloudquery.io for additional policies.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), true, nil, instanceId)
if err != nil {
return err
}
diags := c.DownloadPolicy(cmd.Context(), args)
if diags.HasErrors() {
return fmt.Errorf("policy download has one or more errors, check logs")
}
return nil
},
}
outputDir string
noResults bool
storeResults bool
policyRunCmd = &cobra.Command{
Use: "run",
Short: policyRunHelpMsg,
Long: policyRunHelpMsg,
Example: `
# Download & Run the policies defined in your config
cloudquery policy run
# Run a specific policy that is not defined in the config.hcl
# Run official policy
cloudquery policy run aws
# The following will be the same as above
# Official policies are hosted here: https://github.com/cloudquery-policies
cloudquery policy run aws//cis_v1.2.0
# Run community policy
cloudquery policy run github.com/COMMUNITY_GITHUB_ORG/aws
# See https://hub.cloudquery.io for additional policies.`,
RunE: func(cmd *cobra.Command, args []string) error {
source := args[0]
c, err := console.CreateClient(cmd.Context(), getConfigFile(), false, nil, instanceId)
if err != nil {
return err
}
diags := c.RunPolicies(cmd.Context(), source, outputDir, noResults, storeResults)
errors.CaptureDiagnostics(diags, map[string]string{"command": "policy_run"})
if diags.HasErrors() {
return fmt.Errorf("policy has one or more errors, check logs")
}
return nil
},
Args: cobra.ExactArgs(1),
}
policyTestCmd = &cobra.Command{
Use: "test",
Short: policyTestHelpMsg,
Long: policyTestHelpMsg,
Example: `
# Download & Run the policies defined in your config
cloudquery policy test path/to/policy.hcl path/to/snapshot/dir selector
`,
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), false, nil, instanceId)
if err != nil {
return err
}
err = c.TestPolicies(cmd.Context(), args[0], args[1])
errors.CaptureError(err, map[string]string{"command": "policy_test"})
return err
},
Args: cobra.ExactArgs(2),
}
snapshotPolicyCmd = &cobra.Command{
Use: "snapshot",
Short: policySnapshotHelpMsg,
Long: policySnapshotHelpMsg,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), false, nil, instanceId)
if err != nil {
return err
}
err = c.SnapshotPolicy(cmd.Context(), args[0], args[1])
errors.CaptureError(err, map[string]string{"command": "policy_snapshot"})
return err
},
}
validatePolicyCmd = &cobra.Command{
Use: "validate",
Short: policyValidateHelpMsg,
Long: policyValidateHelpMsg,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), false, nil, instanceId)
if err != nil {
return err
}
diags := c.ValidatePolicy(cmd.Context(), args[0])
errors.CaptureDiagnostics(diags, map[string]string{"command": "policy_validate"})
if diags.HasErrors() {
return fmt.Errorf("policy validate has one or more errors, check logs")
}
return nil
},
}
prunePolicyCmd = &cobra.Command{
Use: "prune",
Short: policyPruneHelpMsg,
Long: policyPruneHelpMsg,
Example: `
# Prune the policy executions which are older than the relative time specified
cloudquery policy prune 24h`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := console.CreateClient(cmd.Context(), getConfigFile(), false, nil, instanceId)
if err != nil {
return err
}
retentionPeriod := args[0]
diags := c.PrunePolicyExecutions(cmd.Context(), retentionPeriod)
errors.CaptureDiagnostics(diags, map[string]string{"command": "policy_prune"})
if diags.HasErrors() {
return fmt.Errorf("policy prune has one or more errors, check logs")
}
return nil
},
}
)
func init() {
describePolicyCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(describePolicyCmd)
policyDownloadCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(policyDownloadCmd)
flags := policyRunCmd.Flags()
flags.StringVar(&outputDir, "output-dir", "", "Generates a new file for each policy at the given dir with the output")
flags.BoolVar(&noResults, "no-results", false, "Do not show policies results")
flags.BoolVar(&storeResults, "enable-db-persistence", false, "Enable storage of policy output in database")
flags.Bool("disable-fetch-check", false, "Disable checking if a respective fetch happened before running policies")
_ = viper.BindPFlag("disable-fetch-check", flags.Lookup("disable-fetch-check"))
policyRunCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(policyRunCmd)
snapshotPolicyCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(snapshotPolicyCmd)
flags = policyTestCmd.Flags()
flags.StringVar(&outputDir, "output-dir", "", "Generates a new file for each policy at the given dir with the output")
flags.BoolVar(&noResults, "no-results", false, "Do not show policies results")
policyRunCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(policyTestCmd)
policyCmd.AddCommand(validatePolicyCmd)
policyCmd.AddCommand(prunePolicyCmd)
policyCmd.SetUsageTemplate(usageTemplateWithFlags)
rootCmd.AddCommand(policyCmd)
}