summaryrefslogtreecommitdiff
path: root/workhorse/internal/config/config.go
blob: 9f214385f81744745ba9bb6e8e71be5dc8c39941 (plain)
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
package config

import (
	"math"
	"net/url"
	"runtime"
	"strings"
	"time"

	"github.com/Azure/azure-storage-blob-go/azblob"
	"github.com/BurntSushi/toml"
	"gitlab.com/gitlab-org/labkit/log"
	"gocloud.dev/blob"
	"gocloud.dev/blob/azureblob"
)

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"`
}

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 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 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"`
}

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 != "" {
		accountName := azureblob.AccountName(creds.AzureCredentials.AccountName)
		accountKey := azureblob.AccountKey(creds.AzureCredentials.AccountKey)

		credential, err := azureblob.NewCredential(accountName, accountKey)
		if err != nil {
			log.WithError(err).Error("error creating Azure credentials")
			return err
		}

		pipeline := azureblob.NewPipeline(credential, azblob.PipelineOptions{})

		azureURLOpener := &azureURLOpener{
			&azureblob.URLOpener{
				AccountName: accountName,
				Pipeline:    pipeline,
				Options:     azureblob.Options{Credential: credential},
			},
		}

		c.ObjectStorageConfig.URLMux.RegisterBucket(azureblob.Scheme, azureURLOpener)
	}

	return nil
}