Skip to content

Commit e40f284

Browse files
authored
CI: enable linter "protogetter" (#3995)
1 parent d97ac90 commit e40f284

File tree

9 files changed

+77
-58
lines changed

9 files changed

+77
-58
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ linters:
3939
- gosec # (gas): Inspects source code for security problems
4040
- musttag # enforce field tags in (un)marshaled structs
4141
- promlinter # Check Prometheus metrics naming via promlint
42-
- protogetter # Reports direct reads from proto message fields when getters should be used
4342
- tagalign # check that struct tags are well aligned
4443
- thelper # thelper detects tests helpers which is not start with t.Helper() method.
4544
- wrapcheck # Checks that errors returned from external packages are wrapped

cmd/notification-dummy/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
3131
JSONFormat: true,
3232
})
3333

34-
func (s *DummyPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
35-
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
36-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
34+
func (s *DummyPlugin) Notify(_ context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
35+
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
36+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
3737
}
3838

39-
cfg := s.PluginConfigByName[notification.Name]
39+
cfg := s.PluginConfigByName[notification.GetName()]
4040

4141
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
4242
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
4343
}
4444

45-
logger.Info(fmt.Sprintf("received signal for %s config", notification.Name))
46-
logger.Debug(notification.Text)
45+
logger.Info(fmt.Sprintf("received signal for %s config", notification.GetName()))
46+
logger.Debug(notification.GetText())
4747

4848
if cfg.OutputFile != nil && *cfg.OutputFile != "" {
4949
f, err := os.OpenFile(*cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
5050
if err != nil {
5151
logger.Error(fmt.Sprintf("Cannot open notification file: %s", err))
5252
}
5353

54-
if _, err := f.WriteString(notification.Text + "\n"); err != nil {
54+
if _, err := f.WriteString(notification.GetText() + "\n"); err != nil {
5555
f.Close()
5656
logger.Error(fmt.Sprintf("Cannot write notification to file: %s", err))
5757
}
@@ -62,14 +62,14 @@ func (s *DummyPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
6262
}
6363
}
6464

65-
fmt.Println(notification.Text)
65+
fmt.Fprintln(os.Stdout, notification.GetText())
6666

6767
return &protobufs.Empty{}, nil
6868
}
6969

70-
func (s *DummyPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
70+
func (s *DummyPlugin) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
7171
d := PluginConfig{}
72-
err := yaml.Unmarshal(config.Config, &d)
72+
err := yaml.Unmarshal(config.GetConfig(), &d)
7373
s.PluginConfigByName[d.Name] = d
7474

7575
return &protobufs.Empty{}, err

cmd/notification-email/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type EmailPlugin struct {
6060
ConfigByName map[string]PluginConfig
6161
}
6262

63-
func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
63+
func (n *EmailPlugin) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
6464
d := PluginConfig{
6565
SMTPPort: 25,
6666
SenderName: "Crowdsec",
@@ -71,7 +71,7 @@ func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (
7171
HeloHost: "localhost",
7272
}
7373

74-
if err := yaml.Unmarshal(config.Config, &d); err != nil {
74+
if err := yaml.Unmarshal(config.GetConfig(), &d); err != nil {
7575
return nil, err
7676
}
7777

@@ -93,12 +93,12 @@ func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (
9393
return &protobufs.Empty{}, nil
9494
}
9595

96-
func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
97-
if _, ok := n.ConfigByName[notification.Name]; !ok {
98-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
96+
func (n *EmailPlugin) Notify(_ context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
97+
if _, ok := n.ConfigByName[notification.GetName()]; !ok {
98+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
9999
}
100100

101-
cfg := n.ConfigByName[notification.Name]
101+
cfg := n.ConfigByName[notification.GetName()]
102102

103103
logger := baseLogger.Named(cfg.Name)
104104

@@ -150,7 +150,7 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
150150
email.SetFrom(fmt.Sprintf("%s <%s>", cfg.SenderName, cfg.SenderEmail)).
151151
AddTo(cfg.ReceiverEmails...).
152152
SetSubject(cfg.EmailSubject)
153-
email.SetBody(mail.TextHTML, notification.Text)
153+
email.SetBody(mail.TextHTML, notification.GetText())
154154

155155
err = email.Send(smtpClient)
156156
if err != nil {

cmd/notification-file/main.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (w *FileWriteCtx) Write(p []byte) (n int, err error) {
3434
if err := w.Ctx.Err(); err != nil {
3535
return 0, err
3636
}
37+
3738
return w.Writer.Write(p)
3839
}
3940

@@ -89,10 +90,12 @@ func (r *LogRotate) rotateLogs(cfg PluginConfig) error {
8990
func (r *LogRotate) rotateLogFile(logPath string, maxBackups int) error {
9091
// Rename the current log file
9192
backupPath := logPath + "." + time.Now().Format("20060102-150405")
93+
9294
err := os.Rename(logPath, backupPath)
9395
if err != nil {
9496
return err
9597
}
98+
9699
glob := logPath + ".*"
97100
if r.Compress {
98101
glob = logPath + ".*.gz"
@@ -169,24 +172,30 @@ func compressFile(src string) error {
169172
func WriteToFileWithCtx(ctx context.Context, cfg PluginConfig, log string) error {
170173
FileWriteMutex.Lock()
171174
defer FileWriteMutex.Unlock()
175+
172176
originalFileInfo, err := FileWriter.Stat()
173177
if err != nil {
174178
logger.Error("Failed to get file info", "error", err)
175179
}
180+
176181
currentFileInfo, _ := os.Stat(cfg.LogPath)
177182
if !os.SameFile(originalFileInfo, currentFileInfo) {
178183
// The file has been rotated outside our control
179184
logger.Info("Log file has been rotated or missing attempting to reopen it")
180185
FileWriter.Close()
186+
181187
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
182188
if err != nil {
183189
return err
184190
}
191+
185192
FileInfo, err := FileWriter.Stat()
186193
if err != nil {
187194
return err
188195
}
196+
189197
FileSize = FileInfo.Size()
198+
190199
logger.Info("Log file has been reopened successfully")
191200
}
192201
n, err := io.WriteString(&FileWriteCtx{Ctx: ctx, Writer: FileWriter}, log)
@@ -204,35 +213,42 @@ func WriteToFileWithCtx(ctx context.Context, cfg PluginConfig, log string) error
204213
}
205214

206215
func (s *FilePlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
207-
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
208-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
216+
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
217+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
209218
}
210-
cfg := s.PluginConfigByName[notification.Name]
211219

212-
return &protobufs.Empty{}, WriteToFileWithCtx(ctx, cfg, notification.Text)
220+
cfg := s.PluginConfigByName[notification.GetName()]
221+
222+
return &protobufs.Empty{}, WriteToFileWithCtx(ctx, cfg, notification.GetText())
213223
}
214224

215-
func (s *FilePlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
225+
func (s *FilePlugin) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
216226
d := PluginConfig{}
217-
err := yaml.Unmarshal(config.Config, &d)
227+
228+
err := yaml.Unmarshal(config.GetConfig(), &d)
218229
if err != nil {
219230
logger.Error("Failed to parse config", "error", err)
220231
return &protobufs.Empty{}, err
221232
}
233+
222234
FileWriteMutex = &sync.Mutex{}
235+
223236
FileWriter, err = os.OpenFile(d.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
224237
if err != nil {
225238
logger.Error("Failed to open log file", "error", err)
226239
return &protobufs.Empty{}, err
227240
}
241+
228242
FileInfo, err := FileWriter.Stat()
229243
if err != nil {
230244
logger.Error("Failed to get file info", "error", err)
231245
return &protobufs.Empty{}, err
232246
}
247+
233248
FileSize = FileInfo.Size()
234249
s.PluginConfigByName[d.Name] = d
235250
logger.SetLevel(hclog.LevelFromString(d.LogLevel))
251+
236252
return &protobufs.Empty{}, err
237253
}
238254

cmd/notification-http/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ func getTLSClient(c *PluginConfig) error {
117117
}
118118

119119
func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
120-
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
121-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
120+
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
121+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
122122
}
123123

124-
cfg := s.PluginConfigByName[notification.Name]
124+
cfg := s.PluginConfigByName[notification.GetName()]
125125

126126
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
127127
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
128128
}
129129

130-
logger.Info(fmt.Sprintf("received signal for %s config", notification.Name))
130+
logger.Info(fmt.Sprintf("received signal for %s config", notification.GetName()))
131131

132-
request, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL, bytes.NewReader([]byte(notification.Text)))
132+
request, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL, bytes.NewReader([]byte(notification.GetText())))
133133
if err != nil {
134134
return nil, err
135135
}
@@ -139,7 +139,7 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
139139
request.Header.Add(headerName, headerValue)
140140
}
141141

142-
logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.Text))
142+
logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.GetText()))
143143

144144
resp, err := cfg.Client.Do(request.WithContext(ctx))
145145
if err != nil {
@@ -168,7 +168,7 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
168168
func (s *HTTPPlugin) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
169169
d := PluginConfig{}
170170

171-
err := yaml.Unmarshal(config.Config, &d)
171+
err := yaml.Unmarshal(config.GetConfig(), &d)
172172
if err != nil {
173173
return nil, err
174174
}

cmd/notification-sentinel/main.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,25 @@ func (s *SentinelPlugin) getAuthorizationHeader(now string, length int, pluginNa
5757
}
5858

5959
func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
60-
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
61-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
60+
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
61+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
6262
}
63-
cfg := s.PluginConfigByName[notification.Name]
63+
64+
cfg := s.PluginConfigByName[notification.GetName()]
6465

6566
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
6667
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
6768
}
6869

69-
logger.Info("received notification for sentinel config", "name", notification.Name)
70+
logger.Info("received notification for sentinel config", "name", notification.GetName())
7071

71-
url := fmt.Sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", s.PluginConfigByName[notification.Name].CustomerID)
72-
body := strings.NewReader(notification.Text)
72+
url := fmt.Sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", s.PluginConfigByName[notification.GetName()].CustomerID)
73+
body := strings.NewReader(notification.GetText())
7374

74-
//Cannot use time.RFC1123 as azure wants GMT, not UTC
75+
// Cannot use time.RFC1123 as azure wants GMT, not UTC
7576
now := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
7677

77-
authorization, err := s.getAuthorizationHeader(now, len(notification.Text), notification.Name)
78+
authorization, err := s.getAuthorizationHeader(now, len(notification.GetText()), notification.GetName())
7879
if err != nil {
7980
return &protobufs.Empty{}, err
8081
}
@@ -86,17 +87,19 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not
8687
}
8788

8889
req.Header.Set("Content-Type", "application/json")
89-
req.Header.Set("Log-Type", s.PluginConfigByName[notification.Name].LogType)
90+
req.Header.Set("Log-Type", s.PluginConfigByName[notification.GetName()].LogType)
9091
req.Header.Set("Authorization", authorization)
9192
req.Header.Set("X-Ms-Date", now)
9293

9394
client := &http.Client{}
95+
9496
resp, err := client.Do(req.WithContext(ctx))
9597
if err != nil {
9698
logger.Error("failed to send request", "error", err)
9799
return &protobufs.Empty{}, err
98100
}
99101
defer resp.Body.Close()
102+
100103
logger.Debug("sent notification to sentinel", "status", resp.Status)
101104

102105
if resp.StatusCode != http.StatusOK {
@@ -106,10 +109,11 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not
106109
return &protobufs.Empty{}, nil
107110
}
108111

109-
func (s *SentinelPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
112+
func (s *SentinelPlugin) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
110113
d := PluginConfig{}
111-
err := yaml.Unmarshal(config.Config, &d)
114+
err := yaml.Unmarshal(config.GetConfig(), &d)
112115
s.PluginConfigByName[d.Name] = d
116+
113117
return &protobufs.Empty{}, err
114118
}
115119

cmd/notification-slack/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
3636
})
3737

3838
func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
39-
if _, ok := n.ConfigByName[notification.Name]; !ok {
40-
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
39+
if _, ok := n.ConfigByName[notification.GetName()]; !ok {
40+
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
4141
}
4242

43-
cfg := n.ConfigByName[notification.Name]
43+
cfg := n.ConfigByName[notification.GetName()]
4444

4545
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
4646
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
4747
}
4848

49-
logger.Info(fmt.Sprintf("found notify signal for %s config", notification.Name))
50-
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.Text))
49+
logger.Info(fmt.Sprintf("found notify signal for %s config", notification.GetName()))
50+
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.GetText()))
5151

5252
err := slack.PostWebhookContext(ctx, cfg.Webhook, &slack.WebhookMessage{
53-
Text: notification.Text,
53+
Text: notification.GetText(),
5454
Channel: cfg.Channel,
5555
Username: cfg.Username,
5656
IconEmoji: cfg.IconEmoji,
@@ -63,10 +63,10 @@ func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notificatio
6363
return &protobufs.Empty{}, err
6464
}
6565

66-
func (n *Notify) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
66+
func (n *Notify) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
6767
d := PluginConfig{}
6868

69-
if err := yaml.Unmarshal(config.Config, &d); err != nil {
69+
if err := yaml.Unmarshal(config.GetConfig(), &d); err != nil {
7070
return nil, err
7171
}
7272

cmd/notification-splunk/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ type Payload struct {
4343
}
4444

4545
func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
46-
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
47-
return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", notification.Name)
46+
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
47+
return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", notification.GetName())
4848
}
4949

50-
cfg := s.PluginConfigByName[notification.Name]
50+
cfg := s.PluginConfigByName[notification.GetName()]
5151

5252
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
5353
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
5454
}
5555

56-
logger.Info(fmt.Sprintf("received notify signal for %s config", notification.Name))
56+
logger.Info(fmt.Sprintf("received notify signal for %s config", notification.GetName()))
5757

58-
p := Payload{Event: notification.Text}
58+
p := Payload{Event: notification.GetText()}
5959

6060
data, err := json.Marshal(p)
6161
if err != nil {
@@ -94,9 +94,9 @@ func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notificatio
9494
return &protobufs.Empty{}, nil
9595
}
9696

97-
func (s *Splunk) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
97+
func (s *Splunk) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
9898
d := PluginConfig{}
99-
err := yaml.Unmarshal(config.Config, &d)
99+
err := yaml.Unmarshal(config.GetConfig(), &d)
100100
s.PluginConfigByName[d.Name] = d
101101
logger.Debug(fmt.Sprintf("Splunk plugin '%s' use URL '%s'", d.Name, d.URL))
102102

0 commit comments

Comments
 (0)