Searching for share recipients in the vault ("Safe") could take minutes,
because each search scanned the role assignments of every account in the
system. Vault eligibility is now checked only for the users the search
matched, a few at a time in parallel, so the search is about as fast as
outside the vault.
1753 lines
68 KiB
Go
1753 lines
68 KiB
Go
package svc_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
|
|
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
|
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
|
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
|
|
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
|
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
|
|
"github.com/go-chi/chi/v5"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
libregraph "github.com/owncloud/libre-graph-api-go"
|
|
revactx "github.com/owncloud/reva/v2/pkg/ctx"
|
|
"github.com/owncloud/reva/v2/pkg/permission"
|
|
"github.com/owncloud/reva/v2/pkg/rgrpc/status"
|
|
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
|
|
cs3mocks "github.com/owncloud/reva/v2/tests/cs3mocks/mocks"
|
|
"github.com/stretchr/testify/mock"
|
|
"go-micro.dev/v4/client"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
|
|
settingsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/settings/v0"
|
|
settings "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
|
|
settingsmocks "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0/mocks"
|
|
"github.com/owncloud/ocis/v2/services/graph/mocks"
|
|
"github.com/owncloud/ocis/v2/services/graph/pkg/config"
|
|
"github.com/owncloud/ocis/v2/services/graph/pkg/config/defaults"
|
|
identitymocks "github.com/owncloud/ocis/v2/services/graph/pkg/identity/mocks"
|
|
service "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0"
|
|
)
|
|
|
|
type userList struct {
|
|
Value []*service.UserWithAttributes
|
|
}
|
|
|
|
var _ = Describe("Users", func() {
|
|
var (
|
|
svc service.Service
|
|
ctx context.Context
|
|
cfg *config.Config
|
|
gatewayClient *cs3mocks.GatewayAPIClient
|
|
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
|
|
eventsPublisher mocks.Publisher
|
|
roleService *mocks.RoleService
|
|
valueService *settingsmocks.ValueService
|
|
permissionService *mocks.Permissions
|
|
identityBackend *identitymocks.Backend
|
|
|
|
rr *httptest.ResponseRecorder
|
|
|
|
currentUser = &userv1beta1.User{
|
|
Id: &userv1beta1.UserId{
|
|
OpaqueId: "user",
|
|
},
|
|
}
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
eventsPublisher.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
|
|
|
pool.RemoveSelector("GatewaySelector" + "com.owncloud.api.gateway")
|
|
gatewayClient = &cs3mocks.GatewayAPIClient{}
|
|
gatewaySelector = pool.GetSelector[gateway.GatewayAPIClient](
|
|
"GatewaySelector",
|
|
"com.owncloud.api.gateway",
|
|
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
|
|
return gatewayClient
|
|
},
|
|
)
|
|
|
|
identityBackend = &identitymocks.Backend{}
|
|
roleService = &mocks.RoleService{}
|
|
valueService = &settingsmocks.ValueService{}
|
|
permissionService = &mocks.Permissions{}
|
|
|
|
rr = httptest.NewRecorder()
|
|
ctx = context.Background()
|
|
|
|
cfg = defaults.FullDefaultConfig()
|
|
cfg.Identity.LDAP.CACert = "" // skip the startup checks, we don't use LDAP at all in this tests
|
|
cfg.TokenManager.JWTSecret = "loremipsum"
|
|
cfg.Commons = &shared.Commons{}
|
|
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
|
|
cfg.Application.ID = "some-application-ID"
|
|
|
|
})
|
|
|
|
When("OCM is disabled", func() {
|
|
BeforeEach(func() {
|
|
svc, _ = service.NewService(
|
|
service.Config(cfg),
|
|
service.WithGatewaySelector(gatewaySelector),
|
|
service.EventsPublisher(&eventsPublisher),
|
|
service.WithIdentityBackend(identityBackend),
|
|
service.WithRoleService(roleService),
|
|
service.WithValueService(valueService),
|
|
service.PermissionService(permissionService),
|
|
)
|
|
})
|
|
|
|
Describe("GetMe", func() {
|
|
It("handles missing user", func() {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me", nil)
|
|
svc.GetMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusInternalServerError))
|
|
})
|
|
|
|
It("gets the information", func() {
|
|
user := &libregraph.User{
|
|
Id: libregraph.PtrString("user"),
|
|
Identities: []libregraph.ObjectIdentity{
|
|
{
|
|
Issuer: libregraph.PtrString("https://idp.example.com"),
|
|
IssuerAssignedId: libregraph.PtrString("the-oidc-sub"),
|
|
},
|
|
},
|
|
}
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me", nil)
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.GetMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// The identity (issuerAssignedId, the OIDC sub) must come from the
|
|
// backend, not be fabricated from the internal user UUID. See
|
|
// OCISDEV-751.
|
|
Expect(responseUser.GetIdentities()).To(HaveLen(1))
|
|
Expect(responseUser.GetIdentities()[0].GetIssuerAssignedId()).To(Equal("the-oidc-sub"))
|
|
})
|
|
|
|
It("expands the memberOf", func() {
|
|
user := &libregraph.User{
|
|
Id: libregraph.PtrString("user1"),
|
|
MemberOf: []libregraph.Group{
|
|
{DisplayName: libregraph.PtrString("somegroup")},
|
|
},
|
|
}
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me?$expand=memberOf", nil)
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.GetMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(responseUser.GetId()).To(Equal("user1"))
|
|
Expect(responseUser.GetMemberOf()).To(HaveLen(1))
|
|
Expect(responseUser.GetMemberOf()[0].GetDisplayName()).To(Equal("somegroup"))
|
|
|
|
})
|
|
|
|
It("expands the appRoleAssignments", func() {
|
|
assignments := []*settingsmsg.UserRoleAssignment{
|
|
{
|
|
Id: "some-appRoleAssignment-ID",
|
|
AccountUuid: "user",
|
|
RoleId: "some-appRole-ID",
|
|
},
|
|
}
|
|
user := &libregraph.User{
|
|
Id: libregraph.PtrString("user"),
|
|
}
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me?$expand=appRoleAssignments", nil)
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.GetMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(responseUser.GetId()).To(Equal("user"))
|
|
Expect(responseUser.GetAppRoleAssignments()).To(HaveLen(1))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetId()).To(Equal("some-appRoleAssignment-ID"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetAppRoleId()).To(Equal("some-appRole-ID"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetPrincipalId()).To(Equal("user"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetResourceId()).To(Equal("some-application-ID"))
|
|
})
|
|
})
|
|
|
|
Describe("GetUsers", func() {
|
|
It("handles invalid requests", func() {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$invalid=true", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("lists the users with 2fa", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
users := []*libregraph.User{user}
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users", nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(res.Value)).To(Equal(1))
|
|
Expect(res.Value[0].GetId()).To(Equal("user1"))
|
|
})
|
|
It("denies listing without 2fa", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
Expect(rr.Header().Get("X-Ocis-Mfa-Required")).To(Equal("true"))
|
|
})
|
|
It("denies listing for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
It("denies using to short search terms for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=a", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
It("denies using to short quoted search terms for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=%22ab%22", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
It("denies filters other that 'userType eq ...' for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=%22abc%22&$filter="+url.QueryEscape("memberOf/any(n:n/id eq 25cb7bc0-3168-4a0c-adbe-396f478ad494)"), nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("only returns a restricted set of attributes for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
user := libregraph.NewUser("abc user", "username")
|
|
user.SetId("user1")
|
|
user.SetPasswordProfile(
|
|
libregraph.PasswordProfile{
|
|
Password: libregraph.PtrString("password"),
|
|
},
|
|
)
|
|
user.SetUserType("usertype")
|
|
users := []*libregraph.User{user}
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=abc", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
userMap, err := res.Value[0].ToMap()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
for k := range userMap {
|
|
Expect(k).Should(BeElementOf([]string{"displayName", "onPremisesSamAccountName", "mail", "id", "userType"}))
|
|
}
|
|
})
|
|
|
|
Describe("user attributes", func() {
|
|
var (
|
|
user *libregraph.User
|
|
users []*libregraph.User
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
cfg.API.UserSearchDisplayedAttributes = []string{"displayName", "onPremisesSamAccountName", "mail"}
|
|
|
|
user = libregraph.NewUser("Albert Einstein", "einstein")
|
|
user.SetId("user1")
|
|
user.SetMail("albert.einstein@example.com")
|
|
user.SetUserType("Member")
|
|
user.SetPasswordProfile(libregraph.PasswordProfile{Password: libregraph.PtrString("secret")})
|
|
users = []*libregraph.User{user}
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
|
})
|
|
|
|
It("returns full user objects and configured attributes for privileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=einstein", nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var res userList
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(res.Value).To(HaveLen(1))
|
|
|
|
Expect(res.Value[0].User.GetId()).To(Equal("user1"))
|
|
Expect(res.Value[0].User.GetDisplayName()).To(Equal("Albert Einstein"))
|
|
Expect(res.Value[0].User.GetOnPremisesSamAccountName()).To(Equal("einstein"))
|
|
Expect(res.Value[0].User.GetMail()).To(Equal("albert.einstein@example.com"))
|
|
Expect(res.Value[0].User.HasPasswordProfile()).To(BeTrue())
|
|
|
|
Expect(res.Value[0].Attributes).To(ConsistOf(
|
|
"Albert Einstein",
|
|
"einstein",
|
|
"albert.einstein@example.com",
|
|
))
|
|
})
|
|
|
|
It("returns restricted user objects and configured attributes for unprivileged users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=einstein", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var res userList
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(res.Value).To(HaveLen(1))
|
|
|
|
Expect(res.Value[0].User.GetId()).To(Equal("user1"))
|
|
Expect(res.Value[0].User.GetDisplayName()).To(Equal("Albert Einstein"))
|
|
Expect(res.Value[0].User.GetUserType()).To(Equal("Member"))
|
|
Expect(res.Value[0].User.GetOnPremisesSamAccountName()).To(Equal(""))
|
|
Expect(res.Value[0].User.HasMail()).To(BeTrue())
|
|
Expect(res.Value[0].User.HasPasswordProfile()).To(BeFalse())
|
|
|
|
Expect(res.Value[0].Attributes).To(ConsistOf(
|
|
"Albert Einstein",
|
|
"einstein",
|
|
"albert.einstein@example.com",
|
|
))
|
|
})
|
|
})
|
|
|
|
It("sorts", func() {
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
user.SetMail("z@example.com")
|
|
user.SetDisplayName("9")
|
|
user.SetOnPremisesSamAccountName("9")
|
|
user2 := &libregraph.User{}
|
|
user2.SetId("user2")
|
|
user2.SetMail("a@example.com")
|
|
user2.SetDisplayName("1")
|
|
user2.SetOnPremisesSamAccountName("1")
|
|
users := []*libregraph.User{user, user2}
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
|
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
getUsers := func(path string) []*service.UserWithAttributes {
|
|
r := httptest.NewRequest(http.MethodGet, path, nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
rec := httptest.NewRecorder()
|
|
svc.GetUsers(rec, r)
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rec.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
return res.Value
|
|
}
|
|
|
|
unsorted := getUsers("/graph/v1.0/users")
|
|
Expect(len(unsorted)).To(Equal(2))
|
|
Expect(unsorted[0].GetId()).To(Equal("user1"))
|
|
Expect(unsorted[1].GetId()).To(Equal("user2"))
|
|
|
|
byMail := getUsers("/graph/v1.0/users?$orderby=mail")
|
|
Expect(len(byMail)).To(Equal(2))
|
|
Expect(byMail[0].GetId()).To(Equal("user2"))
|
|
Expect(byMail[1].GetId()).To(Equal("user1"))
|
|
byMail = getUsers("/graph/v1.0/users?$orderby=mail%20asc")
|
|
Expect(len(byMail)).To(Equal(2))
|
|
Expect(byMail[0].GetId()).To(Equal("user2"))
|
|
Expect(byMail[1].GetId()).To(Equal("user1"))
|
|
byMail = getUsers("/graph/v1.0/users?$orderby=mail%20desc")
|
|
Expect(len(byMail)).To(Equal(2))
|
|
Expect(byMail[0].GetId()).To(Equal("user1"))
|
|
Expect(byMail[1].GetId()).To(Equal("user2"))
|
|
|
|
byDisplayName := getUsers("/graph/v1.0/users?$orderby=displayName")
|
|
Expect(len(byDisplayName)).To(Equal(2))
|
|
Expect(byDisplayName[0].GetId()).To(Equal("user2"))
|
|
Expect(byDisplayName[1].GetId()).To(Equal("user1"))
|
|
byDisplayName = getUsers("/graph/v1.0/users?$orderby=displayName%20asc")
|
|
Expect(len(byDisplayName)).To(Equal(2))
|
|
Expect(byDisplayName[0].GetId()).To(Equal("user2"))
|
|
Expect(byDisplayName[1].GetId()).To(Equal("user1"))
|
|
byDisplayName = getUsers("/graph/v1.0/users?$orderby=displayName%20desc")
|
|
Expect(len(byDisplayName)).To(Equal(2))
|
|
Expect(byDisplayName[0].GetId()).To(Equal("user1"))
|
|
Expect(byDisplayName[1].GetId()).To(Equal("user2"))
|
|
|
|
byOnPremisesSamAccountName := getUsers("/graph/v1.0/users?$orderby=onPremisesSamAccountName")
|
|
Expect(len(byOnPremisesSamAccountName)).To(Equal(2))
|
|
Expect(byOnPremisesSamAccountName[0].GetId()).To(Equal("user2"))
|
|
Expect(byOnPremisesSamAccountName[1].GetId()).To(Equal("user1"))
|
|
byOnPremisesSamAccountName = getUsers("/graph/v1.0/users?$orderby=onPremisesSamAccountName%20asc")
|
|
Expect(len(byOnPremisesSamAccountName)).To(Equal(2))
|
|
Expect(byOnPremisesSamAccountName[0].GetId()).To(Equal("user2"))
|
|
Expect(byOnPremisesSamAccountName[1].GetId()).To(Equal("user1"))
|
|
byOnPremisesSamAccountName = getUsers("/graph/v1.0/users?$orderby=onPremisesSamAccountName%20desc")
|
|
Expect(len(byOnPremisesSamAccountName)).To(Equal(2))
|
|
Expect(byOnPremisesSamAccountName[0].GetId()).To(Equal("user1"))
|
|
Expect(byOnPremisesSamAccountName[1].GetId()).To(Equal("user2"))
|
|
|
|
// Handles invalid sort field
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$orderby=invalid", nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("expands the appRoleAssignments", func() {
|
|
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
user.SetMail("z@example.com")
|
|
user.SetDisplayName("9")
|
|
user.SetOnPremisesSamAccountName("9")
|
|
user2 := &libregraph.User{}
|
|
user2.SetId("user2")
|
|
user2.SetMail("a@example.com")
|
|
user2.SetDisplayName("1")
|
|
user2.SetOnPremisesSamAccountName("1")
|
|
users := []*libregraph.User{user, user2}
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
|
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(func(ctx context.Context, in *settings.ListRoleAssignmentsRequest, opts ...client.CallOption) *settings.ListRoleAssignmentsResponse {
|
|
return &settings.ListRoleAssignmentsResponse{Assignments: []*settingsmsg.UserRoleAssignment{
|
|
{
|
|
Id: "some-appRoleAssignment-ID",
|
|
AccountUuid: in.GetAccountUuid(),
|
|
RoleId: "some-appRole-ID",
|
|
},
|
|
}}
|
|
}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$expand=appRoleAssignments", nil)
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
responseUsers := res.Value
|
|
Expect(len(responseUsers)).To(Equal(2))
|
|
Expect(responseUsers[0].GetId()).To(Equal("user1"))
|
|
Expect(responseUsers[0].GetAppRoleAssignments()).To(HaveLen(1))
|
|
Expect(responseUsers[0].GetAppRoleAssignments()[0].GetId()).To(Equal("some-appRoleAssignment-ID"))
|
|
Expect(responseUsers[0].GetAppRoleAssignments()[0].GetAppRoleId()).To(Equal("some-appRole-ID"))
|
|
Expect(responseUsers[0].GetAppRoleAssignments()[0].GetPrincipalId()).To(Equal("user1"))
|
|
Expect(responseUsers[0].GetAppRoleAssignments()[0].GetResourceId()).To(Equal("some-application-ID"))
|
|
|
|
Expect(responseUsers[1].GetId()).To(Equal("user2"))
|
|
Expect(responseUsers[1].GetAppRoleAssignments()).To(HaveLen(1))
|
|
Expect(responseUsers[1].GetAppRoleAssignments()[0].GetId()).To(Equal("some-appRoleAssignment-ID"))
|
|
Expect(responseUsers[1].GetAppRoleAssignments()[0].GetAppRoleId()).To(Equal("some-appRole-ID"))
|
|
Expect(responseUsers[1].GetAppRoleAssignments()[0].GetPrincipalId()).To(Equal("user2"))
|
|
Expect(responseUsers[1].GetAppRoleAssignments()[0].GetResourceId()).To(Equal("some-application-ID"))
|
|
|
|
})
|
|
})
|
|
|
|
DescribeTable("GetUsers handles unsupported or invalid filters",
|
|
func(filter string, status int) {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape(filter), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(status))
|
|
},
|
|
Entry("with invalid filter", "invalid", http.StatusBadRequest),
|
|
Entry("with unsupported filter for user property", "mail eq 'unsupported'", http.StatusNotImplemented),
|
|
Entry("with unsupported filter operation", "mail add 10", http.StatusBadRequest),
|
|
Entry("with unsupported logical operation", "memberOf/any(n:n/id eq 1) or memberOf/any(n:n/id eq 2)", http.StatusNotImplemented),
|
|
Entry("with unsupported lambda query ", `drives/any(n:n/id eq '1')`, http.StatusNotImplemented),
|
|
Entry("with unsupported lambda token ", "memberOf/all(n:n/id eq 1)", http.StatusNotImplemented),
|
|
Entry("with unsupported filter operation ", "memberOf/any(n:n/id ne 1)", http.StatusNotImplemented),
|
|
Entry("with unsupported filter operand type", "memberOf/any(n:n/id eq 1)", http.StatusNotImplemented),
|
|
Entry("with unsupported memberOf lambda filter property", "memberOf/any(n:n/name eq 'name')", http.StatusNotImplemented),
|
|
Entry("with unsupported appRoleAssignments lambda filter property", "appRoleAssignments/any(n:n/id eq 'id')", http.StatusNotImplemented),
|
|
Entry("with unsupported appRoleAssignments lambda filter property",
|
|
"appRoleAssignments/any(n:n/id eq 'id') and appRoleAssignments/any(n:n/id eq 'id')", http.StatusNotImplemented),
|
|
Entry("with unsupported appRoleAssignments lambda filter operation",
|
|
"appRoleAssignments/all(n:n/appRoleId eq 'id') and appRoleAssignments/any(n:n/appRoleId eq 'id')", http.StatusNotImplemented),
|
|
Entry("with unsupported appRoleAssignments lambda filter operation",
|
|
"appRoleAssignments/any(n:n/appRoleId ne 'id') and appRoleAssignments/any(n:n/appRoleId eq 'id')", http.StatusNotImplemented),
|
|
Entry("with unsupported userType in filter", "userType eq 'unsupported'", http.StatusNotImplemented),
|
|
Entry("with unsupported property in eq filter", "unsupported eq 'unsupported'", http.StatusNotImplemented),
|
|
)
|
|
|
|
DescribeTable("With a valid filter",
|
|
func(filter string, status int) {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
user := &libregraph.User{}
|
|
user.SetId("25cb7bc0-3168-4a0c-adbe-396f478ad494")
|
|
users := []*libregraph.User{user}
|
|
identityBackend.On("GetGroupMembers", mock.Anything, "25cb7bc0-3168-4a0c-adbe-396f478ad494", mock.Anything).Return(users, nil)
|
|
identityBackend.On("GetGroupMembers", mock.Anything, "2713f1d5-6822-42bd-ad56-9f6c55a3a8fa", mock.Anything).Return([]*libregraph.User{}, nil)
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Return([]*libregraph.User{user}, nil)
|
|
roleService.On("ListRoleAssignmentsFiltered", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(func(ctx context.Context, in *settings.ListRoleAssignmentsFilteredRequest, opts ...client.CallOption) *settings.ListRoleAssignmentsResponse {
|
|
return &settings.ListRoleAssignmentsResponse{Assignments: []*settingsmsg.UserRoleAssignment{
|
|
{
|
|
Id: "some-appRoleAssignment-ID",
|
|
AccountUuid: user.GetId(),
|
|
RoleId: "some-appRole-ID",
|
|
},
|
|
}}
|
|
}, nil)
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(func(ctx context.Context, in *settings.ListRoleAssignmentsRequest, opts ...client.CallOption) *settings.ListRoleAssignmentsResponse {
|
|
return &settings.ListRoleAssignmentsResponse{Assignments: []*settingsmsg.UserRoleAssignment{
|
|
{
|
|
Id: "some-appRoleAssignment-ID",
|
|
AccountUuid: user.GetId(),
|
|
RoleId: "some-appRole-ID",
|
|
},
|
|
}}
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape(filter), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(status))
|
|
},
|
|
Entry("with memberOf lambda filter with UUID", "memberOf/any(n:n/id eq 25cb7bc0-3168-4a0c-adbe-396f478ad494)", http.StatusOK),
|
|
Entry("with memberOf lambda filter with UUID string", "memberOf/any(n:n/id eq '25cb7bc0-3168-4a0c-adbe-396f478ad494')", http.StatusOK),
|
|
Entry("with appRoleAssignments lambda filter with appRoleId", "appRoleAssignments/any(n:n/appRoleId eq 'some-appRole-ID')", http.StatusOK),
|
|
Entry("with two memberOf lambda filters combined with and",
|
|
"memberOf/any(n:n/id eq 25cb7bc0-3168-4a0c-adbe-396f478ad494) and memberOf/any(n:n/id eq 2713f1d5-6822-42bd-ad56-9f6c55a3a8fa)",
|
|
http.StatusOK),
|
|
Entry("with two memberOf lambda filters combined with or",
|
|
"memberOf/any(n:n/id eq 25cb7bc0-3168-4a0c-adbe-396f478ad494) or memberOf/any(n:n/id eq 2713f1d5-6822-42bd-ad56-9f6c55a3a8fa)",
|
|
http.StatusOK),
|
|
Entry("with supported appRoleAssignments lambda filter property",
|
|
"appRoleAssignments/any(n:n/appRoleId eq 'some-appRoleAssignment-ID') and memberOf/any(n:n/id eq 2713f1d5-6822-42bd-ad56-9f6c55a3a8fa)",
|
|
http.StatusOK),
|
|
)
|
|
|
|
Describe("the vaultEligible filter", func() {
|
|
// vaultRole is a role bundle that grants vault mode, vaultLessRole is one that does not.
|
|
const (
|
|
vaultRole = "vault-granting-role-ID"
|
|
vaultLessRole = "ordinary-role-ID"
|
|
eligibleUser = "25cb7bc0-3168-4a0c-adbe-396f478ad494"
|
|
otherUser = "2713f1d5-6822-42bd-ad56-9f6c55a3a8fa"
|
|
thirdUser = "3a6e6e0e-3f8b-4e3e-9f8b-1a2b3c4d5e6f"
|
|
)
|
|
|
|
// expectRoles makes the settings service report both roles, only one of which
|
|
// carries the vault mode permission.
|
|
expectRoles := func() {
|
|
roleService.On("ListRoles", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListBundlesResponse{
|
|
Bundles: []*settingsmsg.Bundle{
|
|
{
|
|
Id: vaultRole,
|
|
Type: settingsmsg.Bundle_TYPE_ROLE,
|
|
Settings: []*settingsmsg.Setting{
|
|
{
|
|
Name: permission.VaultMode,
|
|
Value: &settingsmsg.Setting_PermissionValue{PermissionValue: &settingsmsg.Permission{}},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Id: vaultLessRole,
|
|
Type: settingsmsg.Bundle_TYPE_ROLE,
|
|
Settings: []*settingsmsg.Setting{
|
|
{
|
|
Name: "Some.OtherPermission",
|
|
Value: &settingsmsg.Setting_PermissionValue{PermissionValue: &settingsmsg.Permission{}},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
}
|
|
|
|
// expectAssignments stubs ListRoleAssignments (the per-account assignment lookup)
|
|
// for a single account.
|
|
expectAssignments := func(accountUUID string, assignments []*settingsmsg.UserRoleAssignment, err error) {
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.MatchedBy(
|
|
func(in *settings.ListRoleAssignmentsRequest) bool {
|
|
return in.GetAccountUuid() == accountUUID
|
|
}), mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, err)
|
|
}
|
|
|
|
// requestVaultEligibleUsers builds a GetUsers request for filter, executes it against
|
|
// svc, and unmarshals a successful response body into a userList. It fails the test
|
|
// if the response isn't 200.
|
|
requestVaultEligibleUsers := func(filter string) userList {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape(filter), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
res := userList{}
|
|
Expect(json.Unmarshal(data, &res)).To(Succeed())
|
|
return res
|
|
}
|
|
|
|
BeforeEach(func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Operation: settingsmsg.Permission_OPERATION_UNKNOWN,
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
eligible := &libregraph.User{}
|
|
eligible.SetId(eligibleUser)
|
|
ineligible := &libregraph.User{}
|
|
ineligible.SetId(otherUser)
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Return([]*libregraph.User{eligible, ineligible}, nil)
|
|
})
|
|
|
|
DescribeTable("returns only the users assigned to a role that grants vault mode",
|
|
func(filter string) {
|
|
expectRoles()
|
|
// Only the eligible user's own assignments hold the vault-granting role;
|
|
// the other user holds a role that doesn't grant it.
|
|
expectAssignments(eligibleUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-ID", AccountUuid: eligibleUser, RoleId: vaultRole},
|
|
}, nil)
|
|
expectAssignments(otherUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "other-assignment-ID", AccountUuid: otherUser, RoleId: vaultLessRole},
|
|
}, nil)
|
|
|
|
res := requestVaultEligibleUsers(filter)
|
|
Expect(res.Value).To(HaveLen(1))
|
|
Expect(res.Value[0].GetId()).To(Equal(eligibleUser))
|
|
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoleAssignmentsFiltered", mock.Anything, mock.Anything, mock.Anything)
|
|
},
|
|
Entry("plain", "vaultEligible eq true"),
|
|
Entry("parenthesized", "(vaultEligible eq true)"),
|
|
)
|
|
|
|
It("drops a user with no role assignments at all, as a normal (non-error) outcome", func() {
|
|
expectRoles()
|
|
expectAssignments(eligibleUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-ID", AccountUuid: eligibleUser, RoleId: vaultRole},
|
|
}, nil)
|
|
expectAssignments(otherUser, nil, nil)
|
|
|
|
res := requestVaultEligibleUsers("vaultEligible eq true")
|
|
Expect(res.Value).To(HaveLen(1))
|
|
Expect(res.Value[0].GetId()).To(Equal(eligibleUser))
|
|
})
|
|
|
|
It("returns nobody when no role grants vault mode, without listing anyone's assignments", func() {
|
|
roleService.On("ListRoles", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListBundlesResponse{
|
|
Bundles: []*settingsmsg.Bundle{{Id: vaultLessRole, Type: settingsmsg.Bundle_TYPE_ROLE}},
|
|
}, nil)
|
|
|
|
res := requestVaultEligibleUsers("vaultEligible eq true")
|
|
Expect(res.Value).To(BeEmpty())
|
|
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything)
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoleAssignmentsFiltered", mock.Anything, mock.Anything, mock.Anything)
|
|
})
|
|
|
|
It("makes no calls to the role service when the directory search returns no users", func() {
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Unset()
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Return([]*libregraph.User{}, nil)
|
|
|
|
res := requestVaultEligibleUsers("vaultEligible eq true")
|
|
Expect(res.Value).To(BeEmpty())
|
|
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoles", mock.Anything, mock.Anything, mock.Anything)
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything)
|
|
roleService.AssertNotCalled(GinkgoT(), "ListRoleAssignmentsFiltered", mock.Anything, mock.Anything, mock.Anything)
|
|
})
|
|
|
|
It("fails the request when the role list cannot be fetched", func() {
|
|
roleService.On("ListRoles", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(nil, errors.New("settings service unavailable"))
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape("vaultEligible eq true"), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).ToNot(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("fails the request when a per-user role assignment check fails", func() {
|
|
expectRoles()
|
|
expectAssignments(eligibleUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-ID", AccountUuid: eligibleUser, RoleId: vaultRole},
|
|
}, nil)
|
|
expectAssignments(otherUser, nil, errors.New("settings service unavailable"))
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape("vaultEligible eq true"), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).ToNot(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("preserves the directory search's original order under concurrent completion, dropping a middle ineligible user", func() {
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Unset()
|
|
first := &libregraph.User{}
|
|
first.SetId(thirdUser)
|
|
second := &libregraph.User{}
|
|
second.SetId(eligibleUser)
|
|
third := &libregraph.User{}
|
|
third.SetId(otherUser)
|
|
// Deliberately not alphabetical/sorted, so that a concurrency bug that
|
|
// reorders results would be caught.
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything).Return([]*libregraph.User{first, second, third}, nil)
|
|
|
|
expectRoles()
|
|
|
|
// Force the first user's lookup to finish last: it blocks on lastDone, which the
|
|
// last user's lookup closes only after it has already returned. Since SetLimit
|
|
// is 10, all three per-user goroutines run concurrently (the middle and last
|
|
// lookups aren't blocked waiting on the first), so this reliably makes completion
|
|
// order (third, second, first) the reverse of directory order (first, second,
|
|
// third) without any sleeps.
|
|
lastDone := make(chan struct{})
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.MatchedBy(
|
|
func(in *settings.ListRoleAssignmentsRequest) bool {
|
|
return in.GetAccountUuid() == thirdUser
|
|
}), mock.Anything).Run(func(mock.Arguments) {
|
|
<-lastDone
|
|
}).Return(&settings.ListRoleAssignmentsResponse{Assignments: []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-1", AccountUuid: thirdUser, RoleId: vaultRole},
|
|
}}, nil)
|
|
|
|
// The middle user holds no vault-granting role, so it must be dropped from the
|
|
// result rather than just reordered.
|
|
expectAssignments(eligibleUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-2", AccountUuid: eligibleUser, RoleId: vaultLessRole},
|
|
}, nil)
|
|
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.MatchedBy(
|
|
func(in *settings.ListRoleAssignmentsRequest) bool {
|
|
return in.GetAccountUuid() == otherUser
|
|
}), mock.Anything).Run(func(mock.Arguments) {
|
|
close(lastDone)
|
|
}).Return(&settings.ListRoleAssignmentsResponse{Assignments: []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-3", AccountUuid: otherUser, RoleId: vaultRole},
|
|
}}, nil)
|
|
|
|
res := requestVaultEligibleUsers("vaultEligible eq true")
|
|
Expect(res.Value).To(HaveLen(2))
|
|
Expect(res.Value[0].GetId()).To(Equal(thirdUser))
|
|
Expect(res.Value[1].GetId()).To(Equal(otherUser))
|
|
})
|
|
|
|
DescribeTable("rejects everything but 'eq true'",
|
|
func(filter string, status int) {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$filter="+url.QueryEscape(filter), nil)
|
|
r = r.WithContext(revactx.SetMFA(r.Context()))
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(status))
|
|
},
|
|
Entry("with 'false'", "vaultEligible eq false", http.StatusNotImplemented),
|
|
Entry("with a string operand", "vaultEligible eq 'true'", http.StatusNotImplemented),
|
|
Entry("with a numeric operand", "vaultEligible eq 1", http.StatusNotImplemented),
|
|
)
|
|
|
|
// The picker is used by ordinary users, so the filter has to survive the guard that
|
|
// otherwise restricts unprivileged users to 'userType eq ...'.
|
|
It("is allowed for an unprivileged user", func() {
|
|
expectRoles()
|
|
expectAssignments(eligibleUser, []*settingsmsg.UserRoleAssignment{
|
|
{Id: "assignment-ID", AccountUuid: eligibleUser, RoleId: vaultRole},
|
|
}, nil)
|
|
expectAssignments(otherUser, nil, nil)
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Unset()
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).
|
|
Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodGet,
|
|
`/graph/v1.0/users?$search=%22abc%22&$filter=`+url.QueryEscape("vaultEligible eq true"), nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
})
|
|
})
|
|
|
|
Describe("GetUser", func() {
|
|
It("handles missing userids", func() {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users", nil)
|
|
svc.GetUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("gets the user", func() {
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
|
|
// anyone can get the userId
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", *user.Id)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.GetUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(responseUser.GetId()).To(Equal("user1"))
|
|
Expect(len(responseUser.GetDrives())).To(Equal(0))
|
|
})
|
|
|
|
It("includes the personal space if requested", func() {
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
|
|
// requires privileges
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
|
Status: status.NewOK(ctx),
|
|
TotalBytes: 10,
|
|
}, nil)
|
|
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
|
Status: status.NewOK(ctx),
|
|
StorageSpaces: []*provider.StorageSpace{
|
|
{
|
|
Id: &provider.StorageSpaceId{OpaqueId: "drive1"},
|
|
Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"},
|
|
SpaceType: "project",
|
|
},
|
|
{
|
|
Id: &provider.StorageSpaceId{OpaqueId: "personal"},
|
|
Owner: &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "user1"}},
|
|
Root: &provider.ResourceId{SpaceId: "personal", OpaqueId: "personal"},
|
|
SpaceType: "personal",
|
|
},
|
|
},
|
|
}, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$expand=drive", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", *user.Id)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.GetUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(responseUser.GetId()).To(Equal("user1"))
|
|
Expect(*responseUser.GetDrive().Id).To(Equal("personal"))
|
|
})
|
|
|
|
It("includes the drives if requested", func() {
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
|
|
// requires privileges
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
|
Status: status.NewOK(ctx),
|
|
TotalBytes: 10,
|
|
}, nil)
|
|
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
|
Status: status.NewOK(ctx),
|
|
StorageSpaces: []*provider.StorageSpace{
|
|
{
|
|
Id: &provider.StorageSpaceId{OpaqueId: "drive1"},
|
|
Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"},
|
|
},
|
|
},
|
|
}, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$expand=drives", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", *user.Id)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.GetUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(responseUser.GetId()).To(Equal("user1"))
|
|
Expect(len(responseUser.GetDrives())).To(Equal(1))
|
|
})
|
|
|
|
It("expands the appRoleAssignments", func() {
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
|
|
// requires privileges
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
|
|
assignments := []*settingsmsg.UserRoleAssignment{
|
|
{
|
|
Id: "some-appRoleAssignment-ID",
|
|
AccountUuid: "user1",
|
|
RoleId: "some-appRole-ID",
|
|
},
|
|
}
|
|
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, nil)
|
|
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
|
Return(&settings.GetValueResponse{
|
|
Value: &settingsmsg.ValueWithIdentifier{
|
|
Value: &settingsmsg.Value{
|
|
Value: &settingsmsg.Value_ListValue{
|
|
ListValue: &settingsmsg.ListValue{
|
|
Values: []*settingsmsg.ListOptionValue{
|
|
{
|
|
Option: &settingsmsg.ListOptionValue_StringValue{
|
|
StringValue: "it",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/user1?$expand=appRoleAssignments", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", user.GetId())
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.GetUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
responseUser := &libregraph.User{}
|
|
err = json.Unmarshal(data, &responseUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(responseUser.GetId()).To(Equal("user1"))
|
|
Expect(responseUser.GetAppRoleAssignments()).To(HaveLen(1))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetId()).To(Equal("some-appRoleAssignment-ID"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetAppRoleId()).To(Equal("some-appRole-ID"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetPrincipalId()).To(Equal("user1"))
|
|
Expect(responseUser.GetAppRoleAssignments()[0].GetResourceId()).To(Equal("some-application-ID"))
|
|
})
|
|
})
|
|
|
|
Describe("GetUserDrive", func() {
|
|
// grantPerms sets the permission constraint returned for every
|
|
// GetPermissionByID call. CONSTRAINT_ALL makes
|
|
// contextUserHasFullAccountPerms return true (admin); CONSTRAINT_OWN
|
|
// makes it return false (non-admin).
|
|
grantPerms := func(c settingsmsg.Permission_Constraint) {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{Constraint: c},
|
|
}, nil)
|
|
}
|
|
// expectPersonalSpace mocks a successful personal-drive listing owned by ownerID.
|
|
expectPersonalSpace := func(ownerID string) {
|
|
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
|
Status: status.NewOK(ctx),
|
|
TotalBytes: 10,
|
|
}, nil)
|
|
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
|
Status: status.NewOK(ctx),
|
|
StorageSpaces: []*provider.StorageSpace{
|
|
{
|
|
Id: &provider.StorageSpaceId{OpaqueId: "personal"},
|
|
Owner: &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: ownerID}},
|
|
Root: &provider.ResourceId{SpaceId: "personal", OpaqueId: "personal"},
|
|
SpaceType: "personal",
|
|
},
|
|
},
|
|
}, nil)
|
|
}
|
|
|
|
doRequest := func(userID string) {
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/"+userID+"/drive", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", userID)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.GetUserDrive(rr, r)
|
|
}
|
|
|
|
It("returns the caller's own drive (self, non-admin)", func() {
|
|
grantPerms(settingsmsg.Permission_CONSTRAINT_OWN)
|
|
expectPersonalSpace("user")
|
|
|
|
doRequest("user")
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
drive := &libregraph.Drive{}
|
|
Expect(json.Unmarshal(data, drive)).To(Succeed())
|
|
Expect(drive.GetId()).To(Equal("personal"))
|
|
})
|
|
|
|
It("returns the personal drive for the /me path (empty userID)", func() {
|
|
grantPerms(settingsmsg.Permission_CONSTRAINT_OWN)
|
|
expectPersonalSpace("user")
|
|
|
|
doRequest("")
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("allows a full-account admin to read another user's drive", func() {
|
|
grantPerms(settingsmsg.Permission_CONSTRAINT_ALL)
|
|
expectPersonalSpace("other")
|
|
|
|
doRequest("other")
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
})
|
|
|
|
It("denies a non-admin reading another user's drive (IDOR), without listing spaces", func() {
|
|
grantPerms(settingsmsg.Permission_CONSTRAINT_OWN)
|
|
|
|
doRequest("other")
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusNotFound))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(data)).To(ContainSubstring("no drive returned from storage"))
|
|
gatewayClient.AssertNotCalled(GinkgoT(), "ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything)
|
|
})
|
|
})
|
|
|
|
Describe("PostUser", func() {
|
|
var (
|
|
user *libregraph.User
|
|
|
|
assertHandleBadAttributes = func(user *libregraph.User) {
|
|
userJson, err := json.Marshal(user)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users", bytes.NewBuffer(userJson))
|
|
svc.PostUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
}
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
user = libregraph.NewUser("Display Name", "user")
|
|
})
|
|
|
|
It("handles invalid bodies", func() {
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users?$invalid=true", nil)
|
|
svc.PostUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("handles missing display names", func() {
|
|
user.SetDisplayName("")
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("handles missing OnPremisesSamAccountName", func() {
|
|
user.SetOnPremisesSamAccountName("")
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("handles bad Mails", func() {
|
|
user.SetMail("not-a-mail-address")
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("handles set Ids - they are read-only", func() {
|
|
user.SetId("/users/user")
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("handles set memberOf - it's read-only", func() {
|
|
group := libregraph.Group{}
|
|
group.SetId("/groups/group")
|
|
group.SetDisplayName("Group")
|
|
user.SetMemberOf([]libregraph.Group{group})
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("handles set userType - it's read-only", func() {
|
|
user.SetUserType("Member")
|
|
assertHandleBadAttributes(user)
|
|
})
|
|
|
|
It("creates a user", func() {
|
|
roleService.On("AssignRoleToUser", mock.Anything, mock.Anything).Return(&settings.AssignRoleToUserResponse{}, nil)
|
|
identityBackend.On("CreateUser", mock.Anything, mock.Anything).Return(func(ctx context.Context, user libregraph.User) *libregraph.User {
|
|
user.SetId("/users/user")
|
|
return &user
|
|
}, nil)
|
|
userJson, err := json.Marshal(user)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users", bytes.NewBuffer(userJson))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.PostUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusCreated))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
createdUser := libregraph.User{}
|
|
err = json.Unmarshal(data, &createdUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(createdUser.GetUserType()).To(Equal("Member"))
|
|
})
|
|
|
|
It("creates a member user", func() {
|
|
roleService.On("AssignRoleToUser", mock.Anything, mock.Anything).Return(&settings.AssignRoleToUserResponse{}, nil)
|
|
identityBackend.On("CreateUser", mock.Anything, mock.Anything).Return(func(ctx context.Context, user libregraph.User) *libregraph.User {
|
|
user.SetId("/users/user")
|
|
return &user
|
|
}, nil)
|
|
|
|
userJson, err := json.Marshal(user)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users", bytes.NewBuffer(userJson))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.PostUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusCreated))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
createdUser := libregraph.User{}
|
|
err = json.Unmarshal(data, &createdUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(createdUser.GetUserType()).To(Equal("Member"))
|
|
})
|
|
|
|
Describe("Handling usernames with spaces", func() {
|
|
var (
|
|
newSvc = func(usernameMatch string) service.Service {
|
|
localCfg := defaults.FullDefaultConfig()
|
|
localCfg.Identity.LDAP.CACert = "" // skip the startup checks, we don't use LDAP at all in this tests
|
|
localCfg.TokenManager.JWTSecret = "loremipsum"
|
|
localCfg.Commons = &shared.Commons{}
|
|
localCfg.GRPCClientTLS = &shared.GRPCClientTLS{}
|
|
|
|
localCfg.API.UsernameMatch = usernameMatch
|
|
|
|
localSvc, _ := service.NewService(
|
|
service.Config(localCfg),
|
|
service.WithGatewaySelector(gatewaySelector),
|
|
service.EventsPublisher(&eventsPublisher),
|
|
service.WithIdentityBackend(identityBackend),
|
|
service.WithRoleService(roleService),
|
|
)
|
|
|
|
return localSvc
|
|
}
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
user.SetOnPremisesSamAccountName("username with spaces")
|
|
})
|
|
|
|
It("rejects a username with spaces if match regex is default", func() {
|
|
userJson, err := json.Marshal(user)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/me/users", bytes.NewBuffer(userJson))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
|
|
newSvc("default").PostUser(rr, r)
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("creates a user with spaces in username if regex is none", func() {
|
|
roleService.On("AssignRoleToUser", mock.Anything, mock.Anything).Return(&settings.AssignRoleToUserResponse{}, nil)
|
|
identityBackend.On("CreateUser", mock.Anything, mock.Anything).Return(func(ctx context.Context, user libregraph.User) *libregraph.User {
|
|
user.SetId("/users/user")
|
|
return &user
|
|
}, nil)
|
|
userJson, err := json.Marshal(user)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/me/users", bytes.NewBuffer(userJson))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
newSvc("none").PostUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusCreated))
|
|
})
|
|
})
|
|
|
|
})
|
|
|
|
Describe("DeleteUser", func() {
|
|
It("handles missing userids", func() {
|
|
r := httptest.NewRequest(http.MethodDelete, "/graph/v1.0/users/{userid}", nil)
|
|
svc.DeleteUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("prevents a user from deleting themselves", func() {
|
|
lu := libregraph.User{}
|
|
lu.SetId(currentUser.Id.OpaqueId)
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(&lu, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodDelete, "/graph/v1.0/users/{userid}", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", currentUser.Id.OpaqueId)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.DeleteUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusForbidden))
|
|
})
|
|
|
|
It("deletes a user from deleting themselves", func() {
|
|
otheruser := &userv1beta1.User{
|
|
Id: &userv1beta1.UserId{
|
|
OpaqueId: "otheruser",
|
|
},
|
|
}
|
|
|
|
lu := libregraph.User{}
|
|
lu.SetId(otheruser.Id.OpaqueId)
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(&lu, nil)
|
|
identityBackend.On("DeleteUser", mock.Anything, mock.Anything).Return(nil)
|
|
gatewayClient.On("DeleteStorageSpace", mock.Anything, mock.Anything).Return(&provider.DeleteStorageSpaceResponse{
|
|
Status: status.NewOK(ctx),
|
|
}, nil)
|
|
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
|
Status: status.NewOK(ctx),
|
|
StorageSpaces: []*provider.StorageSpace{
|
|
{
|
|
Opaque: &typesv1beta1.Opaque{},
|
|
Id: &provider.StorageSpaceId{OpaqueId: "drive1"},
|
|
Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"},
|
|
SpaceType: "personal",
|
|
Owner: otheruser,
|
|
},
|
|
},
|
|
}, nil)
|
|
|
|
r := httptest.NewRequest(http.MethodDelete, "/graph/v1.0/users/{userid}", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", lu.GetId())
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.DeleteUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusNoContent))
|
|
gatewayClient.AssertNumberOfCalls(GinkgoT(), "DeleteStorageSpace", 2) // 2 calls for the home space. first trash, then purge
|
|
})
|
|
})
|
|
|
|
Describe("PatchMe", func() {
|
|
It("rejects passwordProfile changes (CWE-620)", func() {
|
|
update := libregraph.NewUserUpdate()
|
|
pp := libregraph.NewPasswordProfile()
|
|
pp.SetPassword("newpassword")
|
|
update.SetPasswordProfile(*pp)
|
|
body, err := json.Marshal(update)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.PatchMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("rejects accountEnabled changes", func() {
|
|
update := libregraph.NewUserUpdate()
|
|
update.SetAccountEnabled(false)
|
|
body, err := json.Marshal(update)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.PatchMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("rejects onPremisesSamAccountName changes", func() {
|
|
update := libregraph.NewUserUpdate()
|
|
update.SetOnPremisesSamAccountName("newusername")
|
|
body, err := json.Marshal(update)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/me", bytes.NewBuffer(body))
|
|
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
|
svc.PatchMe(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
})
|
|
|
|
Describe("PatchUser", func() {
|
|
var (
|
|
user *libregraph.User
|
|
userUpdate *libregraph.UserUpdate
|
|
expectedUser *libregraph.User
|
|
assertHandleBadAttributes = func(userid string, update *libregraph.UserUpdate) {
|
|
userJson, err := json.Marshal(update)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/users/{userid}", bytes.NewBuffer(userJson))
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", userid)
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.PatchUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
}
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
user = libregraph.NewUser("Display Name", "user")
|
|
user.SetMail("user@example.com")
|
|
user.SetId("/users/user")
|
|
|
|
userUpdate = libregraph.NewUserUpdate()
|
|
|
|
expectedUser = libregraph.NewUser("Display Name", "user")
|
|
expectedUser.SetMail(user.GetMail())
|
|
expectedUser.SetId(user.GetId())
|
|
|
|
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
|
})
|
|
|
|
It("handles missing userids", func() {
|
|
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/users/{userid}", nil)
|
|
svc.PatchUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("handles invalid bodies", func() {
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users?$invalid=true", nil)
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", user.GetId())
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.PatchUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("handles set Ids - they are read-only", func() {
|
|
userUpdate.SetId("/users/user")
|
|
assertHandleBadAttributes(user.GetId(), userUpdate)
|
|
})
|
|
|
|
It("handles set memberOf - it's read-only", func() {
|
|
group := libregraph.Group{}
|
|
group.SetId("/groups/group")
|
|
group.SetDisplayName("Group")
|
|
userUpdate.SetMemberOf([]libregraph.Group{group})
|
|
assertHandleBadAttributes(user.GetId(), userUpdate)
|
|
})
|
|
|
|
It("handles set userType - it's read-only", func() {
|
|
userUpdate.SetUserType("Member")
|
|
assertHandleBadAttributes(user.GetId(), userUpdate)
|
|
})
|
|
|
|
It("handles invalid email", func() {
|
|
userUpdate.SetMail("invalid")
|
|
data, err := json.Marshal(userUpdate)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users?$invalid=true", bytes.NewBuffer(data))
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", userUpdate.GetId())
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.PatchUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
|
})
|
|
|
|
It("updates attributes", func() {
|
|
userUpdate.SetMail("mail@mail.test")
|
|
userUpdate.SetDisplayName("New Display Name")
|
|
|
|
expectedUser.SetMail("mail@mail.test")
|
|
expectedUser.SetDisplayName("New Display Name")
|
|
// requires privileges
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
|
Permission: &settingsmsg.Permission{
|
|
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
|
},
|
|
}, nil)
|
|
identityBackend.On("UpdateUser", mock.Anything, user.GetId(), mock.Anything).Return(expectedUser, nil)
|
|
|
|
data, err := json.Marshal(userUpdate)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/users", bytes.NewBuffer(data))
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("userID", user.GetId())
|
|
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
|
svc.PatchUser(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err = io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
unmarshaledUser := libregraph.User{}
|
|
err = json.Unmarshal(data, &unmarshaledUser)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(unmarshaledUser.GetMail()).To(Equal("mail@mail.test"))
|
|
Expect(unmarshaledUser.GetDisplayName()).To(Equal("New Display Name"))
|
|
})
|
|
})
|
|
})
|
|
When("OCM is enabled", func() {
|
|
BeforeEach(func() {
|
|
cfg.IncludeOCMSharees = true
|
|
svc, _ = service.NewService(
|
|
service.Config(cfg),
|
|
service.WithGatewaySelector(gatewaySelector),
|
|
service.EventsPublisher(&eventsPublisher),
|
|
service.WithIdentityBackend(identityBackend),
|
|
service.WithRoleService(roleService),
|
|
service.WithValueService(valueService),
|
|
service.PermissionService(permissionService),
|
|
)
|
|
})
|
|
Describe("GetUsers", func() {
|
|
It("does not list the federated users without a filter", func() {
|
|
gatewayClient.AssertNotCalled(GinkgoT(), "FindAcceptedUsers, mock.Anything, mock.Anything")
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(
|
|
[]*libregraph.User{}, nil,
|
|
)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=user", nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(res.Value)).To(Equal(0))
|
|
})
|
|
It("does list federated users when the right filter is used", func() {
|
|
gatewayClient.On("FindAcceptedUsers", mock.Anything, mock.Anything).Return(&invitepb.FindAcceptedUsersResponse{
|
|
Status: status.NewOK(ctx),
|
|
AcceptedUsers: []*userv1beta1.User{
|
|
{
|
|
Username: "federated",
|
|
Id: &userv1beta1.UserId{
|
|
OpaqueId: "federated",
|
|
Type: userv1beta1.UserType_USER_TYPE_FEDERATED,
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(
|
|
[]*libregraph.User{}, nil,
|
|
)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=federated&$filter="+url.QueryEscape("userType eq 'Federated'"), nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(res.Value)).To(Equal(1))
|
|
Expect(res.Value[0].GetId()).To(Equal("federated"))
|
|
Expect(res.Value[0].GetUserType()).To(Equal("Federated"))
|
|
})
|
|
It("does not list federated users when filtering for 'Member' users", func() {
|
|
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
|
|
|
user := &libregraph.User{}
|
|
user.SetId("user1")
|
|
user.SetUserType("Member")
|
|
users := []*libregraph.User{user}
|
|
|
|
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(
|
|
users, nil,
|
|
)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=user&$filter="+url.QueryEscape("userType eq 'Member'"), nil)
|
|
svc.GetUsers(rr, r)
|
|
|
|
Expect(rr.Code).To(Equal(http.StatusOK))
|
|
data, err := io.ReadAll(rr.Body)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
res := userList{}
|
|
err = json.Unmarshal(data, &res)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(res.Value)).To(Equal(1))
|
|
Expect(res.Value[0].GetId()).To(Equal("user1"))
|
|
Expect(res.Value[0].GetUserType()).To(Equal("Member"))
|
|
})
|
|
})
|
|
})
|
|
})
|