Repository navigation
feat: add fetch audit logging for remote resource tracking #1094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package fetch | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
| ) | ||
|
|
||
| // FetchAuditEntry is a JSONL audit record for a remote resource fetch. | ||
| type FetchAuditEntry struct { | ||
| TraceID string `json:"trace_id"` | ||
| FetchTime time.Time `json:"fetch_time"` | ||
| URL string `json:"url"` | ||
| SHA256 string `json:"sha256"` | ||
| FetchType string `json:"fetch_type"` | ||
| AllowedBy string `json:"allowed_by"` | ||
| CacheHit bool `json:"cache_hit"` | ||
| } | ||
|
|
||
| // AppendFetchAudit writes a fetch audit entry as a JSON line to the given log path. | ||
| func AppendFetchAudit(logPath string, entry FetchAuditEntry) error { | ||
| if err := os.MkdirAll(filepath.Dir(logPath), 0o755); err != nil { | ||
| return fmt.Errorf("creating audit log directory: %w", err) | ||
| } | ||
| f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) | ||
| if err != nil { | ||
| return fmt.Errorf("opening audit log: %w", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| data, err := json.Marshal(entry) | ||
| if err != nil { | ||
| return fmt.Errorf("marshaling audit entry: %w", err) | ||
| } | ||
| if _, err := fmt.Fprintf(f, "%s\n", data); err != nil { | ||
| return fmt.Errorf("writing audit entry: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package fetch | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAppendFetchAudit_SingleEntry(t *testing.T) { | ||
| logPath := filepath.Join(t.TempDir(), "audit.jsonl") | ||
|
|
||
| entry := FetchAuditEntry{ | ||
| TraceID: "trace-001", | ||
| FetchTime: time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC), | ||
| URL: "https://example.com/resource", | ||
| SHA256: "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", | ||
| FetchType: "http_get", | ||
| AllowedBy: "allowlist-rule-1", | ||
| CacheHit: false, | ||
| } | ||
|
|
||
| err := AppendFetchAudit(logPath, entry) | ||
| require.NoError(t, err) | ||
|
|
||
| data, err := os.ReadFile(logPath) | ||
| require.NoError(t, err) | ||
|
|
||
| var got FetchAuditEntry | ||
| err = json.Unmarshal([]byte(strings.TrimSpace(string(data))), &got) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, "trace-001", got.TraceID) | ||
| assert.Equal(t, time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC), got.FetchTime) | ||
| assert.Equal(t, "https://example.com/resource", got.URL) | ||
| assert.Equal(t, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", got.SHA256) | ||
| assert.Equal(t, "http_get", got.FetchType) | ||
| assert.Equal(t, "allowlist-rule-1", got.AllowedBy) | ||
| assert.False(t, got.CacheHit) | ||
| } | ||
|
|
||
| func TestAppendFetchAudit_MultipleEntries(t *testing.T) { | ||
| logPath := filepath.Join(t.TempDir(), "audit.jsonl") | ||
|
|
||
| for i := range 3 { | ||
| entry := FetchAuditEntry{ | ||
| TraceID: fmt.Sprintf("trace-%03d", i), | ||
| FetchTime: time.Now().UTC(), | ||
| URL: fmt.Sprintf("https://example.com/resource/%d", i), | ||
| SHA256: "deadbeef", | ||
| FetchType: "http_get", | ||
| AllowedBy: "allowlist", | ||
| CacheHit: i > 0, | ||
| } | ||
| err := AppendFetchAudit(logPath, entry) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| data, err := os.ReadFile(logPath) | ||
| require.NoError(t, err) | ||
|
|
||
| lines := strings.Split(strings.TrimSpace(string(data)), "\n") | ||
| assert.Len(t, lines, 3) | ||
|
|
||
| for _, line := range lines { | ||
| var got FetchAuditEntry | ||
| err := json.Unmarshal([]byte(line), &got) | ||
| assert.NoError(t, err) | ||
| } | ||
| } | ||
|
|
||
| func TestAppendFetchAudit_CreatesParentDirs(t *testing.T) { | ||
| logPath := filepath.Join(t.TempDir(), "a", "b", "c", "audit.jsonl") | ||
|
|
||
| entry := FetchAuditEntry{ | ||
| TraceID: "trace-nested", | ||
| FetchTime: time.Now().UTC(), | ||
| URL: "https://example.com/nested", | ||
| SHA256: "cafebabe", | ||
| FetchType: "http_get", | ||
| AllowedBy: "allowlist", | ||
| CacheHit: true, | ||
| } | ||
|
|
||
| err := AppendFetchAudit(logPath, entry) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = os.Stat(logPath) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestAppendFetchAudit_JSONFields(t *testing.T) { | ||
| entry := FetchAuditEntry{ | ||
| TraceID: "trace-fields", | ||
| FetchTime: time.Now().UTC(), | ||
| URL: "https://example.com/fields", | ||
| SHA256: "fieldhash", | ||
| FetchType: "http_get", | ||
| AllowedBy: "allowlist", | ||
| CacheHit: false, | ||
| } | ||
|
|
||
| data, err := json.Marshal(entry) | ||
| require.NoError(t, err) | ||
|
|
||
| var m map[string]interface{} | ||
| err = json.Unmarshal(data, &m) | ||
| require.NoError(t, err) | ||
|
|
||
| expectedKeys := []string{"trace_id", "fetch_time", "url", "sha256", "fetch_type", "allowed_by", "cache_hit"} | ||
| for _, key := range expectedKeys { | ||
| assert.Contains(t, m, key) | ||
| } | ||
| assert.Len(t, m, len(expectedKeys)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.