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

[v14] Minor fixes to the S3 uploader #35777

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/events/s3sessions/s3handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"io"
"net/url"
"path/filepath"
"path"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -366,11 +366,11 @@ func (h *Handler) path(sessionID session.ID) string {
if h.Path == "" {
return string(sessionID) + ".tar"
}
return strings.TrimPrefix(filepath.Join(h.Path, string(sessionID)+".tar"), "/")
return strings.TrimPrefix(path.Join(h.Path, string(sessionID)+".tar"), "/")
}

func (h *Handler) fromPath(path string) session.ID {
return session.ID(strings.TrimSuffix(filepath.Base(path), ".tar"))
func (h *Handler) fromPath(p string) session.ID {
return session.ID(strings.TrimSuffix(path.Base(p), ".tar"))
}

// ensureBucket makes sure bucket exists, and if it does not, creates it
Expand Down
14 changes: 14 additions & 0 deletions lib/events/s3sessions/s3handler_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package s3sessions

import (
"context"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -115,3 +116,16 @@ func TestConfig_SetFromURL(t *testing.T) {
})
}
}

func TestUploadMetadata(t *testing.T) {
handler, err := NewHandler(context.Background(), Config{
Region: "us-west-1",
Path: "/test/",
Bucket: "teleport-unit-tests",
})
require.NoError(t, err)
defer handler.Close()

meta := handler.GetUploadMetadata("test-session-id")
require.Equal(t, "s3://teleport-unit-tests/test/test-session-id", meta.URL)
}
5 changes: 3 additions & 2 deletions lib/events/s3sessions/s3handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
package s3sessions

import (
"context"
"fmt"
"net/url"
"os"
Expand All @@ -39,10 +40,10 @@ func TestMain(m *testing.M) {

// TestStreams tests various streaming upload scenarios
func TestStreams(t *testing.T) {
handler, err := NewHandler(Config{
handler, err := NewHandler(context.Background(), Config{
Region: "us-west-1",
Path: "/test/",
Bucket: fmt.Sprintf("teleport-unit-tests"),
Bucket: "teleport-unit-tests",
})
require.Nil(t, err)

Expand Down
10 changes: 9 additions & 1 deletion lib/events/s3sessions/s3stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"net/url"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -264,8 +265,15 @@ func (h *Handler) ListUploads(ctx context.Context) ([]events.StreamUpload, error

// GetUploadMetadata gets the metadata for session upload
func (h *Handler) GetUploadMetadata(sessionID session.ID) events.UploadMetadata {
sessionURL, err := url.JoinPath(teleport.SchemeS3+"://"+h.Bucket, h.Path, sessionID.String())
if err != nil {
// this should never happen, but if it does revert to legacy behavior
// which omitted h.Path
sessionURL = fmt.Sprintf("%v://%v/%v", teleport.SchemeS3, h.Bucket, sessionID)
}

return events.UploadMetadata{
URL: fmt.Sprintf("%v://%v/%v", teleport.SchemeS3, h.Bucket, sessionID),
URL: sessionURL,
SessionID: sessionID,
}
}
Expand Down