Skip to content

Commit 6d8b782

Browse files
authored
Pass error callback function to auditing http filter. (#606)
1 parent 21b062c commit 6d8b782

File tree

7 files changed

+43
-17
lines changed

7 files changed

+43
-17
lines changed

.golangci.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
run:
22
concurrency: 4
3-
deadline: 10m
43
linters:
54
disable:
65
- musttag
@@ -11,4 +10,4 @@ linters:
1110
presets:
1211
- bugs
1312
- unused
14-
fast: true
13+
fast: true

cmd/metal-api/internal/service/audit-service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (r *auditResource) find(request *restful.Request, response *restful.Respons
6464
return
6565
}
6666

67-
backendResult, err := r.a.Search(auditing.EntryFilter{
67+
backendResult, err := r.a.Search(request.Request.Context(), auditing.EntryFilter{
6868
Limit: requestPayload.Limit,
6969
From: requestPayload.From,
7070
To: requestPayload.To,
@@ -73,6 +73,7 @@ func (r *auditResource) find(request *restful.Request, response *restful.Respons
7373
Type: auditing.EntryType(requestPayload.Type),
7474
User: requestPayload.User,
7575
Tenant: requestPayload.Tenant,
76+
Project: requestPayload.Project,
7677
Detail: auditing.EntryDetail(requestPayload.Detail),
7778
Phase: auditing.EntryPhase(requestPayload.Phase),
7879
Path: requestPayload.Path,

cmd/metal-api/internal/service/v1/audit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type AuditFindRequest struct {
1818
RequestId string `json:"rqid" optional:"true"`
1919
Type string `json:"type" optional:"true"`
2020

21-
User string `json:"user" optional:"true"`
22-
Tenant string `json:"tenant" optional:"true"`
21+
User string `json:"user" optional:"true"`
22+
Tenant string `json:"tenant" optional:"true"`
23+
Project string `json:"project" optional:"true"`
2324

2425
Detail string `json:"detail" optional:"true"`
2526
Phase string `json:"phase" optional:"true"`

cmd/metal-api/main.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,14 @@ func initRestServices(audit auditing.Auditing, withauth bool, ipmiSuperUser meta
791791
}
792792

793793
if audit != nil {
794-
httpFilter, err := auditing.HttpFilter(audit, logger.WithGroup("audit-middleware"))
794+
filterOpt := auditing.NewHttpFilterErrorCallback(func(err error, response *restful.Response) {
795+
httperr := httperrors.InternalServerError(err)
796+
if err := response.WriteHeaderAndEntity(httperr.StatusCode, httperr); err != nil {
797+
logger.Error("failed to send response", "error", err)
798+
}
799+
})
800+
801+
httpFilter, err := auditing.HttpFilter(audit, logger.WithGroup("audit-middleware"), filterOpt)
795802
if err != nil {
796803
log.Fatalf("unable to create http filter for auditing: %s", err)
797804
}
@@ -915,16 +922,16 @@ func createAuditingClient(log *slog.Logger) (auditing.Auditing, error) {
915922
return nil, nil
916923
}
917924

918-
c := auditing.Config{
919-
Component: "metal-api",
925+
return auditing.NewMeilisearch(auditing.Config{
926+
Component: "metal-api",
927+
Log: log,
928+
}, auditing.MeilisearchConfig{
920929
URL: viper.GetString("auditing-url"),
921930
APIKey: viper.GetString("auditing-api-key"),
922931
IndexPrefix: viper.GetString("auditing-index-prefix"),
923932
RotationInterval: auditing.Interval(viper.GetString("auditing-index-interval")),
924933
Keep: viper.GetInt64("auditing-keep"),
925-
Log: log, //FIXME
926-
}
927-
return auditing.New(c)
934+
})
928935
}
929936

930937
func run() error {

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ require (
2020
github.com/looplab/fsm v1.0.2
2121
github.com/metal-stack/go-ipam v1.14.8
2222
github.com/metal-stack/masterdata-api v0.11.5
23-
github.com/metal-stack/metal-lib v0.19.0
24-
github.com/metal-stack/security v0.9.1
23+
github.com/metal-stack/metal-lib v0.20.1
24+
github.com/metal-stack/security v0.9.3
2525
github.com/metal-stack/v v1.0.3
2626
github.com/nsqio/go-nsq v1.1.0
2727
github.com/prometheus/client_golang v1.20.5
@@ -110,6 +110,7 @@ require (
110110
github.com/lestrrat-go/jwx/v2 v2.1.3 // indirect
111111
github.com/lestrrat-go/option v1.0.1 // indirect
112112
github.com/lib/pq v1.10.9 // indirect
113+
github.com/lopezator/migrator v0.3.1 // indirect
113114
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
114115
github.com/magiconair/properties v1.8.9 // indirect
115116
github.com/mailru/easyjson v0.9.0 // indirect

go.sum

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,22 @@ github.com/icza/dyno v0.0.0-20230330125955-09f820a8d9c0 h1:nHoRIX8iXob3Y2kdt9Ksj
174174
github.com/icza/dyno v0.0.0-20230330125955-09f820a8d9c0/go.mod h1:c1tRKs5Tx7E2+uHGSyyncziFjvGpgv4H2HrqXeUQ/Uk=
175175
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
176176
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
177+
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
178+
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
179+
github.com/jackc/pgconn v1.11.0 h1:HiHArx4yFbwl91X3qqIHtUFoiIfLNJXCQRsnzkiwwaQ=
180+
github.com/jackc/pgconn v1.11.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
181+
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
182+
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
177183
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
178184
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
185+
github.com/jackc/pgproto3/v2 v2.2.0 h1:r7JypeP2D3onoQTCxWdTpCtJ4D+qpKr0TxvoyMhZ5ns=
186+
github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
179187
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
180188
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
189+
github.com/jackc/pgtype v1.10.0 h1:ILnBWrRMSXGczYvmkYD6PsYyVFUNLTnIUJHHDLmqk38=
190+
github.com/jackc/pgtype v1.10.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
191+
github.com/jackc/pgx/v4 v4.15.0 h1:B7dTkXsdILD3MF987WGGCcg+tvLW6bZJdEcqVFeU//w=
192+
github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw=
181193
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
182194
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
183195
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
@@ -233,6 +245,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
233245
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
234246
github.com/looplab/fsm v1.0.2 h1:f0kdMzr4CRpXtaKKRUxwLYJ7PirTdwrtNumeLN+mDx8=
235247
github.com/looplab/fsm v1.0.2/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
248+
github.com/lopezator/migrator v0.3.1 h1:ZFPT6aC7+nGWkqhleynABZ6ftycSf6hmHHLOaryq1Og=
249+
github.com/lopezator/migrator v0.3.1/go.mod h1:X+lHDMZ9Ci3/KdbypJcQYFFwipVrJsX4fRCQ4QLauYk=
236250
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0=
237251
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
238252
github.com/magiconair/properties v1.8.9 h1:nWcCbLq1N2v/cpNsy5WvQ37Fb+YElfq20WJ/a8RkpQM=
@@ -257,10 +271,10 @@ github.com/metal-stack/go-ipam v1.14.8 h1:M840hp1RcM2fHfMjFG5rn02yKFq6hXh+WvmYIS
257271
github.com/metal-stack/go-ipam v1.14.8/go.mod h1:LzAKT0X9dGAMs1uoLyJfJZkhwMMDLaSYvx9n6MJ9FI4=
258272
github.com/metal-stack/masterdata-api v0.11.5 h1:r7bYdhdVgOjCk6k7K/SCLlHALH23ZuMGY8E4udk4wXQ=
259273
github.com/metal-stack/masterdata-api v0.11.5/go.mod h1:Xk8kqxAp3NkAc2BX8yTIWgSlD77T897QSdRSluWvP4w=
260-
github.com/metal-stack/metal-lib v0.19.0 h1:4yBnp/jPGgX9KeCje3A4MFL2oDjgjOjgsIK391LltRI=
261-
github.com/metal-stack/metal-lib v0.19.0/go.mod h1:fCMaWwVGA/xAoGvBk72/nfzqBkHly0iOzrWpc55Fau4=
262-
github.com/metal-stack/security v0.9.1 h1:cx3afSJPSOh03E9gKjdG6mbNU+ox/dqV7q8T9MkrHxo=
263-
github.com/metal-stack/security v0.9.1/go.mod h1:ENm5kPjqh4uYvn79sAIxd6GZBwtF2GSsGdkLELrB/D4=
274+
github.com/metal-stack/metal-lib v0.20.1 h1:gxNg512dS5yzDebELtPZjmoWond0Gw0HHEkSVIAOWRE=
275+
github.com/metal-stack/metal-lib v0.20.1/go.mod h1:zYzXYpNA4nQ+ANx19s/+1Yb/Q6xhS1nQK2yK2/ryXZM=
276+
github.com/metal-stack/security v0.9.3 h1:ZF5rGeZ4fIFe0DFFQWkXsUDCzODyjdrpvKmeaLOz9lo=
277+
github.com/metal-stack/security v0.9.3/go.mod h1:ENm5kPjqh4uYvn79sAIxd6GZBwtF2GSsGdkLELrB/D4=
264278
github.com/metal-stack/v v1.0.3 h1:Sh2oBlnxrCUD+mVpzfC8HiqL045YWkxs0gpTvkjppqs=
265279
github.com/metal-stack/v v1.0.3/go.mod h1:YTahEu7/ishwpYKnp/VaW/7nf8+PInogkfGwLcGPdXg=
266280
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=

spec/metal-api.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@
469469
"phase": {
470470
"type": "string"
471471
},
472+
"project": {
473+
"type": "string"
474+
},
472475
"remote_addr": {
473476
"type": "string"
474477
},

0 commit comments

Comments
 (0)