diff --git a/daemon/rule/loader.go b/daemon/rule/loader.go index 6f37abe556..e10188ab18 100644 --- a/daemon/rule/loader.go +++ b/daemon/rule/loader.go @@ -182,7 +182,8 @@ func (l *Loader) Replace(rule *Rule, saveToDisk bool) error { // Save a rule to disk. func (l *Loader) Save(rule *Rule, path string) error { - rule.Updated = time.Now() + // When saving the rule, use always RFC3339 format for the Created field (#1140). + rule.Updated = time.Now().Format(time.RFC3339) raw, err := json.MarshalIndent(rule, "", " ") if err != nil { return fmt.Errorf("Error while saving rule %s to %s: %s", rule, path, err) diff --git a/daemon/rule/rule.go b/daemon/rule/rule.go index 8e5101b748..46d336298a 100644 --- a/daemon/rule/rule.go +++ b/daemon/rule/rule.go @@ -38,22 +38,24 @@ const ( // The fields match the ones saved as json to disk. // If a .json rule file is modified on disk, it's reloaded automatically. type Rule struct { - Created time.Time `json:"created"` - Updated time.Time `json:"updated"` - Name string `json:"name"` - Description string `json:"description"` - Action Action `json:"action"` - Duration Duration `json:"duration"` - Operator Operator `json:"operator"` - Enabled bool `json:"enabled"` - Precedence bool `json:"precedence"` - Nolog bool `json:"nolog"` + // Save date fields as string, to avoid issues marshalling Time (#1140). + Created string `json:"created"` + Updated string `json:"updated"` + + Name string `json:"name"` + Description string `json:"description"` + Action Action `json:"action"` + Duration Duration `json:"duration"` + Operator Operator `json:"operator"` + Enabled bool `json:"enabled"` + Precedence bool `json:"precedence"` + Nolog bool `json:"nolog"` } // Create creates a new rule object with the specified parameters. func Create(name, description string, enabled, precedence, nolog bool, action Action, duration Duration, op *Operator) *Rule { return &Rule{ - Created: time.Now(), + Created: time.Now().Format(time.RFC3339), Enabled: enabled, Precedence: precedence, Nolog: nolog, @@ -135,8 +137,15 @@ func (r *Rule) Serialize() *protocol.Rule { r.Operator.Lock() defer r.Operator.Unlock() + created, err := time.Parse(time.RFC3339, r.Created) + if err != nil { + log.Warning("Error parsing rule Created date (it should be in RFC3339 format): %s (%s)", err, string(r.Name)) + log.Warning("using current time instead: %s", created) + created = time.Now() + } + protoRule := &protocol.Rule{ - Created: r.Created.Unix(), + Created: created.Unix(), Name: string(r.Name), Description: string(r.Description), Enabled: bool(r.Enabled),