Skip to content

Commit 98cccf8

Browse files
feat(audit-logs): add smart time-range defaults
Signed-off-by: Nishchay Rajput <nishchayr@iitbhilai.ac.in>
1 parent fe5bc59 commit 98cccf8

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

cmd/harbor/root/logs.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,28 @@ func buildAuditLogQuery(baseQuery, operation, resourceType, resource, username,
194194

195195
from := strings.TrimSpace(fromTime)
196196
to := strings.TrimSpace(toTime)
197-
if (from != "" && to == "") || (from == "" && to != "") {
198-
return "", fmt.Errorf("both --from-time and --to-time must be provided together")
197+
198+
// --to-time alone is not allowed; if provided, --from-time must also be present
199+
if from == "" && to != "" {
200+
return "", fmt.Errorf("--to-time cannot be used without --from-time")
199201
}
200202

201-
if from != "" && to != "" {
203+
// If --from-time is present, use it with either provided --to-time or default to current time
204+
if from != "" {
202205
normalizedFrom, err := normalizeAuditTime(from)
203206
if err != nil {
204207
return "", fmt.Errorf("invalid --from-time: %w", err)
205208
}
206209

207-
normalizedTo, err := normalizeAuditTime(to)
208-
if err != nil {
209-
return "", fmt.Errorf("invalid --to-time: %w", err)
210+
normalizedTo := to
211+
if to == "" {
212+
normalizedTo = time.Now().Format("2006-01-02 15:04:05")
213+
} else {
214+
var err error
215+
normalizedTo, err = normalizeAuditTime(to)
216+
if err != nil {
217+
return "", fmt.Errorf("invalid --to-time: %w", err)
218+
}
210219
}
211220

212221
parts = append(parts, fmt.Sprintf("op_time=[%s~%s]", normalizedFrom, normalizedTo))

cmd/harbor/root/logs_test.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414
package root
1515

16-
import "testing"
16+
import (
17+
"regexp"
18+
"testing"
19+
)
1720

1821
func TestBuildAuditLogQuery(t *testing.T) {
1922
tests := []struct {
@@ -25,13 +28,13 @@ func TestBuildAuditLogQuery(t *testing.T) {
2528
username string
2629
fromTime string
2730
toTime string
28-
expected string
31+
expectedRx string // regex pattern to match (for times that vary)
2932
wantErr bool
3033
}{
3134
{
32-
name: "returns base query only",
33-
baseQuery: "operation=push",
34-
expected: "operation=push",
35+
name: "returns base query only",
36+
baseQuery: "operation=push",
37+
expectedRx: "^operation=push$",
3538
},
3639
{
3740
name: "builds query with convenience filters",
@@ -40,16 +43,21 @@ func TestBuildAuditLogQuery(t *testing.T) {
4043
resourceType: "artifact",
4144
resource: "library/nginx",
4245
username: "admin",
43-
expected: "operation_result=true,operation=create_artifact,resource_type=artifact,resource=library/nginx,username=admin",
46+
expectedRx: "^operation_result=true,operation=create_artifact,resource_type=artifact,resource=library/nginx,username=admin$",
4447
},
4548
{
46-
name: "builds range query with normalized times",
47-
fromTime: "2025-01-01T01:02:03Z",
48-
toTime: "2025-01-01 05:06:07",
49-
expected: "op_time=[2025-01-01 01:02:03~2025-01-01 05:06:07]",
49+
name: "builds range query with both times specified",
50+
fromTime: "2025-01-01T01:02:03Z",
51+
toTime: "2025-01-01 05:06:07",
52+
expectedRx: "^op_time=\\[2025-01-01 01:02:03~2025-01-01 05:06:07\\]$",
53+
},
54+
{
55+
name: "from-time alone defaults to-time to current time",
56+
fromTime: "2025-01-01T01:02:03Z",
57+
expectedRx: "^op_time=\\[2025-01-01 01:02:03~.*\\]$", // matches any end time
5058
},
5159
{
52-
name: "fails when one range bound is missing",
60+
name: "to-time alone is rejected",
5361
toTime: "2025-01-01 05:06:07",
5462
wantErr: true,
5563
},
@@ -59,6 +67,12 @@ func TestBuildAuditLogQuery(t *testing.T) {
5967
toTime: "2025-01-01 05:06:07",
6068
wantErr: true,
6169
},
70+
{
71+
name: "fails for invalid to time",
72+
fromTime: "2025-01-01T01:02:03Z",
73+
toTime: "invalid-time",
74+
wantErr: true,
75+
},
6276
}
6377

6478
for _, tt := range tests {
@@ -84,8 +98,9 @@ func TestBuildAuditLogQuery(t *testing.T) {
8498
t.Fatalf("unexpected error: %v", err)
8599
}
86100

87-
if query != tt.expected {
88-
t.Fatalf("expected query %q, got %q", tt.expected, query)
101+
matched, _ := regexp.MatchString(tt.expectedRx, query)
102+
if !matched {
103+
t.Fatalf("expected query to match regex %q, got %q", tt.expectedRx, query)
89104
}
90105
})
91106
}

0 commit comments

Comments
 (0)