Skip to content

Commit 362b7cb

Browse files
authored
fix: Fixed an error with parsing timeout in antivirus config (#4644)
2 parents b444eb8 + 848c5ce commit 362b7cb

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

pkg/config/config/antivirus.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ func getAntivirusFromContext(contextName string) *AntivirusContextConfig {
6969
}
7070

7171
var cfg AntivirusContextConfig
72-
if err := mapstructure.Decode(avData, &cfg); err != nil {
72+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
73+
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
74+
Result: &cfg,
75+
})
76+
if err != nil {
77+
log.Warnf("Failed to create antivirus config decoder for context %q: %v", contextName, err)
78+
return nil
79+
}
80+
if err := decoder.Decode(avData); err != nil {
81+
log.Warnf("Failed to decode antivirus config for context %q: %v", contextName, err)
7382
return nil
7483
}
7584

pkg/config/config/config_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ func TestConfigUnmarshal(t *testing.T) {
130130
assert.Equal(t, true, myContextSharing.AutoAcceptTrusted)
131131
assert.Equal(t, []string{"linagora.com"}, myContextSharing.TrustedDomains)
132132

133+
// Test GetAntivirusConfig for my-context (includes duration parsing)
134+
myContextAntivirus := GetAntivirusConfig("my-context")
135+
assert.Equal(t, true, myContextAntivirus.Enabled)
136+
assert.Equal(t, "localhost:3310", myContextAntivirus.Address)
137+
assert.Equal(t, 5*time.Minute, myContextAntivirus.Timeout)
138+
assert.Equal(t, int64(104857600), myContextAntivirus.MaxFileSize)
139+
assert.Equal(t, "warn", myContextAntivirus.OnInfected)
140+
assert.Equal(t, true, myContextAntivirus.Notifications.EmailOnInfected)
141+
assert.Equal(t, []string{"download", "preview"}, myContextAntivirus.Actions["pending"])
142+
assert.Equal(t, []string{"delete"}, myContextAntivirus.Actions["infected"])
143+
144+
// Test GetAntivirusConfig for non-existent context returns disabled
145+
nonExistentAntivirus := GetAntivirusConfig("non-existent")
146+
assert.Equal(t, false, nonExistentAntivirus.Enabled)
147+
133148
// Test GetSharingConfig for default context
134149
defaultSharing := GetSharingConfig("default")
135150
assert.Equal(t, true, defaultSharing.AutoAcceptTrusted)
@@ -156,6 +171,21 @@ func TestConfigUnmarshal(t *testing.T) {
156171
"auto_accept_trusted": true,
157172
"trusted_domains": []interface{}{"linagora.com"},
158173
},
174+
"antivirus": map[string]interface{}{
175+
"enabled": true,
176+
"address": "localhost:3310",
177+
"timeout": "5m",
178+
"max_file_size": 104857600,
179+
"on_infected": "warn",
180+
"notifications": map[string]interface{}{
181+
"email_on_infected": true,
182+
},
183+
"actions": map[string]interface{}{
184+
"pending": []interface{}{"download", "preview"},
185+
"clean": []interface{}{"download", "share", "preview", "delete"},
186+
"infected": []interface{}{"delete"},
187+
},
188+
},
159189
"logos": map[string]interface{}{
160190
"coachco2": map[string]interface{}{
161191
"light": []interface{}{

pkg/config/config/testdata/full_config.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ contexts:
254254
auto_accept_trusted: true
255255
trusted_domains:
256256
- linagora.com
257+
antivirus:
258+
enabled: true
259+
address: localhost:3310
260+
timeout: 5m
261+
max_file_size: 104857600
262+
on_infected: warn
263+
notifications:
264+
email_on_infected: true
265+
actions:
266+
pending:
267+
- download
268+
- preview
269+
clean:
270+
- download
271+
- share
272+
- preview
273+
- delete
274+
infected:
275+
- delete
257276

258277
default:
259278
sharing:

0 commit comments

Comments
 (0)