cleanupUploads use jwt token

This commit is contained in:
Konstantin Lebedev 2022-04-12 13:46:11 +05:00
parent 2dfbe210e0
commit 76b1c5ce67

View file

@ -3,6 +3,7 @@ package shell
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"io" "io"
"math" "math"
@ -39,6 +40,8 @@ func (c *commandS3CleanUploads) Do(args []string, commandEnv *CommandEnv, writer
return nil return nil
} }
signingKey := util.GetViper().GetString("jwt.signing.key")
var filerBucketsPath string var filerBucketsPath string
filerBucketsPath, err = readFilerBucketsPath(commandEnv) filerBucketsPath, err = readFilerBucketsPath(commandEnv)
if err != nil { if err != nil {
@ -55,7 +58,7 @@ func (c *commandS3CleanUploads) Do(args []string, commandEnv *CommandEnv, writer
} }
for _, bucket := range buckets { for _, bucket := range buckets {
if err := c.cleanupUploads(commandEnv, writer, filerBucketsPath, bucket, *uploadedTimeAgo); err != nil { if err := c.cleanupUploads(commandEnv, writer, filerBucketsPath, bucket, *uploadedTimeAgo, signingKey); err != nil {
fmt.Fprintf(writer, fmt.Sprintf("failed cleanup uploads for backet %s: %v", bucket, err)) fmt.Fprintf(writer, fmt.Sprintf("failed cleanup uploads for backet %s: %v", bucket, err))
} }
} }
@ -64,7 +67,7 @@ func (c *commandS3CleanUploads) Do(args []string, commandEnv *CommandEnv, writer
} }
func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io.Writer, filerBucketsPath string, bucket string, timeAgo time.Duration) error { func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io.Writer, filerBucketsPath string, bucket string, timeAgo time.Duration, signingKey string) error {
uploadsDir := filerBucketsPath + "/" + bucket + "/.uploads" uploadsDir := filerBucketsPath + "/" + bucket + "/.uploads"
var staleUploads []string var staleUploads []string
now := time.Now() now := time.Now()
@ -79,11 +82,16 @@ func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io
return fmt.Errorf("list uploads under %v: %v", uploadsDir, err) return fmt.Errorf("list uploads under %v: %v", uploadsDir, err)
} }
var encodedJwt security.EncodedJwt
if signingKey != "" {
encodedJwt = security.GenJwtForFilerServer(security.SigningKey(signingKey), 15*60)
}
for _, staleUpload := range staleUploads { for _, staleUpload := range staleUploads {
deleteUrl := fmt.Sprintf("http://%s%s/%s?recursive=true&ignoreRecursiveError=true", commandEnv.option.FilerAddress.ToHttpAddress(), uploadsDir, staleUpload) deleteUrl := fmt.Sprintf("http://%s%s/%s?recursive=true&ignoreRecursiveError=true", commandEnv.option.FilerAddress.ToHttpAddress(), uploadsDir, staleUpload)
fmt.Fprintf(writer, "purge %s\n", deleteUrl) fmt.Fprintf(writer, "purge %s\n", deleteUrl)
err = util.Delete(deleteUrl, "") err = util.Delete(deleteUrl, string(encodedJwt))
if err != nil && err.Error() != "" { if err != nil && err.Error() != "" {
return fmt.Errorf("purge %s/%s: %v", uploadsDir, staleUpload, err) return fmt.Errorf("purge %s/%s: %v", uploadsDir, staleUpload, err)
} }