Skip to content

Commit

Permalink
proxy configuration on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Oct 17, 2024
1 parent 735baaf commit 0f357e6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions bindings/proxy/client/client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package client

import (
"net/http"
"time"

"github.com/apache/thrift/lib/go/thrift"
"github.com/balazsgrill/potatodrive/bindings/proxy"
"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,
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions bindings/proxy/client/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 6 additions & 1 deletion test/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0f357e6

Please sign in to comment.