This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dir_account_store.go
93 lines (76 loc) · 1.92 KB
/
dir_account_store.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
package network
import (
"context"
"errors"
"fmt"
"sync"
"github.com/nats-io/jwt"
gnatsd "github.com/nats-io/nats-server/v2/server"
nsc "github.com/nats-io/nsc/cmd/store"
)
// reads account JWT files from a NSC format directory, implements gnatsd.AccountResolver
type dirAccountStore struct {
srv accountNotificationReceiver
store string
nsc *nsc.Store
sync.Mutex
}
type accountNotificationReceiver interface {
LookupAccount(name string) (*gnatsd.Account, error)
UpdateAccountClaims(a *gnatsd.Account, ac *jwt.AccountClaims)
}
func newDirAccountStore(s accountNotificationReceiver, store string) (as *dirAccountStore, err error) {
nscStore, err := nsc.LoadStore(store)
if err != nil {
return nil, fmt.Errorf("could not load NSC format store %s: %s", store, err)
}
return &dirAccountStore{
srv: s,
store: store,
nsc: nscStore,
}, nil
}
func (f *dirAccountStore) Start(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
// TODO monitor files
for {
select {
case <-ctx.Done():
return
}
}
}
func (f *dirAccountStore) Stop() {
// noop till we have file notify
}
// Fetch implements gnatsd.AccountResolver
func (f *dirAccountStore) Fetch(name string) (jwt string, err error) {
f.Lock()
defer f.Unlock()
infos, err := f.nsc.List(nsc.Accounts)
if err != nil {
return "", err
}
for _, i := range infos {
if i.IsDir() {
c, err := f.nsc.LoadClaim(nsc.Accounts, i.Name(), nsc.JwtName(i.Name()))
if err != nil {
return "", err
}
if c != nil {
if c.Subject == name {
data, err := f.nsc.Read(nsc.Accounts, i.Name(), nsc.JwtName(i.Name()))
if err != nil {
return "", err
}
return string(data), nil
}
}
}
}
return "", fmt.Errorf("no matching JWT found for %s", name)
}
// Store implements gnatsd.AccountResolver
func (f *dirAccountStore) Store(name string, jwt string) error {
return errors.New("dirAccountStore does not support writing")
}