Skip to content

Commit 5a2a395

Browse files
committed
fixes and move new v4 file naming to custom tmpv4 folder
1 parent 4cde6db commit 5a2a395

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

routers/api/actions/artifacts_chunks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func listChunksByRunID(st storage.ObjectStorage, runID int64) (map[int64][]*chun
124124
}
125125

126126
func listChunksByRunIDV4(st storage.ObjectStorage, runID, artifactID int64, blist *BlockList) ([]*chunkFileItem, error) {
127-
storageDir := fmt.Sprintf("tmp%d", runID)
127+
storageDir := fmt.Sprintf("tmpv4%d", runID)
128128
var chunks []*chunkFileItem
129129
chunkMap := map[string]*chunkFileItem{}
130130
dummy := &chunkFileItem{}

routers/api/actions/artifactsv4.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,34 @@ func ArtifactsV4Routes(prefix string) *web.Router {
160160
return m
161161
}
162162

163-
func (r artifactV4Routes) buildSignature(endp, expires, artifactName string, taskID int64) []byte {
163+
func (r artifactV4Routes) buildSignature(endp, expires, artifactName string, taskID, artifactID int64) []byte {
164164
mac := hmac.New(sha256.New, setting.GetGeneralTokenSigningSecret())
165165
mac.Write([]byte(endp))
166166
mac.Write([]byte(expires))
167167
mac.Write([]byte(artifactName))
168168
mac.Write([]byte(fmt.Sprint(taskID)))
169+
mac.Write([]byte(fmt.Sprint(artifactID)))
169170
return mac.Sum(nil)
170171
}
171172

172-
func (r artifactV4Routes) buildArtifactURL(ctx *ArtifactContext, endp, artifactName string, taskID int64) string {
173+
func (r artifactV4Routes) buildArtifactURL(ctx *ArtifactContext, endp, artifactName string, taskID, artifactID int64) string {
173174
expires := time.Now().Add(60 * time.Minute).Format("2006-01-02 15:04:05.999999999 -0700 MST")
174175
uploadURL := strings.TrimSuffix(httplib.GuessCurrentAppURL(ctx), "/") + strings.TrimSuffix(r.prefix, "/") +
175-
"/" + endp + "?sig=" + base64.URLEncoding.EncodeToString(r.buildSignature(endp, expires, artifactName, taskID)) + "&expires=" + url.QueryEscape(expires) + "&artifactName=" + url.QueryEscape(artifactName) + "&taskID=" + fmt.Sprint(taskID)
176+
"/" + endp + "?sig=" + base64.URLEncoding.EncodeToString(r.buildSignature(endp, expires, artifactName, taskID, artifactID)) + "&expires=" + url.QueryEscape(expires) + "&artifactName=" + url.QueryEscape(artifactName) + "&taskID=" + fmt.Sprint(taskID) + "&artifactID=" + fmt.Sprint(artifactID)
176177
return uploadURL
177178
}
178179

179180
func (r artifactV4Routes) verifySignature(ctx *ArtifactContext, endp string) (*actions.ActionTask, string, bool) {
180181
rawTaskID := ctx.Req.URL.Query().Get("taskID")
182+
rawArtifactID := ctx.Req.URL.Query().Get("artifactID")
181183
sig := ctx.Req.URL.Query().Get("sig")
182184
expires := ctx.Req.URL.Query().Get("expires")
183185
artifactName := ctx.Req.URL.Query().Get("artifactName")
184186
dsig, _ := base64.URLEncoding.DecodeString(sig)
185187
taskID, _ := strconv.ParseInt(rawTaskID, 10, 64)
188+
artifactID, _ := strconv.ParseInt(rawArtifactID, 10, 64)
186189

187-
expecedsig := r.buildSignature(endp, expires, artifactName, taskID)
190+
expecedsig := r.buildSignature(endp, expires, artifactName, taskID, artifactID)
188191
if !hmac.Equal(dsig, expecedsig) {
189192
log.Error("Error unauthorized")
190193
ctx.Error(http.StatusUnauthorized, "Error unauthorized")
@@ -289,7 +292,7 @@ func (r *artifactV4Routes) createArtifact(ctx *ArtifactContext) {
289292

290293
respData := CreateArtifactResponse{
291294
Ok: true,
292-
SignedUploadUrl: r.buildArtifactURL(ctx, "UploadArtifact", artifactName, ctx.ActionTask.ID),
295+
SignedUploadUrl: r.buildArtifactURL(ctx, "UploadArtifact", artifactName, ctx.ActionTask.ID, artifact.ID),
293296
}
294297
r.sendProtbufBody(ctx, &respData)
295298
}
@@ -327,7 +330,7 @@ func (r *artifactV4Routes) uploadArtifact(ctx *ArtifactContext) {
327330
return
328331
}
329332
} else {
330-
_, err := r.fs.Save(fmt.Sprintf("tmp%d/block-%d-%d-%s", task.Job.RunID, task.Job.RunID, ctx.Req.ContentLength, base64.URLEncoding.EncodeToString([]byte(blockid))), ctx.Req.Body, -1)
333+
_, err := r.fs.Save(fmt.Sprintf("tmpv4%d/block-%d-%d-%s", task.Job.RunID, task.Job.RunID, ctx.Req.ContentLength, base64.URLEncoding.EncodeToString([]byte(blockid))), ctx.Req.Body, -1)
331334
if err != nil {
332335
log.Error("Error runner api getting task: task is not running")
333336
ctx.Error(http.StatusInternalServerError, "Error runner api getting task: task is not running")
@@ -336,7 +339,9 @@ func (r *artifactV4Routes) uploadArtifact(ctx *ArtifactContext) {
336339
}
337340
ctx.JSON(http.StatusCreated, "appended")
338341
case "blocklist":
339-
_, err := r.fs.Save(fmt.Sprintf("tmp%d/%d-blocklist", task.Job.RunID, task.Job.RunID), ctx.Req.Body, -1)
342+
rawArtifactID := ctx.Req.URL.Query().Get("artifactID")
343+
artifactID, _ := strconv.ParseInt(rawArtifactID, 10, 64)
344+
_, err := r.fs.Save(fmt.Sprintf("tmpv4%d/%d-%d-blocklist", task.Job.RunID, task.Job.RunID, artifactID), ctx.Req.Body, -1)
340345
if err != nil {
341346
log.Error("Error runner api getting task: task is not running")
342347
ctx.Error(http.StatusInternalServerError, "Error runner api getting task: task is not running")
@@ -354,6 +359,23 @@ type Latest struct {
354359
Value string `xml:",chardata"`
355360
}
356361

362+
func (r *artifactV4Routes) readBlockList(runID, artifactID int64) (*BlockList, error) {
363+
blockListName := fmt.Sprintf("tmpv4%d/%d-%d-blocklist", runID, runID, artifactID)
364+
s, err := r.fs.Open(blockListName)
365+
if err != nil {
366+
return nil, err
367+
}
368+
err = r.fs.Delete(blockListName)
369+
if err != nil {
370+
log.Warn("Failed to delete blockList %s: %v", blockListName, err)
371+
}
372+
373+
xdec := xml.NewDecoder(s)
374+
blockList := &BlockList{}
375+
err = xdec.Decode(blockList)
376+
return blockList, err
377+
}
378+
357379
func (r *artifactV4Routes) finalizeArtifact(ctx *ArtifactContext) {
358380
var req FinalizeArtifactRequest
359381

@@ -373,11 +395,10 @@ func (r *artifactV4Routes) finalizeArtifact(ctx *ArtifactContext) {
373395
return
374396
}
375397

376-
blockListName := fmt.Sprintf("tmp%d/%d-blocklist", runID, runID)
377398
var chunks []*chunkFileItem
378-
s, err := r.fs.Open(blockListName)
399+
blockList, err := r.readBlockList(runID, artifact.ID)
379400
if err != nil {
380-
log.Warn("Error merge chunks: %v", err)
401+
log.Warn("Failed to read BlockList, fallback to old behavior: %v", err)
381402
chunkMap, err := listChunksByRunID(r.fs, runID)
382403
if err != nil {
383404
log.Error("Error merge chunks: %v", err)
@@ -391,19 +412,6 @@ func (r *artifactV4Routes) finalizeArtifact(ctx *ArtifactContext) {
391412
return
392413
}
393414
} else {
394-
err = r.fs.Delete(blockListName)
395-
if err != nil {
396-
log.Warn("Failed to delete blockList %s: %v", blockListName, err)
397-
}
398-
399-
xdec := xml.NewDecoder(s)
400-
blockList := &BlockList{}
401-
err = xdec.Decode(blockList)
402-
if err != nil {
403-
log.Error("Error merge chunks: %v", err)
404-
ctx.Error(http.StatusInternalServerError, "Error merge chunks")
405-
return
406-
}
407415
chunks, err = listChunksByRunIDV4(r.fs, runID, artifact.ID, blockList)
408416
if err != nil {
409417
log.Error("Error merge chunks: %v", err)
@@ -514,7 +522,7 @@ func (r *artifactV4Routes) getSignedArtifactURL(ctx *ArtifactContext) {
514522
}
515523
}
516524
if respData.SignedUrl == "" {
517-
respData.SignedUrl = r.buildArtifactURL(ctx, "DownloadArtifact", artifactName, ctx.ActionTask.ID)
525+
respData.SignedUrl = r.buildArtifactURL(ctx, "DownloadArtifact", artifactName, ctx.ActionTask.ID, artifact.ID)
518526
}
519527
r.sendProtbufBody(ctx, &respData)
520528
}

0 commit comments

Comments
 (0)