Skip to content
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

feature. change audit scheme and remove database constraint #431

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10974,17 +10974,23 @@ const docTemplate = `{
"message": {
"type": "string"
},
"organization": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse"
},
"organizationId": {
"type": "string"
},
"updatedAt": {
"type": "string"
},
"user": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles"
"userAccountId": {
"type": "string"
},
"userId": {
"type": "string"
},
"userName": {
"type": "string"
},
"userRoles": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -14849,32 +14855,6 @@ const docTemplate = `{
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles": {
"type": "object",
"properties": {
"accountId": {
"type": "string"
},
"department": {
"type": "string"
},
"email": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"roles": {
"type": "array",
"items": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleRoleResponse"
}
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.StackConfResponse": {
"type": "object",
"required": [
Expand Down
42 changes: 11 additions & 31 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -10968,17 +10968,23 @@
"message": {
"type": "string"
},
"organization": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse"
},
"organizationId": {
"type": "string"
},
"updatedAt": {
"type": "string"
},
"user": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles"
"userAccountId": {
"type": "string"
},
"userId": {
"type": "string"
},
"userName": {
"type": "string"
},
"userRoles": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -14843,32 +14849,6 @@
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles": {
"type": "object",
"properties": {
"accountId": {
"type": "string"
},
"department": {
"type": "string"
},
"email": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"roles": {
"type": "array",
"items": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleRoleResponse"
}
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.StackConfResponse": {
"type": "object",
"required": [
Expand Down
29 changes: 8 additions & 21 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,18 @@ definitions:
type: string
message:
type: string
organization:
$ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse'
organizationId:
type: string
updatedAt:
type: string
user:
$ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles'
userAccountId:
type: string
userId:
type: string
userName:
type: string
userRoles:
type: string
type: object
github_com_openinfradev_tks-api_pkg_domain.Axis:
properties:
Expand Down Expand Up @@ -3072,23 +3076,6 @@ definitions:
name:
type: string
type: object
github_com_openinfradev_tks-api_pkg_domain.SimpleUserResponseWithRoles:
properties:
accountId:
type: string
department:
type: string
email:
type: string
id:
type: string
name:
type: string
roles:
items:
$ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleRoleResponse'
type: array
type: object
github_com_openinfradev_tks-api_pkg_domain.StackConfResponse:
properties:
tksCpNode:
Expand Down
6 changes: 0 additions & 6 deletions internal/delivery/http/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ func (h *AuditHandler) GetAudits(w http.ResponseWriter, r *http.Request) {
if err := serializer.Map(r.Context(), audit, &out.Audits[i]); err != nil {
log.Info(r.Context(), err)
}
out.Audits[i].User.Roles = make([]domain.SimpleRoleResponse, len(audit.User.Roles))
for j, role := range audit.User.Roles {
if err := serializer.Map(r.Context(), role, &out.Audits[i].User.Roles[j]); err != nil {
log.Info(r.Context(), err)
}
}
}

if out.Pagination, err = pg.Response(r.Context()); err != nil {
Expand Down
25 changes: 22 additions & 3 deletions internal/middleware/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ type Interface interface {
}

type defaultAudit struct {
repo repository.IAuditRepository
repo repository.IAuditRepository
userRepo repository.IUserRepository
}

func NewDefaultAudit(repo repository.Repository) *defaultAudit {
return &defaultAudit{
repo: repo.Audit,
repo: repo.Audit,
userRepo: repo.User,
}
}

Expand Down Expand Up @@ -59,13 +61,30 @@ func (a *defaultAudit) WithAudit(endpoint internalApi.Endpoint, handler http.Han
message, description = fn(r.Context(), lrw.GetBody().Bytes(), body, statusCode)
r.Body = io.NopCloser(bytes.NewBuffer(body))

u, err := a.userRepo.GetByUuid(r.Context(), userId)
if err != nil {
log.Error(r.Context(), err)
return
}

userRoles := ""
for i, role := range u.Roles {
if i > 0 {
userRoles = userRoles + ","
}
userRoles = userRoles + role.Name
}

dto := model.Audit{
OrganizationId: organizationId,
Group: internalApi.ApiMap[endpoint].Group,
Message: message,
Description: description,
ClientIP: GetClientIpAddress(w, r),
UserId: &userId,
UserId: &u.ID,
UserAccountId: u.AccountId,
UserName: u.Name,
UserRoles: userRoles,
}
if _, err := a.repo.Create(r.Context(), dto); err != nil {
log.Error(r.Context(), err)
Expand Down
6 changes: 4 additions & 2 deletions internal/model/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ type Audit struct {

ID uuid.UUID `gorm:"primarykey"`
OrganizationId string
Organization Organization `gorm:"foreignKey:OrganizationId"`
Organization Organization
Group string
Message string
Description string
ClientIP string
UserId *uuid.UUID `gorm:"type:uuid"`
User User `gorm:"foreignKey:UserId"`
UserAccountId string
UserName string
UserRoles string
}
3 changes: 2 additions & 1 deletion internal/model/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package model

import (
"gorm.io/gorm"
"time"

"gorm.io/gorm"

"github.com/google/uuid"
)

Expand Down
12 changes: 7 additions & 5 deletions internal/repository/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"

"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -32,7 +31,7 @@ func NewAuditRepository(db *gorm.DB) IAuditRepository {

// Logics
func (r *AuditRepository) Get(ctx context.Context, auditId uuid.UUID) (out model.Audit, err error) {
res := r.db.WithContext(ctx).Preload(clause.Associations).Preload("User.Roles").First(&out, "id = ?", auditId)
res := r.db.WithContext(ctx).First(&out, "id = ?", auditId)
if res.Error != nil {
return
}
Expand All @@ -44,8 +43,7 @@ func (r *AuditRepository) Fetch(ctx context.Context, pg *pagination.Pagination)
pg = pagination.NewPagination(nil)
}

db := r.db.WithContext(ctx).Model(&model.Audit{}).Preload(clause.Associations).
Preload("User.Roles")
db := r.db.WithContext(ctx).Model(&model.Audit{})

_, res := pg.Fetch(db, &out)
if res.Error != nil {
Expand All @@ -62,7 +60,11 @@ func (r *AuditRepository) Create(ctx context.Context, dto model.Audit) (auditId
Message: dto.Message,
Description: dto.Description,
ClientIP: dto.ClientIP,
UserId: dto.UserId}
UserId: &dto.ID,
UserAccountId: dto.UserAccountId,
UserName: dto.UserName,
UserRoles: dto.UserRoles,
}
res := r.db.WithContext(ctx).Create(&audit)
if res.Error != nil {
return uuid.Nil, res.Error
Expand Down
4 changes: 0 additions & 4 deletions internal/repository/system-notification-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
)

// Interfaces
Expand Down Expand Up @@ -125,9 +124,6 @@ func (r *SystemNotificationRuleRepository) Update(ctx context.Context, dto model
m.MessageActionProposal = dto.MessageActionProposal
m.UpdatorId = dto.UpdatorId

log.Info(ctx, "KTKFREE1 ", m.SystemNotificationCondition.EnableEmail)
log.Info(ctx, "KTKFREE2 ", m.SystemNotificationCondition.EnablePortal)

res = r.db.WithContext(ctx).Session(&gorm.Session{FullSaveAssociations: true}).Save(&m)
if res.Error != nil {
return res.Error
Expand Down
31 changes: 25 additions & 6 deletions internal/usecase/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,42 @@ type IAuditUsecase interface {
}

type AuditUsecase struct {
repo repository.IAuditRepository
repo repository.IAuditRepository
userRepo repository.IUserRepository
}

func NewAuditUsecase(r repository.Repository) IAuditUsecase {
return &AuditUsecase{
repo: r.Audit,
repo: r.Audit,
userRepo: r.User,
}
}

func (u *AuditUsecase) Create(ctx context.Context, dto model.Audit) (auditId uuid.UUID, err error) {
if dto.UserId == nil {
user, ok := request.UserFrom(ctx)
if dto.UserId == nil || *dto.UserId == uuid.Nil {
userInfo, ok := request.UserFrom(ctx)
if ok {
userId := user.GetUserId()
dto.UserId = &userId
id := userInfo.GetUserId()
dto.UserId = &id
}
}

user, err := u.userRepo.GetByUuid(ctx, *dto.UserId)
if err != nil {
return auditId, err
}

userRoles := ""
for i, role := range user.Roles {
if i > 0 {
userRoles = userRoles + ","
}
userRoles = userRoles + role.Name
}
dto.UserAccountId = user.AccountId
dto.UserName = user.Name
dto.UserRoles = userRoles

auditId, err = u.repo.Create(ctx, dto)
if err != nil {
return uuid.Nil, httpErrors.NewInternalServerError(err, "", "")
Expand Down
22 changes: 12 additions & 10 deletions pkg/domain/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (
)

type AuditResponse struct {
ID string `json:"id"`
OrganizationId string `json:"organizationId"`
Organization SimpleOrganizationResponse `json:"organization"`
Description string `json:"description"`
Group string `json:"group"`
Message string `json:"message"`
ClientIP string `json:"clientIP"`
User SimpleUserResponseWithRoles `json:"user"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID string `json:"id"`
OrganizationId string `json:"organizationId"`
Description string `json:"description"`
Group string `json:"group"`
Message string `json:"message"`
ClientIP string `json:"clientIP"`
UserId string `json:"userId"`
UserAccountId string `json:"userAccountId"`
UserName string `json:"userName"`
UserRoles string `json:"userRoles"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type CreateAuditRequest struct {
Expand Down
Loading