-
Notifications
You must be signed in to change notification settings - Fork 276
/
zpu_config.go
228 lines (206 loc) · 6.27 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
// Copyright 2017 Yahoo Holdings, Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zpu
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/AthenZ/athenz/libs/go/zmssvctoken"
"github.com/AthenZ/athenz/utils/zpe-updater/util"
)
// 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
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
}
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"`
}
func NewZpuConfiguration(root, athensConfFile, zpuConfFile 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", 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", 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"
}
return &ZpuConfiguration{
Zts: athenzConf.ZtsUrl,
Zms: athenzConf.ZmsUrl,
DomainList: zpuConf.Domains,
ZpuOwner: user,
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,
}, nil
}
func ReadAthenzConf(athenzConf string) (*AthenzConf, error) {
var aConf *AthenzConf
if !util.Exists(athenzConf) {
return nil, fmt.Errorf("The Athenz configuration file does not exist at path: %v", athenzConf)
}
data, err := ioutil.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("The ZPU configuration file does not exist at the given path: %v", zpuConf)
}
data, err := ioutil.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) GetZmsPublicKey(key string) string {
for k := range config.ZmsKeysmap {
if k == key {
return config.ZmsKeysmap[key]
}
}
return ""
}