Skip to content

Commit

Permalink
feat: add oauth type (#832)
Browse files Browse the repository at this point in the history
* feat: add oauth type

* chore: address review comments
  • Loading branch information
yashmehrotra authored Jun 11, 2024
1 parent 95abb71 commit 081f8aa
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 31 deletions.
8 changes: 4 additions & 4 deletions connection/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type SFTPConnection struct {
// ConnectionName of the connection. It'll be used to populate the connection fields.
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
// Port for the SSH server. Defaults to 22
Port int `yaml:"port,omitempty" json:"port,omitempty"`
Host string `yaml:"host" json:"host"`
Authentication `yaml:",inline" json:",inline"`
Port int `yaml:"port,omitempty" json:"port,omitempty"`
Host string `yaml:"host" json:"host"`
types.Authentication `yaml:",inline" json:",inline"`
}

func (c *SFTPConnection) HydrateConnection(ctx ConnectionContext) (found bool, err error) {
Expand All @@ -27,7 +27,7 @@ func (c *SFTPConnection) HydrateConnection(ctx ConnectionContext) (found bool, e
}

c.Host = connection.URL
c.Authentication = Authentication{
c.Authentication = types.Authentication{
Username: types.EnvVar{ValueStatic: connection.Username},
Password: types.EnvVar{ValueStatic: connection.Password},
}
Expand Down
6 changes: 3 additions & 3 deletions connection/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type SMBConnection struct {
// ConnectionName of the connection. It'll be used to populate the connection fields.
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
//Port on which smb server is running. Defaults to 445
Port int `yaml:"port,omitempty" json:"port,omitempty"`
Authentication `yaml:",inline" json:",inline"`
Port int `yaml:"port,omitempty" json:"port,omitempty"`
types.Authentication `yaml:",inline" json:",inline"`
//Domain...
Domain string `yaml:"domain,omitempty" json:"domain,omitempty"`
}
Expand All @@ -34,7 +34,7 @@ func (c *SMBConnection) HydrateConnection(ctx ConnectionContext) (found bool, er
return false, nil
}

c.Authentication = Authentication{
c.Authentication = types.Authentication{
Username: types.EnvVar{ValueStatic: connection.Username},
Password: types.EnvVar{ValueStatic: connection.Password},
}
Expand Down
17 changes: 0 additions & 17 deletions connection/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Connection struct {
URL string `gorm:"column:url" json:"url,omitempty" faker:"url" template:"true"`
Username string `gorm:"column:username" json:"username,omitempty" faker:"username" `
Password string `gorm:"column:password" json:"password,omitempty" faker:"password" `
Properties types.JSONStringMap `gorm:"column:properties" json:"properties,omitempty" faker:"-" `
Properties types.JSONStringMap `gorm:"column:properties" json:"properties,omitempty" faker:"-" template:"true"`
Certificate string `gorm:"column:certificate" json:"certificate,omitempty" faker:"-" `
InsecureTLS bool `gorm:"column:insecure_tls;default:false" json:"insecure_tls,omitempty" faker:"-" `
CreatedAt time.Time `gorm:"column:created_at;default:now();<-:create" json:"created_at,omitempty" faker:"-" `
Expand Down Expand Up @@ -123,6 +123,16 @@ func (c Connection) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}

// Auth method only works for a hydrated connection
func (c Connection) Auth() (types.Authentication, error) {
auth := types.Authentication{}
auth.Username.ValueStatic = c.Username
auth.Password.ValueStatic = c.Password
auth.Bearer.ValueStatic = c.Properties["bearer"]
err := auth.OAuth.PopulateFromProperties(c.Properties)
return auth, err
}

func (c Connection) Merge(ctx types.GetEnvVarFromCache, from any) (*Connection, error) {
if v, ok := from.(types.WithUsernamePassword); ok {
username := v.GetUsername()
Expand Down
10 changes: 10 additions & 0 deletions models/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions connection/auth.go → types/auth.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package connection
package types

import (
"strings"

"github.com/flanksource/duty/types"
)

// +kubebuilder:object:generate=true
type Authentication struct {
Username types.EnvVar `yaml:"username,omitempty" json:"username,omitempty"`
Password types.EnvVar `yaml:"password,omitempty" json:"password,omitempty"`
Username EnvVar `yaml:"username,omitempty" json:"username,omitempty"`
Password EnvVar `yaml:"password,omitempty" json:"password,omitempty"`
Bearer EnvVar `yaml:"bearer,omitempty" json:"bearer,omitempty"`
OAuth OAuth `yaml:"oauth,omitempty" json:"oauth,omitempty"`
}

func (auth Authentication) IsEmpty() bool {
return auth.Username.IsEmpty() && auth.Password.IsEmpty()
return (auth.Username.IsEmpty() && auth.Password.IsEmpty()) && auth.OAuth.IsEmpty() && auth.Bearer.IsEmpty()
}

func (auth Authentication) GetUsername() string {
Expand Down
56 changes: 56 additions & 0 deletions types/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package types

import (
"encoding/json"
"fmt"

"github.com/flanksource/commons/collections"
)

// +kubebuilder:object:generate=true
type OAuth struct {
ClientID EnvVar `json:"clientID,omitempty"`
ClientSecret EnvVar `json:"clientSecret,omitempty"`
Scopes []string `json:"scope,omitempty" yaml:"scope,omitempty"`
TokenURL string `json:"tokenURL,omitempty" yaml:"tokenURL,omitempty"`
Params map[string]string `json:"params,omitempty" yaml:"params,omitempty"`
}

func (o OAuth) IsEmpty() bool {
return o.ClientID.IsEmpty() || o.ClientSecret.IsEmpty() || o.TokenURL == ""
}

// PopulateFromProperties needs properties to be hydrated
func (o *OAuth) PopulateFromProperties(props map[string]string) error {
o.ClientID.ValueStatic = props["clientID"]
o.ClientSecret.ValueStatic = props["clientSecret"]
o.TokenURL = props["tokenURL"]
if props["scope"] != "" {
if err := json.Unmarshal([]byte(props["scopes"]), &o.Scopes); err != nil {
return fmt.Errorf("error unmarshaling scopes:%s in oauth: %w", props["scopes"], err)
}
}
if props["params"] != "" {
if err := json.Unmarshal([]byte(props["params"]), &o.Params); err != nil {
return fmt.Errorf("error unmarshaling params:%s in oauth: %w", props["params"], err)
}
}
return nil
}

func (o OAuth) AsProperties() JSONStringMap {
var scopes, params string
if o.Scopes != nil {
scopes, _ = collections.StructToJSON(o.Scopes)
}
if o.Params != nil {
params, _ = collections.StructToJSON(o.Params)
}
return map[string]string{
"clientID": o.ClientID.String(),
"clientSecret": o.ClientSecret.String(),
"tokenURL": o.TokenURL,
"scopes": scopes,
"params": params,
}
}
53 changes: 53 additions & 0 deletions types/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 081f8aa

Please sign in to comment.