-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenant.go
355 lines (292 loc) · 8.23 KB
/
tenant.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"context"
"database/sql"
"encoding/json"
"errors"
"expvar"
"fmt"
"log/slog"
"os"
"path/filepath"
"sync"
"github.com/polyscone/tofu/app"
"github.com/polyscone/tofu/app/account"
"github.com/polyscone/tofu/app/system"
"github.com/polyscone/tofu/errsx"
"github.com/polyscone/tofu/event"
"github.com/polyscone/tofu/slogger"
"github.com/polyscone/tofu/smtp"
"github.com/polyscone/tofu/sqlite"
"github.com/polyscone/tofu/web"
"github.com/polyscone/tofu/web/guard"
"github.com/polyscone/tofu/web/handler"
)
var tenants = make(map[string]Tenant)
type repo struct {
account *sqlite.AccountRepo
system *sqlite.SystemRepo
web *sqlite.WebRepo
}
var cache = struct {
mu sync.Mutex
repos map[string]repo
sqlite map[string]*sqlite.DB
mailers map[string]smtp.Mailer
loggers map[string]*slog.Logger
metrics map[string]*expvar.Map
}{
repos: make(map[string]repo),
sqlite: make(map[string]*sqlite.DB),
mailers: make(map[string]smtp.Mailer),
loggers: make(map[string]*slog.Logger),
metrics: make(map[string]*expvar.Map),
}
func closeCache() {
cache.mu.Lock()
defer cache.mu.Unlock()
for alias, db := range cache.sqlite {
if db == nil {
continue
}
if err := db.Close(); err != nil {
slog.Error("close SQLite database connection", "alias", alias, "error", err)
}
}
}
// newTenant returns a tenant where the host is mapped to a shared name.
//
// Tenants share repositories along with their underlying database connection
// pools based on the name, and all tenants share an SMTP mailer regardless
// of name.
//
// Every tenant gets its own event broker regardless of name so that different
// adapters, even for those handling the same host, can respond to
// application events differently if required.
func newTenant(host string) (*handler.Tenant, error) {
ctx := context.Background()
data, ok := tenants[host]
if !ok {
return nil, fmt.Errorf("find tenant %v: %w", host, web.ErrTenantNotFound)
}
if data.Name == "" {
return nil, fmt.Errorf("name for the tenant %v is empty", host)
}
cache.mu.Lock()
defer cache.mu.Unlock()
metrics, ok := cache.metrics[data.Name]
if !ok {
metrics = expvar.NewMap("tenant." + data.Name)
cache.metrics[data.Name] = metrics
}
logger, ok := cache.loggers[data.Name]
if !ok {
var err error
logger, err = slogger.New(opts.log.style, nil)
if err != nil {
return nil, fmt.Errorf("new logger: %w", err)
}
logger = logger.With("app", data.Name)
logger = logger.With("kind", data.Kind)
cache.loggers[data.Name] = logger
}
repo, ok := cache.repos[data.Name]
if !ok {
var err error
sqliteDB := cache.sqlite[data.Name]
if sqliteDB == nil {
metricsKey := "database.SQLite"
dbMetrics, ok := metrics.Get(metricsKey).(*expvar.Map)
if !ok {
dbMetrics = &expvar.Map{}
dbMetrics.Set("stats", expvar.Func(func() any {
if sqliteDB.DB == nil {
return sql.DBStats{}
}
return sqliteDB.Stats()
}))
metrics.Set(metricsKey, dbMetrics)
}
p := filepath.Join(opts.data, data.Name, "main.sqlite")
sqliteDB, err = sqlite.Open(ctx, sqlite.KindFile, p, dbMetrics)
if err != nil {
return nil, fmt.Errorf("open SQLite database: %w", err)
}
cache.sqlite[data.Name] = sqliteDB
}
repo.account, err = sqlite.NewAccountRepo(ctx, sqliteDB, app.SignInThrottleTTL)
if err != nil {
return nil, fmt.Errorf("new account repo: %w", err)
}
repo.system, err = sqlite.NewSystemRepo(ctx, sqliteDB)
if err != nil {
return nil, fmt.Errorf("new system repo: %w", err)
}
repo.web, err = sqlite.NewWebRepo(ctx, sqliteDB, app.SessionTTL)
if err != nil {
return nil, fmt.Errorf("new web repo: %w", err)
}
cache.repos[data.Name] = repo
}
mailer, ok := cache.mailers[data.Name]
if !ok {
var err error
mailer, err = smtp.NewClient(logger, &smtpConfig{system: repo.system})
if err != nil {
return nil, fmt.Errorf("new dynamic SMTP client: %w", err)
}
cache.mailers[data.Name] = mailer
}
var svc handler.Svc
var err error
broker := event.NewMemoryBroker()
svc.Account, err = account.NewService(broker, repo.account, hasher, data.Kind)
if err != nil {
return nil, fmt.Errorf("new account service: %w", err)
}
svc.System, err = system.NewService(broker, repo.system)
if err != nil {
return nil, fmt.Errorf("new system service: %w", err)
}
var permissions []account.Permission
for _, group := range guard.PermissionGroups {
for _, p := range group.Permissions {
permission, err := account.NewPermission(p.Name)
if err != nil {
return nil, fmt.Errorf("new permission: %w", err)
}
permissions = append(permissions, permission)
}
}
id, err := repo.account.NextRoleID(ctx)
if err != nil {
return nil, fmt.Errorf("next role id: %w", err)
}
superRole := account.NewRole(id, "Super", "Has full access to the system; can't be edited or deleted.", permissions)
role, err := repo.account.FindRoleByName(ctx, superRole.Name)
switch {
case err == nil:
superRole.ID = role.ID
if err := repo.account.SaveRole(ctx, superRole); err != nil {
return nil, fmt.Errorf("save super role: %w", err)
}
case errors.Is(err, app.ErrNotFound):
if err := repo.account.AddRole(ctx, superRole); err != nil {
return nil, fmt.Errorf("add super role: %w", err)
}
default:
return nil, fmt.Errorf("find role by name: %w", err)
}
tenant := handler.Tenant{
Key: host + "." + data.Name,
Kind: data.Kind,
Hosts: data.Hosts,
Data: filepath.Join(opts.data, data.Name),
Dev: opts.dev,
Proxies: opts.server.proxies,
Broker: broker,
Email: mailer,
Logger: logger,
Metrics: metrics,
Svc: svc,
Repo: handler.Repo{
Account: repo.account,
System: repo.system,
Web: repo.web,
},
SuperRole: superRole,
}
return &tenant, nil
}
type Tenant struct {
Name string `json:"-"`
Kind string `json:"-"`
Hosts map[string]string `json:"hosts"`
Aliases map[string][]string `json:"aliases"`
IsDisabled bool `json:"isDisabled"`
}
func initTenants(tenantsPath string) error {
tenants = make(map[string]Tenant)
f, err := os.OpenFile(tenantsPath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("open or create tenants file: %w", err)
}
err = func() error {
defer f.Close()
info, err := f.Stat()
if err != nil {
return fmt.Errorf("tenants file stat info: %w", err)
}
if info.Size() == 0 {
example := map[string]Tenant{
"example": {
Hosts: map[string]string{
"site": "www.example.com",
"pwa": "app.example.com",
},
Aliases: map[string][]string{
"site": {"localhost", "localhost:8080"},
},
IsDisabled: true,
},
}
b, err := json.MarshalIndent(example, "", "\t")
if err != nil {
return fmt.Errorf("marshal example tenant file data: %w", err)
}
if _, err := f.Write(b); err != nil {
return fmt.Errorf("write example tenant file data: %w", err)
}
}
return nil
}()
if err != nil {
return err
}
tenantsData, err := os.ReadFile(tenantsPath)
if err != nil {
return fmt.Errorf("read tenants file: %w", err)
}
data := make(map[string]Tenant)
if err := json.Unmarshal([]byte(tenantsData), &data); err != nil {
return fmt.Errorf("unmarshal tenant data: %w", err)
}
var errs errsx.Map
for name, tenant := range data {
if len(tenant.Hosts) == 0 {
errs.Set(name+".hosts", "must be populated with at least one host")
}
if name == "" && len(tenant.Hosts) > 0 {
for host := range tenant.Hosts {
errs.Set("host "+host, "name cannot be empty")
}
continue
}
for kind, host := range tenant.Hosts {
if dupe, ok := tenants[host]; ok {
errs.Set(host, fmt.Sprintf("cannot associate with %q; already associated with %q", name, dupe.Name))
}
tenant.Name = name
tenant.Kind = kind
if !tenant.IsDisabled {
tenants[host] = tenant
}
}
for kind, hosts := range tenant.Aliases {
for _, host := range hosts {
if dupe, ok := tenants[host]; ok {
errs.Set(host, fmt.Sprintf("cannot associate with %q; already associated with %q", name, dupe.Name))
}
tenant.Name = name
tenant.Kind = kind
if !tenant.IsDisabled {
tenants[host] = tenant
}
}
}
}
if errs != nil {
return fmt.Errorf("tenant configuration errors: %w", errs)
}
return nil
}