Skip to content

Commit

Permalink
删除接口添加atuh header
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwenfeng committed Nov 22, 2024
1 parent 1e2f321 commit fe35a8f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion unmaintained/repeated_vacuum/repeated_vacuum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/pb"
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {

assignResult, targetUrl := genFile(grpcDialOption, i)

util.Delete(targetUrl, string(assignResult.Auth))
util.Delete(targetUrl, string(assignResult.Auth), "")

}

Expand Down
8 changes: 7 additions & 1 deletion weed/command/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@ func writeFiles(idChan chan int, fileIdLineChan chan string, s *stat) {
if isSecure {
jwtAuthorization = operation.LookupJwt(b.masterClient.GetMaster(context.Background()), b.grpcDialOption, df.fp.Fid)
}
if e := util.Delete(fmt.Sprintf("http://%s/%s", df.fp.Server, df.fp.Fid), string(jwtAuthorization)); e == nil {
var authHeader string
if *b.username != "" {
auth := *b.username + ":" + *b.password
authHeader = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}

if e := util.Delete(fmt.Sprintf("http://%s/%s", df.fp.Server, df.fp.Fid), string(jwtAuthorization), authHeader); e == nil {
s.completed++
} else {
s.failed++
Expand Down
2 changes: 1 addition & 1 deletion weed/shell/command_s3_clean_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io
deleteUrl := fmt.Sprintf("http://%s%s/%s?recursive=true&ignoreRecursiveError=true", commandEnv.option.FilerAddress.ToHttpAddress(), uploadsDir, staleUpload)
fmt.Fprintf(writer, "purge %s\n", deleteUrl)

err = util.Delete(deleteUrl, string(encodedJwt))
err = util.Delete(deleteUrl, string(encodedJwt), "")
if err != nil && err.Error() != "" {
return fmt.Errorf("purge %s/%s: %v", uploadsDir, staleUpload, err)
}
Expand Down
2 changes: 1 addition & 1 deletion weed/topology/store_replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func ReplicatedDelete(masterFn operation.GetMasterFn, grpcDialOption grpc.DialOp

if len(remoteLocations) > 0 { //send to other replica locations
if err = DistributedOperation(remoteLocations, func(location operation.Location) error {
return util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", string(jwt))
return util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", string(jwt), r.Header.Get("Authorization"))
}); err != nil {
size = 0
}
Expand Down
18 changes: 10 additions & 8 deletions weed/util/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetAuthenticated(url, jwt string) ([]byte, bool, error) {
if err != nil {
return nil, true, err
}
maybeAddAuth(request, jwt)
maybeAddAuth(request, jwt, "")
request.Header.Add("Accept-Encoding", "gzip")

response, err := client.Do(request)
Expand Down Expand Up @@ -105,15 +105,17 @@ func Head(url string) (http.Header, error) {
return r.Header, nil
}

func maybeAddAuth(req *http.Request, jwt string) {
func maybeAddAuth(req *http.Request, jwt, authHeader string) {
if jwt != "" {
req.Header.Set("Authorization", "BEARER "+string(jwt))
} else if authHeader != "" {
req.Header.Set("Authorization", authHeader)
}
}

func Delete(url string, jwt string) error {
func Delete(url string, jwt, authHeader string) error {
req, err := http.NewRequest(http.MethodDelete, url, nil)
maybeAddAuth(req, jwt)
maybeAddAuth(req, jwt, authHeader)
if err != nil {
return err
}
Expand Down Expand Up @@ -141,7 +143,7 @@ func Delete(url string, jwt string) error {

func DeleteProxied(url string, jwt string) (body []byte, httpStatus int, err error) {
req, err := http.NewRequest(http.MethodDelete, url, nil)
maybeAddAuth(req, jwt)
maybeAddAuth(req, jwt, "")
if err != nil {
return
}
Expand Down Expand Up @@ -199,7 +201,7 @@ func DownloadFile(fileUrl string, jwt string) (filename string, header http.Head
return "", nil, nil, err
}

maybeAddAuth(req, jwt)
maybeAddAuth(req, jwt, "")

response, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -311,7 +313,7 @@ func ReadUrlAsStreamAuthenticated(fileUrl, jwt string, cipherKey []byte, isConte
}

req, err := http.NewRequest(http.MethodGet, fileUrl, nil)
maybeAddAuth(req, jwt)
maybeAddAuth(req, jwt, "")
if err != nil {
return false, err
}
Expand Down Expand Up @@ -401,7 +403,7 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (*htt
req.Header.Add("Accept-Encoding", "gzip")
}

maybeAddAuth(req, jwt)
maybeAddAuth(req, jwt, "")

r, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit fe35a8f

Please sign in to comment.