Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(policies): add policy flags to CLI #129

Merged
merged 3 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/_data/curio_init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ options:
default_value: '[]'
usage: |
specify the comma separated files and directories to skip (supports * syntax), eg. --skip-path users/*.go,users/admin.sql
- name: skip-policy
default_value: '[]'
usage: |
specify the comma separated ids of the policies you would like to skip. Runs all other policies.
- name: only-policy
default_value: '[]'
usage: |
specify the comma separated ids of the policies you would like to run. Skips all other policies.
- name: timeout
default_value: 10m0s
usage: time allowed to complete scan
Expand All @@ -69,4 +77,4 @@ options:
usage: number of processing workers to spawn
see_also:
- ' - '
aliases:
aliases:
8 changes: 8 additions & 0 deletions docs/_data/curio_scan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ options:
default_value: '[]'
usage: |
specify the comma separated files and directories to skip (supports * syntax), eg. --skip-path users/*.go,users/admin.sql
- name: skip-policy
default_value: '[]'
usage: |
specify the comma separated ids of the policies you would like to skip. Runs all other policies.
- name: only-policy
default_value: '[]'
usage: |
specify the comma separated ids of the policies you would like to run. Skips all other policies.
- name: timeout
default_value: 10m0s
usage: time allowed to complete scan
Expand Down
3 changes: 3 additions & 0 deletions integration/flags/.snapshots/TestInitCommand-init
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
policy:
only-policy: []
skip-policy: []
report:
format: json
output: ""
Expand Down
4 changes: 4 additions & 0 deletions integration/flags/.snapshots/TestMetadataFlags-help-scan
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Scan Flags
--quiet suppress non-essential messages
--skip-path strings specify the comma separated files and directories to skip (supports * syntax), eg. --skip-path users/*.go,users/admin.sql

Policy Flags
--only-policy strings specify the comma separated ids of the policies you would like to run. Skips all other policies.
--skip-policy strings specify the comma separated ids of the policies you would like to skip. Runs all other policies.

Worker Flags
--existing-worker string URL of an existing worker
--file-size-max int ignore files with file size larger than this config (default 25000000)
Expand Down
4 changes: 4 additions & 0 deletions integration/flags/.snapshots/TestMetadataFlags-scan-help
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Scan Flags
--quiet suppress non-essential messages
--skip-path strings specify the comma separated files and directories to skip (supports * syntax), eg. --skip-path users/*.go,users/admin.sql

Policy Flags
--only-policy strings specify the comma separated ids of the policies you would like to run. Skips all other policies.
--skip-policy strings specify the comma separated ids of the policies you would like to skip. Runs all other policies.

Worker Flags
--existing-worker string URL of an existing worker
--file-size-max int ignore files with file size larger than this config (default 25000000)
Expand Down
14 changes: 13 additions & 1 deletion pkg/commands/process/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ func FromOptions(opts flag.Options) (Config, error) {
}
}

for _, policy := range policies {
for key := range policies {
policy := policies[key]

if len(opts.PolicyOptions.OnlyPolicy) > 0 && !opts.PolicyOptions.OnlyPolicy[policy.Id] {
delete(policies, key)
continue
}

if opts.PolicyOptions.SkipPolicy[policy.Id] {
delete(policies, key)
continue
}

for _, module := range policy.Modules {
if module.Path != "" {
content, err := policiesFs.ReadFile(module.Path)
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "he

var scanFlags = &flag.Flags{
ScanFlagGroup: flag.NewScanFlagGroup(),
PolicyFlagGroup: flag.NewPolicyFlagGroup(),
WorkerFlagGroup: flag.NewWorkerFlagGroup(),
ReportFlagGroup: flag.NewReportFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
Expand Down
9 changes: 9 additions & 0 deletions pkg/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FlagGroup interface {
type Flags struct {
RepoFlagGroup *RepoFlagGroup
ReportFlagGroup *ReportFlagGroup
PolicyFlagGroup *PolicyFlagGroup
ProcessFlagGroup *ProcessFlagGroup
ScanFlagGroup *ScanFlagGroup
WorkerFlagGroup *WorkerFlagGroup
Expand All @@ -53,6 +54,7 @@ type Flags struct {
type Options struct {
RepoOptions
ReportOptions
PolicyOptions
WorkerOptions
ScanOptions
GeneralOptions
Expand Down Expand Up @@ -154,6 +156,9 @@ func (f *Flags) groups() []FlagGroup {
if f.ProcessFlagGroup != nil {
groups = append(groups, f.ProcessFlagGroup)
}
if f.PolicyFlagGroup != nil {
groups = append(groups, f.PolicyFlagGroup)
}
if f.RepoFlagGroup != nil {
groups = append(groups, f.RepoFlagGroup)
}
Expand Down Expand Up @@ -243,6 +248,10 @@ func (f *Flags) ToOptions(args []string) (Options, error) {
opts.ReportOptions = f.ReportFlagGroup.ToOptions()
}

if f.PolicyFlagGroup != nil {
opts.PolicyOptions = f.PolicyFlagGroup.ToOptions(args)
}

if f.WorkerFlagGroup != nil {
opts.WorkerOptions = f.WorkerFlagGroup.ToOptions()
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/flag/policy_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package flag

var (
SkipPolicyFlag = Flag{
Name: "skip-policy",
ConfigName: "policy.skip-policy",
Value: []string{},
Usage: "specify the comma separated ids of the policies you would like to skip. Runs all other policies.",
}
OnlyPolicyFlag = Flag{
Name: "only-policy",
ConfigName: "policy.only-policy",
Value: []string{},
Usage: "specify the comma separated ids of the policies you would like to run. Skips all other policies.",
}
)

type PolicyFlagGroup struct {
SkipPolicyFlag *Flag
OnlyPolicyFlag *Flag
}

type PolicyOptions struct {
SkipPolicy map[string]bool `json:"skip_policy"`
OnlyPolicy map[string]bool `json:"only_policy"`
}

func NewPolicyFlagGroup() *PolicyFlagGroup {
return &PolicyFlagGroup{
SkipPolicyFlag: &SkipPolicyFlag,
OnlyPolicyFlag: &OnlyPolicyFlag,
}
}

func (f *PolicyFlagGroup) Name() string {
return "Policy"
}

func (f *PolicyFlagGroup) Flags() []*Flag {
return []*Flag{
f.SkipPolicyFlag,
f.OnlyPolicyFlag,
}
}

func (f *PolicyFlagGroup) ToOptions(args []string) PolicyOptions {
return PolicyOptions{
SkipPolicy: argsToMap(f.SkipPolicyFlag),
OnlyPolicy: argsToMap(f.OnlyPolicyFlag),
}
}

func argsToMap(flag *Flag) map[string]bool {
strSlice := getStringSlice(flag)

result := make(map[string]bool)
for _, str := range strSlice {
result[str] = true
}

return result
}