Skip to content

Commit

Permalink
feat: tracing for authz remote (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlh authored Feb 8, 2023
1 parent f40b3f1 commit 7e7d45e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
4 changes: 3 additions & 1 deletion driver/configuration/provider_koanf_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ory/oathkeeper/pipeline/authz"
"github.com/ory/oathkeeper/pipeline/mutate"
"github.com/ory/oathkeeper/x"
"github.com/ory/x/otelx"
)

func setup(t *testing.T) *configuration.KoanfProvider {
Expand Down Expand Up @@ -333,7 +334,8 @@ func TestKoanfProvider(t *testing.T) {
})

t.Run("authorizer=remote_json", func(t *testing.T) {
a := authz.NewAuthorizerRemoteJSON(p)
l := logrusx.New("", "")
a := authz.NewAuthorizerRemoteJSON(p, otelx.NewNoop(l, p.TracingConfig()))
assert.True(t, p.AuthorizerIsEnabled(a.GetID()))
require.NoError(t, a.Validate(nil))

Expand Down
4 changes: 2 additions & 2 deletions driver/registry_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ func (r *RegistryMemory) prepareAuthz() {
authz.NewAuthorizerAllow(r.c),
authz.NewAuthorizerDeny(r.c),
authz.NewAuthorizerKetoEngineACPORY(r.c),
authz.NewAuthorizerRemote(r.c),
authz.NewAuthorizerRemoteJSON(r.c),
authz.NewAuthorizerRemote(r.c, r),
authz.NewAuthorizerRemoteJSON(r.c, r),
}

r.authorizers = map[string]authz.Authorizer{}
Expand Down
13 changes: 11 additions & 2 deletions driver/registry_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
package driver

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/x/logrusx"
)

func TestRegistryMemoryAvailablePipelineAuthorizers(t *testing.T) {
r := NewRegistryMemory()
c, err := configuration.NewKoanfProvider(context.Background(), nil, logrusx.New("", ""))
require.NoError(t, err)
r := NewRegistry(c)
got := r.AvailablePipelineAuthorizers()
assert.ElementsMatch(t, got, []string{"allow", "deny", "keto_engine_acp_ory", "remote", "remote_json"})
}
Expand All @@ -29,7 +36,9 @@ func TestRegistryMemoryPipelineAuthorizer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
r := NewRegistryMemory()
c, err := configuration.NewKoanfProvider(context.Background(), nil, logrusx.New("", ""))
require.NoError(t, err)
r := NewRegistry(c)
a, err := r.PipelineAuthorizer(tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("PipelineAuthorizer() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
5 changes: 4 additions & 1 deletion pipeline/authz/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/ory/x/httpx"

"go.opentelemetry.io/otel/trace"

"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/pipeline"
Expand Down Expand Up @@ -45,7 +47,7 @@ type AuthorizerRemote struct {
}

// NewAuthorizerRemote creates a new AuthorizerRemote.
func NewAuthorizerRemote(c configuration.Provider) *AuthorizerRemote {
func NewAuthorizerRemote(c configuration.Provider, d interface{ Tracer() trace.Tracer }) *AuthorizerRemote {
return &AuthorizerRemote{
c: c,
client: httpx.NewResilientClient().StandardClient(),
Expand Down Expand Up @@ -108,6 +110,7 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
}

res, err := a.client.Do(req.WithContext(r.Context()))

if err != nil {
return errors.WithStack(err)
}
Expand Down
7 changes: 5 additions & 2 deletions pipeline/authz/remote_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/ory/x/httpx"

"go.opentelemetry.io/otel/trace"

"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/pipeline"
Expand Down Expand Up @@ -50,10 +52,10 @@ type AuthorizerRemoteJSON struct {
}

// NewAuthorizerRemoteJSON creates a new AuthorizerRemoteJSON.
func NewAuthorizerRemoteJSON(c configuration.Provider) *AuthorizerRemoteJSON {
func NewAuthorizerRemoteJSON(c configuration.Provider, d interface{ Tracer() trace.Tracer }) *AuthorizerRemoteJSON {
return &AuthorizerRemoteJSON{
c: c,
client: httpx.NewResilientClient().StandardClient(),
client: httpx.NewResilientClient(httpx.ResilientClientWithTracer(d.Tracer())).StandardClient(),
t: x.NewTemplate("remote_json"),
}
}
Expand Down Expand Up @@ -101,6 +103,7 @@ func (a *AuthorizerRemoteJSON) Authorize(r *http.Request, session *authn.Authent
}

res, err := a.client.Do(req.WithContext(r.Context()))

if err != nil {
return errors.WithStack(err)
}
Expand Down
9 changes: 6 additions & 3 deletions pipeline/authz/remote_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ory/oathkeeper/pipeline/authn"
. "github.com/ory/oathkeeper/pipeline/authz"
"github.com/ory/oathkeeper/rule"
"github.com/ory/x/otelx"
)

func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
Expand Down Expand Up @@ -176,7 +177,7 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
if err != nil {
l.WithError(err).Fatal("Failed to initialize configuration")
}
a := NewAuthorizerRemoteJSON(p)
a := NewAuthorizerRemoteJSON(p, otelx.NewNoop(l, p.TracingConfig()))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
r, err := http.NewRequestWithContext(ctx, "", "", nil)
Expand Down Expand Up @@ -260,7 +261,8 @@ func TestAuthorizerRemoteJSONValidate(t *testing.T) {
configx.SkipValidation(),
)
require.NoError(t, err)
a := NewAuthorizerRemoteJSON(p)
l := logrusx.New("", "")
a := NewAuthorizerRemoteJSON(p, otelx.NewNoop(l, p.TracingConfig()))
p.SetForTest(t, configuration.AuthorizerRemoteJSONIsEnabled, tt.enabled)
if err := a.Validate(tt.config); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -312,7 +314,8 @@ func TestAuthorizerRemoteJSONConfig(t *testing.T) {
context.Background(), nil, logrusx.New("", ""),
)
require.NoError(t, err)
a := NewAuthorizerRemoteJSON(p)
l := logrusx.New("", "")
a := NewAuthorizerRemoteJSON(p, otelx.NewNoop(l, p.TracingConfig()))
actual, err := a.Config(tt.raw)
assert.NoError(t, err)
assert.Equal(t, tt.expected, actual)
Expand Down
6 changes: 4 additions & 2 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ory/oathkeeper/pipeline/authn"
. "github.com/ory/oathkeeper/pipeline/authz"
"github.com/ory/oathkeeper/rule"
"github.com/ory/x/otelx"
)

func TestAuthorizerRemoteAuthorize(t *testing.T) {
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
if err != nil {
l.WithError(err).Fatal("Failed to initialize configuration")
}
a := NewAuthorizerRemote(p)
a := NewAuthorizerRemote(p, otelx.NewNoop(l, p.TracingConfig()))
r := &http.Request{
Header: map[string][]string{
"Content-Type": {"text/plain"},
Expand Down Expand Up @@ -255,7 +256,8 @@ func TestAuthorizerRemoteValidate(t *testing.T) {
context.Background(), nil, logrusx.New("", ""),
configx.SkipValidation())
require.NoError(t, err)
a := NewAuthorizerRemote(p)
l := logrusx.New("", "")
a := NewAuthorizerRemote(p, otelx.NewNoop(l, p.TracingConfig()))
p.SetForTest(t, configuration.AuthorizerRemoteIsEnabled, tt.enabled)
if err := a.Validate(tt.config); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 7e7d45e

Please sign in to comment.