/
zpu_config.go
314 lines (284 loc) · 9.06 KB
/
zpu_config.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
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zpu
import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"os"
"strconv"
"github.com/AthenZ/athenz/clients/go/zts"
"github.com/AthenZ/athenz/libs/go/athenz-common/log"
siautil "github.com/AthenZ/athenz/libs/go/sia/util"
"github.com/AthenZ/athenz/libs/go/zmssvctoken"
"github.com/AthenZ/athenz/utils/zpe-updater/util"
"github.com/ardielle/ardielle-go/rdl"
"github.com/go-jose/go-jose/v4"
)
// Default and maximal startup delay values.
const (
DEFAULT_STARTUP_DELAY = 0
MAX_STARTUP_DELAY = 1440
DEFAULT_EXPIRY_CHECK = 2880
)
type ZpuConfiguration struct {
Zts string
Zms string
DomainList string
ZpuOwner string
PolicyFileDir string
TempPolicyFileDir string
SiaDir string
MetricsDir string
ZmsKeysmap map[string]string
ZtsKeysmap map[string]string
StartUpDelay int
ExpiryCheck int
LogSize int
LogAge int
LogBackups int
LogCompression bool
PrivateKeyFile string
CertFile string
CaCertFile string
Proxy bool
CheckZMSSignature bool
JWSPolicySupport bool
PolicyVersions map[string]string
ForceRefresh bool
ExpiredFunc func(rdl.Timestamp) bool
MinutesBetweenZtsCalls int
}
type AthenzConf struct {
ZtsUrl string `json:"ztsUrl"`
ZmsUrl string `json:"zmsUrl"`
ZtsPublicKeys []struct {
Id string `json:"id"`
Key string `json:"key"`
} `json:"ztsPublicKeys"`
ZmsPublicKeys []struct {
Id string `json:"id"`
Key string `json:"key"`
} `json:"zmsPublicKeys"`
}
type ZpuConf struct {
Domains string `json:"domains"`
User string `json:"user"`
PolicyDir string `json:"policyDir"`
TempPolicyDir string `json:"tempPolicyDir"`
MetricsDir string `json:"metricsDir"`
LogMaxSize int `json:"logMaxsize"`
LogMaxAge int `json:"logMaxage"`
LogMaxBackups int `json:"logMaxbackups"`
LogCompress bool `json:"logCompress"`
PrivateKey string `json:"privateKeyFile"`
CertFile string `json:"certFile"`
CaCertFile string `json:"caCertFile"`
Proxy bool `json:"proxy"`
ExpiryCheck int `json:"expiryCheck"`
CheckZMSSignature bool `json:"checkZMSSignature"`
JWSPolicySupport bool `json:"jwsPolicySupport"`
PolicyVersions map[string]string `json:"policyVersions"`
}
func NewZpuConfiguration(root, athensConfFile, zpuConfFile, siaDir string) (*ZpuConfiguration, error) {
zmsKeysmap := make(map[string]string)
ztsKeysmap := make(map[string]string)
athenzConf, err := ReadAthenzConf(athensConfFile)
if err != nil {
return nil, err
}
zpuConf, err := ReadZpuConf(zpuConfFile)
if err != nil {
return nil, err
}
for _, publicKey := range athenzConf.ZtsPublicKeys {
if _, exists := ztsKeysmap[publicKey.Id]; exists {
log.Printf("Zts public Key with id: %v already existed, overwriting it with new value\n", publicKey.Id)
}
key, err := new(zmssvctoken.YBase64).DecodeString(publicKey.Key)
if err != nil {
return nil, fmt.Errorf("unable to decode Zts public Key with id: %v, Error: %v", publicKey.Id, err)
}
ztsKeysmap[publicKey.Id] = string(key)
}
for _, publicKey := range athenzConf.ZmsPublicKeys {
if _, exists := zmsKeysmap[publicKey.Id]; exists {
log.Printf("Zms public Key with id: %v already existed, overwriting it with new value\n", publicKey.Id)
}
key, err := new(zmssvctoken.YBase64).DecodeString(publicKey.Key)
if err != nil {
return nil, fmt.Errorf("unable to decode Zms public Key with id: %v, Error: %v", publicKey.Id, err)
}
zmsKeysmap[publicKey.Id] = string(key)
}
startupDelay := DEFAULT_STARTUP_DELAY
startupDelayString := os.Getenv("STARTUP_DELAY")
if startupDelayString != "" {
startupDelay, err = strconv.Atoi(startupDelayString)
if err != nil {
return nil, fmt.Errorf("unable to set start up delay, Error: %v", err)
}
}
if startupDelay < 0 {
startupDelay = DEFAULT_STARTUP_DELAY
}
if startupDelay > MAX_STARTUP_DELAY {
startupDelay = MAX_STARTUP_DELAY
}
startupDelay *= 60 // convert from min to secs
expiryCheck := zpuConf.ExpiryCheck
if expiryCheck == 0 {
expiryCheck = DEFAULT_EXPIRY_CHECK
}
expiryCheck *= 60 // convert from min to secs
policyDir := zpuConf.PolicyDir
defaultPolicyDir := fmt.Sprintf("%s/var/zpe", root)
if policyDir == "" {
policyDir = defaultPolicyDir
}
tempPolicyDir := zpuConf.TempPolicyDir
defaultTempPolicyDir := fmt.Sprintf("%s/tmp/zpe", root)
if tempPolicyDir == "" {
tempPolicyDir = defaultTempPolicyDir
}
metricDir := zpuConf.MetricsDir
defaultMetricDir := fmt.Sprintf("%s/var/zpe_stat", root)
if metricDir == "" {
metricDir = defaultMetricDir
}
user := zpuConf.User
if user == "" {
user = "root"
}
zpuConfiguration := ZpuConfiguration{
Zts: athenzConf.ZtsUrl,
Zms: athenzConf.ZmsUrl,
DomainList: zpuConf.Domains,
ZpuOwner: user,
SiaDir: siaDir,
PolicyFileDir: policyDir,
TempPolicyFileDir: tempPolicyDir,
MetricsDir: metricDir,
ZtsKeysmap: ztsKeysmap,
ZmsKeysmap: zmsKeysmap,
StartUpDelay: startupDelay,
ExpiryCheck: expiryCheck,
LogAge: zpuConf.LogMaxAge,
LogSize: zpuConf.LogMaxSize,
LogBackups: zpuConf.LogMaxBackups,
LogCompression: zpuConf.LogCompress,
PrivateKeyFile: zpuConf.PrivateKey,
CaCertFile: zpuConf.CaCertFile,
CertFile: zpuConf.CertFile,
Proxy: zpuConf.Proxy,
CheckZMSSignature: zpuConf.CheckZMSSignature,
JWSPolicySupport: zpuConf.JWSPolicySupport,
PolicyVersions: zpuConf.PolicyVersions,
}
zpuConfiguration.loadAthenzJwks()
return &zpuConfiguration, nil
}
func (config ZpuConfiguration) loadAthenzJwks() {
jwkConfFile := fmt.Sprintf("%s/"+siautil.JwkConfFile, config.SiaDir)
jwkConf := &zts.AthenzJWKConfig{}
err := siautil.ReadAthenzJwkConf(jwkConfFile, jwkConf)
if err != nil {
log.Printf(err.Error())
return
}
loadJwkList(jwkConf.Zts.Keys, config.ZtsKeysmap)
loadJwkList(jwkConf.Zms.Keys, config.ZmsKeysmap)
log.Debugf("athenz jwks loaded successfully")
}
func (config ZpuConfiguration) updateZtsJwks(ztsJwks *zts.JWKList) {
loadJwkList(ztsJwks.Keys, config.ZtsKeysmap)
log.Debugf("zts jwks updated successfully")
}
func loadJwkList(jwkList []*zts.JWK, keysMap map[string]string) {
for _, jwk := range jwkList {
keyBytes, err := jwkToPem(jwk)
if err != nil {
log.Printf("failed to convert jwk, err: %s", err.Error())
continue
}
key := string(keyBytes)
if val, ok := keysMap[jwk.Kid]; ok {
log.Debugf("key id: [%s] already exist in the public keys map. replacing value from: [%s] to: [%s]", jwk.Kid, val, key)
}
keysMap[jwk.Kid] = key
}
}
func jwkToPem(athenzJwk *zts.JWK) ([]byte, error) {
jwkJson, err := json.Marshal(athenzJwk)
if err != nil {
return nil, err
}
var jwk jose.JSONWebKey
err = jwk.UnmarshalJSON(jwkJson)
if err != nil {
return nil, err
}
res, err := x509.MarshalPKIXPublicKey(jwk.Key)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: res,
},
), nil
}
func ReadAthenzConf(athenzConf string) (*AthenzConf, error) {
var aConf *AthenzConf
if !util.Exists(athenzConf) {
return nil, fmt.Errorf("athenz configuration file does not exist at path: %v", athenzConf)
}
data, err := os.ReadFile(athenzConf)
if err != nil {
return nil, fmt.Errorf("failed to read the Athenz configuration file, Error:%v", err)
}
err = json.Unmarshal(data, &aConf)
if err != nil {
return nil, fmt.Errorf("failed to parse the Athenz configuration file, Error:%v", err)
}
return aConf, nil
}
func ReadZpuConf(zpuConf string) (*ZpuConf, error) {
var zConf *ZpuConf
if !util.Exists(zpuConf) {
return nil, fmt.Errorf("zpu configuration file does not exist at the given path: %v", zpuConf)
}
data, err := os.ReadFile(zpuConf)
if err != nil {
return nil, fmt.Errorf("failed to read the Zpu configuration file, Error:%v", err)
}
err = json.Unmarshal(data, &zConf)
if err != nil {
return nil, fmt.Errorf("failed to parse the Zpu configuration file, Error:%v", err)
}
return zConf, nil
}
func (config ZpuConfiguration) GetZtsPublicKey(key string) string {
for k := range config.ZtsKeysmap {
if k == key {
return config.ZtsKeysmap[key]
}
}
return ""
}
func (config ZpuConfiguration) PutZtsPublicKey(key, publicKey string) {
config.ZtsKeysmap[key] = publicKey
}
func (config ZpuConfiguration) GetZmsPublicKey(key string) string {
for k := range config.ZmsKeysmap {
if k == key {
return config.ZmsKeysmap[key]
}
}
return ""
}
func (config ZpuConfiguration) PutZmsPublicKey(key, publicKey string) {
config.ZmsKeysmap[key] = publicKey
}