-
Notifications
You must be signed in to change notification settings - Fork 25
/
topaz_config.go
115 lines (94 loc) · 3.01 KB
/
topaz_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
package config
import (
"strings"
"github.com/aserto-dev/aserto-management/controller"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
const ConfigFileVersion = 2
type Config struct {
Common `json:",squash"` // nolint:staticcheck // squash is used by mapstructure
Auth AuthnConfig `json:"auth"`
DecisionLogger DecisionLogConfig `json:"decision_logger"`
ControllerConfig *controller.Config `json:"controller"`
}
type DecisionLogConfig struct {
Type string `json:"type"`
Config map[string]interface{} `json:"config"`
}
type AuthnConfig struct {
APIKeys map[string]string `json:"api_keys"`
Options CallOptions `json:"options"`
}
type CallOptions struct {
Default Options `json:"default"`
Overrides []OptionOverrides `json:"overrides"`
}
type Options struct {
// API Key for machine-to-machine communication, internal to Aserto
EnableAPIKey bool `json:"enable_api_key"`
// Allows calls without any form of authentication
EnableAnonymous bool `json:"enable_anonymous"`
}
type OptionOverrides struct {
// API paths to override
Paths []string `json:"paths"`
// Override options
Override Options `json:"override"`
}
func (co *CallOptions) ForPath(path string) *Options {
for _, override := range co.Overrides {
for _, prefix := range override.Paths {
if strings.HasPrefix(strings.ToLower(path), prefix) {
return &override.Override
}
}
}
return &co.Default
}
func defaults(v *viper.Viper) {
}
func validateVersion(version int) error {
if version != ConfigFileVersion {
return errors.New("unsupported config version")
}
return nil
}
func (c *Config) validation() error {
if _, ok := c.APIConfig.Services["authorizer"]; ok {
if c.Command.Mode == CommandModeRun && c.OPA.InstanceID == "" {
return errors.New("opa.instance_id not set")
}
if len(c.OPA.Config.Bundles) > 1 {
return errors.New("opa.config.bundles - too many bundles")
}
}
if len(c.APIConfig.Services) == 0 {
return errors.New("no api services configured")
}
setDefaultCallsAuthz(c)
if len(c.Auth.APIKeys) > 0 {
c.Auth.Options.Default.EnableAPIKey = true
} else {
c.Auth.Options.Default.EnableAnonymous = true
}
return nil
}
func setDefaultCallsAuthz(cfg *Config) {
if len(cfg.Auth.Options.Overrides) == 0 {
infoPath := OptionOverrides{
Paths: []string{"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo"},
Override: Options{EnableAPIKey: false, EnableAnonymous: true},
}
cfg.Auth.Options.Overrides = append(cfg.Auth.Options.Overrides, infoPath)
}
// We unfortunately have to use a pipe '|' delimiter for these keys in the config file
// and fix them up once we load the config, because of this bug:
// https://github.com/spf13/viper/issues/324
// Keys also become lowercase
for i := 0; i < len(cfg.Auth.Options.Overrides); i++ {
for j := 0; j < len(cfg.Auth.Options.Overrides[i].Paths); j++ {
cfg.Auth.Options.Overrides[i].Paths[j] = strings.ToLower(strings.ReplaceAll(cfg.Auth.Options.Overrides[i].Paths[j], "|", "."))
}
}
}