Skip to content

Commit ecafe1d

Browse files
authored
fix 2808: show certificate path in "lapi status" (#4053)
* fix 2808: show certificate path in "lapi status" * lint (print, space)
1 parent 38c147c commit ecafe1d

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

cmd/crowdsec-cli/clilapi/context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
7979
keySlice := strings.Split(v, ".")
8080
key := keySlice[len(keySlice)-1]
8181
value := []string{v}
82+
8283
if err := cli.addContext(key, value); err != nil {
8384
return err
8485
}
@@ -105,6 +106,7 @@ func (cli *cliLapi) newContextStatusCmd() *cobra.Command {
105106
DisableAutoGenTag: true,
106107
RunE: func(_ *cobra.Command, _ []string) error {
107108
cfg := cli.cfg()
109+
108110
hub, err := require.Hub(cfg, nil)
109111
if err != nil {
110112
return err
@@ -145,6 +147,7 @@ cscli lapi context detect crowdsecurity/sshd-logs
145147
DisableAutoGenTag: true,
146148
RunE: func(cmd *cobra.Command, args []string) error {
147149
cfg := cli.cfg()
150+
148151
if !detectAll && len(args) == 0 {
149152
_ = cmd.Help()
150153
return errors.New("please provide parsers to detect or --all flag")
@@ -168,13 +171,16 @@ cscli lapi context detect crowdsecurity/sshd-logs
168171
}
169172

170173
fieldByParsers := make(map[string][]string)
174+
171175
for _, node := range csParsers.Nodes {
172176
if !detectAll && !slices.Contains(args, node.Name) {
173177
continue
174178
}
179+
175180
if !detectAll {
176181
args = removeFromSlice(node.Name, args)
177182
}
183+
178184
fieldByParsers[node.Name] = make([]string, 0)
179185
fieldByParsers[node.Name] = detectNode(node, *csParsers.Ctx)
180186

@@ -195,18 +201,22 @@ cscli lapi context detect crowdsecurity/sshd-logs
195201
for k := range fieldByParsers {
196202
parsersKey = append(parsersKey, k)
197203
}
204+
198205
sort.Strings(parsersKey)
199206

200207
for _, k := range parsersKey {
201208
if len(fieldByParsers[k]) == 0 {
202209
continue
203210
}
211+
204212
fmt.Fprintf(os.Stdout, "%s :\n\n", k)
205213
values := fieldByParsers[k]
206214
sort.Strings(values)
215+
207216
for _, value := range values {
208217
fmt.Fprintf(os.Stdout, " - %s\n", value)
209218
}
219+
210220
fmt.Fprintln(os.Stdout)
211221
}
212222

@@ -254,6 +264,7 @@ func (cli *cliLapi) newContextCmd() *cobra.Command {
254264
return fmt.Errorf("unable to load CrowdSec agent configuration: %w", err)
255265
}
256266
}
267+
257268
if cfg.DisableAgent {
258269
return errors.New("agent is disabled and lapi context can only be used on the agent")
259270
}

cmd/crowdsec-cli/clilapi/lapi.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (cli *cliLapi) NewCommand() *cobra.Command {
2929
if err := cli.cfg().LoadAPIClient(); err != nil {
3030
return fmt.Errorf("loading api client: %w", err)
3131
}
32+
3233
return nil
3334
},
3435
}

cmd/crowdsec-cli/clilapi/register.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile stri
8585

8686
log.Printf("Local API credentials written to '%s'", dumpFile)
8787
} else {
88-
fmt.Printf("%s\n", string(apiConfigDump))
88+
fmt.Fprintln(os.Stdout, string(apiConfigDump))
8989
}
9090

9191
if msg := reload.UserMessage(); msg != "" {

cmd/crowdsec-cli/clilapi/status.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ func (cli *cliLapi) Status(ctx context.Context, out io.Writer, hub *cwhub.Hub) e
6161
cred := cfg.API.Client.Credentials
6262

6363
fmt.Fprintf(out, "Loaded credentials from %s\n", cfg.API.Client.CredentialsFilePath)
64-
fmt.Fprintf(out, "Trying to authenticate with username %s on %s\n", cred.Login, cred.URL)
64+
65+
if cred.Login != "" {
66+
fmt.Fprintf(out, "Trying to authenticate with username %q on %s\n", cred.Login, cred.URL)
67+
}
68+
69+
if cred.CertPath != "" {
70+
fmt.Fprintf(out, "Trying to authenticate with certificate %q on %s\n", cred.CertPath, cred.URL)
71+
}
6572

6673
_, err := queryLAPIStatus(ctx, hub, cred.URL, cred.Login, cred.Password)
6774
if err != nil {

cmd/crowdsec-cli/clilapi/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package clilapi
22

33
func removeFromSlice(val string, slice []string) []string {
4-
var i int
5-
var value string
4+
var (
5+
i int
6+
value string
7+
)
68

79
valueFound := false
810

0 commit comments

Comments
 (0)