2021-02-22 17:27:13 +05:30
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-01-13 00:05:48 +05:30
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-02-22 17:27:13 +05:30
|
|
|
"math"
|
|
|
|
"net/url"
|
2023-01-13 00:05:48 +05:30
|
|
|
"os"
|
2021-02-22 17:27:13 +05:30
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
2023-04-23 21:23:45 +05:30
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
2021-02-22 17:27:13 +05:30
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"gocloud.dev/blob"
|
|
|
|
"gocloud.dev/blob/azureblob"
|
2023-01-13 00:05:48 +05:30
|
|
|
"gocloud.dev/blob/gcsblob"
|
|
|
|
"gocloud.dev/gcp"
|
|
|
|
"golang.org/x/oauth2/google"
|
2021-02-22 17:27:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
func (d *TomlDuration) UnmarshalText(text []byte) error {
|
2021-02-22 17:27:13 +05:30
|
|
|
temp, err := time.ParseDuration(string(text))
|
|
|
|
d.Duration = temp
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectStorageCredentials struct {
|
|
|
|
Provider string
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
S3Credentials S3Credentials `toml:"s3"`
|
|
|
|
AzureCredentials AzureCredentials `toml:"azurerm"`
|
|
|
|
GoogleCredentials GoogleCredentials `toml:"google"`
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
type GoogleCredentials struct {
|
|
|
|
ApplicationDefault bool `toml:"google_application_default"`
|
|
|
|
JSONKeyString string `toml:"google_json_key_string"`
|
|
|
|
JSONKeyLocation string `toml:"google_json_key_location"`
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
type RedisConfig struct {
|
2021-09-04 01:27:46 +05:30
|
|
|
URL TomlURL
|
|
|
|
Sentinel []TomlURL
|
|
|
|
SentinelMaster string
|
|
|
|
Password string
|
|
|
|
DB *int
|
|
|
|
MaxIdle *int
|
|
|
|
MaxActive *int
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type ImageResizerConfig struct {
|
|
|
|
MaxScalerProcs uint32 `toml:"max_scaler_procs"`
|
|
|
|
MaxFilesize uint64 `toml:"max_filesize"`
|
|
|
|
}
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
type Config struct {
|
2021-10-27 15:23:28 +05:30
|
|
|
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"`
|
2022-07-16 23:28:13 +05:30
|
|
|
Listeners []ListenerConfig `toml:"listeners"`
|
2022-07-23 23:45:48 +05:30
|
|
|
MetricsListener *ListenerConfig `toml:"metrics_listener"`
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
2023-01-13 00:05:48 +05:30
|
|
|
urlOpener, err := creds.AzureCredentials.getURLOpener()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ObjectStorageConfig.URLMux.RegisterBucket(azureblob.Scheme, urlOpener)
|
|
|
|
}
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
if strings.EqualFold(creds.Provider, "Google") && (creds.GoogleCredentials.JSONKeyLocation != "" || creds.GoogleCredentials.JSONKeyString != "" || creds.GoogleCredentials.ApplicationDefault) {
|
|
|
|
urlOpener, err := creds.GoogleCredentials.getURLOpener()
|
2021-02-22 17:27:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-13 00:05:48 +05:30
|
|
|
c.ObjectStorageConfig.URLMux.RegisterBucket(gcsblob.Scheme, urlOpener)
|
|
|
|
}
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (creds *AzureCredentials) getURLOpener() (*azureblob.URLOpener, error) {
|
|
|
|
serviceURLOptions := azureblob.ServiceURLOptions{
|
|
|
|
AccountName: creds.AccountName,
|
|
|
|
}
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
clientFunc := func(svcURL azureblob.ServiceURL, containerName azureblob.ContainerName) (*container.Client, error) {
|
2023-01-13 00:05:48 +05:30
|
|
|
sharedKeyCred, err := azblob.NewSharedKeyCredential(creds.AccountName, creds.AccountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating Azure credentials: %w", err)
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
2023-04-23 21:23:45 +05:30
|
|
|
containerURL := fmt.Sprintf("%s/%s", svcURL, containerName)
|
|
|
|
return container.NewClientWithSharedKeyCredential(containerURL, sharedKeyCred, &container.ClientOptions{})
|
2023-01-13 00:05:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return &azureblob.URLOpener{
|
|
|
|
MakeClient: clientFunc,
|
|
|
|
ServiceURLOptions: serviceURLOptions,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
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
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
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)
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|