Skip to content

Commit

Permalink
🗃️ Add audit table
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Jul 22, 2023
1 parent 439ebd5 commit ff3e622
Show file tree
Hide file tree
Showing 27 changed files with 1,221 additions and 64 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/alicebob/miniredis/v2 v2.30.4
github.com/anchore/syft v0.85.0
github.com/aquasecurity/trivy v0.43.1
github.com/aws/aws-sdk-go v1.44.301
github.com/aws/aws-sdk-go v1.44.306
github.com/bytedance/sonic v1.9.2
github.com/casbin/casbin/v2 v2.72.1
github.com/casbin/gorm-adapter/v3 v3.18.0
github.com/deckarep/golang-set/v2 v2.3.0
github.com/distribution/distribution/v3 v3.0.0-20230714152834-003dd5aaa1f3
github.com/distribution/distribution/v3 v3.0.0-20230719040215-46b3d6201649
github.com/fatih/color v1.15.0
github.com/glebarez/sqlite v1.9.0
github.com/go-playground/validator v9.31.0+incompatible
Expand All @@ -25,7 +25,7 @@ require (
github.com/jackc/pgx/v4 v4.18.1
github.com/jinzhu/copier v0.3.5
github.com/labstack/echo-contrib v0.15.0
github.com/labstack/echo/v4 v4.10.2
github.com/labstack/echo/v4 v4.11.1
github.com/matoous/go-nanoid v1.5.0
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20230713155351-f641ac67a420
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.44.301 h1:VofuXktwHFTBUvoPiHxQis/3uKgu0RtgUwLtNujd3Zs=
github.com/aws/aws-sdk-go v1.44.301/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.44.306 h1:H487V/1N09BDxeGR7oR+LloC2uUpmf4atmqJaBgQOIs=
github.com/aws/aws-sdk-go v1.44.306/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -185,8 +185,8 @@ github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dhui/dktest v0.3.16 h1:i6gq2YQEtcrjKbeJpBkWjE8MmLZPYllcjOFbTZuPDnw=
github.com/distribution/distribution/v3 v3.0.0-20230714152834-003dd5aaa1f3 h1:duBxxXFJuirwRDyQwRyEHbHIdRu4yJ+F5b83d18JGaI=
github.com/distribution/distribution/v3 v3.0.0-20230714152834-003dd5aaa1f3/go.mod h1:+fqBJ4vPYo4Uu1ZE4d+bUtTLRXfdSL3NvCZIZ9GHv58=
github.com/distribution/distribution/v3 v3.0.0-20230719040215-46b3d6201649 h1:JYTx15GvES2j5aMazxCO3znn7YsLDxDaBGsx7d/bSD8=
github.com/distribution/distribution/v3 v3.0.0-20230719040215-46b3d6201649/go.mod h1:+fqBJ4vPYo4Uu1ZE4d+bUtTLRXfdSL3NvCZIZ9GHv58=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/docker/cli v24.0.4+incompatible h1:Y3bYF9ekNTm2VFz5U/0BlMdJy73D+Y1iAAZ8l63Ydzw=
Expand Down Expand Up @@ -564,8 +564,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo-contrib v0.15.0 h1:9K+oRU265y4Mu9zpRDv3X+DGTqUALY6oRHCSZZKCRVU=
github.com/labstack/echo-contrib v0.15.0/go.mod h1:lei+qt5CLB4oa7VHTE0yEfQSEB9XTJI1LUqko9UWvo4=
github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M=
github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
Expand Down
3 changes: 2 additions & 1 deletion pkg/dal/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func main() {
})

g.ApplyBasic(
models.User{},
models.Audit{},
models.Namespace{},
models.Repository{},
models.Artifact{},
Expand All @@ -36,7 +38,6 @@ func main() {
models.Tag{},
models.Blob{},
models.BlobUpload{},
models.User{},
models.CasbinRule{},
)

Expand Down
94 changes: 94 additions & 0 deletions pkg/dal/dao/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dao

import (
"context"
"time"

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/types/enums"
)

//go:generate mockgen -destination=mocks/audit.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao AuditService
//go:generate mockgen -destination=mocks/audit_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao AuditServiceFactory

// AuditService is the interface that provides methods to operate on Audit model
type AuditService interface {
// Create creates a new Audit record in the database
Create(ctx context.Context, audit *models.Audit) error
// HotNamespace get top n hot namespace by user id
HotNamespace(ctx context.Context, userID int64, top int) ([]*models.Namespace, error)
}

type auditService struct {
tx *query.Query
}

// AuditServiceFactory is the interface that provides the audit service factory methods.
type AuditServiceFactory interface {
New(txs ...*query.Query) AuditService
}

type auditServiceFactory struct{}

// NewAuditServiceFactory creates a new audit service factory.
func NewAuditServiceFactory() AuditServiceFactory {
return &auditServiceFactory{}
}

func (f *auditServiceFactory) New(txs ...*query.Query) AuditService {
tx := query.Q
if len(txs) > 0 {
tx = txs[0]
}
return &auditService{
tx: tx,
}
}

// Create create a new artifact if conflict do nothing.
func (s *auditService) Create(ctx context.Context, audit *models.Audit) error {
return s.tx.Audit.WithContext(ctx).Create(audit)
}

// HotNamespace get top n hot namespace by user id
func (s *auditService) HotNamespace(ctx context.Context, userID int64, top int) ([]*models.Namespace, error) {
type result struct {
NamespaceID int64
CreatedAt time.Time
Count int64
}
var rs []result
err := s.tx.Audit.WithContext(ctx).
Where(s.tx.Audit.Action.Neq(enums.AuditActionDelete), s.tx.Audit.UserID.Eq(userID)).
Group(s.tx.Audit.NamespaceID).
Select(s.tx.Audit.NamespaceID, s.tx.Audit.CreatedAt.Max().As(s.tx.Audit.CreatedAt.ColumnName().String()), s.tx.Audit.ID.Count().As("count")).
Limit(top).
UnderlyingDB().
Order("count desc, created_at desc").Find(&rs).Error
if err != nil {
return nil, err
}
if len(rs) == 0 {
return nil, nil
}
var namespaceIDs = make([]int64, 0, len(rs))
for _, audit := range rs {
namespaceIDs = append(namespaceIDs, audit.NamespaceID)
}
return s.tx.Namespace.WithContext(ctx).Where(s.tx.Namespace.ID.In(namespaceIDs...)).Find()
}
50 changes: 50 additions & 0 deletions pkg/dal/dao/mocks/audit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions pkg/dal/dao/mocks/audit_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/dal/dao/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func (s *namespaceService) ListNamespace(ctx context.Context, name *string, pagi
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
query.Order(field.Desc())
query = query.Order(field.Desc())
case enums.SortMethodAsc:
query.Order(field)
query = query.Order(field)
default:
query.Order(s.tx.Namespace.UpdatedAt.Desc())
query = query.Order(s.tx.Namespace.UpdatedAt.Desc())
}
} else {
query.Order(s.tx.Namespace.UpdatedAt.Desc())
query = query.Order(s.tx.Namespace.UpdatedAt.Desc())
}
return query.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dal/dao/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ func (s *repositoryService) ListRepository(ctx context.Context, namespaceID int6
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
query.Order(field.Desc())
query = query.Order(field.Desc())
case enums.SortMethodAsc:
query.Order(field)
query = query.Order(field)
default:
query.Order(s.tx.Repository.UpdatedAt.Desc())
query = query.Order(s.tx.Repository.UpdatedAt.Desc())
}
} else {
query.Order(s.tx.Repository.UpdatedAt.Desc())
query = query.Order(s.tx.Repository.UpdatedAt.Desc())
}
return query.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ CREATE TABLE IF NOT EXISTS `namespaces` (
CONSTRAINT `namespaces_unique_with_name` UNIQUE (`name`, `deleted_at`)
);

CREATE TABLE IF NOT EXISTS `audits` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`user_id` bigint NOT NULL,
`namespace_id` bigint NOT NULL,
`action` ENUM ('create', 'update', 'delete', 'pull', 'push') NOT NULL,
`resource_type` ENUM ('namespace', 'repository', 'tag') NOT NULL,
`resource` varchar(256) NOT NULL,
`created_at` timestamp NOT NULL,
`updated_at` timestamp NOT NULL,
`deleted_at` bigint NOT NULL DEFAULT 0,
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
FOREIGN KEY (`namespace_id`) REFERENCES `namespaces` (`id`)
);

CREATE TABLE IF NOT EXISTS `repositories` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`name` varchar(64) NOT NULL,
Expand Down
28 changes: 28 additions & 0 deletions pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ CREATE TABLE IF NOT EXISTS "namespaces" (
CONSTRAINT "namespaces_unique_with_name" UNIQUE ("name", "deleted_at")
);

CREATE TYPE audit_action AS ENUM (
'create',
'update',
'delete',
'pull',
'push'
);

CREATE TYPE audit_resource_type AS ENUM (
'namespace',
'repository',
'tag'
);

CREATE TABLE IF NOT EXISTS "audits" (
"id" bigserial PRIMARY KEY,
"user_id" bigint NOT NULL,
"namespace_id" bigint NOT NULL,
"action" audit_action NOT NULL,
"resource_type" audit_resource_type NOT NULL,
"resource" varchar(256) NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
"deleted_at" bigint NOT NULL DEFAULT 0,
FOREIGN KEY ("user_id") REFERENCES "users" ("id"),
FOREIGN KEY ("namespace_id") REFERENCES "namespaces" ("id")
);

CREATE TABLE IF NOT EXISTS "repositories" (
"id" bigserial PRIMARY KEY,
"name" varchar(64) NOT NULL,
Expand Down
14 changes: 14 additions & 0 deletions pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ CREATE TABLE IF NOT EXISTS `namespaces` (
CONSTRAINT `namespaces_unique_with_name` UNIQUE (`name`, `deleted_at`)
);

CREATE TABLE IF NOT EXISTS `audits` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`user_id` bigint NOT NULL,
`namespace_id` bigint NOT NULL,
`action` text CHECK (`action` IN ('create', 'update', 'delete', 'pull', 'push')) NOT NULL,
`resource_type` text CHECK (`resource_type` IN ('namespace', 'repository', 'tag')) NOT NULL,
`resource` varchar(256) NOT NULL,
`created_at` timestamp NOT NULL,
`updated_at` timestamp NOT NULL,
`deleted_at` bigint NOT NULL DEFAULT 0,
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
FOREIGN KEY (`namespace_id`) REFERENCES `namespaces` (`id`)
);

CREATE TABLE IF NOT EXISTS `repositories` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`name` varchar(64) NOT NULL,
Expand Down
Loading

0 comments on commit ff3e622

Please sign in to comment.