-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_filesystem.go
92 lines (78 loc) · 2.27 KB
/
connection_filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package artifacts
import (
"fmt"
"net/url"
"strconv"
"github.com/flanksource/artifacts/fs"
"github.com/google/uuid"
"github.com/flanksource/duty/connection"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
)
func GetFSForConnection(ctx context.Context, c models.Connection) (fs.FilesystemRW, error) {
switch c.Type {
case models.ConnectionTypeFolder:
path := c.Properties["path"]
return fs.NewLocalFS(path), nil
case models.ConnectionTypeS3:
var conn connection.S3Connection
if c.ID != uuid.Nil {
conn.ConnectionName = c.ID.String()
} else {
conn.Endpoint = c.URL
conn.AccessKey = types.EnvVar{ValueStatic: c.Username}
conn.SecretKey = types.EnvVar{ValueStatic: c.Password}
conn.SessionToken = types.EnvVar{ValueStatic: c.Username}
conn.Bucket = c.Properties["bucket"]
conn.Region = c.Properties["region"]
if val, ok := c.Properties["usePathStyle"]; ok {
if b, err := strconv.ParseBool(val); err == nil {
conn.UsePathStyle = b
}
}
if objectPath, ok := c.Properties["objectPath"]; ok {
conn.ObjectPath = objectPath
}
}
if err := conn.Populate(ctx); err != nil {
return nil, err
}
return fs.NewS3FS(ctx, conn.Bucket, conn)
case models.ConnectionTypeGCS:
var conn connection.GCSConnection
if c.ID != uuid.Nil {
conn.ConnectionName = c.ID.String()
} else {
conn.Credentials = &types.EnvVar{ValueStatic: c.Certificate}
conn.Endpoint = c.URL
conn.Bucket = c.Properties["bucket"]
}
if err := conn.HydrateConnection(ctx); err != nil {
return nil, err
}
client, err := fs.NewGCSFS(ctx, conn.Bucket, conn)
if err != nil {
return nil, err
}
return client, nil
case models.ConnectionTypeSFTP:
parsedURL, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
client, err := fs.NewSSHFS(fmt.Sprintf("%s:%s", parsedURL.Host, c.Properties["port"]), c.Username, c.Password)
if err != nil {
return nil, err
}
return client, nil
case models.ConnectionTypeSMB:
port := c.Properties["port"]
share := c.Properties["share"]
return fs.NewSMBFS(c.URL, port, share, types.Authentication{
Username: types.EnvVar{ValueStatic: c.Username},
Password: types.EnvVar{ValueStatic: c.Password},
})
}
return nil, nil
}