github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/shared/filestore/s3_overrides.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package filestore
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  
    10  	"github.com/minio/minio-go/v7/pkg/credentials"
    11  )
    12  
    13  // customTransport is used to point the request to a different server.
    14  // This is helpful in situations where a different service is handling AWS S3 requests
    15  // from multiple Mattermost applications, and the Mattermost service itself does not
    16  // have any S3 credentials.
    17  type customTransport struct {
    18  	base   http.RoundTripper
    19  	host   string
    20  	scheme string
    21  	client http.Client
    22  }
    23  
    24  // RoundTrip implements the http.Roundtripper interface.
    25  func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    26  	// Rountrippers should not modify the original request.
    27  	newReq := req.Clone(context.Background())
    28  	*newReq.URL = *req.URL
    29  	req.URL.Scheme = t.scheme
    30  	req.URL.Host = t.host
    31  	return t.client.Do(req)
    32  }
    33  
    34  // customProvider is a dummy credentials provider for the minio client to work
    35  // without actually providing credentials. This is needed with a custom transport
    36  // in cases where the minio client does not actually have credentials with itself,
    37  // rather needs responses from another entity.
    38  //
    39  // It satisfies the credentials.Provider interface.
    40  type customProvider struct {
    41  	isSignV2 bool
    42  }
    43  
    44  // Retrieve just returns empty credentials.
    45  func (cp customProvider) Retrieve() (credentials.Value, error) {
    46  	sign := credentials.SignatureV4
    47  	if cp.isSignV2 {
    48  		sign = credentials.SignatureV2
    49  	}
    50  	return credentials.Value{
    51  		SignerType: sign,
    52  	}, nil
    53  }
    54  
    55  // IsExpired always returns false.
    56  func (cp customProvider) IsExpired() bool { return false }