-
Notifications
You must be signed in to change notification settings - Fork 15
/
storage.go
142 lines (115 loc) · 4.57 KB
/
storage.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (C) GRyCAP - I3M - UPV
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/grycap/cdmi-client-go"
)
const (
// DefaultProvider string identifier for the default StorageProvider
DefaultProvider = "default"
// MinIOName string representing the MinIO provider name
MinIOName = "minio"
// S3Name string representing the S3 provider name
S3Name = "s3"
// OnedataName string representing the Onedata provider name
OnedataName = "onedata"
// WebDavName string representing a storage provider accessed via webdav
WebDavName = "webdav"
// ProviderSeparator separator character used to split provider's name and identifier
ProviderSeparator = "."
)
// StorageIOConfig provides the storage input/output configuration for services
type StorageIOConfig struct {
// Provider reference to the provider's name and identifier specified in StorageProviders
// The provider's name is separated from the ID by a point (e.g. "minio.myidentifier")
Provider string `json:"storage_provider"`
Path string `json:"path"`
Suffix []string `json:"suffix,omitempty"`
Prefix []string `json:"prefix,omitempty"`
}
// StorageProviders stores the credentials of all supported storage providers
type StorageProviders struct {
S3 map[string]*S3Provider `json:"s3,omitempty"`
MinIO map[string]*MinIOProvider `json:"minio,omitempty"`
Onedata map[string]*OnedataProvider `json:"onedata,omitempty"`
WebDav map[string]*WebDavProvider `json:"webdav,omitempty"`
}
// S3Provider stores the credentials of the AWS S3 storage provider
type S3Provider struct {
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Region string `json:"region"`
}
// MinIOProvider stores the credentials of the MinIO storage provider
type MinIOProvider struct {
Endpoint string `json:"endpoint"`
Verify bool `json:"verify"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Region string `json:"region"`
}
// OnedataProvider stores the credentials of the Onedata storage provider
type OnedataProvider struct {
OneproviderHost string `json:"oneprovider_host"`
Token string `json:"token"`
Space string `json:"space"`
}
// WebDavProvider stores the credentials of the a storage provider that can be accessed via webdav
type WebDavProvider struct {
Hostname string `json:"hostname"`
Login string `json:"login"`
Password string `json:"password"`
}
// GetS3Client creates a new S3 Client from a S3Provider
func (s3Provider S3Provider) GetS3Client() *s3.S3 {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(s3Provider.AccessKey, s3Provider.SecretKey, ""),
Region: aws.String(s3Provider.Region),
}
s3Session, _ := session.NewSession(s3Config)
return s3.New(s3Session)
}
// GetS3Client creates a new S3 Client from a MinIOProvider
func (minIOProvider MinIOProvider) GetS3Client() *s3.S3 {
s3MinIOConfig := &aws.Config{
Credentials: credentials.NewStaticCredentials(minIOProvider.AccessKey, minIOProvider.SecretKey, ""),
Endpoint: aws.String(minIOProvider.Endpoint),
Region: aws.String(minIOProvider.Region),
S3ForcePathStyle: aws.Bool(true),
}
// Disable tls verification in client transport if Verify == false
if !minIOProvider.Verify {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
s3MinIOConfig.HTTPClient = &http.Client{Transport: tr}
}
minIOSession, _ := session.NewSession(s3MinIOConfig)
return s3.New(minIOSession)
}
// GetCDMIClient creates a new CDMI Client from a OnedataProvider
func (onedataProvider OnedataProvider) GetCDMIClient() *cdmi.Client {
opHost := strings.TrimRight(onedataProvider.OneproviderHost, "/ ")
// OneproviderHost must contain the "/cdmi" path for creating the CDMI client
opHostCDMI, _ := url.Parse(fmt.Sprintf("https://%s/cdmi", opHost))
return cdmi.New(opHostCDMI, onedataProvider.Token, true)
}