Skip to content

Commit

Permalink
logs and user messages: use "parse" and "serialize" instead of marsha…
Browse files Browse the repository at this point in the history
…l/unmarshal (#3240)
  • Loading branch information
mmetc authored Sep 17, 2024
1 parent 1591a0c commit ce085dc
Show file tree
Hide file tree
Showing 56 changed files with 87 additions and 87 deletions.
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,14 @@ func (cli *cliAlerts) inspect(details bool, alertIDs ...string) error {
case "json":
data, err := json.MarshalIndent(alert, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal alert with id %s: %w", alertID, err)
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
}

fmt.Printf("%s\n", string(data))
case "raw":
data, err := yaml.Marshal(alert)
if err != nil {
return fmt.Errorf("unable to marshal alert with id %s: %w", alertID, err)
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
}

fmt.Println(string(data))
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/clibouncer/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (cli *cliBouncers) List(out io.Writer, db *database.Client) error {
enc.SetIndent("", " ")

if err := enc.Encode(info); err != nil {
return errors.New("failed to marshal")
return errors.New("failed to serialize")
}

return nil
Expand Down Expand Up @@ -234,7 +234,7 @@ func (cli *cliBouncers) add(bouncerName string, key string) error {
case "json":
j, err := json.Marshal(key)
if err != nil {
return errors.New("unable to marshal api key")
return errors.New("unable to serialize api key")
}

fmt.Print(string(j))
Expand Down Expand Up @@ -458,7 +458,7 @@ func (cli *cliBouncers) inspect(bouncer *ent.Bouncer) error {
enc.SetIndent("", " ")

if err := enc.Encode(newBouncerInfo(bouncer)); err != nil {
return errors.New("failed to marshal")
return errors.New("failed to serialize")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF

apiConfigDump, err := yaml.Marshal(apiCfg)
if err != nil {
return fmt.Errorf("unable to marshal api credentials: %w", err)
return fmt.Errorf("unable to serialize api credentials: %w", err)
}

if dumpFile != "" {
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
}
data, err := json.MarshalIndent(out, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal configuration: %w", err)
return fmt.Errorf("failed to serialize configuration: %w", err)
}
fmt.Println(string(data))
case "raw":
Expand Down Expand Up @@ -318,7 +318,7 @@ func (cli *cliConsole) dumpConfig() error {

out, err := yaml.Marshal(serverCfg.ConsoleConfig)
if err != nil {
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", serverCfg.ConsoleConfigPath, err)
return fmt.Errorf("while serializing ConsoleConfig (for %s): %w", serverCfg.ConsoleConfigPath, err)
}

if serverCfg.ConsoleConfigPath == "" {
Expand Down Expand Up @@ -361,7 +361,7 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
if changed {
fileContent, err := yaml.Marshal(cfg.API.Server.OnlineClient.Credentials)
if err != nil {
return fmt.Errorf("cannot marshal credentials: %w", err)
return fmt.Errorf("cannot serialize credentials: %w", err)
}

log.Infof("Updating credentials file: %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clihub/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[st

x, err := json.MarshalIndent(hubStatus, "", " ")
if err != nil {
return fmt.Errorf("failed to unmarshal: %w", err)
return fmt.Errorf("failed to parse: %w", err)
}

out.Write(x)
Expand Down Expand Up @@ -158,7 +158,7 @@ func InspectItem(item *cwhub.Item, wantMetrics bool, output string, prometheusUR
case "json":
b, err := json.MarshalIndent(*item, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal item: %w", err)
return fmt.Errorf("unable to serialize item: %w", err)
}

fmt.Print(string(b))
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
}
data, err := yaml.Marshal(configFileData)
if err != nil {
return fmt.Errorf("marshal: %w", err)
return fmt.Errorf("serialize: %w", err)
}
_, err = fd.Write(data)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/cliitem/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewAppsecRule(cfg configGetter) *cliItem {
}

if err := yaml.Unmarshal(yamlContent, &appsecRule); err != nil {
return fmt.Errorf("unable to unmarshal yaml file %s: %w", item.State.LocalPath, err)
return fmt.Errorf("unable to parse yaml file %s: %w", item.State.LocalPath, err)
}

for _, ruleType := range appsec_rule.SupportedTypes() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clilapi/lapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile stri

apiConfigDump, err := yaml.Marshal(apiCfg)
if err != nil {
return fmt.Errorf("unable to marshal api credentials: %w", err)
return fmt.Errorf("unable to serialize api credentials: %w", err)
}

if dumpFile != "" {
Expand Down
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/climachine/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (cli *cliMachines) List(out io.Writer, db *database.Client) error {
enc.SetIndent("", " ")

if err := enc.Encode(info); err != nil {
return errors.New("failed to marshal")
return errors.New("failed to serialize")
}

return nil
Expand Down Expand Up @@ -378,7 +378,7 @@ func (cli *cliMachines) add(args []string, machinePassword string, dumpFile stri

apiConfigDump, err := yaml.Marshal(apiCfg)
if err != nil {
return fmt.Errorf("unable to marshal api credentials: %w", err)
return fmt.Errorf("unable to serialize api credentials: %w", err)
}

if dumpFile != "" && dumpFile != "-" {
Expand Down Expand Up @@ -626,7 +626,7 @@ func (cli *cliMachines) inspect(machine *ent.Machine) error {
enc.SetIndent("", " ")

if err := enc.Encode(newMachineInfo(machine)); err != nil {
return errors.New("failed to marshal")
return errors.New("failed to serialize")
}

return nil
Expand All @@ -648,7 +648,7 @@ func (cli *cliMachines) inspectHub(machine *ent.Machine) error {
enc.SetIndent("", " ")

if err := enc.Encode(machine.Hubstate); err != nil {
return errors.New("failed to marshal")
return errors.New("failed to serialize")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/climetrics/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (cli *cliMetrics) list() error {
case "json":
x, err := json.MarshalIndent(allMetrics, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal metric types: %w", err)
return fmt.Errorf("failed to serialize metric types: %w", err)
}

fmt.Println(string(x))
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/climetrics/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (ms metricStore) Format(out io.Writer, wantColor string, sections []string,
case "json":
x, err := json.MarshalIndent(want, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal metrics: %w", err)
return fmt.Errorf("failed to serialize metrics: %w", err)
}
out.Write(x)
default:
Expand Down
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/clinotifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (cli *cliNotifications) newListCmd() *cobra.Command {
} else if cfg.Cscli.Output == "json" {
x, err := json.MarshalIndent(ncfgs, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal notification configuration: %w", err)
return fmt.Errorf("failed to serialize notification configuration: %w", err)
}
fmt.Printf("%s", string(x))
} else if cfg.Cscli.Output == "raw" {
Expand Down Expand Up @@ -231,7 +231,7 @@ func (cli *cliNotifications) newInspectCmd() *cobra.Command {
} else if cfg.Cscli.Output == "json" {
x, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal notification configuration: %w", err)
return fmt.Errorf("failed to serialize notification configuration: %w", err)
}
fmt.Printf("%s", string(x))
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func (cli cliNotifications) newTestCmd() *cobra.Command {
CreatedAt: time.Now().UTC().Format(time.RFC3339),
}
if err := yaml.Unmarshal([]byte(alertOverride), alert); err != nil {
return fmt.Errorf("failed to unmarshal alert override: %w", err)
return fmt.Errorf("failed to parse alert override: %w", err)
}

pluginBroker.PluginChannel <- csplugin.ProfileAlert{
Expand Down Expand Up @@ -387,7 +387,7 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not

if alertOverride != "" {
if err := json.Unmarshal([]byte(alertOverride), alert); err != nil {
return fmt.Errorf("can't unmarshal data in the alert flag: %w", err)
return fmt.Errorf("can't parse data in the alert flag: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clisetup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func setupAsString(cs setup.Setup, outYaml bool) (string, error) {
)

wrap := func(err error) error {
return fmt.Errorf("while marshaling setup: %w", err)
return fmt.Errorf("while serializing setup: %w", err)
}

indentLevel := 2
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clisimulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (cli *cliSimulation) dumpSimulationFile() error {

newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
if err != nil {
return fmt.Errorf("unable to marshal simulation configuration: %w", err)
return fmt.Errorf("unable to serialize simulation configuration: %w", err)
}

err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
Expand All @@ -242,7 +242,7 @@ func (cli *cliSimulation) disableGlobalSimulation() error {

newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
if err != nil {
return fmt.Errorf("unable to marshal new simulation configuration: %w", err)
return fmt.Errorf("unable to serialize new simulation configuration: %w", err)
}

err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/config_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (cli *cliConfig) backupHub(dirPath string) error {

upstreamParsersContent, err := json.MarshalIndent(upstreamParsers, "", " ")
if err != nil {
return fmt.Errorf("failed marshaling upstream parsers: %w", err)
return fmt.Errorf("failed to serialize upstream parsers: %w", err)
}

err = os.WriteFile(upstreamParsersFname, upstreamParsersContent, 0o644)
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/config_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (cli *cliConfig) restoreHub(ctx context.Context, dirPath string) error {

err = json.Unmarshal(file, &upstreamList)
if err != nil {
return fmt.Errorf("error unmarshaling %s: %w", upstreamListFN, err)
return fmt.Errorf("error parsing %s: %w", upstreamListFN, err)
}

for _, toinstall := range upstreamList {
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/config_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (cli *cliConfig) showKey(key string) error {
case "json":
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal configuration: %w", err)
return fmt.Errorf("failed to serialize configuration: %w", err)
}

fmt.Println(string(data))
Expand Down Expand Up @@ -212,14 +212,14 @@ func (cli *cliConfig) show() error {
case "json":
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal configuration: %w", err)
return fmt.Errorf("failed to serialize configuration: %w", err)
}

fmt.Println(string(data))
case "raw":
data, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to marshal configuration: %w", err)
return fmt.Errorf("failed to serialize configuration: %w", err)
}

fmt.Println(string(data))
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec/pour.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea
if parsed.MarshaledTime != "" {
z := &time.Time{}
if err := z.UnmarshalText([]byte(parsed.MarshaledTime)); err != nil {
log.Warningf("Failed to unmarshal time from event '%s' : %s", parsed.MarshaledTime, err)
log.Warningf("Failed to parse time from event '%s' : %s", parsed.MarshaledTime, err)
} else {
log.Warning("Starting buckets garbage collection ...")

Expand Down Expand Up @@ -61,7 +61,7 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea

if len(parsed.MarshaledTime) != 0 {
if err := lastProcessedItem.UnmarshalText([]byte(parsed.MarshaledTime)); err != nil {
log.Warningf("failed to unmarshal time from event : %s", err)
log.Warningf("failed to parse time from event : %s", err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/notification-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *FilePlugin) Configure(ctx context.Context, config *protobufs.Config) (*
d := PluginConfig{}
err := yaml.Unmarshal(config.Config, &d)
if err != nil {
logger.Error("Failed to unmarshal config", "error", err)
logger.Error("Failed to parse config", "error", err)
return &protobufs.Empty{}, err
}
FileWriteMutex = &sync.Mutex{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func DataSourceConfigure(commonConfig configuration.DataSourceCommonCfg, metrics
// once to DataSourceCommonCfg, and then later to the dedicated type of the datasource
yamlConfig, err := yaml.Marshal(commonConfig)
if err != nil {
return nil, fmt.Errorf("unable to marshal back interface: %w", err)
return nil, fmt.Errorf("unable to serialize back interface: %w", err)
}

dataSrc, err := GetDataSourceIface(commonConfig.Source)
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (w *AppsecSource) appsecHandler(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(statusCode)
body, err := json.Marshal(appsecResponse)
if err != nil {
logger.Errorf("unable to marshal response: %s", err)
logger.Errorf("unable to serialize response: %s", err)
rw.WriteHeader(http.StatusInternalServerError)
} else {
rw.Write(body)
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (k *KafkaSource) UnmarshalConfig(yamlConfig []byte) error {
k.Config.Mode = configuration.TAIL_MODE
}

k.logger.Debugf("successfully unmarshaled kafka configuration : %+v", k.Config)
k.logger.Debugf("successfully parsed kafka configuration : %+v", k.Config)

return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/kubernetesaudit/k8s_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (ka *KubernetesAuditSource) webhookHandler(w http.ResponseWriter, r *http.R
}
bytesEvent, err := json.Marshal(auditEvent)
if err != nil {
ka.logger.Errorf("Error marshaling audit event: %s", err)
ka.logger.Errorf("Error serializing audit event: %s", err)
continue
}
ka.logger.Tracef("Got audit event: %s", string(bytesEvent))
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/wineventlog/wineventlog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (w *WinEventLogSource) buildXpathQuery() (string, error) {
queryList := QueryList{Select: Select{Path: w.config.EventChannel, Query: query}}
xpathQuery, err := xml.Marshal(queryList)
if err != nil {
w.logger.Errorf("Marshal failed: %v", err)
w.logger.Errorf("Serialize failed: %v", err)
return "", err
}
w.logger.Debugf("xpathQuery: %s", xpathQuery)
Expand Down
2 changes: 1 addition & 1 deletion pkg/alertcontext/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func LoadConsoleContext(c *csconfig.Config, hub *cwhub.Hub) error {

feedback, err := json.Marshal(c.Crowdsec.ContextToSend)
if err != nil {
return fmt.Errorf("marshaling console context: %s", err)
return fmt.Errorf("serializing console context: %s", err)
}

log.Debugf("console context to send: %s", feedback)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/apic_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) {

err := json.Unmarshal([]byte(dbMetric.Payload), dbPayload)
if err != nil {
log.Errorf("unable to unmarshal bouncer metric (%s)", err)
log.Errorf("unable to parse bouncer metric (%s)", err)
continue
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) {

err := json.Unmarshal([]byte(dbMetric.Payload), dbPayload)
if err != nil {
log.Errorf("unable to unmarshal log processor metric (%s)", err)
log.Errorf("unable to parse log processor metric (%s)", err)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/controllers/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func FormatOneAlert(alert *ent.Alert) *models.Alert {
var Metas models.Meta

if err := json.Unmarshal([]byte(eventItem.Serialized), &Metas); err != nil {
log.Errorf("unable to unmarshall events meta '%s' : %s", eventItem.Serialized, err)
log.Errorf("unable to parse events meta '%s' : %s", eventItem.Serialized, err)
}

outputAlert.Events = append(outputAlert.Events, &models.Event{
Expand Down
Loading

0 comments on commit ce085dc

Please sign in to comment.