Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply credentials masking on opa.runtime().config #4165

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion topdown/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,124 @@

package topdown

import "github.com/open-policy-agent/opa/ast"
import (
"fmt"

"github.com/open-policy-agent/opa/ast"
)

func builtinOPARuntime(bctx BuiltinContext, _ []*ast.Term, iter func(*ast.Term) error) error {

if bctx.Runtime == nil {
return iter(ast.ObjectTerm())
}

if bctx.Runtime.Get(ast.StringTerm("config")) != nil {
iface, err := ast.ValueToInterface(bctx.Runtime.Value, illegalResolver{})
if err != nil {
return err
}
if object, ok := iface.(map[string]interface{}); ok {
if cfgRaw, ok := object["config"]; ok {
if config, ok := cfgRaw.(map[string]interface{}); ok {
configPurged, err := activeConfig(config)
if err != nil {
return err
}
object["config"] = configPurged
value, err := ast.InterfaceToValue(object)
if err != nil {
return err
}
return iter(ast.NewTerm(value))
}
}
}
}

return iter(bctx.Runtime)
}

func init() {
RegisterBuiltinFunc(ast.OPARuntime.Name, builtinOPARuntime)
}

func activeConfig(config map[string]interface{}) (interface{}, error) {

if config["services"] != nil {
err := removeServiceCredentials(config["services"])
if err != nil {
return nil, err
}
}

if config["keys"] != nil {
err := removeCryptoKeys(config["keys"])
if err != nil {
return nil, err
}
}

return config, nil
}

func removeServiceCredentials(x interface{}) error {

switch x := x.(type) {
case []interface{}:
for _, v := range x {
err := removeKey(v, "credentials")
if err != nil {
return err
}
}

case map[string]interface{}:
for _, v := range x {
err := removeKey(v, "credentials")
if err != nil {
return err
}
}
default:
return fmt.Errorf("illegal service config type: %T", x)
}

return nil
}

func removeCryptoKeys(x interface{}) error {

switch x := x.(type) {
case map[string]interface{}:
for _, v := range x {
err := removeKey(v, "key", "private_key")
if err != nil {
return err
}
}
default:
return fmt.Errorf("illegal keys config type: %T", x)
}

return nil
}

func removeKey(x interface{}, keys ...string) error {
val, ok := x.(map[string]interface{})
if !ok {
return fmt.Errorf("type assertion error")
}

for _, key := range keys {
delete(val, key)
}

return nil
}

type illegalResolver struct{}

func (illegalResolver) Resolve(ref ast.Ref) (interface{}, error) {
return nil, fmt.Errorf("illegal value: %v", ref)
}
39 changes: 39 additions & 0 deletions topdown/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,42 @@ func TestOPARuntime(t *testing.T) {
}

}

func TestOPARuntimeConfigMasking(t *testing.T) {

ctx := context.Background()
q := NewQuery(ast.MustParseBody("opa.runtime(x)")).WithRuntime(ast.MustParseTerm(`{"config": {
"labels": {"foo": "bar"},
"services": {
"foo": {
"url": "https://remote.example.com",
"credentials": {
"oauth2": {
"client_id": "opa_client",
"client_secret": "sup3rs3cr3t"
}
}
}
}
}}`))
rs, err := q.Run(ctx)
if err != nil {
t.Fatal(err)
} else if len(rs) != 1 {
t.Fatal("Expected result set to contain exactly one result")
}

term := rs[0][ast.Var("x")]
exp := ast.MustParseTerm(`{"config": {
"labels": {"foo": "bar"},
"services": {
"foo": {
"url": "https://remote.example.com"
}
}
}}`)

if ast.Compare(term, exp) != 0 {
t.Fatalf("Expected %v but got %v", exp, term)
}
}