Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lakectl upload using pre-signed use ContentMD5 header for ETag #6750

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/api/helpers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package helpers

import (
"context"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -154,8 +156,7 @@ func clientUploadPreSignHelper(ctx context.Context, client apigen.ClientWithResp
return nil, fmt.Errorf("upload %w %s: %s", ErrRequestFailed, preSignURL, putResp.Status)
}

etag := putResp.Header.Get("Etag")
etag = strings.TrimSpace(etag)
etag := extractEtagFromResponseHeader(putResp.Header)
if etag == "" {
return nil, fmt.Errorf("etag is missing: %w", ErrRequestFailed)
}
Expand Down Expand Up @@ -188,6 +189,24 @@ func clientUploadPreSignHelper(ctx context.Context, client apigen.ClientWithResp
return nil, fmt.Errorf("link object to backing store: %w (%s)", ErrRequestFailed, linkResp.Status())
}

// extractEtagFromResponseHeader extracts the ETag from the response header.
// If the response contains a Content-MD5 header, it will be decoded from base64 and returned as hex.
func extractEtagFromResponseHeader(h http.Header) string {
// prefer Content-MD5 if exists
contentMD5 := h.Get("Content-MD5")
if contentMD5 != "" {
// decode base64, return as hex
decodeMD5, err := base64.StdEncoding.DecodeString(contentMD5)
if err == nil {
return hex.EncodeToString(decodeMD5)
}
}
// fallback to ETag
etag := h.Get("ETag")
etag = strings.TrimFunc(etag, func(r rune) bool { return r == '"' || r == ' ' })
return etag
}

func getPhysicalAddress(ctx context.Context, client apigen.ClientWithResponsesInterface, repoID string, branchID string, params *apigen.GetPhysicalAddressParams) (*apigen.StagingLocation, error) {
resp, err := client.GetPhysicalAddressWithResponse(ctx, repoID, branchID, params)
if err != nil {
Expand Down