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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package config
import (
"context"
"fmt"
"math"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/BurntSushi/toml"
"gocloud.dev/blob"
"gocloud.dev/blob/azureblob"
"gocloud.dev/blob/gcsblob"
"gocloud.dev/gcp"
"golang.org/x/oauth2/google"
)
type TomlURL struct {
url.URL
}
func (u *TomlURL) UnmarshalText(text []byte) error {
temp, err := url.Parse(string(text))
u.URL = *temp
return err
}
type TomlDuration struct {
time.Duration
}
func (d *TomlDuration) UnmarshalText(text []byte) error {
temp, err := time.ParseDuration(string(text))
d.Duration = temp
return err
}
type ObjectStorageCredentials struct {
Provider string
S3Credentials S3Credentials `toml:"s3"`
AzureCredentials AzureCredentials `toml:"azurerm"`
GoogleCredentials GoogleCredentials `toml:"google"`
}
type ObjectStorageConfig struct {
URLMux *blob.URLMux `toml:"-"`
}
type S3Credentials struct {
AwsAccessKeyID string `toml:"aws_access_key_id"`
AwsSecretAccessKey string `toml:"aws_secret_access_key"`
}
type S3Config struct {
Region string `toml:"-"`
Bucket string `toml:"-"`
PathStyle bool `toml:"-"`
Endpoint string `toml:"-"`
UseIamProfile bool `toml:"-"`
ServerSideEncryption string `toml:"-"` // Server-side encryption mode (e.g. AES256, aws:kms)
SSEKMSKeyID string `toml:"-"` // Server-side encryption key-management service key ID (e.g. arn:aws:xxx)
}
type GoCloudConfig struct {
URL string `toml:"-"`
}
type AzureCredentials struct {
AccountName string `toml:"azure_storage_account_name"`
AccountKey string `toml:"azure_storage_access_key"`
}
type GoogleCredentials struct {
ApplicationDefault bool `toml:"google_application_default"`
JSONKeyString string `toml:"google_json_key_string"`
JSONKeyLocation string `toml:"google_json_key_location"`
}
type RedisConfig struct {
URL TomlURL
Sentinel []TomlURL
SentinelMaster string
Password string
DB *int
MaxIdle *int
MaxActive *int
}
type ImageResizerConfig struct {
MaxScalerProcs uint32 `toml:"max_scaler_procs"`
MaxFilesize uint64 `toml:"max_filesize"`
}
type TlsConfig struct {
Certificate string `toml:"certificate"`
Key string `toml:"key"`
MinVersion string `toml:"min_version"`
MaxVersion string `toml:"max_version"`
}
type ListenerConfig struct {
Network string `toml:"network"`
Addr string `toml:"addr"`
Tls *TlsConfig `toml:"tls"`
}
type Config struct {
Redis *RedisConfig `toml:"redis"`
Backend *url.URL `toml:"-"`
CableBackend *url.URL `toml:"-"`
Version string `toml:"-"`
DocumentRoot string `toml:"-"`
DevelopmentMode bool `toml:"-"`
Socket string `toml:"-"`
CableSocket string `toml:"-"`
ProxyHeadersTimeout time.Duration `toml:"-"`
APILimit uint `toml:"-"`
APIQueueLimit uint `toml:"-"`
APIQueueTimeout time.Duration `toml:"-"`
APICILongPollingDuration time.Duration `toml:"-"`
ObjectStorageConfig ObjectStorageConfig `toml:"-"`
ObjectStorageCredentials ObjectStorageCredentials `toml:"object_storage"`
PropagateCorrelationID bool `toml:"-"`
ImageResizerConfig ImageResizerConfig `toml:"image_resizer"`
AltDocumentRoot string `toml:"alt_document_root"`
ShutdownTimeout TomlDuration `toml:"shutdown_timeout"`
TrustedCIDRsForXForwardedFor []string `toml:"trusted_cidrs_for_x_forwarded_for"`
TrustedCIDRsForPropagation []string `toml:"trusted_cidrs_for_propagation"`
Listeners []ListenerConfig `toml:"listeners"`
MetricsListener *ListenerConfig `toml:"metrics_listener"`
}
var DefaultImageResizerConfig = ImageResizerConfig{
MaxScalerProcs: uint32(math.Max(2, float64(runtime.NumCPU())/2)),
MaxFilesize: 250 * 1000, // 250kB,
}
func LoadConfig(data string) (*Config, error) {
cfg := &Config{ImageResizerConfig: DefaultImageResizerConfig}
if _, err := toml.Decode(data, cfg); err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) RegisterGoCloudURLOpeners() error {
c.ObjectStorageConfig.URLMux = new(blob.URLMux)
creds := c.ObjectStorageCredentials
if strings.EqualFold(creds.Provider, "AzureRM") && creds.AzureCredentials.AccountName != "" && creds.AzureCredentials.AccountKey != "" {
urlOpener, err := creds.AzureCredentials.getURLOpener()
if err != nil {
return err
}
c.ObjectStorageConfig.URLMux.RegisterBucket(azureblob.Scheme, urlOpener)
}
if strings.EqualFold(creds.Provider, "Google") && (creds.GoogleCredentials.JSONKeyLocation != "" || creds.GoogleCredentials.JSONKeyString != "" || creds.GoogleCredentials.ApplicationDefault) {
urlOpener, err := creds.GoogleCredentials.getURLOpener()
if err != nil {
return err
}
c.ObjectStorageConfig.URLMux.RegisterBucket(gcsblob.Scheme, urlOpener)
}
return nil
}
func (creds *AzureCredentials) getURLOpener() (*azureblob.URLOpener, error) {
serviceURLOptions := azureblob.ServiceURLOptions{
AccountName: creds.AccountName,
}
clientFunc := func(svcURL azureblob.ServiceURL) (*azblob.ServiceClient, error) {
sharedKeyCred, err := azblob.NewSharedKeyCredential(creds.AccountName, creds.AccountKey)
if err != nil {
return nil, fmt.Errorf("error creating Azure credentials: %w", err)
}
return azblob.NewServiceClientWithSharedKey(string(svcURL), sharedKeyCred, &azblob.ClientOptions{})
}
return &azureblob.URLOpener{
MakeClient: clientFunc,
ServiceURLOptions: serviceURLOptions,
}, nil
}
func (creds *GoogleCredentials) getURLOpener() (*gcsblob.URLOpener, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) // lint:allow context.Background
defer cancel()
gcpCredentials, err := creds.getGCPCredentials(ctx)
if err != nil {
return nil, err
}
client, err := gcp.NewHTTPClient(
gcp.DefaultTransport(),
gcp.CredentialsTokenSource(gcpCredentials),
)
if err != nil {
return nil, fmt.Errorf("error creating Google HTTP client: %w", err)
}
return &gcsblob.URLOpener{
Client: client,
}, nil
}
func (creds *GoogleCredentials) getGCPCredentials(ctx context.Context) (*google.Credentials, error) {
const gcpCredentialsScope = "https://www.googleapis.com/auth/devstorage.read_write"
if creds.ApplicationDefault {
return gcp.DefaultCredentials(ctx)
}
if creds.JSONKeyLocation != "" {
b, err := os.ReadFile(creds.JSONKeyLocation)
if err != nil {
return nil, fmt.Errorf("error reading Google json key location: %w", err)
}
return google.CredentialsFromJSON(ctx, b, gcpCredentialsScope)
}
b := []byte(creds.JSONKeyString)
return google.CredentialsFromJSON(ctx, b, gcpCredentialsScope)
}
|