From 0f357e602f599b2afa3d5f40891fc4938e6d4342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Grill?= Date: Thu, 17 Oct 2024 17:45:13 +0200 Subject: [PATCH] proxy configuration on client side --- bindings/bindings.go | 3 +++ bindings/proxy/client/client.go | 7 +++-- bindings/proxy/client/config.go | 47 +++++++++++++++++++++++++++++++++ test/proxy/proxy_test.go | 7 ++++- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 bindings/proxy/client/config.go diff --git a/bindings/bindings.go b/bindings/bindings.go index 077d8df..a52692d 100644 --- a/bindings/bindings.go +++ b/bindings/bindings.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/rs/zerolog" + "github.com/balazsgrill/potatodrive/bindings/proxy/client" "github.com/balazsgrill/potatodrive/bindings/s3" "github.com/balazsgrill/potatodrive/bindings/sftp" "github.com/balazsgrill/potatodrive/core" @@ -77,6 +78,8 @@ func CreateConfigByType(typestr string) BindingConfig { return &s3.Config{} case "afero-sftp": return &sftp.Config{} + case "afero-http": + return &client.Config{} } return nil } diff --git a/bindings/proxy/client/client.go b/bindings/proxy/client/client.go index 531fc6f..1cb7b14 100644 --- a/bindings/proxy/client/client.go +++ b/bindings/proxy/client/client.go @@ -1,6 +1,7 @@ package client import ( + "net/http" "time" "github.com/apache/thrift/lib/go/thrift" @@ -8,7 +9,7 @@ import ( "github.com/spf13/afero" ) -func Connect(url string) (afero.Fs, error) { +func Connect(url string, httpclient *http.Client) (afero.Fs, error) { conf := &thrift.TConfiguration{ ConnectTimeout: time.Second, SocketTimeout: time.Second, @@ -19,7 +20,9 @@ func Connect(url string) (afero.Fs, error) { TBinaryStrictWrite: thrift.BoolPtr(true), } protocol := thrift.NewTCompactProtocolFactoryConf(conf) - clientfactory := thrift.NewTHttpClientTransportFactory(url) + clientfactory := thrift.NewTHttpClientTransportFactoryWithOptions(url, thrift.THttpClientOptions{ + Client: httpclient, + }) transport, err := clientfactory.GetTransport(nil) if err != nil { return nil, err diff --git a/bindings/proxy/client/config.go b/bindings/proxy/client/config.go new file mode 100644 index 0000000..0dada50 --- /dev/null +++ b/bindings/proxy/client/config.go @@ -0,0 +1,47 @@ +package client + +import ( + "errors" + "net/http" + + "github.com/spf13/afero" +) + +type Config struct { + URL string `flag:"url,Proxy URL" reg:"URL"` + KeyId string `flag:"keyid,Access Key ID" reg:"KeyID"` + KeySecret string `flag:"secret,Access Key Secret" reg:"KeySecret"` +} + +func (c *Config) Validate() error { + if c.URL == "" { + return errors.New("url is mandatory") + } + if c.KeyId == "" { + return errors.New("keyid is mandatory") + } + if c.KeySecret == "" { + return errors.New("secret is mandatory") + } + return nil +} + +type authenticator struct { + *Config + delegate http.RoundTripper +} + +func (a *authenticator) RoundTrip(r *http.Request) (*http.Response, error) { + r.Header.Add("X-Key-ID", a.KeyId) + r.Header.Add("X-Secret", a.KeySecret) + return a.delegate.RoundTrip(r) +} + +func (c *Config) ToFileSystem() (afero.Fs, error) { + httpclient := &http.Client{} + httpclient.Transport = &authenticator{ + Config: c, + delegate: http.DefaultTransport, + } + return Connect(c.URL, httpclient) +} diff --git a/test/proxy/proxy_test.go b/test/proxy/proxy_test.go index 1f11f26..0be485b 100644 --- a/test/proxy/proxy_test.go +++ b/test/proxy/proxy_test.go @@ -20,7 +20,12 @@ func TestProxyConnection(t *testing.T) { go httpserver.ListenAndServe() defer httpserver.Close() - fs2, err := client.Connect("http://localhost:18080") + clientconifg := &client.Config{ + URL: "http://localhost:18080", + KeyId: "", + KeySecret: "", + } + fs2, err := clientconifg.ToFileSystem() if err != nil { t.Fatal(err) }