From 44da474f8915127f150383365c68400b66540c4a Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 2 Jun 2023 11:40:30 -0500 Subject: [PATCH] feat: auth configuration still pending using certificate for SSH --- compose/auth.go | 48 +++++++++++++++++++++++++++++++++++++++ compose/auth_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 compose/auth.go create mode 100644 compose/auth_test.go diff --git a/compose/auth.go b/compose/auth.go new file mode 100644 index 0000000..aa12015 --- /dev/null +++ b/compose/auth.go @@ -0,0 +1,48 @@ +package compose + +import ( + "fmt" + + "golang.org/x/crypto/ssh" + "golang.org/x/net/proxy" +) + +type Auth struct { + Username *string `yaml:"username,omitempty" json:"username,omitempty"` + Password *string `yaml:"password,omitempty" json:"password,omitempty"` + PrivateKey *string `yaml:"privateKey,omitempty" json:"privateKey,omitempty"` +} + +func (a *Auth) SSHClientConfig() (config *ssh.ClientConfig, err error) { + if a.Username == nil { + err = fmt.Errorf("no username provided") + } + if err == nil { + config = new(ssh.ClientConfig) + config.User = *a.Username + if a.Password != nil { + config.Auth = append(config.Auth, ssh.Password(*a.Password)) + } + if a.PrivateKey != nil { + // TODO: FIXME: This code doesn't work + config.Auth = append(config.Auth, ssh.PublicKeys()) + } + } + return config, err +} + +func (a *Auth) Socks5() (auth *proxy.Auth, err error) { + if a.Username == nil { + err = fmt.Errorf("no username provided") + } + if a.Password == nil { + err = fmt.Errorf("no password provided") + } + if err == nil { + auth = &proxy.Auth{ + User: *a.Username, + Password: *a.Password, + } + } + return auth, err +} diff --git a/compose/auth_test.go b/compose/auth_test.go new file mode 100644 index 0000000..505ea9b --- /dev/null +++ b/compose/auth_test.go @@ -0,0 +1,54 @@ +package compose + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAuth_SSHClientConfig(t *testing.T) { + t.Run("Valid", func(tt *testing.T) { + auth := Auth{ + Username: new(string), + Password: new(string), + } + *auth.Username = "sulcud" + *auth.Password = "password" + _, err := auth.SSHClientConfig() + assert.Nil(tt, err) + }) + t.Run("Private Key", func(tt *testing.T) { + // TODO: Implement me! + }) + t.Run("No Username", func(tt *testing.T) { + auth := Auth{} + _, err := auth.SSHClientConfig() + assert.NotNil(tt, err) + }) +} + +func TestAuth_Socks5(t *testing.T) { + t.Run("Valid", func(tt *testing.T) { + auth := Auth{ + Username: new(string), + Password: new(string), + } + *auth.Username = "sulcud" + *auth.Password = "password" + _, err := auth.Socks5() + assert.Nil(tt, err) + }) + t.Run("No Username", func(tt *testing.T) { + auth := Auth{} + _, err := auth.Socks5() + assert.NotNil(tt, err) + }) + t.Run("No Password", func(tt *testing.T) { + auth := Auth{ + Username: new(string), + } + *auth.Username = "sulcud" + _, err := auth.Socks5() + assert.NotNil(tt, err) + }) +}