Skip to content

Commit 39f41f9

Browse files
authored
support for waf- alias in cscli (#4347)
1 parent d8eb85d commit 39f41f9

File tree

4 files changed

+99
-80
lines changed

4 files changed

+99
-80
lines changed

cmd/crowdsec-cli/cliitem/hubappsec.go

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,94 +20,95 @@ func NewAppsecConfig(cfg csconfig.Getter) *cliItem {
2020
name: cwhub.APPSEC_CONFIGS,
2121
singular: "appsec-config",
2222
oneOrMore: "appsec-config(s)",
23+
aliases: []string{"waf-configs"},
2324
help: cliHelp{
24-
example: `cscli appsec-configs list -a
25-
cscli appsec-configs install crowdsecurity/virtual-patching
26-
cscli appsec-configs inspect crowdsecurity/virtual-patching
27-
cscli appsec-configs upgrade crowdsecurity/virtual-patching
28-
cscli appsec-configs remove crowdsecurity/virtual-patching
25+
example: `cscli waf-configs list -a
26+
cscli waf-configs install crowdsecurity/virtual-patching
27+
cscli waf-configs inspect crowdsecurity/virtual-patching
28+
cscli waf-configs upgrade crowdsecurity/virtual-patching
29+
cscli waf-configs remove crowdsecurity/virtual-patching
2930
`,
3031
},
3132
installHelp: cliHelp{
32-
example: `# Install some appsec-configs.
33-
cscli appsec-configs install crowdsecurity/virtual-patching
33+
example: `# Install some waf-configs.
34+
cscli waf-configs install crowdsecurity/virtual-patching
3435
3536
# Show the execution plan without changing anything - compact output sorted by type and name.
36-
cscli appsec-configs install crowdsecurity/virtual-patching --dry-run
37+
cscli waf-configs install crowdsecurity/virtual-patching --dry-run
3738
3839
# Show the execution plan without changing anything - verbose output sorted by execution order.
39-
cscli appsec-configs install crowdsecurity/virtual-patching --dry-run -o raw
40+
cscli waf-configs install crowdsecurity/virtual-patching --dry-run -o raw
4041
4142
# Download only, to be installed later.
42-
cscli appsec-configs install crowdsecurity/virtual-patching --download-only
43+
cscli waf-configs install crowdsecurity/virtual-patching --download-only
4344
4445
# Install over tainted items. Can be used to restore or repair after local modifications or missing dependencies.
45-
cscli appsec-configs install crowdsecurity/virtual-patching --force
46+
cscli waf-configs install crowdsecurity/virtual-patching --force
4647
4748
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
48-
cscli appsec-configs install crowdsecurity/virtual-patching -i
49-
cscli appsec-configs install crowdsecurity/virtual-patching --interactive`,
49+
cscli waf-configs install crowdsecurity/virtual-patching -i
50+
cscli waf-configs install crowdsecurity/virtual-patching --interactive`,
5051
},
5152
removeHelp: cliHelp{
52-
example: `# Uninstall some appsec-configs.
53-
cscli appsec-configs remove crowdsecurity/virtual-patching
53+
example: `# Uninstall some waf-configs.
54+
cscli waf-configs remove crowdsecurity/virtual-patching
5455
5556
# Show the execution plan without changing anything - compact output sorted by type and name.
56-
cscli appsec-configs remove crowdsecurity/virtual-patching --dry-run
57+
cscli waf-configs remove crowdsecurity/virtual-patching --dry-run
5758
5859
# Show the execution plan without changing anything - verbose output sorted by execution order.
59-
cscli appsec-configs remove crowdsecurity/virtual-patching --dry-run -o raw
60+
cscli waf-configs remove crowdsecurity/virtual-patching --dry-run -o raw
6061
6162
# Uninstall and also remove the downloaded files.
62-
cscli appsec-configs remove crowdsecurity/virtual-patching --purge
63+
cscli waf-configs remove crowdsecurity/virtual-patching --purge
6364
6465
# Remove tainted items.
65-
cscli appsec-configs remove crowdsecurity/virtual-patching --force
66+
cscli waf-configs remove crowdsecurity/virtual-patching --force
6667
6768
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
68-
cscli appsec-configs remove crowdsecurity/virtual-patching -i
69-
cscli appsec-configs remove crowdsecurity/virtual-patching --interactive`,
69+
cscli waf-configs remove crowdsecurity/virtual-patching -i
70+
cscli waf-configs remove crowdsecurity/virtual-patching --interactive`,
7071
},
7172
upgradeHelp: cliHelp{
72-
example: `# Upgrade some appsec-configs. If they are not currently installed, they are downloaded but not installed.
73-
cscli appsec-configs upgrade crowdsecurity/virtual-patching
73+
example: `# Upgrade some waf-configs. If they are not currently installed, they are downloaded but not installed.
74+
cscli waf-configs upgrade crowdsecurity/virtual-patching
7475
7576
# Show the execution plan without changing anything - compact output sorted by type and name.
76-
cscli appsec-configs upgrade crowdsecurity/virtual-patching --dry-run
77+
cscli waf-configs upgrade crowdsecurity/virtual-patching --dry-run
7778
7879
# Show the execution plan without changing anything - verbose output sorted by execution order.
79-
cscli appsec-configs upgrade crowdsecurity/virtual-patching --dry-run -o raw
80+
cscli waf-configs upgrade crowdsecurity/virtual-patching --dry-run -o raw
8081
8182
# Upgrade over tainted items. Can be used to restore or repair after local modifications or missing dependencies.
82-
cscli appsec-configs upgrade crowdsecurity/virtual-patching --force
83+
cscli waf-configs upgrade crowdsecurity/virtual-patching --force
8384
8485
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
85-
cscli appsec-configs upgrade crowdsecurity/virtual-patching -i
86-
cscli appsec-configs upgrade crowdsecurity/virtual-patching --interactive`,
86+
cscli waf-configs upgrade crowdsecurity/virtual-patching -i
87+
cscli waf-configs upgrade crowdsecurity/virtual-patching --interactive`,
8788
},
8889
inspectHelp: cliHelp{
89-
example: `# Display metadata, state, ancestor collections of appsec-configs (installed or not).
90-
cscli appsec-configs inspect crowdsecurity/virtual-patching
90+
example: `# Display metadata, state, ancestor collections of waf-configs (installed or not).
91+
cscli waf-configs inspect crowdsecurity/virtual-patching
9192
9293
# If the config is installed, its metrics are collected and shown as well (with an error if crowdsec is not running).
9394
# To avoid this, use --no-metrics.
94-
cscli appsec-configs inspect crowdsecurity/virtual-patching --no-metrics
95+
cscli waf-configs inspect crowdsecurity/virtual-patching --no-metrics
9596
9697
# Display difference between a tainted item and the latest one.
97-
cscli appsec-configs inspect crowdsecurity/virtual-patching --diff
98+
cscli waf-configs inspect crowdsecurity/virtual-patching --diff
9899
99100
# Reverse the above diff
100-
cscli appsec-configs inspect crowdsecurity/virtual-patching --diff --rev`,
101+
cscli waf-configs inspect crowdsecurity/virtual-patching --diff --rev`,
101102
},
102103
listHelp: cliHelp{
103-
example: `# List enabled (installed) appsec-configs.
104-
cscli appsec-configs list
104+
example: `# List enabled (installed) waf-configs.
105+
cscli waf-configs list
105106
106-
# List all available appsec-configs (installed or not).
107-
cscli appsec-configs list -a
107+
# List all available waf-configs (installed or not).
108+
cscli waf-configs list -a
108109
109-
# List specific appsec-configs (installed or not).
110-
cscli appsec-configs list crowdsecurity/virtual-patching crowdsecurity/generic-rules`,
110+
# List specific waf-configs (installed or not).
111+
cscli waf-configs list crowdsecurity/virtual-patching crowdsecurity/generic-rules`,
111112
},
112113
}
113114
}
@@ -162,95 +163,96 @@ func NewAppsecRule(cfg csconfig.Getter) *cliItem {
162163
name: "appsec-rules",
163164
singular: "appsec-rule",
164165
oneOrMore: "appsec-rule(s)",
166+
aliases: []string{"waf-rules"},
165167
help: cliHelp{
166-
example: `cscli appsec-rules list -a
167-
cscli appsec-rules install crowdsecurity/crs
168-
cscli appsec-rules inspect crowdsecurity/crs
169-
cscli appsec-rules upgrade crowdsecurity/crs
170-
cscli appsec-rules remove crowdsecurity/crs
168+
example: `cscli waf-rules list -a
169+
cscli waf-rules install crowdsecurity/crs
170+
cscli waf-rules inspect crowdsecurity/crs
171+
cscli waf-rules upgrade crowdsecurity/crs
172+
cscli waf-rules remove crowdsecurity/crs
171173
`,
172174
},
173175
installHelp: cliHelp{
174-
example: `# Install some appsec-rules.
175-
cscli appsec-rules install crowdsecurity/crs
176+
example: `# Install some waf-rules.
177+
cscli waf-rules install crowdsecurity/crs
176178
177179
# Show the execution plan without changing anything - compact output sorted by type and name.
178-
cscli appsec-rules install crowdsecurity/crs --dry-run
180+
cscli waf-rules install crowdsecurity/crs --dry-run
179181
180182
# Show the execution plan without changing anything - verbose output sorted by execution order.
181-
cscli appsec-rules install crowdsecurity/crs --dry-run -o raw
183+
cscli waf-rules install crowdsecurity/crs --dry-run -o raw
182184
183185
# Download only, to be installed later.
184-
cscli appsec-rules install crowdsecurity/crs --download-only
186+
cscli waf-rules install crowdsecurity/crs --download-only
185187
186188
# Install over tainted items. Can be used to restore or repair after local modifications or missing dependencies.
187-
cscli appsec-rules install crowdsecurity/crs --force
189+
cscli waf-rules install crowdsecurity/crs --force
188190
189191
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
190-
cscli appsec-rules install crowdsecurity/crs -i
191-
cscli appsec-rules install crowdsecurity/crs --interactive`,
192+
cscli waf-rules install crowdsecurity/crs -i
193+
cscli waf-rules install crowdsecurity/crs --interactive`,
192194
},
193195
removeHelp: cliHelp{
194-
example: `# Uninstall some appsec-rules.
195-
cscli appsec-rules remove crowdsecurity/crs
196+
example: `# Uninstall some waf-rules.
197+
cscli waf-rules remove crowdsecurity/crs
196198
197199
# Show the execution plan without changing anything - compact output sorted by type and name.
198-
cscli appsec-rules remove crowdsecurity/crs --dry-run
200+
cscli waf-rules remove crowdsecurity/crs --dry-run
199201
200202
# Show the execution plan without changing anything - verbose output sorted by execution order.
201-
cscli appsec-rules remove crowdsecurity/crs --dry-run -o raw
203+
cscli waf-rules remove crowdsecurity/crs --dry-run -o raw
202204
203205
# Uninstall and also remove the downloaded files.
204-
cscli appsec-rules remove crowdsecurity/crs --purge
206+
cscli waf-rules remove crowdsecurity/crs --purge
205207
206208
# Remove tainted items.
207-
cscli appsec-rules remove crowdsecurity/crs --force
209+
cscli waf-rules remove crowdsecurity/crs --force
208210
209211
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
210-
cscli appsec-rules remove crowdsecurity/crs -i
211-
cscli appsec-rules remove crowdsecurity/crs --interactive`,
212+
cscli waf-rules remove crowdsecurity/crs -i
213+
cscli waf-rules remove crowdsecurity/crs --interactive`,
212214
},
213215
upgradeHelp: cliHelp{
214-
example: `# Upgrade some appsec-rules. If they are not currently installed, they are downloaded but not installed.
215-
cscli appsec-rules upgrade crowdsecurity/crs
216+
example: `# Upgrade some waf-rules. If they are not currently installed, they are downloaded but not installed.
217+
cscli waf-rules upgrade crowdsecurity/crs
216218
217219
# Show the execution plan without changing anything - compact output sorted by type and name.
218-
cscli appsec-rules upgrade crowdsecurity/crs --dry-run
220+
cscli waf-rules upgrade crowdsecurity/crs --dry-run
219221
220222
# Show the execution plan without changing anything - verbose output sorted by execution order.
221-
cscli appsec-rules upgrade crowdsecurity/crs --dry-run -o raw
223+
cscli waf-rules upgrade crowdsecurity/crs --dry-run -o raw
222224
223225
# Upgrade over tainted items. Can be used to restore or repair after local modifications or missing dependencies.
224-
cscli appsec-rules upgrade crowdsecurity/crs --force
226+
cscli waf-rules upgrade crowdsecurity/crs --force
225227
226228
# Prompt for confirmation if running in an interactive terminal; otherwise, the option is ignored.
227-
cscli appsec-rules upgrade crowdsecurity/crs -i
228-
cscli appsec-rules upgrade crowdsecurity/crs --interactive`,
229+
cscli waf-rules upgrade crowdsecurity/crs -i
230+
cscli waf-rules upgrade crowdsecurity/crs --interactive`,
229231
},
230232
inspectHelp: cliHelp{
231-
example: `# Display metadata, state, ancestor collections of appsec-rules (installed or not).
232-
cscli appsec-rules inspect crowdsecurity/crs
233+
example: `# Display metadata, state, ancestor collections of waf-rules (installed or not).
234+
cscli waf-rules inspect crowdsecurity/crs
233235
234236
# If the rule is installed, its metrics are collected and shown as well (with an error if crowdsec is not running).
235237
# To avoid this, use --no-metrics.
236-
cscli appsec-configs inspect crowdsecurity/crs --no-metrics
238+
cscli waf-rules inspect crowdsecurity/crs --no-metrics
237239
238240
# Display difference between a tainted item and the latest one.
239-
cscli appsec-rules inspect crowdsecurity/crs --diff
241+
cscli waf-rules inspect crowdsecurity/crs --diff
240242
241243
# Reverse the above diff
242-
cscli appsec-rules inspect crowdsecurity/crs --diff --rev`,
244+
cscli waf-rules inspect crowdsecurity/crs --diff --rev`,
243245
},
244246
inspectDetail: inspectDetail,
245247
listHelp: cliHelp{
246-
example: `# List enabled (installed) appsec-rules.
247-
cscli appsec-rules list
248+
example: `# List enabled (installed) waf-rules.
249+
cscli waf-rules list
248250
249-
# List all available appsec-rules (installed or not).
250-
cscli appsec-rules list -a
251+
# List all available waf-rules (installed or not).
252+
cscli waf-rules list -a
251253
252-
# List specific appsec-rules (installed or not).
253-
cscli appsec-rules list crowdsecurity/crs crowdsecurity/vpatch-git-config`,
254+
# List specific waf-rules (installed or not).
255+
cscli waf-rules list crowdsecurity/crs crowdsecurity/vpatch-git-config`,
254256
},
255257
}
256258
}

cmd/crowdsec-cli/cliitem/item.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type cliItem struct {
3030
name string // plural, as used in the hub index
3131
singular string
3232
oneOrMore string // parenthetical pluralizaion: "parser(s)"
33+
aliases []string
3334
help cliHelp
3435
installHelp cliHelp
3536
removeHelp cliHelp
@@ -40,12 +41,20 @@ type cliItem struct {
4041
}
4142

4243
func (cli *cliItem) NewCommand() *cobra.Command {
44+
aliases := make([]string, 0, 1+len(cli.aliases))
45+
if cli.singular != "" {
46+
aliases = append(aliases, cli.singular)
47+
}
48+
if len(cli.aliases) > 0 {
49+
aliases = append(aliases, cli.aliases...)
50+
}
51+
4352
cmd := &cobra.Command{
4453
Use: cmp.Or(cli.help.use, cli.name+" <action> [item]..."),
4554
Short: cmp.Or(cli.help.short, "Manage hub "+cli.name),
4655
Long: cli.help.long,
4756
Example: cli.help.example,
48-
Aliases: []string{cli.singular},
57+
Aliases: aliases,
4958
DisableAutoGenTag: true,
5059
Args: args.NoArgs,
5160
RunE: func(cmd *cobra.Command, _ []string) error {

cmd/crowdsec-cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (cli *cliRoot) NewCommand() (*cobra.Command, error) {
218218

219219
// list of valid subcommands for the shell completion
220220
validArgs := []string{
221-
"alerts", "appsec-configs", "appsec-rules", "bouncers", "capi", "collections",
221+
"alerts", "appsec-configs", "waf-configs", "appsec-rules", "waf-rules", "bouncers", "capi", "collections",
222222
"completion", "config", "console", "contexts", "dashboard", "decisions", "explain",
223223
"hub", "hubtest", "lapi", "machines", "metrics", "notifications", "parsers",
224224
"postoverflows", "scenarios", "simulation", "support", "version",

test/bats/20_hub.bats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,11 @@ teardown() {
199199
rune -0 cscli hub types -o json
200200
assert_json '["parsers","postoverflows","scenarios","contexts","appsec-configs","appsec-rules","collections"]'
201201
}
202+
203+
@test "cscli waf aliases for hub items" {
204+
rune -0 cscli waf-configs list -o json
205+
rune -0 jq -e '."appsec-configs"' <(output)
206+
207+
rune -0 cscli waf-rules list -o json
208+
rune -0 jq -e '."appsec-rules"' <(output)
209+
}

0 commit comments

Comments
 (0)