From e2f673c943a9b500daf27886289f33e57cf00bee Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 9 Nov 2022 11:07:53 +0800 Subject: [PATCH 1/5] #3157 fix bug --- routers/api/v1/repo/cloudbrain.go | 13 ++++++++++--- routers/api/v1/repo/modelarts.go | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/routers/api/v1/repo/cloudbrain.go b/routers/api/v1/repo/cloudbrain.go index 2e25fdefe..68d2923a4 100755 --- a/routers/api/v1/repo/cloudbrain.go +++ b/routers/api/v1/repo/cloudbrain.go @@ -634,7 +634,7 @@ func CloudbrainGetLog(ctx *context.APIContext) { endLine += 1 } } - + result = getLogFromModelDir(job.JobName, startLine, endLine, resultPath) if result == nil { log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"]) @@ -649,14 +649,21 @@ func CloudbrainGetLog(ctx *context.APIContext) { if ctx.Data["existStr"] != nil && result["Lines"].(int) < 50 { content = content + ctx.Data["existStr"].(string) } + logFileName := result["FileName"] + + //Logs can only be downloaded if the file exists + //and the current user is an administrator or the creator of the task + canLogDownload := logFileName != nil && logFileName != "" && + (ctx.IsSigned && (ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID)) + re := map[string]interface{}{ "JobID": ID, - "LogFileName": result["FileName"], + "LogFileName": logFileName, "StartLine": result["StartLine"], "EndLine": result["EndLine"], "Content": content, "Lines": result["Lines"], - "CanLogDownload": result["FileName"] != "", + "CanLogDownload": canLogDownload, "StartTime": job.StartTime, } //result := CloudbrainGetLogByJobId(job.JobID, job.JobName) diff --git a/routers/api/v1/repo/modelarts.go b/routers/api/v1/repo/modelarts.go index cd129dd5f..9f9e67ccc 100755 --- a/routers/api/v1/repo/modelarts.go +++ b/routers/api/v1/repo/modelarts.go @@ -281,15 +281,6 @@ func TrainJobGetLog(ctx *context.APIContext) { return } - prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, task.JobName, modelarts.LogPath, versionName), "/") + "/job" - _, err = storage.GetObsLogFileName(prefix) - var canLogDownload bool - if err != nil { - canLogDownload = false - } else { - canLogDownload = true - } - ctx.Data["log_file_name"] = resultLogFile.LogFileList[0] ctx.JSON(http.StatusOK, map[string]interface{}{ @@ -299,11 +290,26 @@ func TrainJobGetLog(ctx *context.APIContext) { "EndLine": result.EndLine, "Content": result.Content, "Lines": result.Lines, - "CanLogDownload": canLogDownload, + "CanLogDownload": canLogDownload(ctx.User, task), "StartTime": task.StartTime, }) } +func canLogDownload(user *models.User, task *models.Cloudbrain) bool { + if user == nil { + return false + } + if !user.IsAdmin && user.ID != task.UserID { + return false + } + prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, task.JobName, modelarts.LogPath, task.VersionName), "/") + "/job" + _, err := storage.GetObsLogFileName(prefix) + if err != nil { + return false + } + return true +} + func trainJobGetLogContent(jobID string, versionID int64, baseLine string, order string, lines int) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) { resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(versionID, 10)) From dc30a93f943d8e7a91495ed51202ae9b015de857 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 9 Nov 2022 14:32:15 +0800 Subject: [PATCH 2/5] #3157 fix bug --- models/cloudbrain.go | 7 +++++++ routers/api/v1/repo/cloudbrain.go | 3 +-- routers/api/v1/repo/modelarts.go | 5 +---- routers/repo/grampus.go | 7 +------ 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index dacb1b03a..f536895a2 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -291,6 +291,13 @@ func (task *Cloudbrain) IsRunning() bool { status == string(JobRunning) || status == GrampusStatusRunning } +func (task *Cloudbrain) IsUserHasRight(user *User) bool { + if user == nil { + return false + } + return user.IsAdmin || user.ID == task.UserID +} + func ConvertDurationToStr(duration int64) string { if duration <= 0 { return DURATION_STR_ZERO diff --git a/routers/api/v1/repo/cloudbrain.go b/routers/api/v1/repo/cloudbrain.go index 68d2923a4..7022dc011 100755 --- a/routers/api/v1/repo/cloudbrain.go +++ b/routers/api/v1/repo/cloudbrain.go @@ -653,8 +653,7 @@ func CloudbrainGetLog(ctx *context.APIContext) { //Logs can only be downloaded if the file exists //and the current user is an administrator or the creator of the task - canLogDownload := logFileName != nil && logFileName != "" && - (ctx.IsSigned && (ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID)) + canLogDownload := logFileName != nil && logFileName != "" && job.IsUserHasRight(ctx.User) re := map[string]interface{}{ "JobID": ID, diff --git a/routers/api/v1/repo/modelarts.go b/routers/api/v1/repo/modelarts.go index 9f9e67ccc..16e4997a3 100755 --- a/routers/api/v1/repo/modelarts.go +++ b/routers/api/v1/repo/modelarts.go @@ -296,10 +296,7 @@ func TrainJobGetLog(ctx *context.APIContext) { } func canLogDownload(user *models.User, task *models.Cloudbrain) bool { - if user == nil { - return false - } - if !user.IsAdmin && user.ID != task.UserID { + if task == nil || !task.IsUserHasRight(user) { return false } prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, task.JobName, modelarts.LogPath, task.VersionName), "/") + "/job" diff --git a/routers/repo/grampus.go b/routers/repo/grampus.go index 0620350f6..e16d39a00 100755 --- a/routers/repo/grampus.go +++ b/routers/repo/grampus.go @@ -941,12 +941,7 @@ func GrampusGetLog(ctx *context.Context) { ctx.ServerError(err.Error(), err) return } - var canLogDownload bool - if err != nil { - canLogDownload = false - } else { - canLogDownload = true - } + canLogDownload := err == nil && job.IsUserHasRight(ctx.User) ctx.JSON(http.StatusOK, map[string]interface{}{ "JobName": job.JobName, "Content": content, From 3aca1dc0749ccbe1af32063a2a621fe721f6bb01 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 9 Nov 2022 15:12:54 +0800 Subject: [PATCH 3/5] #3157 fix bug --- routers/api/v1/repo/cloudbrain.go | 23 +++++++++++++++++++---- routers/api/v1/repo/modelarts.go | 22 +++++++++++++++++----- routers/repo/grampus.go | 12 ++++++++++-- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/routers/api/v1/repo/cloudbrain.go b/routers/api/v1/repo/cloudbrain.go index 7022dc011..9da1251e4 100755 --- a/routers/api/v1/repo/cloudbrain.go +++ b/routers/api/v1/repo/cloudbrain.go @@ -607,6 +607,24 @@ func CloudbrainGetLog(ctx *context.APIContext) { lines := ctx.QueryInt("lines") baseLine := ctx.Query("base_line") order := ctx.Query("order") + + //Logs can only be downloaded if the file exists + //and the current user is an administrator or the creator of the task + if !job.IsUserHasRight(ctx.User) { + re := map[string]interface{}{ + "JobID": ID, + "LogFileName": "", + "StartLine": 0, + "EndLine": 0, + "Content": "", + "Lines": 0, + "CanLogDownload": false, + "StartTime": job.StartTime, + } + ctx.JSON(http.StatusOK, re) + return + } + var result map[string]interface{} resultPath := "/model" if job.JobType == string(models.JobTypeInference) || job.JobType == string(models.JobTypeModelSafety) { @@ -650,10 +668,7 @@ func CloudbrainGetLog(ctx *context.APIContext) { content = content + ctx.Data["existStr"].(string) } logFileName := result["FileName"] - - //Logs can only be downloaded if the file exists - //and the current user is an administrator or the creator of the task - canLogDownload := logFileName != nil && logFileName != "" && job.IsUserHasRight(ctx.User) + canLogDownload := logFileName != nil && logFileName != "" re := map[string]interface{}{ "JobID": ID, diff --git a/routers/api/v1/repo/modelarts.go b/routers/api/v1/repo/modelarts.go index 16e4997a3..00cdea0ef 100755 --- a/routers/api/v1/repo/modelarts.go +++ b/routers/api/v1/repo/modelarts.go @@ -274,6 +274,21 @@ func TrainJobGetLog(ctx *context.APIContext) { log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error()) return } + + if !task.IsUserHasRight(ctx.User) { + ctx.JSON(http.StatusOK, map[string]interface{}{ + "JobID": jobID, + "LogFileName": "", + "StartLine": 0, + "EndLine": 0, + "Content": "", + "Lines": 0, + "CanLogDownload": false, + "StartTime": task.StartTime, + }) + return + } + resultLogFile, result, err := trainJobGetLogContent(jobID, task.VersionID, baseLine, order, lines_int) if err != nil { log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error()) @@ -290,15 +305,12 @@ func TrainJobGetLog(ctx *context.APIContext) { "EndLine": result.EndLine, "Content": result.Content, "Lines": result.Lines, - "CanLogDownload": canLogDownload(ctx.User, task), + "CanLogDownload": canLogDownload(task), "StartTime": task.StartTime, }) } -func canLogDownload(user *models.User, task *models.Cloudbrain) bool { - if task == nil || !task.IsUserHasRight(user) { - return false - } +func canLogDownload(task *models.Cloudbrain) bool { prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, task.JobName, modelarts.LogPath, task.VersionName), "/") + "/job" _, err := storage.GetObsLogFileName(prefix) if err != nil { diff --git a/routers/repo/grampus.go b/routers/repo/grampus.go index e16d39a00..a2a021a43 100755 --- a/routers/repo/grampus.go +++ b/routers/repo/grampus.go @@ -935,17 +935,25 @@ func GrampusGetLog(ctx *context.Context) { return } + if !job.IsUserHasRight(ctx.User) { + ctx.JSON(http.StatusOK, map[string]interface{}{ + "JobName": job.JobName, + "Content": "", + "CanLogDownload": false, + }) + return + } + content, err := grampus.GetTrainJobLog(job.JobID) if err != nil { log.Error("GetTrainJobLog failed: %v", err, ctx.Data["MsgID"]) ctx.ServerError(err.Error(), err) return } - canLogDownload := err == nil && job.IsUserHasRight(ctx.User) ctx.JSON(http.StatusOK, map[string]interface{}{ "JobName": job.JobName, "Content": content, - "CanLogDownload": canLogDownload, + "CanLogDownload": true, }) return From 208a99809fb830d1a29e0074bd713623f3c313c7 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 9 Nov 2022 15:36:01 +0800 Subject: [PATCH 4/5] Revert "#3157" This reverts commit 3aca1dc0749ccbe1af32063a2a621fe721f6bb01. --- routers/api/v1/repo/cloudbrain.go | 23 ++++------------------- routers/api/v1/repo/modelarts.go | 22 +++++----------------- routers/repo/grampus.go | 12 ++---------- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/routers/api/v1/repo/cloudbrain.go b/routers/api/v1/repo/cloudbrain.go index 9da1251e4..7022dc011 100755 --- a/routers/api/v1/repo/cloudbrain.go +++ b/routers/api/v1/repo/cloudbrain.go @@ -607,24 +607,6 @@ func CloudbrainGetLog(ctx *context.APIContext) { lines := ctx.QueryInt("lines") baseLine := ctx.Query("base_line") order := ctx.Query("order") - - //Logs can only be downloaded if the file exists - //and the current user is an administrator or the creator of the task - if !job.IsUserHasRight(ctx.User) { - re := map[string]interface{}{ - "JobID": ID, - "LogFileName": "", - "StartLine": 0, - "EndLine": 0, - "Content": "", - "Lines": 0, - "CanLogDownload": false, - "StartTime": job.StartTime, - } - ctx.JSON(http.StatusOK, re) - return - } - var result map[string]interface{} resultPath := "/model" if job.JobType == string(models.JobTypeInference) || job.JobType == string(models.JobTypeModelSafety) { @@ -668,7 +650,10 @@ func CloudbrainGetLog(ctx *context.APIContext) { content = content + ctx.Data["existStr"].(string) } logFileName := result["FileName"] - canLogDownload := logFileName != nil && logFileName != "" + + //Logs can only be downloaded if the file exists + //and the current user is an administrator or the creator of the task + canLogDownload := logFileName != nil && logFileName != "" && job.IsUserHasRight(ctx.User) re := map[string]interface{}{ "JobID": ID, diff --git a/routers/api/v1/repo/modelarts.go b/routers/api/v1/repo/modelarts.go index 00cdea0ef..16e4997a3 100755 --- a/routers/api/v1/repo/modelarts.go +++ b/routers/api/v1/repo/modelarts.go @@ -274,21 +274,6 @@ func TrainJobGetLog(ctx *context.APIContext) { log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error()) return } - - if !task.IsUserHasRight(ctx.User) { - ctx.JSON(http.StatusOK, map[string]interface{}{ - "JobID": jobID, - "LogFileName": "", - "StartLine": 0, - "EndLine": 0, - "Content": "", - "Lines": 0, - "CanLogDownload": false, - "StartTime": task.StartTime, - }) - return - } - resultLogFile, result, err := trainJobGetLogContent(jobID, task.VersionID, baseLine, order, lines_int) if err != nil { log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error()) @@ -305,12 +290,15 @@ func TrainJobGetLog(ctx *context.APIContext) { "EndLine": result.EndLine, "Content": result.Content, "Lines": result.Lines, - "CanLogDownload": canLogDownload(task), + "CanLogDownload": canLogDownload(ctx.User, task), "StartTime": task.StartTime, }) } -func canLogDownload(task *models.Cloudbrain) bool { +func canLogDownload(user *models.User, task *models.Cloudbrain) bool { + if task == nil || !task.IsUserHasRight(user) { + return false + } prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, task.JobName, modelarts.LogPath, task.VersionName), "/") + "/job" _, err := storage.GetObsLogFileName(prefix) if err != nil { diff --git a/routers/repo/grampus.go b/routers/repo/grampus.go index a2a021a43..e16d39a00 100755 --- a/routers/repo/grampus.go +++ b/routers/repo/grampus.go @@ -935,25 +935,17 @@ func GrampusGetLog(ctx *context.Context) { return } - if !job.IsUserHasRight(ctx.User) { - ctx.JSON(http.StatusOK, map[string]interface{}{ - "JobName": job.JobName, - "Content": "", - "CanLogDownload": false, - }) - return - } - content, err := grampus.GetTrainJobLog(job.JobID) if err != nil { log.Error("GetTrainJobLog failed: %v", err, ctx.Data["MsgID"]) ctx.ServerError(err.Error(), err) return } + canLogDownload := err == nil && job.IsUserHasRight(ctx.User) ctx.JSON(http.StatusOK, map[string]interface{}{ "JobName": job.JobName, "Content": content, - "CanLogDownload": true, + "CanLogDownload": canLogDownload, }) return From ce24e32f900caba3844bbe2fc37bdf21dc4fc44a Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 9 Nov 2022 17:07:11 +0800 Subject: [PATCH 5/5] #3157 fix bug --- routers/repo/grampus.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routers/repo/grampus.go b/routers/repo/grampus.go index e16d39a00..aa3b7ef78 100755 --- a/routers/repo/grampus.go +++ b/routers/repo/grampus.go @@ -938,7 +938,11 @@ func GrampusGetLog(ctx *context.Context) { content, err := grampus.GetTrainJobLog(job.JobID) if err != nil { log.Error("GetTrainJobLog failed: %v", err, ctx.Data["MsgID"]) - ctx.ServerError(err.Error(), err) + ctx.JSON(http.StatusOK, map[string]interface{}{ + "JobName": job.JobName, + "Content": "", + "CanLogDownload": false, + }) return } canLogDownload := err == nil && job.IsUserHasRight(ctx.User)