Skip to content

Commit

Permalink
feat: auth configuration
Browse files Browse the repository at this point in the history
still pending using certificate for SSH
  • Loading branch information
shoriwe committed Jun 2, 2023
1 parent 84ebbcf commit 44da474
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
48 changes: 48 additions & 0 deletions compose/auth.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 54 additions & 0 deletions compose/auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 44da474

Please sign in to comment.