From 0880837fd015ad4896ebd54c5773781c333f96e3 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 9 Feb 2023 20:07:23 +0530 Subject: [PATCH] MM-50419: Fix panic at interface conversion (#22284) We were incorrectly returning a subset of the methods, whereas at other places, clients needed access to other interfaces as well. https://mattermost.atlassian.net/browse/MM-50419 ```release-note NONE ``` --- shared/filestore/s3store.go | 15 ++++++++++----- shared/filestore/s3store_test.go | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/shared/filestore/s3store.go b/shared/filestore/s3store.go index 3dcbbe9b1a..bf11158583 100644 --- a/shared/filestore/s3store.go +++ b/shared/filestore/s3store.go @@ -57,6 +57,11 @@ var ( imageMimeTypes = map[string]string{".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".bmp": "image/bmp", ".png": "image/png", ".tiff": "image/tiff", ".tif": "image/tif"} ) +var ( + // Ensure that the ReaderAt interface is implemented. + _ io.ReaderAt = (*s3WithCancel)(nil) +) + func isFileExtImage(ext string) bool { ext = strings.ToLower(ext) return imageExtensions[ext] @@ -208,7 +213,7 @@ func (b *S3FileBackend) MakeBucket() error { // s3WithCancel is a wrapper struct which cancels the context // when the object is closed. type s3WithCancel struct { - io.ReadSeekCloser + *s3.Object timer *time.Timer cancel context.CancelFunc } @@ -216,7 +221,7 @@ type s3WithCancel struct { func (sc *s3WithCancel) Close() error { sc.timer.Stop() sc.cancel() - return sc.ReadSeekCloser.Close() + return sc.Object.Close() } // CancelTimeout attempts to cancel the timeout for this reader. It allows calling @@ -237,9 +242,9 @@ func (b *S3FileBackend) Reader(path string) (ReadCloseSeeker, error) { } sc := &s3WithCancel{ - ReadSeekCloser: minioObject, - timer: time.AfterFunc(b.timeout, cancel), - cancel: cancel, + Object: minioObject, + timer: time.AfterFunc(b.timeout, cancel), + cancel: cancel, } return sc, nil diff --git a/shared/filestore/s3store_test.go b/shared/filestore/s3store_test.go index 5222226a5b..c664dbf77f 100644 --- a/shared/filestore/s3store_test.go +++ b/shared/filestore/s3store_test.go @@ -10,7 +10,6 @@ import ( "encoding/base64" "errors" "fmt" - "io" "net/http/httptest" "net/http/httputil" "net/url" @@ -19,6 +18,7 @@ import ( "testing" "time" + s3 "github.com/minio/minio-go/v7" "github.com/stretchr/testify/require" ) @@ -283,20 +283,25 @@ func TestS3WithCancel(t *testing.T) { }) } -func newMockS3WithCancel(timeout time.Duration, closeErr error) (*s3WithCancel, context.Context) { +func newMockS3WithCancel(timeout time.Duration, closeErr error) (*fauxCloser, context.Context) { ctx, cancel := context.WithCancel(context.Background()) - return &s3WithCancel{ - ReadSeekCloser: fauxCloser{strings.NewReader("testdata"), closeErr}, - timer: time.AfterFunc(timeout, cancel), - cancel: cancel, + return &fauxCloser{ + s3WithCancel: &s3WithCancel{ + Object: &s3.Object{}, + timer: time.AfterFunc(timeout, cancel), + cancel: cancel, + }, + closeErr: closeErr, }, ctx } type fauxCloser struct { - io.ReadSeeker + *s3WithCancel closeErr error } func (fc fauxCloser) Close() error { + fc.s3WithCancel.timer.Stop() + fc.s3WithCancel.cancel() return fc.closeErr }