Remove "accounts" service
This commit is contained in:
committed by
Ralf Haferkamp
parent
5ba1b8f2c1
commit
d25aa7b20f
@@ -6,10 +6,7 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
|
||||
accountssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/accounts/v0"
|
||||
|
||||
revactx "github.com/cs3org/reva/v2/pkg/ctx"
|
||||
"github.com/go-micro/plugins/v4/client/grpc"
|
||||
"github.com/owncloud/ocis/v2/extensions/proxy/pkg/config"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
)
|
||||
@@ -58,9 +55,6 @@ type Selector func(r *http.Request) (string, error)
|
||||
func LoadSelector(cfg *config.PolicySelector) (Selector, error) {
|
||||
selCount := 0
|
||||
|
||||
if cfg.Migration != nil {
|
||||
selCount++
|
||||
}
|
||||
if cfg.Static != nil {
|
||||
selCount++
|
||||
}
|
||||
@@ -74,7 +68,7 @@ func LoadSelector(cfg *config.PolicySelector) (Selector, error) {
|
||||
return nil, ErrMultipleSelectors
|
||||
}
|
||||
|
||||
if cfg.Migration == nil && cfg.Static == nil && cfg.Claims == nil && cfg.Regex == nil {
|
||||
if cfg.Static == nil && cfg.Claims == nil && cfg.Regex == nil {
|
||||
return nil, ErrSelectorConfigIncomplete
|
||||
}
|
||||
|
||||
@@ -82,12 +76,6 @@ func LoadSelector(cfg *config.PolicySelector) (Selector, error) {
|
||||
return NewStaticSelector(cfg.Static), nil
|
||||
}
|
||||
|
||||
if cfg.Migration != nil {
|
||||
return NewMigrationSelector(
|
||||
cfg.Migration,
|
||||
accountssvc.NewAccountsService("com.owncloud.accounts", grpc.NewClient())), nil
|
||||
}
|
||||
|
||||
if cfg.Claims != nil {
|
||||
if cfg.Claims.SelectorCookieName == "" {
|
||||
cfg.Claims.SelectorCookieName = SelectorCookieName
|
||||
@@ -118,41 +106,6 @@ func NewStaticSelector(cfg *config.StaticSelectorConf) Selector {
|
||||
}
|
||||
}
|
||||
|
||||
// NewMigrationSelector selects the policy based on the existence of the oidc "preferred_username" claim in the accounts-service.
|
||||
// The policy for each case is configurable:
|
||||
// "policy_selector": {
|
||||
// "migration": {
|
||||
// "acc_found_policy" : "ocis",
|
||||
// "acc_not_found_policy": "oc10",
|
||||
// "unauthenticated_policy": "oc10"
|
||||
// }
|
||||
// },
|
||||
//
|
||||
// This selector can be used in migration-scenarios where some users have already migrated from ownCloud10 to OCIS and
|
||||
// thus have an entry in ocis-accounts. All users without accounts entry are routed to the legacy ownCloud10 instance.
|
||||
func NewMigrationSelector(cfg *config.MigrationSelectorConf, ss accountssvc.AccountsService) Selector {
|
||||
var acc = ss
|
||||
return func(r *http.Request) (s string, err error) {
|
||||
var claims map[string]interface{}
|
||||
if claims = oidc.FromContext(r.Context()); claims == nil {
|
||||
return cfg.UnauthenticatedPolicy, nil
|
||||
}
|
||||
|
||||
var userID string
|
||||
var ok bool
|
||||
if userID, ok = claims[oidc.PreferredUsername].(string); !ok {
|
||||
// TODO clarify: what if the user just has no username ...
|
||||
return cfg.AccNotFoundPolicy, nil
|
||||
}
|
||||
|
||||
if _, err := acc.GetAccount(r.Context(), &accountssvc.GetAccountRequest{Id: userID}); err != nil {
|
||||
return cfg.AccNotFoundPolicy, nil
|
||||
}
|
||||
return cfg.AccFoundPolicy, nil
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// NewClaimsSelector selects the policy based on the "ocis.routing.policy" claim
|
||||
// The policy for corner cases is configurable:
|
||||
// "policy_selector": {
|
||||
|
||||
@@ -2,7 +2,6 @@ package policy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -11,9 +10,6 @@ import (
|
||||
revactx "github.com/cs3org/reva/v2/pkg/ctx"
|
||||
"github.com/owncloud/ocis/v2/extensions/proxy/pkg/config"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
||||
accountsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/accounts/v0"
|
||||
accountssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/accounts/v0"
|
||||
"go-micro.dev/v4/client"
|
||||
)
|
||||
|
||||
func TestLoadSelector(t *testing.T) {
|
||||
@@ -22,20 +18,13 @@ func TestLoadSelector(t *testing.T) {
|
||||
expectedErr error
|
||||
}
|
||||
sCfg := &config.StaticSelectorConf{Policy: "reva"}
|
||||
mcfg := &config.MigrationSelectorConf{
|
||||
AccFoundPolicy: "found",
|
||||
AccNotFoundPolicy: "not_found",
|
||||
UnauthenticatedPolicy: "unauth",
|
||||
}
|
||||
ccfg := &config.ClaimsSelectorConf{}
|
||||
rcfg := &config.RegexSelectorConf{}
|
||||
|
||||
table := []test{
|
||||
{cfg: &config.PolicySelector{Static: sCfg, Migration: mcfg}, expectedErr: ErrMultipleSelectors},
|
||||
{cfg: &config.PolicySelector{Static: sCfg, Claims: ccfg, Regex: rcfg}, expectedErr: ErrMultipleSelectors},
|
||||
{cfg: &config.PolicySelector{}, expectedErr: ErrSelectorConfigIncomplete},
|
||||
{cfg: &config.PolicySelector{Static: sCfg}, expectedErr: nil},
|
||||
{cfg: &config.PolicySelector{Migration: mcfg}, expectedErr: nil},
|
||||
{cfg: &config.PolicySelector{Claims: ccfg}, expectedErr: nil},
|
||||
{cfg: &config.PolicySelector{Regex: rcfg}, expectedErr: nil},
|
||||
}
|
||||
@@ -74,60 +63,6 @@ func TestStaticSelector(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type migrationTestCase struct {
|
||||
AccSvcShouldReturnError bool
|
||||
Claims map[string]interface{}
|
||||
Expected string
|
||||
}
|
||||
|
||||
func TestMigrationSelector(t *testing.T) {
|
||||
cfg := config.MigrationSelectorConf{
|
||||
AccFoundPolicy: "found",
|
||||
AccNotFoundPolicy: "not_found",
|
||||
UnauthenticatedPolicy: "unauth",
|
||||
}
|
||||
var tests = []migrationTestCase{
|
||||
{true, map[string]interface{}{oidc.PreferredUsername: "Hans"}, "not_found"},
|
||||
{true, map[string]interface{}{oidc.Email: "hans@example.test"}, "not_found"},
|
||||
{false, map[string]interface{}{oidc.PreferredUsername: "Hans"}, "found"},
|
||||
{false, nil, "unauth"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
sut := NewMigrationSelector(&cfg, mockAccSvc(tc.AccSvcShouldReturnError))
|
||||
r := httptest.NewRequest("GET", "https://example.com", nil)
|
||||
ctx := oidc.NewContext(r.Context(), tc.Claims)
|
||||
nr := r.WithContext(ctx)
|
||||
|
||||
got, err := sut(nr)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if got != tc.Expected {
|
||||
t.Errorf("Expected Policy %v got %v", tc.Expected, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mockAccSvc(retErr bool) accountssvc.AccountsService {
|
||||
if retErr {
|
||||
return &accountssvc.MockAccountsService{
|
||||
GetFunc: func(ctx context.Context, in *accountssvc.GetAccountRequest, opts ...client.CallOption) (record *accountsmsg.Account, err error) {
|
||||
return nil, fmt.Errorf("error returned by mockAccountsService GET")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return &accountssvc.MockAccountsService{
|
||||
GetFunc: func(ctx context.Context, in *accountssvc.GetAccountRequest, opts ...client.CallOption) (record *accountsmsg.Account, err error) {
|
||||
return &accountsmsg.Account{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type testCase struct {
|
||||
Name string
|
||||
Context context.Context
|
||||
|
||||
Reference in New Issue
Block a user