-
Notifications
You must be signed in to change notification settings - Fork 3
/
memresolver.go
180 lines (157 loc) · 4.36 KB
/
memresolver.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
/*
* Copyright 2018-2019 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package authb
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/nats-io/jwt/v2"
)
type MemResolverConfigBuilder struct {
operator string
claims map[string]string
pubToName map[string]string
dir string
sysAccount string
}
func NewMemResolverConfigBuilder() *MemResolverConfigBuilder {
cb := MemResolverConfigBuilder{}
cb.claims = make(map[string]string)
cb.pubToName = make(map[string]string)
return &cb
}
func (cb *MemResolverConfigBuilder) SetOutputDir(fp string) error {
cb.dir = fp
return nil
}
func (cb *MemResolverConfigBuilder) SetSystemAccount(id string) error {
cb.sysAccount = id
return nil
}
func (cb *MemResolverConfigBuilder) Add(rawClaim []byte) error {
token := string(rawClaim)
gc, err := jwt.DecodeGeneric(token)
if err != nil {
return err
}
switch gc.ClaimType() {
case jwt.OperatorClaim:
oc, err := jwt.DecodeOperatorClaims(token)
if err != nil {
return err
}
cb.operator = token
cb.pubToName[oc.Subject] = oc.Name
cb.pubToName["__OPERATOR__"] = oc.Subject
case jwt.AccountClaim:
ac, err := jwt.DecodeAccountClaims(token)
if err != nil {
return err
}
cb.claims[ac.Subject] = token
cb.pubToName[ac.Subject] = ac.Name
}
return nil
}
func (cb *MemResolverConfigBuilder) GenerateConfig() ([]byte, error) {
var buf bytes.Buffer
opk := cb.pubToName["__OPERATOR__"]
if opk == "" {
return nil, errors.New("operator is not set")
}
buf.WriteString(fmt.Sprintf("// Operator %q\n", cb.pubToName[opk]))
buf.WriteString(fmt.Sprintf("operator: %s\n\n", cb.operator))
if cb.sysAccount != "" {
buf.WriteString(fmt.Sprintf("system_account: %s\n\n", cb.sysAccount))
}
var keys []string
for k := range cb.claims {
keys = append(keys, k)
}
sort.Strings(keys)
buf.WriteString("resolver: MEMORY\n\n")
buf.WriteString("resolver_preload: {\n")
for _, k := range keys {
v := cb.claims[k]
buf.WriteString(fmt.Sprintf(" // Account %q\n", cb.pubToName[k]))
buf.WriteString(fmt.Sprintf(" %s: %s\n\n", k, v))
}
buf.WriteString("}\n")
return buf.Bytes(), nil
}
func JwtName(name string) string {
return fmt.Sprintf("%s.jwt", strings.TrimSpace(name))
}
func (cb *MemResolverConfigBuilder) writeFile(dir string, name string, token string) (string, error) {
fp := filepath.Join(dir, JwtName(name))
err := os.WriteFile(fp, []byte(token), 0o666)
return fp, err
}
func (cb *MemResolverConfigBuilder) GenerateDir() ([]byte, error) {
var buf bytes.Buffer
if err := os.MkdirAll(cb.dir, 0o700); err != nil {
return nil, err
}
opk := cb.pubToName["__OPERATOR__"]
if opk == "" {
return nil, errors.New("operator is not set")
}
fn, err := cb.writeFile(cb.dir, cb.pubToName[opk], cb.operator)
if err != nil {
return nil, err
}
buf.WriteString(fmt.Sprintf("// Operator %q\n", cb.pubToName[opk]))
buf.WriteString(fmt.Sprintf("operator: %q\n\n", filepath.Join(".", filepath.Base(fn))))
if cb.sysAccount != "" {
buf.WriteString(fmt.Sprintf("system_account: %s\n\n", cb.sysAccount))
}
var keys []string
for k := range cb.claims {
keys = append(keys, k)
}
sort.Strings(keys)
buf.WriteString("resolver: MEMORY\n\n")
buf.WriteString("resolver_preload: {\n")
for _, k := range keys {
v := cb.claims[k]
n := cb.pubToName[k]
buf.WriteString(fmt.Sprintf(" // Account %q\n", n))
fn, err := cb.writeFile(cb.dir, n, v)
if err != nil {
return nil, err
}
rel, err := filepath.Rel(cb.dir, fn)
if err != nil {
return nil, err
}
buf.WriteString(fmt.Sprintf(" %s: %q\n\n", k, rel))
}
buf.WriteString("}\n")
err = os.WriteFile(filepath.Join(cb.dir, "resolver.conf"), buf.Bytes(), 0o666)
if err != nil {
return nil, err
}
return nil, nil
}
func (cb *MemResolverConfigBuilder) Generate() ([]byte, error) {
if cb.dir != "" {
return cb.GenerateDir()
}
return cb.GenerateConfig()
}