worker id required for logging endpoint

This commit is contained in:
simon987 2019-01-29 20:12:59 -05:00
parent 3a88642c5c
commit 58f20aa33d
8 changed files with 356 additions and 254 deletions

View File

@ -54,11 +54,15 @@ func (api *WebAPI) ReceiveGitWebHook(r *Request) {
} }
payload := &GitPayload{} payload := &GitPayload{}
if r.GetJson(payload) { err := json.Unmarshal(r.Ctx.Request.Body(), payload)
if err != nil {
r.Ctx.SetStatusCode(400)
return
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"payload": payload, "payload": payload,
}).Info("Received git WebHook") }).Info("Received git WebHook")
}
if !isProductionBranch(payload) { if !isProductionBranch(payload) {
return return
@ -72,7 +76,7 @@ func (api *WebAPI) ReceiveGitWebHook(r *Request) {
version := getVersion(payload) version := getVersion(payload)
project.Version = version project.Version = version
err := api.Database.UpdateProject(project) err = api.Database.UpdateProject(project)
handleErr(err, r) handleErr(err, r)
} }

View File

@ -37,11 +37,3 @@ func (r *Request) Json(object interface{}, code int) {
} }
} }
func (r *Request) GetJson(x interface{}) bool {
err := json.Unmarshal(r.Ctx.Request.Body(), x)
handleErr(err, r)
return err == nil
}

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/json"
"errors" "errors"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -20,6 +21,7 @@ type LogRequest struct {
Scope string `json:"scope"` Scope string `json:"scope"`
Message string `json:"Message"` Message string `json:"Message"`
TimeStamp int64 `json:"timestamp"` TimeStamp int64 `json:"timestamp"`
worker *storage.Worker
} }
type GetLogResponse struct { type GetLogResponse struct {
@ -52,63 +54,112 @@ func (api *WebAPI) SetupLogger() {
api.Database.SetupLoggerHook() api.Database.SetupLoggerHook()
} }
func parseLogEntry(r *Request) *LogRequest { func (api *WebAPI) parseLogEntry(r *Request) (*LogRequest, error) {
worker, err := api.validateSignature(r)
if err != nil {
return nil, err
}
entry := LogRequest{} entry := LogRequest{}
if r.GetJson(&entry) { err = json.Unmarshal(r.Ctx.Request.Body(), &entry)
if len(entry.Message) == 0 { if err != nil {
handleErr(errors.New("invalid message"), r) return nil, err
} else if len(entry.Scope) == 0 {
handleErr(errors.New("invalid scope"), r)
} else if entry.TimeStamp <= 0 {
handleErr(errors.New("invalid timestamp"), r)
}
} }
return &entry if len(entry.Message) == 0 {
return nil, errors.New("invalid message")
} else if len(entry.Scope) == 0 {
return nil, errors.New("invalid scope")
} else if entry.TimeStamp <= 0 {
return nil, errors.New("invalid timestamp")
}
entry.worker = worker
return &entry, nil
} }
func LogTrace(r *Request) { func (api *WebAPI) LogTrace(r *Request) {
entry := parseLogEntry(r) entry, err := api.parseLogEntry(r)
if err != nil {
r.Json(GetLogResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"scope": entry.Scope, "scope": entry.Scope,
"worker": entry.worker.Id,
}).WithTime(entry.Time()).Trace(entry.Message) }).WithTime(entry.Time()).Trace(entry.Message)
} }
func LogInfo(r *Request) { func (api *WebAPI) LogInfo(r *Request) {
entry := parseLogEntry(r) entry, err := api.parseLogEntry(r)
if err != nil {
r.Json(GetLogResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"scope": entry.Scope, "scope": entry.Scope,
"worker": entry.worker.Id,
}).WithTime(entry.Time()).Info(entry.Message) }).WithTime(entry.Time()).Info(entry.Message)
} }
func LogWarn(r *Request) { func (api *WebAPI) LogWarn(r *Request) {
entry := parseLogEntry(r) entry, err := api.parseLogEntry(r)
if err != nil {
r.Json(GetLogResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"scope": entry.Scope, "scope": entry.Scope,
"worker": entry.worker.Id,
}).WithTime(entry.Time()).Warn(entry.Message) }).WithTime(entry.Time()).Warn(entry.Message)
} }
func LogError(r *Request) { func (api *WebAPI) LogError(r *Request) {
entry := parseLogEntry(r) entry, err := api.parseLogEntry(r)
if err != nil {
r.Json(GetLogResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"scope": entry.Scope, "scope": entry.Scope,
"worker": entry.worker.Id,
}).WithTime(entry.Time()).Error(entry.Message) }).WithTime(entry.Time()).Error(entry.Message)
} }
func (api *WebAPI) GetLog(r *Request) { func (api *WebAPI) GetLog(r *Request) {
req := &GetLogRequest{} req := &GetLogRequest{}
if r.GetJson(req) { err := json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(GetLogResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
if req.isValid() { if req.isValid() {
logs := api.Database.GetLogs(req.Since, req.Level) logs := api.Database.GetLogs(req.Since, req.Level)
@ -132,7 +183,6 @@ func (api *WebAPI) GetLog(r *Request) {
Message: "Invalid log request", Message: "Invalid log request",
}, 400) }, 400)
} }
}
} }
func (r GetLogRequest) isValid() bool { func (r GetLogRequest) isValid() bool {

View File

@ -43,10 +43,10 @@ func New() *WebAPI {
api.router.GET("/", LogRequestMiddleware(Index)) api.router.GET("/", LogRequestMiddleware(Index))
api.router.POST("/log/trace", LogRequestMiddleware(LogTrace)) api.router.POST("/log/trace", LogRequestMiddleware(api.LogTrace))
api.router.POST("/log/info", LogRequestMiddleware(LogInfo)) api.router.POST("/log/info", LogRequestMiddleware(api.LogInfo))
api.router.POST("/log/warn", LogRequestMiddleware(LogWarn)) api.router.POST("/log/warn", LogRequestMiddleware(api.LogWarn))
api.router.POST("/log/error", LogRequestMiddleware(LogError)) api.router.POST("/log/error", LogRequestMiddleware(api.LogError))
api.router.POST("/worker/create", LogRequestMiddleware(api.WorkerCreate)) api.router.POST("/worker/create", LogRequestMiddleware(api.WorkerCreate))
api.router.POST("/worker/update", LogRequestMiddleware(api.WorkerUpdate)) api.router.POST("/worker/update", LogRequestMiddleware(api.WorkerUpdate))

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/json"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"src/task_tracker/storage" "src/task_tracker/storage"
"strconv" "strconv"
@ -57,8 +58,14 @@ type GetAllProjectsStatsResponse struct {
func (api *WebAPI) ProjectCreate(r *Request) { func (api *WebAPI) ProjectCreate(r *Request) {
createReq := &CreateProjectRequest{} createReq := &CreateProjectRequest{}
if r.GetJson(createReq) { err := json.Unmarshal(r.Ctx.Request.Body(), createReq)
if err != nil {
r.Json(CreateProjectResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
project := &storage.Project{ project := &storage.Project{
Name: createReq.Name, Name: createReq.Name,
Version: createReq.Version, Version: createReq.Version,
@ -96,18 +103,28 @@ func (api *WebAPI) ProjectCreate(r *Request) {
Message: "Invalid project", Message: "Invalid project",
}, 400) }, 400)
} }
}
} }
func (api *WebAPI) ProjectUpdate(r *Request) { func (api *WebAPI) ProjectUpdate(r *Request) {
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64) id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
handleErr(err, r) //todo handle invalid id if err != nil || id <= 0 {
r.Json(CreateProjectResponse{
Ok: false,
Message: "Invalid project id",
}, 400)
return
}
updateReq := &UpdateProjectRequest{} updateReq := &UpdateProjectRequest{}
if r.GetJson(updateReq) { err = json.Unmarshal(r.Ctx.Request.Body(), updateReq)
if err != nil {
r.Json(CreateProjectResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
project := &storage.Project{ project := &storage.Project{
Id: id, Id: id,
Name: updateReq.Name, Name: updateReq.Name,
@ -150,7 +167,6 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
Message: "Invalid project", Message: "Invalid project",
}, 400) }, 400)
} }
}
} }
func isValidProject(project *storage.Project) bool { func isValidProject(project *storage.Project) bool {
@ -187,7 +203,13 @@ func (api *WebAPI) ProjectGet(r *Request) {
func (api *WebAPI) ProjectGetStats(r *Request) { func (api *WebAPI) ProjectGetStats(r *Request) {
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64) id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
handleErr(err, r) if err != nil {
r.Json(GetProjectStatsResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
stats := api.Database.GetProjectStats(id) stats := api.Database.GetProjectStats(id)

View File

@ -5,6 +5,7 @@ import (
"crypto" "crypto"
"crypto/hmac" "crypto/hmac"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/dchest/siphash" "github.com/dchest/siphash"
@ -45,9 +46,15 @@ type GetTaskResponse struct {
func (api *WebAPI) TaskCreate(r *Request) { func (api *WebAPI) TaskCreate(r *Request) {
var createReq CreateTaskRequest createReq := &CreateTaskRequest{}
if r.GetJson(&createReq) { err := json.Unmarshal(r.Ctx.Request.Body(), createReq)
if err != nil {
r.Json(CreateProjectResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
task := &storage.Task{ task := &storage.Task{
MaxRetries: createReq.MaxRetries, MaxRetries: createReq.MaxRetries,
Recipe: createReq.Recipe, Recipe: createReq.Recipe,
@ -84,7 +91,6 @@ func (api *WebAPI) TaskCreate(r *Request) {
Message: "Invalid task", Message: "Invalid task",
}, 400) }, 400)
} }
}
} }
func (req *CreateTaskRequest) IsValid() bool { func (req *CreateTaskRequest) IsValid() bool {
@ -215,9 +221,14 @@ func (api *WebAPI) TaskRelease(r *Request) {
return return
} }
var req ReleaseTaskRequest req := &ReleaseTaskRequest{}
if r.GetJson(&req) { err = json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(CreateProjectResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
}
res := api.Database.ReleaseTask(req.TaskId, worker.Id, req.Success) res := api.Database.ReleaseTask(req.TaskId, worker.Id, req.Success)
response := ReleaseTaskResponse{ response := ReleaseTaskResponse{
@ -240,5 +251,4 @@ func (api *WebAPI) TaskRelease(r *Request) {
} }
r.OkJson(response) r.OkJson(response)
}
} }

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/json"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"math/rand" "math/rand"
"src/task_tracker/storage" "src/task_tracker/storage"
@ -46,12 +47,12 @@ type WorkerAccessResponse struct {
func (api *WebAPI) WorkerCreate(r *Request) { func (api *WebAPI) WorkerCreate(r *Request) {
workerReq := &CreateWorkerRequest{} workerReq := &CreateWorkerRequest{}
if !r.GetJson(workerReq) { err := json.Unmarshal(r.Ctx.Request.Body(), workerReq)
if err != nil {
return return
} }
identity := getIdentity(r) identity := getIdentity(r)
if !canCreateWorker(r, workerReq, identity) { if !canCreateWorker(r, workerReq, identity) {
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
@ -123,7 +124,14 @@ func (api *WebAPI) WorkerGet(r *Request) {
func (api *WebAPI) WorkerGrantAccess(r *Request) { func (api *WebAPI) WorkerGrantAccess(r *Request) {
req := &WorkerAccessRequest{} req := &WorkerAccessRequest{}
if r.GetJson(req) { err := json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(GetTaskResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
ok := api.Database.GrantAccess(req.WorkerId, req.ProjectId) ok := api.Database.GrantAccess(req.WorkerId, req.ProjectId)
@ -137,14 +145,19 @@ func (api *WebAPI) WorkerGrantAccess(r *Request) {
Message: "Worker already has access to this project", Message: "Worker already has access to this project",
}) })
} }
}
} }
func (api *WebAPI) WorkerRemoveAccess(r *Request) { func (api *WebAPI) WorkerRemoveAccess(r *Request) {
req := &WorkerAccessRequest{} req := &WorkerAccessRequest{}
if r.GetJson(req) { err := json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(GetTaskResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
ok := api.Database.RemoveAccess(req.WorkerId, req.ProjectId) ok := api.Database.RemoveAccess(req.WorkerId, req.ProjectId)
if ok { if ok {
@ -157,7 +170,6 @@ func (api *WebAPI) WorkerRemoveAccess(r *Request) {
Message: "Worker did not have access to this project", Message: "Worker did not have access to this project",
}) })
} }
}
} }
func (api *WebAPI) WorkerUpdate(r *Request) { func (api *WebAPI) WorkerUpdate(r *Request) {
@ -172,8 +184,14 @@ func (api *WebAPI) WorkerUpdate(r *Request) {
} }
req := &UpdateWorkerRequest{} req := &UpdateWorkerRequest{}
if r.GetJson(req) { err = json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(GetTaskResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
worker.Alias = req.Alias worker.Alias = req.Alias
ok := api.Database.UpdateWorker(worker) ok := api.Database.UpdateWorker(worker)
@ -188,7 +206,6 @@ func (api *WebAPI) WorkerUpdate(r *Request) {
Message: "Could not update worker", Message: "Could not update worker",
}) })
} }
}
} }
func (api *WebAPI) workerCreate(request *CreateWorkerRequest, identity *storage.Identity) (*storage.Worker, error) { func (api *WebAPI) workerCreate(request *CreateWorkerRequest, identity *storage.Identity) (*storage.Worker, error) {

View File

@ -12,11 +12,12 @@ import (
func TestTraceValid(t *testing.T) { func TestTraceValid(t *testing.T) {
w := genWid()
r := Post("/log/trace", api.LogRequest{ r := Post("/log/trace", api.LogRequest{
Scope: "test", Scope: "test",
Message: "This is a test message", Message: "This is a test message",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 200 { if r.StatusCode != 200 {
t.Fail() t.Fail()
@ -24,64 +25,68 @@ func TestTraceValid(t *testing.T) {
} }
func TestTraceInvalidScope(t *testing.T) { func TestTraceInvalidScope(t *testing.T) {
w := genWid()
r := Post("/log/trace", api.LogRequest{ r := Post("/log/trace", api.LogRequest{
Message: "this is a test message", Message: "this is a test message",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 500 { if r.StatusCode == 200 {
t.Fail() t.Error()
} }
r = Post("/log/trace", api.LogRequest{ r = Post("/log/trace", api.LogRequest{
Scope: "", Scope: "",
Message: "this is a test message", Message: "this is a test message",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 500 { if r.StatusCode == 200 {
t.Fail() t.Error()
} }
if GenericJson(r.Body)["message"] != "invalid scope" { if len(GenericJson(r.Body)["message"].(string)) <= 0 {
t.Fail() t.Error()
} }
} }
func TestTraceInvalidMessage(t *testing.T) { func TestTraceInvalidMessage(t *testing.T) {
w := genWid()
r := Post("/log/trace", api.LogRequest{ r := Post("/log/trace", api.LogRequest{
Scope: "test", Scope: "test",
Message: "", Message: "",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 500 { if r.StatusCode == 200 {
t.Fail() t.Error()
} }
if GenericJson(r.Body)["message"] != "invalid message" { if len(GenericJson(r.Body)["message"].(string)) <= 0 {
t.Fail() t.Error()
} }
} }
func TestTraceInvalidTime(t *testing.T) { func TestTraceInvalidTime(t *testing.T) {
w := genWid()
r := Post("/log/trace", api.LogRequest{ r := Post("/log/trace", api.LogRequest{
Scope: "test", Scope: "test",
Message: "test", Message: "test",
}, nil) }, w)
if r.StatusCode != 500 { if r.StatusCode == 200 {
t.Fail() t.Error()
} }
if GenericJson(r.Body)["message"] != "invalid timestamp" { if len(GenericJson(r.Body)["message"].(string)) <= 0 {
t.Fail() t.Error()
} }
} }
func TestWarnValid(t *testing.T) { func TestWarnValid(t *testing.T) {
w := genWid()
r := Post("/log/warn", api.LogRequest{ r := Post("/log/warn", api.LogRequest{
Scope: "test", Scope: "test",
Message: "test", Message: "test",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 200 { if r.StatusCode != 200 {
t.Fail() t.Fail()
} }
@ -89,11 +94,12 @@ func TestWarnValid(t *testing.T) {
func TestInfoValid(t *testing.T) { func TestInfoValid(t *testing.T) {
w := genWid()
r := Post("/log/info", api.LogRequest{ r := Post("/log/info", api.LogRequest{
Scope: "test", Scope: "test",
Message: "test", Message: "test",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 200 { if r.StatusCode != 200 {
t.Fail() t.Fail()
} }
@ -101,11 +107,12 @@ func TestInfoValid(t *testing.T) {
func TestErrorValid(t *testing.T) { func TestErrorValid(t *testing.T) {
w := genWid()
r := Post("/log/error", api.LogRequest{ r := Post("/log/error", api.LogRequest{
Scope: "test", Scope: "test",
Message: "test", Message: "test",
TimeStamp: time.Now().Unix(), TimeStamp: time.Now().Unix(),
}, nil) }, w)
if r.StatusCode != 200 { if r.StatusCode != 200 {
t.Fail() t.Fail()
} }