mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-07 14:26:01 +02:00
* refactor backend, implement encrypted store * refactor in-memory store * wip * wip * wip * add syncer test * fix redis expiry * fix linting issues * fix test by skipping non-config records * fix backoff import * fix init issues * fix query * wait for initial sync before starting directory sync * add type to SyncLatest * add more log messages, fix deadlock in in-memory store, always return server version from SyncLatest * update sync types and tests * add redis tests * skip macos in github actions * add comments to proto * split getBackend into separate methods * handle errors in initVersion * return different error for not found vs other errors in get * use exponential backoff for redis transaction retry * rename raw to result * use context instead of close channel * store type urls as constants in databroker * use timestampb instead of ptypes * fix group merging not waiting * change locked names * update GetAll to return latest record version * add method to grpcutil to get the type url for a protobuf type
222 lines
5.4 KiB
Go
222 lines
5.4 KiB
Go
package evaluator
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/pomerium/pomerium/config"
|
|
"github.com/pomerium/pomerium/pkg/grpc/databroker"
|
|
"github.com/pomerium/pomerium/pkg/grpc/directory"
|
|
"github.com/pomerium/pomerium/pkg/grpc/session"
|
|
"github.com/pomerium/pomerium/pkg/grpc/user"
|
|
)
|
|
|
|
func TestJSONMarshal(t *testing.T) {
|
|
opt := config.NewDefaultOptions()
|
|
opt.AuthenticateURL = mustParseURL("https://authenticate.example.com")
|
|
e, err := New(opt, NewStoreFromProtos(
|
|
&session.Session{
|
|
UserId: "user1",
|
|
},
|
|
&directory.User{
|
|
Id: "user1",
|
|
GroupIds: []string{"group1", "group2"},
|
|
},
|
|
&directory.Group{
|
|
Id: "group1",
|
|
Name: "admin",
|
|
Email: "admin@example.com",
|
|
},
|
|
&directory.Group{
|
|
Id: "group2",
|
|
Name: "test",
|
|
},
|
|
))
|
|
require.NoError(t, err)
|
|
bs, _ := json.Marshal(e.newInput(&Request{
|
|
HTTP: RequestHTTP{
|
|
Method: "GET",
|
|
URL: "https://example.com",
|
|
Headers: map[string]string{
|
|
"Accept": "application/json",
|
|
},
|
|
ClientCertificate: "CLIENT_CERTIFICATE",
|
|
},
|
|
Session: RequestSession{
|
|
ID: "SESSION_ID",
|
|
},
|
|
}, true))
|
|
assert.JSONEq(t, `{
|
|
"http": {
|
|
"client_certificate": "CLIENT_CERTIFICATE",
|
|
"headers": {
|
|
"Accept": "application/json"
|
|
},
|
|
"method": "GET",
|
|
"url": "https://example.com"
|
|
},
|
|
"session": {
|
|
"id": "SESSION_ID"
|
|
},
|
|
"is_valid_client_certificate": true
|
|
}`, string(bs))
|
|
}
|
|
|
|
func TestEvaluator_Evaluate(t *testing.T) {
|
|
sessionID := uuid.New().String()
|
|
userID := uuid.New().String()
|
|
|
|
ctx := context.Background()
|
|
allowedPolicy := []config.Policy{{From: "https://foo.com", AllowedUsers: []string{"foo@example.com"}}}
|
|
forbiddenPolicy := []config.Policy{{From: "https://bar.com", AllowedUsers: []string{"bar@example.com"}}}
|
|
|
|
tests := []struct {
|
|
name string
|
|
reqURL string
|
|
policies []config.Policy
|
|
customPolicies []string
|
|
sessionID string
|
|
expectedStatus int
|
|
}{
|
|
{"allowed", "https://foo.com/path", allowedPolicy, nil, sessionID, http.StatusOK},
|
|
{"forbidden", "https://bar.com/path", forbiddenPolicy, nil, sessionID, http.StatusForbidden},
|
|
{"unauthorized", "https://foo.com/path", allowedPolicy, nil, "", http.StatusUnauthorized},
|
|
{"custom policy overwrite main policy", "https://foo.com/path", allowedPolicy, []string{"deny = true"}, sessionID, http.StatusForbidden},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
store := NewStoreFromProtos()
|
|
data, _ := ptypes.MarshalAny(&session.Session{
|
|
Version: "1",
|
|
Id: sessionID,
|
|
UserId: userID,
|
|
IdToken: &session.IDToken{
|
|
Issuer: "TestEvaluatorEvaluate",
|
|
Subject: userID,
|
|
IssuedAt: ptypes.TimestampNow(),
|
|
},
|
|
OauthToken: &session.OAuthToken{
|
|
AccessToken: "ACCESS TOKEN",
|
|
TokenType: "Bearer",
|
|
RefreshToken: "REFRESH TOKEN",
|
|
},
|
|
})
|
|
store.UpdateRecord(&databroker.Record{
|
|
Version: 1,
|
|
Type: "type.googleapis.com/session.Session",
|
|
Id: sessionID,
|
|
Data: data,
|
|
})
|
|
data, _ = ptypes.MarshalAny(&user.User{
|
|
Version: "1",
|
|
Id: userID,
|
|
Email: "foo@example.com",
|
|
})
|
|
store.UpdateRecord(&databroker.Record{
|
|
Version: 1,
|
|
Type: "type.googleapis.com/user.User",
|
|
Id: userID,
|
|
Data: data,
|
|
})
|
|
|
|
e, err := New(&config.Options{
|
|
AuthenticateURL: mustParseURL("https://authn.example.com"),
|
|
Policies: tc.policies,
|
|
}, store)
|
|
require.NoError(t, err)
|
|
res, err := e.Evaluate(ctx, &Request{
|
|
HTTP: RequestHTTP{Method: "GET", URL: tc.reqURL},
|
|
Session: RequestSession{ID: tc.sessionID},
|
|
CustomPolicies: tc.customPolicies,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, res)
|
|
assert.Equal(t, tc.expectedStatus, res.Status)
|
|
})
|
|
}
|
|
}
|
|
|
|
func mustParseURL(str string) *url.URL {
|
|
u, err := url.Parse(str)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return u
|
|
}
|
|
|
|
func BenchmarkEvaluator_Evaluate(b *testing.B) {
|
|
store := NewStore()
|
|
e, err := New(&config.Options{
|
|
AuthenticateURL: mustParseURL("https://authn.example.com"),
|
|
}, store)
|
|
if !assert.NoError(b, err) {
|
|
return
|
|
}
|
|
|
|
lastSessionID := ""
|
|
|
|
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",
|
|
},
|
|
})
|
|
store.UpdateRecord(&databroker.Record{
|
|
Version: uint64(i),
|
|
Type: "type.googleapis.com/session.Session",
|
|
Id: sessionID,
|
|
Data: data,
|
|
})
|
|
data, _ = ptypes.MarshalAny(&user.User{
|
|
Version: fmt.Sprint(i),
|
|
Id: userID,
|
|
})
|
|
store.UpdateRecord(&databroker.Record{
|
|
Version: uint64(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{
|
|
HTTP: RequestHTTP{
|
|
Method: "GET",
|
|
URL: "https://example.com/path",
|
|
Headers: map[string]string{},
|
|
},
|
|
Session: RequestSession{
|
|
ID: lastSessionID,
|
|
},
|
|
})
|
|
}
|
|
}
|