Skip to content

Commit 999bdd7

Browse files
feat: save audit timestamp (#437)
* feat: save timestamp in recorded audits * fix: audit timestamp generation * fix: use nanoseconds * fix tets
1 parent 4a55aa4 commit 999bdd7

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

internal/audit/agent_log_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func TestLogAgent(t *testing.T) {
4444
ID: "some user",
4545
Groups: []string{"g1", "g2"},
4646
},
47-
Request: RequestInfo{Body: []byte("some body")},
47+
Request: RequestInfo{Body: []byte("some body")},
48+
Timestamp: 123123123,
4849
})
4950

5051
entries := hook.AllEntries()
@@ -78,6 +79,7 @@ func TestLogAgent(t *testing.T) {
7879
"groups": []string{"g1", "g2"},
7980
"id": "some user",
8081
},
82+
"timestamp": int64(123123123),
8183
}, trailData)
8284
}
8385

@@ -105,7 +107,8 @@ func TestLogAgentWithGlobalLabels(t *testing.T) {
105107
ID: "some user",
106108
Groups: []string{"g1", "g2"},
107109
},
108-
Request: RequestInfo{Body: []byte("some body")},
110+
Request: RequestInfo{Body: []byte("some body")},
111+
Timestamp: 543543543,
109112
})
110113

111114
entries := hook.AllEntries()
@@ -141,5 +144,6 @@ func TestLogAgentWithGlobalLabels(t *testing.T) {
141144
"groups": []string{"g1", "g2"},
142145
"id": "some user",
143146
},
147+
"timestamp": int64(543543543),
144148
}, trailData)
145149
}

internal/audit/audit.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Audit struct {
4545
Subject SubjectInfo
4646
Request RequestInfo
4747
Labels Labels
48+
Timestamp int64
4849
}
4950

5051
type AuthzInfo struct {
@@ -85,6 +86,7 @@ type auditToPrint struct {
8586
Subject SubjectInfo `audit:"subject"`
8687
Request RequestInfo `audit:"request"`
8788
Labels Labels `audit:"labels"`
89+
Timestamp int64 `audit:"timestamp"`
8890
}
8991

9092
func (a *Audit) toPrint(data map[string]any) auditToPrint {
@@ -95,9 +97,10 @@ func (a *Audit) toPrint(data map[string]any) auditToPrint {
9597
Allowed: a.Authorization.Allowed,
9698
PolicyName: a.Authorization.PolicyName,
9799
},
98-
Subject: a.Subject,
99-
Request: a.Request,
100-
Labels: a.Labels,
100+
Subject: a.Subject,
101+
Request: a.Request,
102+
Labels: a.Labels,
103+
Timestamp: a.Timestamp,
101104
}
102105
if data != nil {
103106
print.applyDataFromPolicy(data)

sdk/evaluator.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"encoding/json"
2020
"errors"
21+
"time"
2122

2223
"github.com/rond-authz/rond/core"
2324
"github.com/rond-authz/rond/internal/audit"
@@ -124,6 +125,7 @@ func (e evaluator) EvaluateRequestPolicy(ctx context.Context, rondInput core.Inp
124125
Path: rondInput.Request.Path,
125126
UserAgent: rondInput.Request.Headers.Get(userAgentHeaderKey),
126127
},
128+
Timestamp: time.Now().UnixNano(),
127129
}); err != nil {
128130
logger.WithField("error", map[string]any{
129131
"aggregationId": options.Audit.AggregationID,
@@ -160,6 +162,7 @@ func (e evaluator) EvaluateRequestPolicy(ctx context.Context, rondInput core.Inp
160162
Path: rondInput.Request.Path,
161163
UserAgent: rondInput.Request.Headers.Get(userAgentHeaderKey),
162164
},
165+
Timestamp: time.Now().UnixNano(),
163166
}); err != nil {
164167
logger.WithField("error", map[string]any{
165168
"aggregationId": options.Audit.AggregationID,
@@ -213,6 +216,7 @@ func (e evaluator) EvaluateResponsePolicy(ctx context.Context, rondInput core.In
213216
Path: rondInput.Request.Path,
214217
UserAgent: rondInput.Request.Headers.Get(userAgentHeaderKey),
215218
},
219+
Timestamp: time.Now().UnixNano(),
216220
}); err != nil {
217221
logger.WithField("error", map[string]any{
218222
"aggregationId": options.Audit.AggregationID,
@@ -237,6 +241,7 @@ func (e evaluator) EvaluateResponsePolicy(ctx context.Context, rondInput core.In
237241
Path: rondInput.Request.Path,
238242
UserAgent: rondInput.Request.Headers.Get(userAgentHeaderKey),
239243
},
244+
Timestamp: time.Now().UnixNano(),
240245
}); err != nil {
241246
logger.WithField("error", map[string]any{
242247
"aggregationId": options.Audit.AggregationID,

sdk/evaluator_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ func TestEvaluateRequestPolicy(t *testing.T) {
477477
authz["roleId"] = testCase.expectedAuditRoleID
478478
}
479479

480+
record := trailRecords[0].Fields["trail"]
481+
require.NotNil(t, record.(map[string]any)["timestamp"])
482+
delete(record.(map[string]any), "timestamp")
483+
480484
require.Equal(t, map[string]any{
481485
"authorization": authz,
482486
"labels": testCase.expectedAuditLabels,
@@ -487,7 +491,7 @@ func TestEvaluateRequestPolicy(t *testing.T) {
487491
"subject": map[string]any{
488492
"id": "my-user",
489493
},
490-
}, trailRecords[0].Fields["trail"])
494+
}, record)
491495
}
492496
})
493497
})
@@ -605,6 +609,9 @@ func TestEvaluateRequestPolicy(t *testing.T) {
605609
"My-Header-Key": headerValue,
606610
}
607611

612+
require.NotNil(t, trailRecord["timestamp"])
613+
delete(trailRecord, "timestamp")
614+
608615
require.Equal(t, map[string]any{
609616
"authorization": authz,
610617
"labels": labels,
@@ -705,6 +712,9 @@ func TestEvaluateRequestPolicy(t *testing.T) {
705712
"My-Header-Key": headerValue,
706713
}
707714

715+
require.NotNil(t, trailRecord["timestamp"])
716+
delete(trailRecord, "timestamp")
717+
708718
require.Equal(t, map[string]any{
709719
"authorization": authz,
710720
"labels": labels,
@@ -971,6 +981,8 @@ func TestEvaluateResponsePolicy(t *testing.T) {
971981
foundRecord := trailRecords[0].Fields["trail"].(map[string]any)
972982
require.NotEmpty(t, foundRecord["id"])
973983
delete(foundRecord, "id")
984+
require.NotEmpty(t, foundRecord["timestamp"])
985+
delete(foundRecord, "timestamp")
974986

975987
var labels map[string]any
976988
require.Equal(t, map[string]any{

0 commit comments

Comments
 (0)