Skip to content

Commit c6f5d66

Browse files
committed
chore: update the endpoint url
- add support for different formats of date time from the service Signed-off-by: Owen Rumney <owen.rumney@aquasec.com>
1 parent f8873cb commit c6f5d66

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

pkg/notification/notice.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ import (
1414
"github.com/aquasecurity/trivy/pkg/version/app"
1515
)
1616

17+
// flexibleTime is a custom time type that can handle
18+
// different date formats in JSON. It implements the
19+
// UnmarshalJSON method to parse the date string into a time.Time object.
20+
type flexibleTime struct {
21+
time.Time
22+
}
23+
1724
type versionInfo struct {
18-
LatestVersion string `json:"latest_version"`
19-
LatestDate time.Time `json:"latest_date"`
25+
LatestVersion string `json:"latest_version"`
26+
LatestDate flexibleTime `json:"latest_date"`
2027
}
2128

2229
type announcement struct {
@@ -48,7 +55,7 @@ type VersionChecker struct {
4855
// to the NewVersionChecker function.
4956
func NewVersionChecker(opts ...Option) *VersionChecker {
5057
v := &VersionChecker{
51-
updatesApi: "https://check.trivy.cloud/updates",
58+
updatesApi: "https://check.trivy.dev/updates",
5259
currentVersion: app.Version(),
5360
}
5461

@@ -73,6 +80,7 @@ func (v *VersionChecker) RunUpdateCheck(ctx context.Context, args []string) {
7380
}
7481

7582
go func() {
83+
logger.Debug("Running version check")
7684
args = getFlags(args)
7785
client := &http.Client{
7886
Timeout: 3 * time.Second,
@@ -109,6 +117,7 @@ func (v *VersionChecker) RunUpdateCheck(ctx context.Context, args []string) {
109117
if !v.skipUpdateCheck && !v.quiet {
110118
v.responseReceived = true
111119
}
120+
logger.Debug("Version check completed", log.String("latest_version", v.latestVersion.Trivy.LatestVersion))
112121
v.done = true
113122
}()
114123
}
@@ -120,6 +129,8 @@ func (v *VersionChecker) PrintNotices(output io.Writer) {
120129
return
121130
}
122131

132+
logger := log.WithPrefix("notification")
133+
logger.Debug("Printing notices")
123134
var notices []string
124135

125136
notices = append(notices, v.Warnings()...)
@@ -175,3 +186,29 @@ func getFlags(args []string) []string {
175186
}
176187
return flags
177188
}
189+
190+
func (fd *flexibleTime) UnmarshalJSON(b []byte) error {
191+
s := strings.Trim(string(b), `"`)
192+
if s == "" {
193+
return nil
194+
}
195+
196+
// Try parsing with time component
197+
layouts := []string{
198+
time.RFC3339,
199+
"2006-01-02",
200+
time.RFC1123,
201+
}
202+
203+
var err error
204+
for _, layout := range layouts {
205+
var t time.Time
206+
t, err = time.Parse(layout, s)
207+
if err == nil {
208+
fd.Time = t
209+
return nil
210+
}
211+
}
212+
213+
return fmt.Errorf("unable to parse date: %s", s)
214+
}

pkg/notification/notice_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (u *updatesServer) handler(w http.ResponseWriter, r *http.Request) {
196196
response := updateResponse{
197197
Trivy: versionInfo{
198198
LatestVersion: u.expectedVersion,
199-
LatestDate: time.Now(),
199+
LatestDate: flexibleTime{Time: time.Now()},
200200
},
201201
Announcements: u.expectedAnnouncements,
202202
}
@@ -207,3 +207,36 @@ func (u *updatesServer) handler(w http.ResponseWriter, r *http.Request) {
207207
}
208208
w.Write(out)
209209
}
210+
211+
func TestFlexibleDate(t *testing.T) {
212+
tests := []struct {
213+
name string
214+
dateStr string
215+
expected time.Time
216+
}{
217+
{
218+
name: "RFC3339 date format",
219+
dateStr: `"2023-10-01T12:00:00Z"`,
220+
expected: time.Date(2023, 10, 1, 12, 0, 0, 0, time.UTC),
221+
},
222+
{
223+
name: "RFC1123 date format",
224+
dateStr: `"Sun, 01 Oct 2023 12:00:00 GMT"`,
225+
expected: time.Date(2023, 10, 1, 12, 0, 0, 0, time.UTC),
226+
},
227+
{
228+
name: "RFC3339 date only format",
229+
dateStr: `"2023-10-01"`,
230+
expected: time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC),
231+
},
232+
}
233+
234+
for _, tt := range tests {
235+
t.Run(tt.name, func(t *testing.T) {
236+
var ft flexibleTime
237+
err := json.Unmarshal([]byte(tt.dateStr), &ft)
238+
require.NoError(t, err)
239+
assert.Equal(t, tt.expected.Unix(), ft.Unix())
240+
})
241+
}
242+
}

0 commit comments

Comments
 (0)