-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add open policy agent filters (#2407)
* Add Open Policy Agent based filters opaAuthorizeRequest() and opaServeResponse() see also https://opensource.zalando.com/skipper/reference/filters/#open-policy-agent Signed-off-by: Magnus Jungsbluth <[email protected]> Signed-off-by: Sandor Szücs <[email protected]> Co-authored-by: Sandor Szücs <[email protected]>
- Loading branch information
1 parent
e0dd155
commit 9ed7ece
Showing
22 changed files
with
2,815 additions
and
16 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
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,73 @@ | ||
package openpolicyagent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
ext_authz_v3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" | ||
"github.com/open-policy-agent/opa-envoy-plugin/envoyauth" | ||
"github.com/open-policy-agent/opa-envoy-plugin/opa/decisionlog" | ||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/rego" | ||
"github.com/open-policy-agent/opa/server" | ||
"github.com/open-policy-agent/opa/tracing" | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
func (opa *OpenPolicyAgentInstance) Eval(ctx context.Context, req *ext_authz_v3.CheckRequest) (*envoyauth.EvalResult, error) { | ||
result, stopeval, err := envoyauth.NewEvalResult() | ||
span := opentracing.SpanFromContext(ctx) | ||
if span != nil { | ||
span.SetTag("opa.decision_id", result.DecisionID) | ||
} | ||
|
||
if err != nil { | ||
opa.Logger().WithFields(map[string]interface{}{"err": err}).Error("Unable to generate decision ID.") | ||
return nil, err | ||
} | ||
|
||
var input map[string]interface{} | ||
defer func() { | ||
stopeval() | ||
err := opa.logDecision(ctx, input, result, err) | ||
if err != nil { | ||
opa.Logger().WithFields(map[string]interface{}{"err": err}).Error("Unable to log decision to control plane.") | ||
} | ||
}() | ||
|
||
if ctx.Err() != nil { | ||
return nil, fmt.Errorf("check request timed out before query execution: %w", ctx.Err()) | ||
} | ||
|
||
logger := opa.manager.Logger().WithFields(map[string]interface{}{"decision-id": result.DecisionID}) | ||
input, err = envoyauth.RequestToInput(req, logger, nil, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert request to input: %w", err) | ||
} | ||
|
||
inputValue, err := ast.InterfaceToValue(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = envoyauth.Eval(ctx, opa, inputValue, result, rego.DistributedTracingOpts(tracing.Options{opa})) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (opa *OpenPolicyAgentInstance) logDecision(ctx context.Context, input interface{}, result *envoyauth.EvalResult, err error) error { | ||
info := &server.Info{ | ||
Timestamp: time.Now(), | ||
Input: &input, | ||
} | ||
|
||
if opa.EnvoyPluginConfig().Path != "" { | ||
info.Path = opa.EnvoyPluginConfig().Path | ||
} | ||
|
||
return decisionlog.LogDecision(ctx, opa.manager, info, result, err) | ||
} |
Oops, something went wrong.