/
instance.go
553 lines (463 loc) · 13.5 KB
/
instance.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package instance
import (
"crypto/subtle"
"errors"
"fmt"
"net/url"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/cozy/cozy-stack/pkg/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/couchdb/mango"
"github.com/cozy/cozy-stack/pkg/crypto"
"github.com/cozy/cozy-stack/pkg/jobs"
"github.com/cozy/cozy-stack/pkg/permissions"
"github.com/cozy/cozy-stack/pkg/settings"
"github.com/cozy/cozy-stack/pkg/vfs"
"github.com/cozy/cozy-stack/web/jsonapi"
"github.com/spf13/afero"
)
/* #nosec */
const (
registerTokenLen = 16
sessionSecretLen = 64
oauthSecretLen = 128
)
// DefaultLocale is the default locale when creating an instance
const DefaultLocale = "en"
var (
// ErrNotFound is used when the seeked instance was not found
ErrNotFound = errors.New("Instance not found")
// ErrExists is used the instance already exists
ErrExists = errors.New("Instance already exists")
// ErrIllegalDomain is used when the domain named contains illegal characters
ErrIllegalDomain = errors.New("Domain name contains illegal characters")
// ErrMissingToken is returned by RegisterPassphrase if token is empty
ErrMissingToken = errors.New("Empty register token")
// ErrInvalidToken is returned by RegisterPassphrase if token is invalid
ErrInvalidToken = errors.New("Invalid register token")
// ErrMissingPassphrase is returned when the new passphrase is missing
ErrMissingPassphrase = errors.New("Missing new passphrase")
// ErrInvalidPassphrase is returned when the passphrase is invalid
ErrInvalidPassphrase = errors.New("Invalid passphrase")
)
// An Instance has the informations relatives to the logical cozy instance,
// like the domain, the locale or the access to the databases and files storage
// It is a couchdb.Doc to be persisted in couchdb.
type Instance struct {
DocID string `json:"_id,omitempty"` // couchdb _id
DocRev string `json:"_rev,omitempty"` // couchdb _rev
Domain string `json:"domain"` // The main DNS domain, like example.cozycloud.cc
Locale string `json:"locale"` // The locale used on the server
StorageURL string `json:"storage"` // Where the binaries are persisted
Dev bool `json:"dev"` // Whether or not the instance is for development
// PassphraseHash is a hash of the user's passphrase. For more informations,
// see crypto.GenerateFromPassphrase.
PassphraseHash []byte `json:"passphrase_hash,omitempty"`
// Secure assets
// Register token is used on registration to prevent from stealing instances
// waiting for registration. The registerToken secret is only shared (in
// clear) with the instance's user.
RegisterToken []byte `json:"register_token,omitempty"`
// SessionSecret is used to authenticate session cookies
SessionSecret []byte `json:"session_secret,omitempty"`
// OAuthSecret is used to authenticate OAuth2 token
OAuthSecret []byte `json:"oauth_secret,omitempty"`
storage afero.Fs
}
// Options holds the parameters to create a new instance.
type Options struct {
Domain string
Locale string
Timezone string
Email string
Apps []string
Dev bool
}
// DocType implements couchdb.Doc
func (i *Instance) DocType() string { return consts.Instances }
// ID implements couchdb.Doc
func (i *Instance) ID() string { return i.DocID }
// SetID implements couchdb.Doc
func (i *Instance) SetID(v string) { i.DocID = v }
// Rev implements couchdb.Doc
func (i *Instance) Rev() string { return i.DocRev }
// SetRev implements couchdb.Doc
func (i *Instance) SetRev(v string) { i.DocRev = v }
// SelfLink is used to generate a JSON-API link for the instance
func (i *Instance) SelfLink() string {
return "/instances/" + i.DocID
}
// Relationships is used to generate the content relationship in JSON-API format
func (i *Instance) Relationships() jsonapi.RelationshipMap {
return jsonapi.RelationshipMap{}
}
// Included is part of the jsonapi.Object interface
func (i *Instance) Included() []jsonapi.Object {
return nil
}
// settings is a struct used for the settings of an instance
type instanceSettings struct {
Timezone string `json:"tz,omitempty"`
Email string `json:"email,omitempty"`
}
func (s *instanceSettings) ID() string { return consts.InstanceSettingsID }
func (s *instanceSettings) Rev() string { return "" }
func (s *instanceSettings) DocType() string { return consts.Settings }
func (s *instanceSettings) SetID(_ string) {}
func (s *instanceSettings) SetRev(_ string) {}
// FS returns the afero storage provider where the binaries for
// the current instance are persisted
func (i *Instance) FS() afero.Fs {
if i.storage == nil {
if err := i.makeStorageFs(); err != nil {
panic(err)
}
}
return i.storage
}
// JobsBroker returns the jobs broker associated with the instance
func (i *Instance) JobsBroker() jobs.Broker {
return jobs.NewMemBroker(i.Domain, jobs.GetWorkersList())
}
// Prefix returns the prefix to use in database naming for the
// current instance
func (i *Instance) Prefix() string {
return i.Domain + "/"
}
// Scheme returns the scheme used for URLs. It is https by default and http
// for development instances.
func (i *Instance) Scheme() string {
if i.Dev {
return "http"
}
return "https"
}
// SubDomain returns the full url for a subdomain of this instance
// useful with apps slugs
func (i *Instance) SubDomain(s string) string {
var domain string
if config.GetConfig().Subdomains == config.NestedSubdomains {
domain = s + "." + i.Domain
} else {
parts := strings.SplitN(i.Domain, ".", 2)
domain = parts[0] + "-" + s + "." + parts[1]
}
u := url.URL{
Scheme: i.Scheme(),
Host: domain,
Path: "/",
}
return u.String()
}
// FromURL normalizes a given url with the scheme and domain of the instance.
func (i *Instance) FromURL(u *url.URL) string {
u2 := url.URL{
Scheme: i.Scheme(),
Host: i.Domain,
Path: u.Path,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return u2.String()
}
// PageURL returns the full URL for a path on the cozy stack
func (i *Instance) PageURL(path string, queries url.Values) string {
var query string
if queries != nil {
query = queries.Encode()
}
u := url.URL{
Scheme: i.Scheme(),
Host: i.Domain,
Path: path,
RawQuery: query,
}
return u.String()
}
// createInCouchdb creates the instance doc in the global database
func (i *Instance) createInCouchdb() (err error) {
if _, err = Get(i.Domain); err == nil {
return ErrExists
}
if err != nil && err != ErrNotFound {
return err
}
err = couchdb.CreateDoc(couchdb.GlobalDB, i)
if err != nil {
return err
}
byDomain := mango.IndexOnFields("domain")
return couchdb.DefineIndex(couchdb.GlobalDB, consts.Instances, byDomain)
}
// createRootDir creates the root directory for this instance
func (i *Instance) createRootDir() error {
rootFsURL := config.BuildAbsFsURL("/")
domainURL := config.BuildRelFsURL(i.Domain)
rootFs, err := createFs(rootFsURL)
if err != nil {
return err
}
if err = rootFs.MkdirAll(domainURL.Path, 0755); err != nil {
return err
}
defer func() {
if err != nil {
if rmerr := rootFs.RemoveAll(domainURL.Path); rmerr != nil {
log.Warn("[instance] Could not remove the instance directory")
}
}
}()
if err = vfs.CreateRootDirDoc(i); err != nil {
return err
}
if err = vfs.CreateTrashDir(i); err != nil {
return err
}
return nil
}
// createFSIndexes creates the indexes needed by VFS
func (i *Instance) createFSIndexes() error {
for _, index := range vfs.Indexes {
err := couchdb.DefineIndex(i, consts.Files, index)
if err != nil {
return err
}
}
return couchdb.DefineViews(i, consts.Files, vfs.Views)
}
// createAppsDB creates the database needed for Apps
func (i *Instance) createAppsDB() error {
return couchdb.CreateDB(i, consts.Manifests)
}
// createSettings creates the settings database and some documents like the
// default theme
func (i *Instance) createSettings() error {
err := couchdb.CreateDB(i, consts.Settings)
if err != nil {
return err
}
return settings.CreateDefaultTheme(i)
}
func (i *Instance) createPermissionsDB() error {
err := couchdb.CreateDB(i, consts.Permissions)
if err != nil {
return err
}
return couchdb.DefineIndex(i, consts.Permissions, permissions.Index)
}
// Create builds an instance and initializes it
func Create(opts *Options) (*Instance, error) {
domain := opts.Domain
if strings.ContainsAny(domain, vfs.ForbiddenFilenameChars) || domain == ".." || domain == "." {
return nil, ErrIllegalDomain
}
if config.GetConfig().Subdomains == config.FlatSubdomains {
parts := strings.SplitN(domain, ".", 2)
if strings.Contains(parts[0], "-") {
return nil, ErrIllegalDomain
}
}
locale := opts.Locale
if locale == "" {
locale = DefaultLocale
}
i := new(Instance)
i.Locale = locale
i.Domain = domain
i.StorageURL = config.BuildRelFsURL(domain).String()
i.Dev = opts.Dev
i.PassphraseHash = nil
i.RegisterToken = crypto.GenerateRandomBytes(registerTokenLen)
i.SessionSecret = crypto.GenerateRandomBytes(sessionSecretLen)
i.OAuthSecret = crypto.GenerateRandomBytes(oauthSecretLen)
var err error
err = i.makeStorageFs()
if err != nil {
return nil, err
}
err = i.createInCouchdb()
if err != nil {
return nil, err
}
err = i.createRootDir()
if err != nil {
return nil, err
}
err = i.createAppsDB()
if err != nil {
return nil, err
}
err = i.createSettings()
if err != nil {
return nil, err
}
err = i.createFSIndexes()
if err != nil {
return nil, err
}
err = i.createPermissionsDB()
if err != nil {
return nil, err
}
doc := &instanceSettings{
Timezone: opts.Timezone,
Email: opts.Email,
}
if err = couchdb.CreateNamedDoc(i, doc); err != nil {
return nil, err
}
// TODO atomicity with defer
// TODO figure out what to do with locale
// TODO install apps
return i, nil
}
func (i *Instance) makeStorageFs() error {
u, err := url.Parse(i.StorageURL)
if err != nil {
return err
}
i.storage, err = createFs(u)
return err
}
// Get retrieves the instance for a request by its host.
func Get(domain string) (*Instance, error) {
var instances []*Instance
req := &couchdb.FindRequest{
Selector: mango.Equal("domain", domain),
Limit: 1,
}
err := couchdb.FindDocs(couchdb.GlobalDB, consts.Instances, req, &instances)
if couchdb.IsNoDatabaseError(err) {
return nil, ErrNotFound
}
if err != nil {
return nil, err
}
if len(instances) == 0 {
return nil, ErrNotFound
}
err = instances[0].makeStorageFs()
if err != nil {
return nil, err
}
return instances[0], nil
}
// List returns the list of declared instances.
//
// TODO: pagination
// TODO: don't return the design docs
func List() ([]*Instance, error) {
var docs []*Instance
req := &couchdb.AllDocsRequest{Limit: 100}
err := couchdb.GetAllDocs(couchdb.GlobalDB, consts.Instances, req, &docs)
if err != nil {
return nil, err
}
return docs, nil
}
// Destroy is used to remove the instance. All the data linked to this
// instance will be permanently deleted.
func Destroy(domain string) (*Instance, error) {
i, err := Get(domain)
if err != nil {
return nil, err
}
if err = couchdb.DeleteDoc(couchdb.GlobalDB, i); err != nil {
return nil, err
}
if err = couchdb.DeleteAllDBs(i); err != nil {
return nil, err
}
rootFsURL := config.BuildAbsFsURL("/")
domainURL := config.BuildRelFsURL(i.Domain)
rootFs, err := createFs(rootFsURL)
if err != nil {
return nil, err
}
if err = rootFs.RemoveAll(domainURL.Path); err != nil {
return nil, err
}
return i, nil
}
// RegisterPassphrase replace the instance registerToken by a passphrase
func (i *Instance) RegisterPassphrase(pass, tok []byte) error {
if len(pass) == 0 {
return ErrMissingPassphrase
}
if len(i.RegisterToken) == 0 {
return ErrMissingToken
}
if subtle.ConstantTimeCompare(i.RegisterToken, tok) != 1 {
return ErrInvalidToken
}
hash, err := crypto.GenerateFromPassphrase(pass)
if err != nil {
return err
}
i.RegisterToken = nil
i.setPassphraseAndSecret(hash)
return couchdb.UpdateDoc(couchdb.GlobalDB, i)
}
// UpdatePassphrase replace the passphrase
func (i *Instance) UpdatePassphrase(pass, current []byte) error {
if len(pass) == 0 {
return ErrMissingPassphrase
}
// the needUpdate flag is not checked against since the passphrase will be
// regenerated with updated parameters just after, if the passphrase match.
_, err := crypto.CompareHashAndPassphrase(i.PassphraseHash, current)
if err != nil {
return ErrInvalidPassphrase
}
hash, err := crypto.GenerateFromPassphrase(pass)
if err != nil {
return err
}
i.setPassphraseAndSecret(hash)
return couchdb.UpdateDoc(couchdb.GlobalDB, i)
}
func (i *Instance) setPassphraseAndSecret(hash []byte) {
i.PassphraseHash = hash
i.SessionSecret = crypto.GenerateRandomBytes(sessionSecretLen)
}
// CheckPassphrase confirm an instance passport
func (i *Instance) CheckPassphrase(pass []byte) error {
if len(pass) == 0 {
return ErrMissingPassphrase
}
needUpdate, err := crypto.CompareHashAndPassphrase(i.PassphraseHash, pass)
if err != nil {
return err
}
if !needUpdate {
return nil
}
newHash, err := crypto.GenerateFromPassphrase(pass)
if err != nil {
return err
}
i.PassphraseHash = newHash
err = couchdb.UpdateDoc(couchdb.GlobalDB, i)
if err != nil {
log.Error("[instance] Failed to update hash in db", err)
}
return nil
}
func createFs(u *url.URL) (fs afero.Fs, err error) {
switch u.Scheme {
case "file":
fs = afero.NewBasePathFs(afero.NewOsFs(), u.Path)
case "mem":
fs = afero.NewMemMapFs()
default:
err = fmt.Errorf("Unknown storage provider: %v", u.Scheme)
}
return
}
// ensure Instance implements couchdb.Doc & vfs.Context
var (
_ couchdb.Doc = &Instance{}
_ vfs.Context = &Instance{}
)