forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add built-in function to get runtime info
These changes add support for accessing runtime information inside of policies. In some cases, policies need to access environment variables or configuration that OPA was booted with. These changes add a built-in function that allows policies to gain access to this information. The built-in function itself is relatively trivial. Most of the required changes were plumbing the runtime information from the entrypoint down into the evaluation engine. The alternative would have been to introduce a global variable containing this information however that would be have been harder to reason about in library integrations. Fixes open-policy-agent#420 Signed-off-by: Torin Sandall <torinsandall@gmail.com>
- Loading branch information
Showing
17 changed files
with
272 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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 ( | ||
"io/ioutil" | ||
"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 { | ||
ConfigFile string | ||
} | ||
|
||
// Term returns the runtime information as an ast.Term object. | ||
func Term(params Params) (*ast.Term, error) { | ||
|
||
obj := ast.NewObject() | ||
|
||
if params.ConfigFile != "" { | ||
|
||
bs, err := ioutil.ReadFile(params.ConfigFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var x interface{} | ||
if err := util.Unmarshal(bs, &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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.