Skip to content

Commit 38bb16e

Browse files
committed
feat: adds the audit middleware to records the system audit events
Signed-off-by: chlins <chlins.zhang@gmail.com>
1 parent 8d2d672 commit 38bb16e

File tree

15 files changed

+805
-1
lines changed

15 files changed

+805
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@
13061306
- **deps:** bump github.com/prometheus/client_golang from 1.12.2 to 1.13.0 ([#1564](https://github.com/dragonflyoss/dragonfly/issues/1564))
13071307
- **deps:** bump go.opentelemetry.io/otel/exporters/jaeger from 1.14.0 to 1.15.1 ([#2376](https://github.com/dragonflyoss/dragonfly/issues/2376))
13081308
- **deps:** bump go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin from 0.32.0 to 0.34.0 ([#1547](https://github.com/dragonflyoss/dragonfly/issues/1547))
1309-
- **deps:** bump github.com/sirupsen/logrus from 1.8.1 to 1.9.0 ([#1544](https://github.com/dragonflyoss/dragonfly/issues/1544))
1309+
- **deps:** bump github.com/sirupsen/es, len(auditslogrus from 1.8.1 to 1.9.0 ([#1544](https://github.com/dragonflyoss/dragonfly/issues/1544))
13101310
- **deps:** bump github.com/jarcoal/httpmock from 1.0.8 to 1.2.0 ([#1542](https://github.com/dragonflyoss/dragonfly/issues/1542))
13111311
- **deps:** bump go.opentelemetry.io/otel/exporters/jaeger from 1.8.0 to 1.9.0 ([#1541](https://github.com/dragonflyoss/dragonfly/issues/1541))
13121312
- **deps:** bump google.golang.org/grpc from 1.47.0 to 1.48.0 ([#1508](https://github.com/dragonflyoss/dragonfly/issues/1508))

api/manager/docs.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,61 @@ const docTemplate = `{
241241
}
242242
}
243243
},
244+
"/api/v1/audits": {
245+
"get": {
246+
"description": "Get Audits",
247+
"consumes": [
248+
"application/json"
249+
],
250+
"produces": [
251+
"application/json"
252+
],
253+
"tags": [
254+
"Audit"
255+
],
256+
"summary": "Get Audits",
257+
"parameters": [
258+
{
259+
"type": "integer",
260+
"default": 0,
261+
"description": "current page",
262+
"name": "page",
263+
"in": "query",
264+
"required": true
265+
},
266+
{
267+
"maximum": 50,
268+
"minimum": 2,
269+
"type": "integer",
270+
"default": 10,
271+
"description": "return max item count, default 10, max 50",
272+
"name": "per_page",
273+
"in": "query",
274+
"required": true
275+
}
276+
],
277+
"responses": {
278+
"200": {
279+
"description": "OK",
280+
"schema": {
281+
"type": "array",
282+
"items": {
283+
"$ref": "#/definitions/d7y_io_dragonfly_v2_manager_models.Audit"
284+
}
285+
}
286+
},
287+
"400": {
288+
"description": "Bad Request"
289+
},
290+
"404": {
291+
"description": "Not Found"
292+
},
293+
"500": {
294+
"description": "Internal Server Error"
295+
}
296+
}
297+
}
298+
},
244299
"/api/v1/buckets": {
245300
"get": {
246301
"description": "Get Buckets",
@@ -4140,6 +4195,50 @@ const docTemplate = `{
41404195
}
41414196
}
41424197
},
4198+
"d7y_io_dragonfly_v2_manager_models.Audit": {
4199+
"type": "object",
4200+
"properties": {
4201+
"actor_name": {
4202+
"type": "string"
4203+
},
4204+
"actor_type": {
4205+
"type": "string"
4206+
},
4207+
"created_at": {
4208+
"type": "string"
4209+
},
4210+
"detail": {
4211+
"$ref": "#/definitions/d7y_io_dragonfly_v2_manager_models.JSONMap"
4212+
},
4213+
"event_type": {
4214+
"type": "string"
4215+
},
4216+
"id": {
4217+
"type": "integer"
4218+
},
4219+
"is_del": {
4220+
"type": "integer"
4221+
},
4222+
"operated_at": {
4223+
"type": "string"
4224+
},
4225+
"operation": {
4226+
"type": "string"
4227+
},
4228+
"path": {
4229+
"type": "string"
4230+
},
4231+
"state": {
4232+
"type": "string"
4233+
},
4234+
"status_code": {
4235+
"type": "integer"
4236+
},
4237+
"updated_at": {
4238+
"type": "string"
4239+
}
4240+
}
4241+
},
41434242
"d7y_io_dragonfly_v2_manager_models.Config": {
41444243
"type": "object",
41454244
"properties": {

api/manager/swagger.json

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,61 @@
235235
}
236236
}
237237
},
238+
"/api/v1/audits": {
239+
"get": {
240+
"description": "Get Audits",
241+
"consumes": [
242+
"application/json"
243+
],
244+
"produces": [
245+
"application/json"
246+
],
247+
"tags": [
248+
"Audit"
249+
],
250+
"summary": "Get Audits",
251+
"parameters": [
252+
{
253+
"type": "integer",
254+
"default": 0,
255+
"description": "current page",
256+
"name": "page",
257+
"in": "query",
258+
"required": true
259+
},
260+
{
261+
"maximum": 50,
262+
"minimum": 2,
263+
"type": "integer",
264+
"default": 10,
265+
"description": "return max item count, default 10, max 50",
266+
"name": "per_page",
267+
"in": "query",
268+
"required": true
269+
}
270+
],
271+
"responses": {
272+
"200": {
273+
"description": "OK",
274+
"schema": {
275+
"type": "array",
276+
"items": {
277+
"$ref": "#/definitions/d7y_io_dragonfly_v2_manager_models.Audit"
278+
}
279+
}
280+
},
281+
"400": {
282+
"description": "Bad Request"
283+
},
284+
"404": {
285+
"description": "Not Found"
286+
},
287+
"500": {
288+
"description": "Internal Server Error"
289+
}
290+
}
291+
}
292+
},
238293
"/api/v1/buckets": {
239294
"get": {
240295
"description": "Get Buckets",
@@ -4134,6 +4189,50 @@
41344189
}
41354190
}
41364191
},
4192+
"d7y_io_dragonfly_v2_manager_models.Audit": {
4193+
"type": "object",
4194+
"properties": {
4195+
"actor_name": {
4196+
"type": "string"
4197+
},
4198+
"actor_type": {
4199+
"type": "string"
4200+
},
4201+
"created_at": {
4202+
"type": "string"
4203+
},
4204+
"detail": {
4205+
"$ref": "#/definitions/d7y_io_dragonfly_v2_manager_models.JSONMap"
4206+
},
4207+
"event_type": {
4208+
"type": "string"
4209+
},
4210+
"id": {
4211+
"type": "integer"
4212+
},
4213+
"is_del": {
4214+
"type": "integer"
4215+
},
4216+
"operated_at": {
4217+
"type": "string"
4218+
},
4219+
"operation": {
4220+
"type": "string"
4221+
},
4222+
"path": {
4223+
"type": "string"
4224+
},
4225+
"state": {
4226+
"type": "string"
4227+
},
4228+
"status_code": {
4229+
"type": "integer"
4230+
},
4231+
"updated_at": {
4232+
"type": "string"
4233+
}
4234+
}
4235+
},
41374236
"d7y_io_dragonfly_v2_manager_models.Config": {
41384237
"type": "object",
41394238
"properties": {

api/manager/swagger.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ definitions:
2525
user_id:
2626
type: integer
2727
type: object
28+
d7y_io_dragonfly_v2_manager_models.Audit:
29+
properties:
30+
actor_name:
31+
type: string
32+
actor_type:
33+
type: string
34+
created_at:
35+
type: string
36+
detail:
37+
$ref: '#/definitions/d7y_io_dragonfly_v2_manager_models.JSONMap'
38+
event_type:
39+
type: string
40+
id:
41+
type: integer
42+
is_del:
43+
type: integer
44+
operated_at:
45+
type: string
46+
operation:
47+
type: string
48+
path:
49+
type: string
50+
state:
51+
type: string
52+
status_code:
53+
type: integer
54+
updated_at:
55+
type: string
56+
type: object
2857
d7y_io_dragonfly_v2_manager_models.Config:
2958
properties:
3059
bio:
@@ -1526,6 +1555,44 @@ paths:
15261555
summary: Update Application
15271556
tags:
15281557
- Application
1558+
/api/v1/audits:
1559+
get:
1560+
consumes:
1561+
- application/json
1562+
description: Get Audits
1563+
parameters:
1564+
- default: 0
1565+
description: current page
1566+
in: query
1567+
name: page
1568+
required: true
1569+
type: integer
1570+
- default: 10
1571+
description: return max item count, default 10, max 50
1572+
in: query
1573+
maximum: 50
1574+
minimum: 2
1575+
name: per_page
1576+
required: true
1577+
type: integer
1578+
produces:
1579+
- application/json
1580+
responses:
1581+
"200":
1582+
description: OK
1583+
schema:
1584+
items:
1585+
$ref: '#/definitions/d7y_io_dragonfly_v2_manager_models.Audit'
1586+
type: array
1587+
"400":
1588+
description: Bad Request
1589+
"404":
1590+
description: Not Found
1591+
"500":
1592+
description: Internal Server Error
1593+
summary: Get Audits
1594+
tags:
1595+
- Audit
15291596
/api/v1/buckets:
15301597
get:
15311598
consumes:

manager/database/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func New(cfg *config.Config) (*Database, error) {
101101

102102
func migrate(db *gorm.DB) error {
103103
return db.AutoMigrate(
104+
&models.Audit{},
104105
&models.Job{},
105106
&models.SeedPeerCluster{},
106107
&models.SeedPeer{},

manager/handlers/audit.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 The Dragonfly Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package handlers
18+
19+
import (
20+
"net/http"
21+
22+
"github.com/gin-gonic/gin"
23+
24+
// nolint
25+
_ "d7y.io/dragonfly/v2/manager/models"
26+
"d7y.io/dragonfly/v2/manager/types"
27+
)
28+
29+
// @Summary Get Audits
30+
// @Description Get Audits
31+
// @Tags Audit
32+
// @Accept json
33+
// @Produce json
34+
// @Param page query int true "current page" default(0)
35+
// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
36+
// @Success 200 {object} []models.Audit
37+
// @Failure 400
38+
// @Failure 404
39+
// @Failure 500
40+
// @Router /api/v1/audits [get]
41+
func (h *Handlers) GetAudits(ctx *gin.Context) {
42+
var query types.GetAuditsQuery
43+
if err := ctx.ShouldBindQuery(&query); err != nil {
44+
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
45+
return
46+
}
47+
48+
h.setPaginationDefault(&query.Page, &query.PerPage)
49+
audits, count, err := h.service.GetAudits(ctx.Request.Context(), query)
50+
if err != nil {
51+
ctx.Error(err) //nolint: errcheck
52+
return
53+
}
54+
55+
h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
56+
ctx.JSON(http.StatusOK, audits)
57+
}

0 commit comments

Comments
 (0)