Skip to content

Commit df34a5b

Browse files
kylos101ona-agent
andcommitted
fix(agent-smith): add custom JSON marshaling for Duration in filesystem config
- Add Duration wrapper type with custom JSON marshaling/unmarshaling - Fixes error: 'cannot unmarshal string into Go struct field scanInterval of type time.Duration' - Allows scanInterval to be specified as string like '5m' in JSON config - Update agent.go to extract Duration.Duration when creating detector config - Resolves runtime config parsing error in Kubernetes deployment Co-authored-by: Ona <[email protected]>
1 parent ec97a2a commit df34a5b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

components/ee/agent-smith/pkg/agent/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func NewAgentSmith(cfg config.Config) (*Smith, error) {
144144
// Create filesystem detector config
145145
fsConfig := detector.FilesystemScanningConfig{
146146
Enabled: cfg.FilesystemScanning.Enabled,
147-
ScanInterval: cfg.FilesystemScanning.ScanInterval,
147+
ScanInterval: cfg.FilesystemScanning.ScanInterval.Duration,
148148
MaxFileSize: cfg.FilesystemScanning.MaxFileSize,
149149
WorkingArea: cfg.FilesystemScanning.WorkingArea,
150150
}

components/ee/agent-smith/pkg/config/config.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,36 @@ type WorkspaceManagerConfig struct {
193193

194194
// FilesystemScanning configures filesystem signature scanning
195195
type FilesystemScanning struct {
196-
Enabled bool `json:"enabled"`
197-
ScanInterval time.Duration `json:"scanInterval"`
198-
MaxFileSize int64 `json:"maxFileSize"`
199-
WorkingArea string `json:"workingArea"`
196+
Enabled bool `json:"enabled"`
197+
ScanInterval Duration `json:"scanInterval"`
198+
MaxFileSize int64 `json:"maxFileSize"`
199+
WorkingArea string `json:"workingArea"`
200+
}
201+
202+
// Duration wraps time.Duration to provide JSON marshaling/unmarshaling
203+
type Duration struct {
204+
time.Duration
205+
}
206+
207+
// UnmarshalJSON implements json.Unmarshaler interface
208+
func (d *Duration) UnmarshalJSON(data []byte) error {
209+
var s string
210+
if err := json.Unmarshal(data, &s); err != nil {
211+
return err
212+
}
213+
214+
duration, err := time.ParseDuration(s)
215+
if err != nil {
216+
return err
217+
}
218+
219+
d.Duration = duration
220+
return nil
221+
}
222+
223+
// MarshalJSON implements json.Marshaler interface
224+
func (d Duration) MarshalJSON() ([]byte, error) {
225+
return json.Marshal(d.Duration.String())
200226
}
201227

202228
// Slackwebhooks holds slack notification configuration for different levels of penalty severity

0 commit comments

Comments
 (0)