Files
releases/internal/runtime/runtime.go
T
Torin Sandall 2d425494aa Refactor discovery implementation
These changes refactor the discovery implementation a bit to improve
test coverage and remove duplication of common logic shared with the
bundle plugin.

Specifically, the downloading logic has been moved into a separate
package that is shared by bundle and discovery. Second, test coverage in
the discovery implementation is increased from ~15% to ~85%.

These changes also include a few functional improvements:

- The default decision paths can be updated dynamically
- The decision logger can be enabled dynamically
- Discovery downloading errors are reported in status updates
- Discovery bundle is evaluated with all runtime params
- Custom plugins can be created dynamically
- Status updates include both discovery and bundle status

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-12-08 00:45:36 +01:00

56 lines
1.2 KiB
Go

// Copyright 2018 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 runtime contains utilities to return runtime information on the OPA instance.
package runtime
import (
"os"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/util"
)
// Params controls the types of runtime information to return.
type Params struct {
Config []byte
}
// Term returns the runtime information as an ast.Term object.
func Term(params Params) (*ast.Term, error) {
obj := ast.NewObject()
if params.Config != nil {
var x interface{}
if err := util.Unmarshal(params.Config, &x); err != nil {
return nil, err
}
v, err := ast.InterfaceToValue(x)
if err != nil {
return nil, err
}
obj.Insert(ast.StringTerm("config"), ast.NewTerm(v))
}
env := ast.NewObject()
for _, s := range os.Environ() {
parts := strings.SplitN(s, "=", 2)
if len(parts) == 1 {
env.Insert(ast.StringTerm(parts[0]), ast.NullTerm())
} else if len(parts) > 1 {
env.Insert(ast.StringTerm(parts[0]), ast.StringTerm(parts[1]))
}
}
obj.Insert(ast.StringTerm("env"), ast.NewTerm(env))
return ast.NewTerm(obj), nil
}