mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-08 22:03:29 +02:00
authorize: avoid serializing databroker data map to improve performance (#995)
This commit is contained in:
parent
7110948296
commit
3ad8cbf4ec
5 changed files with 104 additions and 29 deletions
|
@ -30,6 +30,12 @@ import (
|
||||||
"github.com/pomerium/pomerium/internal/log"
|
"github.com/pomerium/pomerium/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sessionTypeURL = "type.googleapis.com/session.Session"
|
||||||
|
userTypeURL = "type.googleapis.com/user.User"
|
||||||
|
directoryUserTypeURL = "type.googleapis.com/directory.User"
|
||||||
|
)
|
||||||
|
|
||||||
// Evaluator specifies the interface for a policy engine.
|
// Evaluator specifies the interface for a policy engine.
|
||||||
type Evaluator struct {
|
type Evaluator struct {
|
||||||
rego *rego.Rego
|
rego *rego.Rego
|
||||||
|
@ -202,15 +208,25 @@ func (e *Evaluator) SignedJWT(req *Request) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type input struct {
|
type input struct {
|
||||||
DataBrokerData DataBrokerData `json:"databroker_data"`
|
DataBrokerData dataBrokerDataInput `json:"databroker_data"`
|
||||||
HTTP RequestHTTP `json:"http"`
|
HTTP RequestHTTP `json:"http"`
|
||||||
Session RequestSession `json:"session"`
|
Session RequestSession `json:"session"`
|
||||||
IsValidClientCertificate bool `json:"is_valid_client_certificate"`
|
IsValidClientCertificate bool `json:"is_valid_client_certificate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dataBrokerDataInput struct {
|
||||||
|
Session interface{} `json:"session,omitempty"`
|
||||||
|
User interface{} `json:"user,omitempty"`
|
||||||
|
DirectoryUser interface{} `json:"directory_user,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Evaluator) newInput(req *Request, isValidClientCertificate bool) *input {
|
func (e *Evaluator) newInput(req *Request, isValidClientCertificate bool) *input {
|
||||||
i := new(input)
|
i := new(input)
|
||||||
i.DataBrokerData = req.DataBrokerData
|
i.DataBrokerData.Session = req.DataBrokerData.Get(sessionTypeURL, req.Session.ID)
|
||||||
|
if obj, ok := i.DataBrokerData.Session.(interface{ GetUserId() string }); ok {
|
||||||
|
i.DataBrokerData.User = req.DataBrokerData.Get(userTypeURL, obj.GetUserId())
|
||||||
|
i.DataBrokerData.DirectoryUser = req.DataBrokerData.Get(directoryUserTypeURL, obj.GetUserId())
|
||||||
|
}
|
||||||
i.HTTP = req.HTTP
|
i.HTTP = req.HTTP
|
||||||
i.Session = req.Session
|
i.Session = req.Session
|
||||||
i.IsValidClientCertificate = isValidClientCertificate
|
i.IsValidClientCertificate = isValidClientCertificate
|
||||||
|
|
|
@ -1,16 +1,23 @@
|
||||||
package evaluator
|
package evaluator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/golang/protobuf/ptypes"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/pomerium/pomerium/config"
|
"github.com/pomerium/pomerium/config"
|
||||||
|
"github.com/pomerium/pomerium/internal/grpc/databroker"
|
||||||
"github.com/pomerium/pomerium/internal/grpc/directory"
|
"github.com/pomerium/pomerium/internal/grpc/directory"
|
||||||
|
"github.com/pomerium/pomerium/internal/grpc/session"
|
||||||
|
"github.com/pomerium/pomerium/internal/grpc/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJSONMarshal(t *testing.T) {
|
func TestJSONMarshal(t *testing.T) {
|
||||||
|
@ -25,7 +32,7 @@ func TestJSONMarshal(t *testing.T) {
|
||||||
"type.googleapis.com/user.User": map[string]interface{}{},
|
"type.googleapis.com/user.User": map[string]interface{}{},
|
||||||
}
|
}
|
||||||
|
|
||||||
bs, _ := json.Marshal(input{
|
bs, _ := json.Marshal(new(Evaluator).newInput(&Request{
|
||||||
DataBrokerData: dbd,
|
DataBrokerData: dbd,
|
||||||
HTTP: RequestHTTP{
|
HTTP: RequestHTTP{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
@ -40,18 +47,9 @@ func TestJSONMarshal(t *testing.T) {
|
||||||
ImpersonateEmail: "y@example.com",
|
ImpersonateEmail: "y@example.com",
|
||||||
ImpersonateGroups: []string{"group1"},
|
ImpersonateGroups: []string{"group1"},
|
||||||
},
|
},
|
||||||
IsValidClientCertificate: true,
|
}, true))
|
||||||
})
|
|
||||||
assert.JSONEq(t, `{
|
assert.JSONEq(t, `{
|
||||||
"databroker_data": {
|
"databroker_data": {
|
||||||
"type.googleapis.com/directory.User": {
|
|
||||||
"user1": {
|
|
||||||
"id": "user1",
|
|
||||||
"groups": ["group1", "group2"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type.googleapis.com/session.Session": {},
|
|
||||||
"type.googleapis.com/user.User": {}
|
|
||||||
},
|
},
|
||||||
"http": {
|
"http": {
|
||||||
"client_certificate": "CLIENT_CERTIFICATE",
|
"client_certificate": "CLIENT_CERTIFICATE",
|
||||||
|
@ -97,3 +95,68 @@ func mustParseURL(str string) *url.URL {
|
||||||
}
|
}
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkEvaluator_Evaluate(b *testing.B) {
|
||||||
|
e, err := New(&config.Options{
|
||||||
|
AuthenticateURL: mustParseURL("https://authn.example.com"),
|
||||||
|
})
|
||||||
|
if !assert.NoError(b, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSessionID := ""
|
||||||
|
|
||||||
|
dbd := make(DataBrokerData)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
sessionID := uuid.New().String()
|
||||||
|
lastSessionID = sessionID
|
||||||
|
userID := uuid.New().String()
|
||||||
|
data, _ := ptypes.MarshalAny(&session.Session{
|
||||||
|
Version: fmt.Sprint(i),
|
||||||
|
Id: sessionID,
|
||||||
|
UserId: userID,
|
||||||
|
IdToken: &session.IDToken{
|
||||||
|
Issuer: "benchmark",
|
||||||
|
Subject: userID,
|
||||||
|
IssuedAt: ptypes.TimestampNow(),
|
||||||
|
},
|
||||||
|
OauthToken: &session.OAuthToken{
|
||||||
|
AccessToken: "ACCESS TOKEN",
|
||||||
|
TokenType: "Bearer",
|
||||||
|
RefreshToken: "REFRESH TOKEN",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
dbd.Update(&databroker.Record{
|
||||||
|
Version: fmt.Sprint(i),
|
||||||
|
Type: "type.googleapis.com/session.Session",
|
||||||
|
Id: sessionID,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
data, _ = ptypes.MarshalAny(&user.User{
|
||||||
|
Version: fmt.Sprint(i),
|
||||||
|
Id: userID,
|
||||||
|
})
|
||||||
|
dbd.Update(&databroker.Record{
|
||||||
|
Version: fmt.Sprint(i),
|
||||||
|
Type: "type.googleapis.com/user.User",
|
||||||
|
Id: userID,
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
ctx := context.Background()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
e.Evaluate(ctx, &Request{
|
||||||
|
DataBrokerData: dbd,
|
||||||
|
HTTP: RequestHTTP{
|
||||||
|
Method: "GET",
|
||||||
|
URL: "https://example.com/path",
|
||||||
|
Headers: map[string]string{},
|
||||||
|
},
|
||||||
|
Session: RequestSession{
|
||||||
|
ID: lastSessionID,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ default allow = false
|
||||||
|
|
||||||
|
|
||||||
route := first_allowed_route(input.http.url)
|
route := first_allowed_route(input.http.url)
|
||||||
session := input.databroker_data["type.googleapis.com/session.Session"][input.session.id]
|
session := input.databroker_data.session
|
||||||
user := input.databroker_data["type.googleapis.com/user.User"][session.user_id]
|
user := input.databroker_data.user
|
||||||
directory_user := input.databroker_data["type.googleapis.com/directory.User"][session.user_id]
|
directory_user := input.databroker_data.directory_user
|
||||||
|
|
||||||
|
|
||||||
# allow public
|
# allow public
|
||||||
|
|
|
@ -7,16 +7,12 @@ test_email_allowed {
|
||||||
"allowed_users": ["x@example.com"]
|
"allowed_users": ["x@example.com"]
|
||||||
}] with
|
}] with
|
||||||
input.databroker_data as {
|
input.databroker_data as {
|
||||||
"type.googleapis.com/session.Session": {
|
"session": {
|
||||||
"session1": {
|
|
||||||
"user_id": "user1"
|
"user_id": "user1"
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"type.googleapis.com/user.User": {
|
"user": {
|
||||||
"user1": {
|
|
||||||
"email": "x@example.com"
|
"email": "x@example.com"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} with
|
} with
|
||||||
input.http as { "url": "http://example.com" } with
|
input.http as { "url": "http://example.com" } with
|
||||||
input.session as { "id": "session1" }
|
input.session as { "id": "session1" }
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue