aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 47b69f93f0a68cd02b4e60dee80e81d32b798be3 (plain) (blame)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Handle configuration
 *
 * Copyright (c) 2024  Runxi Yu <me@runxiyu.org>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"

	"git.sr.ht/~emersion/go-scfg"
)

/*
 * We use two structs. The first has all of its values as pointers, and scfg
 * unmarshals the configuration to it. Then we take each value, dereference
 * it, and throw it into a normal config struct without pointers, reporting
 * missing values.
 * We should probably use reflection instead.
 */

var configWithPointers struct {
	URL    *string `scfg:"url"`
	Prod   *bool   `scfg:"prod"`
	Listen struct {
		Proto *string `scfg:"proto"`
		Net   *string `scfg:"net"`
		Addr  *string `scfg:"addr"`
		Trans *string `scfg:"trans"`
		TLS   struct {
			Cert *string `scfg:"cert"`
			Key  *string `scfg:"key"`
		} `scfg:"tls"`
	} `scfg:"listen"`
	DB struct {
		Type *string `scfg:"type"`
		Conn *string `scfg:"conn"`
	} `scfg:"db"`
	Auth struct {
		Fake      *int    `scfg:"fake"`
		Client    *string `scfg:"client"`
		Authorize *string `scfg:"authorize"`
		Jwks      *string `scfg:"jwks"`
		Token     *string `scfg:"token"`
		Secret    *string `scfg:"secret"`
		Expr      *int    `scfg:"expr"`
	} `scfg:"auth"`
	Perf struct {
		MessageArgumentsCap *int  `scfg:"msg_args_cap"`
		MessageBytesCap     *int  `scfg:"msg_bytes_cap"`
		ReadHeaderTimeout   *int  `scfg:"read_header_timeout"`
		UsemDelayShiftBits  *int  `scfg:"usem_delay_shift_bits"`
		PropagateImmediate  *bool `scfg:"propagate_immediate"`
	} `scfg:"perf"`
}

var config struct {
	URL    string
	Prod   bool
	Listen struct {
		Proto string
		Net   string
		Addr  string
		Trans string
		TLS   struct {
			Cert string
			Key  string
		}
	}
	DB struct {
		Type string
		Conn string
	}
	Auth struct {
		Fake      int
		Client    string
		Authorize string
		Jwks      string
		Token     string
		Secret    string
		Expr      int
	}
	Perf struct {
		MessageArgumentsCap int
		MessageBytesCap     int
		ReadHeaderTimeout   int
		UsemDelayShiftBits  int
		PropagateImmediate  bool
	} `scfg:"perf"`
}

func fetchConfig(path string) (retErr error) {
	defer func() {
		if v := recover(); v != nil {
			s, ok := v.(error)
			if ok {
				retErr = fmt.Errorf(
					"%w: %w",
					errCannotProcessConfig,
					s,
				)
			}
			retErr = fmt.Errorf("%w: %v", errCannotProcessConfig, v)
			return
		}
		if retErr != nil {
			retErr = fmt.Errorf(
				"%w: %w",
				errCannotProcessConfig,
				retErr,
			)
			return
		}
	}()

	f, err := os.Open(path)
	if err != nil {
		return fmt.Errorf("%w: %w", errCannotOpenConfig, err)
	}

	err = scfg.NewDecoder(bufio.NewReader(f)).Decode(&configWithPointers)
	if err != nil {
		return fmt.Errorf("%w: %w", errCannotDecodeConfig, err)
	}

	if configWithPointers.URL == nil {
		return fmt.Errorf("%w: url", errMissingConfigValue)
	}
	config.URL = *(configWithPointers.URL)

	if configWithPointers.Prod == nil {
		return fmt.Errorf("%w: prod", errMissingConfigValue)
	}
	config.Prod = *(configWithPointers.Prod)

	if configWithPointers.Listen.Proto == nil {
		return fmt.Errorf("%w: listen.proto", errMissingConfigValue)
	}
	config.Listen.Proto = *(configWithPointers.Listen.Proto)

	if configWithPointers.Listen.Net == nil {
		return fmt.Errorf("%w: listen.net", errMissingConfigValue)
	}
	config.Listen.Net = *(configWithPointers.Listen.Net)

	if configWithPointers.Listen.Addr == nil {
		return fmt.Errorf("%w: listen.addr", errMissingConfigValue)
	}
	config.Listen.Addr = *(configWithPointers.Listen.Addr)

	if configWithPointers.Listen.Trans == nil {
		return fmt.Errorf("%w: listen.trans", errMissingConfigValue)
	}
	config.Listen.Trans = *(configWithPointers.Listen.Trans)

	if config.Listen.Trans == "tls" {
		if configWithPointers.Listen.TLS.Cert == nil {
			return fmt.Errorf(
				"%w: listen.tls.cert",
				errMissingConfigValue,
			)
		}
		config.Listen.TLS.Cert = *(configWithPointers.Listen.TLS.Cert)

		if configWithPointers.Listen.TLS.Key == nil {
			return fmt.Errorf(
				"%w: listen.tls.key",
				errMissingConfigValue,
			)
		}
		config.Listen.TLS.Key = *(configWithPointers.Listen.TLS.Key)
	}

	if configWithPointers.DB.Type == nil {
		return fmt.Errorf("%w: db.type", errMissingConfigValue)
	}
	config.DB.Type = *(configWithPointers.DB.Type)

	if configWithPointers.DB.Conn == nil {
		return fmt.Errorf("%w: db.conn", errMissingConfigValue)
	}
	config.DB.Conn = *(configWithPointers.DB.Conn)

	if configWithPointers.Auth.Fake == nil {
		config.Auth.Fake = 0
	} else {
		config.Auth.Fake = *(configWithPointers.Auth.Fake)
		switch config.Auth.Fake {
		case 0:
			/* It's okay to set it to 0 in production */
		case 4712, 9080: /* Don't use them unless you know what you're doing */
			if config.Prod {
				return fmt.Errorf(
					"%w: fake authentication is incompatible with production mode",
					errIllegalConfig,
				)
			}
			log.Println(
				"!!! WARNING: Fake authentication is enabled. Any WebSocket connection would have a fake account. This is a HUGE security hole. You should only use this while benchmarking.",
			)
		default:
			return fmt.Errorf(
				"%w: invalid option for auth.fake",
				errIllegalConfig,
			)
		}
	}

	if configWithPointers.Auth.Client == nil {
		return fmt.Errorf("%w: auth.client", errMissingConfigValue)
	}
	config.Auth.Client = *(configWithPointers.Auth.Client)

	if configWithPointers.Auth.Authorize == nil {
		return fmt.Errorf("%w: auth.authorize", errMissingConfigValue)
	}
	config.Auth.Authorize = *(configWithPointers.Auth.Authorize)

	if configWithPointers.Auth.Jwks == nil {
		return fmt.Errorf("%w: auth.jwks", errMissingConfigValue)
	}
	config.Auth.Jwks = *(configWithPointers.Auth.Jwks)

	if configWithPointers.Auth.Token == nil {
		return fmt.Errorf("%w: auth.token", errMissingConfigValue)
	}
	config.Auth.Token = *(configWithPointers.Auth.Token)

	if configWithPointers.Auth.Secret == nil {
		return fmt.Errorf("%w: auth.secret", errMissingConfigValue)
	}
	config.Auth.Secret = *(configWithPointers.Auth.Secret)

	if configWithPointers.Auth.Expr == nil {
		return fmt.Errorf("%w: auth.expr", errMissingConfigValue)
	}
	config.Auth.Expr = *(configWithPointers.Auth.Expr)

	if configWithPointers.Perf.MessageArgumentsCap == nil {
		return fmt.Errorf(
			"%w: perf.msg_args_cap",
			errMissingConfigValue,
		)
	}
	config.Perf.MessageArgumentsCap = *(configWithPointers.Perf.MessageArgumentsCap)

	if configWithPointers.Perf.MessageBytesCap == nil {
		return fmt.Errorf(
			"%w: perf.msg_bytes_cap",
			errMissingConfigValue,
		)
	}
	config.Perf.MessageBytesCap = *(configWithPointers.Perf.MessageBytesCap)

	if configWithPointers.Perf.ReadHeaderTimeout == nil {
		return fmt.Errorf(
			"%w: perf.read_header_timeout",
			errMissingConfigValue,
		)
	}
	config.Perf.ReadHeaderTimeout = *(configWithPointers.Perf.ReadHeaderTimeout)

	if configWithPointers.Perf.UsemDelayShiftBits == nil {
		return fmt.Errorf(
			"%w: perf.usem_delay_shift_bits",
			errMissingConfigValue,
		)
	}
	config.Perf.UsemDelayShiftBits = *(configWithPointers.Perf.UsemDelayShiftBits)

	if configWithPointers.Perf.PropagateImmediate == nil {
		return fmt.Errorf(
			"%w: perf.propagate_immediate",
			errMissingConfigValue,
		)
	}
	config.Perf.PropagateImmediate = *(configWithPointers.Perf.PropagateImmediate)

	return nil
}