-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
287 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,60 @@ | ||
package audit | ||
|
||
import ( | ||
"github.com/openinfradev/tks-api/internal/repository" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
|
||
internalApi "github.com/openinfradev/tks-api/internal/delivery/api" | ||
"github.com/openinfradev/tks-api/internal/middleware/logging" | ||
"github.com/openinfradev/tks-api/internal/repository" | ||
"github.com/openinfradev/tks-api/pkg/log" | ||
) | ||
|
||
type Interface interface { | ||
WithAudit(handler http.Handler) http.Handler | ||
WithAudit(endpoint internalApi.Endpoint, handler http.Handler) http.Handler | ||
} | ||
|
||
type defaultAudit struct { | ||
repo repository.Repository | ||
repo repository.IAuditRepository | ||
} | ||
|
||
func NewDefaultAudit(repo repository.Repository) *defaultAudit { | ||
return &defaultAudit{ | ||
repo: repo, | ||
repo: repo.Audit, | ||
} | ||
} | ||
|
||
// TODO: implement audit logic | ||
func (a *defaultAudit) WithAudit(handler http.Handler) http.Handler { | ||
func (a *defaultAudit) WithAudit(endpoint internalApi.Endpoint, handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// TODO: implement audit logic | ||
ctx := r.Context() | ||
|
||
log.InfoWithContext(ctx, endpoint) | ||
|
||
GetIpAddress(w, r) | ||
|
||
lrw := logging.NewLoggingResponseWriter(w) | ||
handler.ServeHTTP(lrw, r) | ||
|
||
handler.ServeHTTP(w, r) | ||
statusCode := lrw.GetStatusCode() | ||
|
||
log.Infof("%v", endpoint) | ||
log.Infof("%v", internalApi.CreateStack) | ||
|
||
// check & matching | ||
if statusCode >= 200 && statusCode < 300 { | ||
if endpoint == internalApi.CreateStack { | ||
log.Info("스택을 생성하였습니다.") | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func GetIpAddress(w http.ResponseWriter, r *http.Request) { | ||
clientAddr, _, err := net.SplitHostPort(r.RemoteAddr) | ||
log.Info(err) | ||
log.Info(clientAddr) | ||
|
||
xforward := r.Header.Get("X-Forwarded-For") | ||
fmt.Println("X-Forwarded-For : ", xforward) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package logging | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"github.com/openinfradev/tks-api/internal" | ||
"github.com/openinfradev/tks-api/pkg/log" | ||
) | ||
|
||
func LoggingMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
r = r.WithContext(context.WithValue(ctx, internal.ContextKeyRequestID, uuid.New().String())) | ||
|
||
log.InfoWithContext(r.Context(), fmt.Sprintf("***** START [%s %s] ***** ", r.Method, r.RequestURI)) | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err == nil { | ||
log.InfoWithContext(r.Context(), fmt.Sprintf("REQUEST BODY : %s", bytes.NewBuffer(body).String())) | ||
} | ||
r.Body = io.NopCloser(bytes.NewBuffer(body)) | ||
lrw := NewLoggingResponseWriter(w) | ||
|
||
next.ServeHTTP(lrw, r) | ||
|
||
statusCode := lrw.GetStatusCode() | ||
log.InfofWithContext(r.Context(), "[API_RESPONSE] [%d][%s][%s]", statusCode, http.StatusText(statusCode), lrw.GetBody().String()) | ||
log.InfofWithContext(r.Context(), "***** END [%s %s] *****", r.Method, r.RequestURI) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package logging | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
) | ||
|
||
type loggingResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
body bytes.Buffer | ||
} | ||
|
||
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter { | ||
var buf bytes.Buffer | ||
return &loggingResponseWriter{w, http.StatusOK, buf} | ||
} | ||
|
||
func (lrw *loggingResponseWriter) WriteHeader(code int) { | ||
lrw.statusCode = code | ||
lrw.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
func (lrw *loggingResponseWriter) Write(buf []byte) (int, error) { | ||
lrw.body.Write(buf) | ||
return lrw.ResponseWriter.Write(buf) | ||
} | ||
|
||
func (lrw *loggingResponseWriter) GetBody() *bytes.Buffer { | ||
return &lrw.body | ||
} | ||
|
||
func (lrw *loggingResponseWriter) GetStatusCode() int { | ||
return lrw.statusCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package repository | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
|
||
"github.com/openinfradev/tks-api/internal/pagination" | ||
"github.com/openinfradev/tks-api/pkg/domain" | ||
) | ||
|
||
// Interfaces | ||
type IAuditRepository interface { | ||
Get(auditId uuid.UUID) (domain.Audit, error) | ||
Fetch(pg *pagination.Pagination) ([]domain.Audit, error) | ||
Create(dto domain.Audit) (auditId uuid.UUID, err error) | ||
Delete(dto domain.Audit) (err error) | ||
} | ||
|
||
type AuditRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewAuditRepository(db *gorm.DB) IAuditRepository { | ||
return &AuditRepository{ | ||
db: db, | ||
} | ||
} | ||
|
||
// Models | ||
type Audit struct { | ||
gorm.Model | ||
|
||
ID uuid.UUID `gorm:"primarykey"` | ||
OrganizationId string | ||
Organization Organization `gorm:"foreignKey:OrganizationId"` | ||
Type string | ||
Message string | ||
ClientIP string | ||
UserId *uuid.UUID `gorm:"type:uuid"` | ||
User User `gorm:"foreignKey:UserId"` | ||
} | ||
|
||
func (c *Audit) BeforeCreate(tx *gorm.DB) (err error) { | ||
c.ID = uuid.New() | ||
return nil | ||
} | ||
|
||
// Logics | ||
func (r *AuditRepository) Get(auditId uuid.UUID) (out domain.Audit, err error) { | ||
return out, fmt.Errorf("to be implemented") | ||
} | ||
|
||
func (r *AuditRepository) Fetch(pg *pagination.Pagination) (out []domain.Audit, err error) { | ||
return out, fmt.Errorf("to be implemented") | ||
} | ||
|
||
func (r *AuditRepository) Create(dto domain.Audit) (auditId uuid.UUID, err error) { | ||
audit := Audit{ | ||
OrganizationId: dto.OrganizationId, | ||
Type: dto.Type, | ||
Message: dto.Message, | ||
ClientIP: dto.ClientIP, | ||
UserId: dto.UserId} | ||
res := r.db.Create(&audit) | ||
if res.Error != nil { | ||
return uuid.Nil, res.Error | ||
} | ||
return audit.ID, nil | ||
} | ||
|
||
func (r *AuditRepository) Delete(dto domain.Audit) (err error) { | ||
return fmt.Errorf("to be implemented") | ||
} | ||
|
||
/* | ||
func reflectAudit(audit Audit) (out domain.Audit) { | ||
if err := serializer.Map(audit.Model, &out); err != nil { | ||
log.Error(err) | ||
} | ||
if err := serializer.Map(audit, &out); err != nil { | ||
log.Error(err) | ||
} | ||
return | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.