This repository has been archived by the owner on May 16, 2024. It is now read-only.
/
config.go
69 lines (58 loc) · 1.78 KB
/
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
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
package internal
import (
"time"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tidwall/gjson"
)
const (
AuthTimeoutDefault = time.Second
)
// Config is used to extract any WASMPlugin configuration defined in the deployed YML
type Config struct {
AuthClusterName string
AuthAuthority string
AuthTimeout uint32
}
func NewConfig() *Config {
configuration := getPluginConfiguration()
config := Config{
AuthClusterName: getStringFromConfig(configuration, "auth_cluster_name"),
AuthAuthority: getStringFromConfig(configuration, "auth_authority"),
AuthTimeout: uint32(getInt64FromConfig(configuration, "auth_timeout_ms", AuthTimeoutDefault.Milliseconds())),
}
return &config
}
func getPluginConfiguration() gjson.Result {
proxywasm.LogWarnf("Getting WASM plugin config...")
configuration, err := proxywasm.GetPluginConfiguration()
if err != nil {
proxywasm.LogCriticalf("error reading plugin configuration: %v", err)
}
if len(configuration) == 0 {
proxywasm.LogCritical("WASM plugin config was empty")
return gjson.Result{}
}
if !gjson.ValidBytes(configuration) {
proxywasm.LogCriticalf("WASM plugin config was invalid: %s", configuration)
return gjson.Result{}
}
result := gjson.ParseBytes(configuration)
return result
}
func getStringFromConfig(configuration gjson.Result, key string) string {
result := configuration.Get(key)
if result.Exists() {
return result.String()
}
proxywasm.LogCriticalf("Configuration for '%s' wasn't set in config:%s", key, configuration)
return ""
}
func getInt64FromConfig(configuration gjson.Result, key string, defaultResult int64) int64 {
result := configuration.Get(key)
if result.Exists() {
return result.Int()
}
return defaultResult
}