|
| 1 | +package audit_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/tabbed/pqtype" |
| 13 | + |
| 14 | + "github.com/coder/coder/coderd/audit" |
| 15 | + "github.com/coder/coder/coderd/database" |
| 16 | +) |
| 17 | + |
| 18 | +func TestExporter(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + var tests = []struct { |
| 22 | + name string |
| 23 | + filterDecision audit.FilterDecision |
| 24 | + backendDecision audit.FilterDecision |
| 25 | + shouldExport bool |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "ShouldDrop", |
| 29 | + filterDecision: audit.FilterDecisionDrop, |
| 30 | + backendDecision: audit.FilterDecisionStore, |
| 31 | + shouldExport: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "ShouldStore", |
| 35 | + filterDecision: audit.FilterDecisionStore, |
| 36 | + backendDecision: audit.FilterDecisionStore, |
| 37 | + shouldExport: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "ShouldNotStore", |
| 41 | + filterDecision: audit.FilterDecisionExport, |
| 42 | + backendDecision: audit.FilterDecisionStore, |
| 43 | + shouldExport: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "ShouldExport", |
| 47 | + filterDecision: audit.FilterDecisionExport, |
| 48 | + backendDecision: audit.FilterDecisionExport, |
| 49 | + shouldExport: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "ShouldNotExport", |
| 53 | + filterDecision: audit.FilterDecisionStore, |
| 54 | + backendDecision: audit.FilterDecisionExport, |
| 55 | + shouldExport: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "ShouldStoreOrExport", |
| 59 | + filterDecision: audit.FilterDecisionStore | audit.FilterDecisionExport, |
| 60 | + backendDecision: audit.FilterDecisionExport, |
| 61 | + shouldExport: true, |
| 62 | + }, |
| 63 | + // When more filters are written they should have their own tests. |
| 64 | + { |
| 65 | + name: "DefaultFilter", |
| 66 | + filterDecision: func() audit.FilterDecision { |
| 67 | + decision, _ := audit.DefaultFilter.Check(context.Background(), randomAuditLog()) |
| 68 | + return decision |
| 69 | + }(), |
| 70 | + backendDecision: audit.FilterDecisionExport, |
| 71 | + shouldExport: true, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, test := range tests { |
| 76 | + test := test |
| 77 | + t.Run(test.name, func(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + var ( |
| 81 | + backend = &testBackend{decision: test.backendDecision} |
| 82 | + exporter = audit.NewExporter( |
| 83 | + audit.FilterFunc(func(_ context.Context, _ database.AuditLog) (audit.FilterDecision, error) { |
| 84 | + return test.filterDecision, nil |
| 85 | + }), |
| 86 | + backend, |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + err := exporter.Export(context.Background(), randomAuditLog()) |
| 91 | + require.NoError(t, err) |
| 92 | + require.Equal(t, len(backend.alogs) > 0, test.shouldExport) |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func randomAuditLog() database.AuditLog { |
| 98 | + _, inet, _ := net.ParseCIDR("127.0.0.1/32") |
| 99 | + return database.AuditLog{ |
| 100 | + ID: uuid.New(), |
| 101 | + Time: time.Now(), |
| 102 | + UserID: uuid.New(), |
| 103 | + OrganizationID: uuid.New(), |
| 104 | + Ip: pqtype.Inet{ |
| 105 | + IPNet: *inet, |
| 106 | + Valid: true, |
| 107 | + }, |
| 108 | + UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", |
| 109 | + ResourceType: database.ResourceTypeOrganization, |
| 110 | + ResourceID: uuid.New(), |
| 111 | + ResourceTarget: "colin's organization", |
| 112 | + Action: database.AuditActionDelete, |
| 113 | + Diff: []byte{}, |
| 114 | + StatusCode: http.StatusNoContent, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +type testBackend struct { |
| 119 | + decision audit.FilterDecision |
| 120 | + |
| 121 | + alogs []database.AuditLog |
| 122 | +} |
| 123 | + |
| 124 | +func (t *testBackend) Decision() audit.FilterDecision { |
| 125 | + return t.decision |
| 126 | +} |
| 127 | + |
| 128 | +func (t *testBackend) Export(_ context.Context, alog database.AuditLog) error { |
| 129 | + t.alogs = append(t.alogs, alog) |
| 130 | + return nil |
| 131 | +} |
0 commit comments