mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
3bf93d9796
Following #8891, which moved top-level config validation to an embedded Rego policy, this migrates the `server/metrics` and `metrics_export` configs onto Rego as well. Plugins register their recognized options via `config.RegisterConfigSpec` (derived from their struct fields) so unknown-option warnings live with each struct that brings the config. The goal is migrate more `validateAndInjectDefaults` in follow up PRs, this setups the foundation for other migrations to follow. --------- Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
// Copyright 2025 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
|
|
"github.com/open-policy-agent/opa/internal/configpolicy"
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/version"
|
|
)
|
|
|
|
// coreValidationModule injects OPA's config defaults and reports unrecognized
|
|
// options.
|
|
//
|
|
//go:embed validate.rego
|
|
var coreValidationModule string
|
|
|
|
const coreValidationPolicyName = "opa/config/validate.rego"
|
|
|
|
// validationQuery binds the policy's result document (processed/warnings/errors) to x.
|
|
const validationQuery = "data.opa.config = x"
|
|
|
|
var validationPolicy = configpolicy.New(coreValidationPolicyName, coreValidationModule, validationQuery)
|
|
|
|
// evaluateConfigPolicy runs the policy against raw, returning the config with
|
|
// defaults injected and any warnings. Specs registered via RegisterConfigSpec
|
|
// are supplied so plugin-owned options are recognized.
|
|
func evaluateConfigPolicy(ctx context.Context, raw any, id string) (map[string]any, []string, error) {
|
|
input := map[string]any{
|
|
"config": raw,
|
|
"runtime": map[string]any{
|
|
"id": id,
|
|
"version": version.Version,
|
|
},
|
|
"specs": registeredConfigSpecs(),
|
|
}
|
|
return validationPolicy.Eval(ctx, input)
|
|
}
|
|
|
|
func compileValidationPolicy() (*ast.Compiler, error) {
|
|
return validationPolicy.Compiler()
|
|
}
|