Skip to content

Commit

Permalink
feat: add s3 and gcs connection structs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Dec 20, 2023
1 parent 6d0a932 commit f09491a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 13 deletions.
6 changes: 0 additions & 6 deletions connection/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ type AWSConnection struct {
// ConnectionName of the connection. It'll be used to populate the endpoint, accessKey and secretKey.
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
AccessKey types.EnvVar `yaml:"accessKey" json:"accessKey,omitempty"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
SecretKey types.EnvVar `yaml:"secretKey" json:"secretKey,omitempty"`
SessionToken types.EnvVar `yaml:"sessionToken,omitempty" json:"sessionToken,omitempty"`
Region string `yaml:"region,omitempty" json:"region,omitempty"`
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
// Skip TLS verify when connecting to aws
SkipTLSVerify bool `yaml:"skipTLSVerify,omitempty" json:"skipTLSVerify,omitempty"`
// glob path to restrict matches to a subset
ObjectPath string `yaml:"objectPath,omitempty" json:"objectPath,omitempty"`
// Use path style path: http://s3.amazonaws.com/BUCKET/KEY instead of http://BUCKET.s3.amazonaws.com/KEY
UsePathStyle bool `yaml:"usePathStyle,omitempty" json:"usePathStyle,omitempty"`
}

func (t *AWSConnection) GetUsername() types.EnvVar {
Expand All @@ -35,7 +30,6 @@ func (t *AWSConnection) GetPassword() types.EnvVar {
func (t *AWSConnection) GetProperties() map[string]string {
return map[string]string{
"region": t.Region,
"bucket": t.Bucket,
}
}

Expand Down
8 changes: 1 addition & 7 deletions connection/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
type GCPConnection struct {
// ConnectionName of the connection. It'll be used to populate the endpoint and credentials.
ConnectionName string `yaml:"connection,omitempty" json:"connection,omitempty"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
Endpoint string `yaml:"endpoint" json:"endpoint,omitempty"`
Credentials *types.EnvVar `yaml:"credentials" json:"credentials,omitempty"`
}
Expand All @@ -18,6 +17,7 @@ func (g *GCPConnection) Validate() *GCPConnection {
if g == nil {
return &GCPConnection{}
}

return g
}

Expand All @@ -44,9 +44,3 @@ func (t *GCPConnection) GetCertificate() types.EnvVar {
func (t *GCPConnection) GetURL() types.EnvVar {
return types.EnvVar{ValueStatic: t.Endpoint}
}

func (t *GCPConnection) GetProperties() map[string]string {
return map[string]string{
"bucket": t.Bucket,
}
}
44 changes: 44 additions & 0 deletions connection/gcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package connection

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

// +kubebuilder:object:generate=true
type GCSConnection struct {
GCPConnection `json:",inline"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
}

func (g *GCSConnection) Validate() *GCSConnection {
if g == nil {
return &GCSConnection{}
}

return g
}

// HydrateConnection attempts to find the connection by name
// and populate the endpoint and credentials.
func (g *GCSConnection) HydrateConnection(ctx ConnectionContext) error {
connection, err := ctx.HydrateConnectionByURL(g.ConnectionName)
if err != nil {
return err
}

if connection != nil {
g.Credentials = &types.EnvVar{ValueStatic: connection.Certificate}
g.Endpoint = connection.URL
if val, ok := connection.Properties["bucket"]; ok {
g.Bucket = val
}
}

return nil
}

func (t *GCSConnection) GetProperties() map[string]string {
return map[string]string{
"bucket": t.Bucket,
}
}
60 changes: 60 additions & 0 deletions connection/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package connection

import (
"fmt"
"strconv"

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

// +kubebuilder:object:generate=true
type S3Connection struct {
AWSConnection `yaml:",inline"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
// glob path to restrict matches to a subset
ObjectPath string `yaml:"objectPath,omitempty" json:"objectPath,omitempty"`
// Use path style path: http://s3.amazonaws.com/BUCKET/KEY instead of http://BUCKET.s3.amazonaws.com/KEY
UsePathStyle bool `yaml:"usePathStyle,omitempty" json:"usePathStyle,omitempty"`
}

func (t *S3Connection) GetProperties() map[string]string {
return collections.MergeMap(
t.AWSConnection.GetProperties(),
map[string]string{
"bucket": t.Bucket,
"objectPath": t.ObjectPath,
"usePathStyle": strconv.FormatBool(t.UsePathStyle),
})
}

// Populate populates an AWSConnection with credentials and other information.
// If a connection name is specified, it'll be used to populate the endpoint, accessKey and secretKey.
func (t *S3Connection) Populate(ctx ConnectionContext) error {
if err := t.AWSConnection.Populate(ctx); err != nil {
return err
}

if t.ConnectionName != "" {
connection, err := ctx.HydrateConnectionByURL(t.ConnectionName)
if err != nil {
return fmt.Errorf("could not parse EC2 access key: %v", err)
}

if region, ok := connection.Properties["bucket"]; ok {
t.Bucket = region
}

if objectPath, ok := connection.Properties["objectPath"]; ok {
t.ObjectPath = objectPath
}

if usePathStyle, ok := connection.Properties["usePathStyle"]; ok {
if val, err := strconv.ParseBool(usePathStyle); err == nil {
t.UsePathStyle = val
}
}

}

return nil
}
32 changes: 32 additions & 0 deletions connection/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
ConnectionTypeEmail = "email"
ConnectionTypeFolder = "folder"
ConnectionTypeGCP = "google_cloud"
ConnectionTypeGCS = "gcs"
ConnectionTypeGenericWebhook = "generic_webhook"
ConnectionTypeGit = "git"
ConnectionTypeGithub = "github"
Expand Down

0 comments on commit f09491a

Please sign in to comment.