config: fix databroker policies (#1821)

This commit is contained in:
Caleb Doxsey 2021-01-25 17:18:50 -07:00 committed by GitHub
parent bcc8c17855
commit 84e8f6cc05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 20 deletions

View file

@ -69,7 +69,7 @@ func validateOptions(o *config.Options) error {
// newPolicyEvaluator returns an policy evaluator.
func newPolicyEvaluator(opts *config.Options, store *evaluator.Store) (*evaluator.Evaluator, error) {
metrics.AddPolicyCountCallback("pomerium-authorize", func() int64 {
return int64(len(opts.Policies))
return int64(len(opts.GetAllPolicies()))
})
ctx := context.Background()
_, span := trace.StartSpan(ctx, "authorize.newPolicyEvaluator")

View file

@ -52,7 +52,7 @@ func New(options *config.Options, store *Store) (*Evaluator, error) {
e := &Evaluator{
custom: NewCustomEvaluator(store.opaStore),
authenticateHost: options.AuthenticateURL.Host,
policies: options.Policies,
policies: options.GetAllPolicies(),
}
if options.ClientCA != "" {
e.clientCA = options.ClientCA
@ -75,7 +75,7 @@ func New(options *config.Options, store *Store) (*Evaluator, error) {
}
store.UpdateAdmins(options.Administrators)
store.UpdateRoutePolicies(options.Policies)
store.UpdateRoutePolicies(options.GetAllPolicies())
e.rego = rego.New(
rego.Store(store.opaStore),

View file

@ -245,7 +245,7 @@ func (a *Authorize) getEvaluatorRequestFromCheckRequest(in *envoy_service_auth_v
func (a *Authorize) getMatchingPolicy(requestURL *url.URL) *config.Policy {
options := a.currentOptions.Load()
for _, p := range options.Policies {
for _, p := range options.GetAllPolicies() {
if p.Matches(requestURL) {
return &p
}

View file

@ -112,6 +112,9 @@ type Options struct {
PolicyEnv string `yaml:",omitempty"`
PolicyFile string `mapstructure:"policy_file" yaml:"policy_file,omitempty"`
// AdditionalPolicies are any additional policies added to the options.
AdditionalPolicies []Policy `yaml:"-"`
// AuthenticateURL represents the externally accessible http endpoints
// used for authentication requests and callbacks
AuthenticateURLString string `mapstructure:"authenticate_service_url" yaml:"authenticate_service_url,omitempty"`
@ -336,7 +339,7 @@ func newOptionsFromConfig(configFile string) (*Options, error) {
}
serviceName := telemetry.ServiceName(o.Services)
metrics.AddPolicyCountCallback(serviceName, func() int64 {
return int64(len(o.Policies))
return int64(len(o.GetAllPolicies()))
})
metrics.SetConfigChecksum(serviceName, o.Checksum())
@ -404,6 +407,12 @@ func (o *Options) parsePolicy() error {
return err
}
}
for i := range o.AdditionalPolicies {
p := &o.AdditionalPolicies[i]
if err := p.Validate(); err != nil {
return err
}
}
return nil
}
@ -654,7 +663,7 @@ func (o *Options) Validate() error {
// assert group membership (except for azure which can be derived from the client
// id, secret and provider url)
if o.ServiceAccount == "" && o.Provider != "azure" {
for _, p := range o.Policies {
for _, p := range o.GetAllPolicies() {
if len(p.AllowedGroups) != 0 {
return fmt.Errorf("config: `allowed_groups` requires `idp_service_account`")
}
@ -751,6 +760,17 @@ func (o *Options) GetOauthOptions() oauth.Options {
}
}
// GetAllPolicies gets all the policies in the options.
func (o *Options) GetAllPolicies() []Policy {
if o == nil {
return nil
}
policies := make([]Policy, 0, len(o.Policies)+len(o.AdditionalPolicies))
policies = append(policies, o.Policies...)
policies = append(policies, o.AdditionalPolicies...)
return policies
}
// Checksum returns the checksum of the current options struct
func (o *Options) Checksum() uint64 {
return hashutil.MustHash(o)

View file

@ -283,12 +283,14 @@ func (mgr *Manager) GetConfig() *config.Config {
}
func sourceHostnames(cfg *config.Config) []string {
if len(cfg.Options.Policies) == 0 {
policies := cfg.Options.GetAllPolicies()
if len(policies) == 0 {
return nil
}
dedupe := map[string]struct{}{}
for _, p := range cfg.Options.Policies {
for _, p := range policies {
dedupe[p.Source.Hostname()] = struct{}{}
}
if cfg.Options.AuthenticateURL != nil {

View file

@ -75,8 +75,8 @@ func (srv *Server) buildClusters(options *config.Options) ([]*envoy_config_clust
}
if config.IsProxy(options.Services) {
for i := range options.Policies {
policy := options.Policies[i]
for i, p := range options.GetAllPolicies() {
policy := p
if policy.EnvoyOpts == nil {
policy.EnvoyOpts = newDefaultEnvoyClusterConfig()
}

View file

@ -439,7 +439,7 @@ func getAllRouteableDomains(options *config.Options, addr string) []string {
}
}
if config.IsProxy(options.Services) && addr == options.Addr {
for _, policy := range options.Policies {
for _, policy := range options.GetAllPolicies() {
for _, h := range urlutil.GetDomainsForURL(policy.Source.URL) {
lookup[h] = struct{}{}
}

View file

@ -181,8 +181,8 @@ func buildPolicyRoutes(options *config.Options, domain string) []*envoy_config_r
var routes []*envoy_config_route_v3.Route
responseHeadersToAdd := toEnvoyHeaders(options.Headers)
for i := range options.Policies {
policy := options.Policies[i]
for i, p := range options.GetAllPolicies() {
policy := p
if !hostMatchesDomain(policy.Source.URL, domain) {
continue
}
@ -445,7 +445,7 @@ func setHostRewriteOptions(policy *config.Policy, action *envoy_config_route_v3.
}
func hasPublicPolicyMatchingURL(options *config.Options, requestURL *url.URL) bool {
for _, policy := range options.Policies {
for _, policy := range options.GetAllPolicies() {
if policy.AllowPublicUnauthenticatedAccess && policy.Matches(requestURL) {
return true
}

View file

@ -82,7 +82,7 @@ func (src *ConfigSource) rebuild(firstTime bool) {
src.runUpdater(cfg)
seen := map[uint64]struct{}{}
for _, policy := range cfg.Options.Policies {
for _, policy := range cfg.Options.GetAllPolicies() {
seen[policy.RouteID()] = struct{}{}
}
@ -128,7 +128,7 @@ func (src *ConfigSource) rebuild(firstTime bool) {
}
// add the additional policies here since calling `Validate` will reset them.
cfg.Options.Policies = append(cfg.Options.Policies, additionalPolicies...)
cfg.Options.AdditionalPolicies = append(cfg.Options.AdditionalPolicies, additionalPolicies...)
src.computedConfig = cfg
if !firstTime {

View file

@ -65,7 +65,7 @@ func TestConfigSource(t *testing.T) {
assert.NoError(t, ctx.Err())
return
case cfg := <-cfgs:
assert.Len(t, cfg.Options.Policies, 0)
assert.Len(t, cfg.Options.AdditionalPolicies, 0)
}
select {
@ -73,7 +73,7 @@ func TestConfigSource(t *testing.T) {
assert.NoError(t, ctx.Err())
return
case cfg := <-cfgs:
assert.Len(t, cfg.Options.Policies, 1)
assert.Len(t, cfg.Options.AdditionalPolicies, 1)
}
}

View file

@ -71,7 +71,7 @@ func New(cfg *config.Config) (*Proxy, error) {
p.currentRouter.Store(httputil.NewRouter())
metrics.AddPolicyCountCallback("pomerium-proxy", func() int64 {
return int64(len(p.currentOptions.Load().Policies))
return int64(len(p.currentOptions.Load().GetAllPolicies()))
})
return p, nil
@ -94,7 +94,7 @@ func (p *Proxy) OnConfigChange(cfg *config.Config) {
}
func (p *Proxy) setHandlers(opts *config.Options) {
if len(opts.Policies) == 0 {
if len(opts.GetAllPolicies()) == 0 {
log.Warn().Msg("proxy: configuration has no policies")
}
r := httputil.NewRouter()