diff --git a/changelog/unreleased/bugfix-ldap-masterid-empty-filter.md b/changelog/unreleased/bugfix-ldap-masterid-empty-filter.md new file mode 100644 index 00000000000..d5358710150 --- /dev/null +++ b/changelog/unreleased/bugfix-ldap-masterid-empty-filter.md @@ -0,0 +1,25 @@ +Bugfix: Fix the LDAP master-ID filter when the base filter is empty + +The function `EnhanceFilterWithMasterID` in `ocis-pkg/ldap/filter.go` adds a +master-ID clause to an LDAP filter with OR. An empty filter means "no +restriction". Before this fix, the function replaced an empty filter with a +master-ID-only filter. This action restricted an unrestricted search to +master-ID users only. + +The bug broke the search for a user on another instance in multi-instance +deployments. This search uses the Graph API endpoint `/graph/v1.0/users`. +Multi-instance deployments use this search to find a user to share a resource +with. + +Now the function keeps an empty filter unchanged. The function still adds a +master-ID clause to a filter that is not empty. + +The `graph`, `auth-basic`, `users`, and `groups` services also use this +function to compute their default LDAP user filter when +`OCIS_MULTI_INSTANCE_MASTER_ID` is set. An empty user filter now means these +services resolve or authenticate every user in the shared LDAP directory, +not only master-ID users. Each of these services now rejects startup when +`OCIS_MULTI_INSTANCE_MASTER_ID` is set and its own user filter is empty. +Set an explicit `OCIS_LDAP_USER_FILTER` for every multi-instance deployment. + +https://github.com/owncloud/ocis/pull/13096 diff --git a/ocis-pkg/ldap/filter.go b/ocis-pkg/ldap/filter.go index ab245b961e6..b0c030feaf8 100644 --- a/ocis-pkg/ldap/filter.go +++ b/ocis-pkg/ldap/filter.go @@ -7,14 +7,13 @@ import ( "github.com/go-ldap/ldap/v3" ) -// EnhanceFilterWithMasterID OR's the given LDAP filter with master-id match clauses. -// Returns filter unchanged when masterID is empty or both attributes are empty. +// EnhanceFilterWithMasterID adds master-ID match clauses to the given LDAP filter with OR. +// An empty filter means "no restriction". Do not OR an empty filter with a master-ID clause. +// This action would restrict an unrestricted search to the master ID only. +// The function returns the filter without a change when masterID is empty, when both +// attributes are empty, or when filter is empty. func EnhanceFilterWithMasterID(filter, masterID, memberAttr, guestAttr string) string { - if masterID == "" { - return filter - } - - if memberAttr == "" && guestAttr == "" { + if masterID == "" || (memberAttr == "" && guestAttr == "") || filter == "" { return filter } @@ -35,9 +34,5 @@ func EnhanceFilterWithMasterID(filter, masterID, memberAttr, guestAttr string) s masterIDFilter = fmt.Sprintf("(|%s)", strings.Join(masterIDParts, "")) } - if filter == "" { - return masterIDFilter - } - return fmt.Sprintf("(|%s%s)", filter, masterIDFilter) } diff --git a/ocis-pkg/ldap/filter_test.go b/ocis-pkg/ldap/filter_test.go index e2be5763677..9739f734544 100644 --- a/ocis-pkg/ldap/filter_test.go +++ b/ocis-pkg/ldap/filter_test.go @@ -54,12 +54,12 @@ func TestEnhanceFilterWithMasterID(t *testing.T) { expectedFilter: "(|(ownCloudGuestOf=instance-1)(ownCloudGuestOf=0000-0000-000-0000))", }, { - name: "empty existing filter", + name: "empty existing filter stays unrestricted", filter: "", masterID: "0000-0000-000-0000", memberAttr: "ownCloudMemberOf", guestAttr: "ownCloudGuestOf", - expectedFilter: "(|(ownCloudMemberOf=0000-0000-000-0000)(ownCloudGuestOf=0000-0000-000-0000))", + expectedFilter: "", }, { name: "LDAP injection protection", diff --git a/ocis-pkg/shared/errors.go b/ocis-pkg/shared/errors.go index 2c09f0eeed3..7e557959232 100644 --- a/ocis-pkg/shared/errors.go +++ b/ocis-pkg/shared/errors.go @@ -46,6 +46,13 @@ func MissingLDAPBindPassword(service string) error { service, defaults.BaseConfigPath()) } +func MissingLDAPUserFilterForMasterID(service string) error { + return fmt.Errorf("The ldap user_filter has not been set for %s, but OCIS_MULTI_INSTANCE_MASTER_ID is set. "+ + "Set an explicit user_filter to scope this instance to its own users. "+ + "Without a user_filter, every user in the shared LDAP directory can access this instance.", + service) +} + func MissingServiceUserPassword(service, serviceUser string) error { return fmt.Errorf("The password of service user %s has not been set properly in your config for %s. "+ "Make sure your %s config contains the proper values "+ diff --git a/services/auth-basic/pkg/config/config.go b/services/auth-basic/pkg/config/config.go index 16e4fbf6029..01a7167304d 100644 --- a/services/auth-basic/pkg/config/config.go +++ b/services/auth-basic/pkg/config/config.go @@ -82,7 +82,7 @@ type LDAPProvider struct { IDP string `yaml:"idp" env:"OCIS_URL;OCIS_OIDC_ISSUER;AUTH_BASIC_IDP_URL" desc:"The identity provider value to set in the userids of the CS3 user objects for users returned by this user provider." introductionVersion:"pre5.0"` DisableUserMechanism string `yaml:"disable_user_mechanism" env:"OCIS_LDAP_DISABLE_USER_MECHANISM;AUTH_BASIC_DISABLE_USER_MECHANISM" desc:"An option to control the behavior for disabling users. Valid options are 'none', 'attribute' and 'group'. If set to 'group', disabling a user via API will add the user to the configured group for disabled users, if set to 'attribute' this will be done in the ldap user entry, if set to 'none' the disable request is not processed." introductionVersion:"pre5.0"` LdapDisabledUsersGroupDN string `yaml:"ldap_disabled_users_group_dn" env:"OCIS_LDAP_DISABLED_USERS_GROUP_DN;AUTH_BASIC_DISABLED_USERS_GROUP_DN" desc:"The distinguished name of the group to which added users will be classified as disabled when 'disable_user_mechanism' is set to 'group'." introductionVersion:"pre5.0"` - MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"` + MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance allows login for every user in the shared LDAP directory." introductionVersion:"7.0"` UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"` UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"` UserSchema LDAPUserSchema `yaml:"user_schema"` diff --git a/services/auth-basic/pkg/config/parser/parse.go b/services/auth-basic/pkg/config/parser/parse.go index 77a36f5025c..ae93bb37c8f 100644 --- a/services/auth-basic/pkg/config/parser/parse.go +++ b/services/auth-basic/pkg/config/parser/parse.go @@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error { return shared.MissingLDAPBindPassword(cfg.Service.Name) } + if cfg.AuthProvider == "ldap" && cfg.AuthProviders.LDAP.MasterID != "" && cfg.AuthProviders.LDAP.UserFilter == "" { + return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name) + } + return nil } diff --git a/services/auth-basic/pkg/config/parser/parse_test.go b/services/auth-basic/pkg/config/parser/parse_test.go new file mode 100644 index 00000000000..6cdbeeca0a5 --- /dev/null +++ b/services/auth-basic/pkg/config/parser/parse_test.go @@ -0,0 +1,63 @@ +package parser + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/owncloud/ocis/v2/services/auth-basic/pkg/config" +) + +func TestValidateMasterIDRequiresUserFilter(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "auth-basic"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + AuthProvider: "ldap", + AuthProviders: config.AuthProviders{ + LDAP: config.LDAPProvider{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.Error(t, err) +} + +func TestValidateMasterIDWithUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "auth-basic"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + AuthProvider: "ldap", + AuthProviders: config.AuthProviders{ + LDAP: config.LDAPProvider{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "(ownCloudMemberOf=instance-1)", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +} + +func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "auth-basic"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + AuthProvider: "ldap", + AuthProviders: config.AuthProviders{ + LDAP: config.LDAPProvider{ + BindPassword: "secret", + MasterID: "", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +} diff --git a/services/graph/pkg/config/config.go b/services/graph/pkg/config/config.go index 29174f7bb32..62df2f3e4fe 100644 --- a/services/graph/pkg/config/config.go +++ b/services/graph/pkg/config/config.go @@ -198,6 +198,6 @@ type Validation struct { type MultiInstanceConfig struct { Enabled bool `yaml:"enabled" env:"OCIS_MULTI_INSTANCE_ENABLED" desc:"Enable multiple instances of Infinite Scale." introductionVersion:"8.0.0"` InstanceID string `yaml:"instanceid" env:"OCIS_MULTI_INSTANCE_INSTANCEID" desc:"The unique ID of this instance." introductionVersion:"8.0.0"` - MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master ID that grants access to all instances. Users with this ID in their memberOf or guestOf claims can access any instance. Leave empty to disable." introductionVersion:"8.1.0"` + MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master ID that grants access to all instances. Users with this ID in their memberOf or guestOf claims can access any instance. Leave empty to disable. Also set 'OCIS_LDAP_USER_FILTER'. Without an explicit user filter, this instance resolves every user in the shared LDAP directory." introductionVersion:"8.1.0"` QueryRegexp string `yaml:"query_regexp" env:"OCIS_MULTI_INSTANCE_QUERY_TEMPLATE" desc:"The regular expression extracting username and instancename from a user provided search." introductionVersion:"8.0.0"` } diff --git a/services/graph/pkg/config/parser/parse.go b/services/graph/pkg/config/parser/parse.go index 3bcd06f4f72..ffa91c68f02 100644 --- a/services/graph/pkg/config/parser/parse.go +++ b/services/graph/pkg/config/parser/parse.go @@ -98,6 +98,10 @@ func validateLDAPSettings(cfg *config.Config) error { return shared.MissingLDAPBindPassword(cfg.Service.Name) } + if cfg.MultiInstance.MasterID != "" && cfg.Identity.LDAP.UserFilter == "" { + return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name) + } + // ensure that "GroupBaseDN" is below "GroupBaseDN" if cfg.Identity.LDAP.WriteEnabled && cfg.Identity.LDAP.GroupCreateBaseDN != cfg.Identity.LDAP.GroupBaseDN { baseDN, err := ldap.ParseDN(cfg.Identity.LDAP.GroupBaseDN) diff --git a/services/graph/pkg/config/parser/parse_test.go b/services/graph/pkg/config/parser/parse_test.go new file mode 100644 index 00000000000..fba9ae1f3db --- /dev/null +++ b/services/graph/pkg/config/parser/parse_test.go @@ -0,0 +1,63 @@ +package parser + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/owncloud/ocis/v2/services/graph/pkg/config" +) + +func TestValidateLDAPSettingsMasterIDRequiresUserFilter(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "graph"}, + Identity: config.Identity{ + LDAP: config.LDAP{ + BindPassword: "secret", + UserFilter: "", + }, + }, + MultiInstance: config.MultiInstanceConfig{ + MasterID: "master-1", + }, + } + + err := validateLDAPSettings(cfg) + assert.Error(t, err) +} + +func TestValidateLDAPSettingsMasterIDWithUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "graph"}, + Identity: config.Identity{ + LDAP: config.LDAP{ + BindPassword: "secret", + UserFilter: "(ownCloudMemberOf=instance-1)", + }, + }, + MultiInstance: config.MultiInstanceConfig{ + MasterID: "master-1", + }, + } + + err := validateLDAPSettings(cfg) + assert.NoError(t, err) +} + +func TestValidateLDAPSettingsNoMasterIDWithEmptyUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "graph"}, + Identity: config.Identity{ + LDAP: config.LDAP{ + BindPassword: "secret", + UserFilter: "", + }, + }, + MultiInstance: config.MultiInstanceConfig{ + MasterID: "", + }, + } + + err := validateLDAPSettings(cfg) + assert.NoError(t, err) +} diff --git a/services/groups/pkg/config/config.go b/services/groups/pkg/config/config.go index cc691b322b8..eaeb43b7ee2 100644 --- a/services/groups/pkg/config/config.go +++ b/services/groups/pkg/config/config.go @@ -79,7 +79,7 @@ type LDAPDriver struct { UserObjectClass string `yaml:"user_object_class" env:"OCIS_LDAP_USER_OBJECTCLASS;GROUPS_LDAP_USER_OBJECTCLASS" desc:"The object class to use for users in the default user search filter ('inetOrgPerson')." introductionVersion:"pre5.0"` GroupObjectClass string `yaml:"group_object_class" env:"OCIS_LDAP_GROUP_OBJECTCLASS;GROUPS_LDAP_GROUP_OBJECTCLASS" desc:"The object class to use for groups in the default group search filter ('groupOfNames')." introductionVersion:"pre5.0"` IDP string `yaml:"idp" env:"OCIS_URL;OCIS_OIDC_ISSUER;GROUPS_IDP_URL" desc:"The identity provider value to set in the group IDs of the CS3 group objects for groups returned by this group provider." introductionVersion:"pre5.0"` - MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"` + MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance resolves every user in the shared LDAP directory." introductionVersion:"7.0"` UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"` UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"` UserSchema LDAPUserSchema `yaml:"user_schema"` diff --git a/services/groups/pkg/config/parser/parse.go b/services/groups/pkg/config/parser/parse.go index ed312dfc44d..20ad5de818c 100644 --- a/services/groups/pkg/config/parser/parse.go +++ b/services/groups/pkg/config/parser/parse.go @@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error { return shared.MissingLDAPBindPassword(cfg.Service.Name) } + if cfg.Driver == "ldap" && cfg.Drivers.LDAP.MasterID != "" && cfg.Drivers.LDAP.UserFilter == "" { + return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name) + } + return nil } diff --git a/services/groups/pkg/config/parser/parse_test.go b/services/groups/pkg/config/parser/parse_test.go new file mode 100644 index 00000000000..d333f2c74f5 --- /dev/null +++ b/services/groups/pkg/config/parser/parse_test.go @@ -0,0 +1,63 @@ +package parser + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/owncloud/ocis/v2/services/groups/pkg/config" +) + +func TestValidateMasterIDRequiresUserFilter(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "groups"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.Error(t, err) +} + +func TestValidateMasterIDWithUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "groups"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "(ownCloudMemberOf=instance-1)", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +} + +func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "groups"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +} diff --git a/services/users/pkg/config/config.go b/services/users/pkg/config/config.go index 584d2632f77..b317b360064 100644 --- a/services/users/pkg/config/config.go +++ b/services/users/pkg/config/config.go @@ -84,7 +84,7 @@ type LDAPDriver struct { DisableUserMechanism string `yaml:"disable_user_mechanism" env:"OCIS_LDAP_DISABLE_USER_MECHANISM;USERS_LDAP_DISABLE_USER_MECHANISM" desc:"An option to control the behavior for disabling users. Valid options are 'none', 'attribute' and 'group'. If set to 'group', disabling a user via API will add the user to the configured group for disabled users, if set to 'attribute' this will be done in the ldap user entry, if set to 'none' the disable request is not processed." introductionVersion:"pre5.0"` UserTypeAttribute string `yaml:"user_type_attribute" env:"OCIS_LDAP_USER_SCHEMA_USER_TYPE;USERS_LDAP_USER_TYPE_ATTRIBUTE" desc:"LDAP Attribute to distinguish between 'Member' and 'Guest' users. Default is 'ownCloudUserType'." introductionVersion:"pre5.0"` LdapDisabledUsersGroupDN string `yaml:"ldap_disabled_users_group_dn" env:"OCIS_LDAP_DISABLED_USERS_GROUP_DN;USERS_LDAP_DISABLED_USERS_GROUP_DN" desc:"The distinguished name of the group to which added users will be classified as disabled when 'disable_user_mechanism' is set to 'group'." introductionVersion:"pre5.0"` - MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance." introductionVersion:"7.0"` + MasterID string `yaml:"master_id" env:"OCIS_MULTI_INSTANCE_MASTER_ID" desc:"The master instance ID for multi-instance setups. When set, LDAP user filters are enhanced to also match users belonging to the master instance. Also set 'user_filter'. Without an explicit 'user_filter', this instance resolves every user in the shared LDAP directory." introductionVersion:"7.0"` UserMemberAttribute string `yaml:"user_member_attribute" env:"OCIS_LDAP_USER_MEMBER_ATTRIBUTE" desc:"LDAP attribute that marks a user as a member of an instance." introductionVersion:"7.0"` UserGuestAttribute string `yaml:"user_guest_attribute" env:"OCIS_LDAP_USER_GUEST_ATTRIBUTE" desc:"LDAP attribute that marks a user as a guest of an instance." introductionVersion:"7.0"` UserSchema LDAPUserSchema `yaml:"user_schema"` diff --git a/services/users/pkg/config/parser/parse.go b/services/users/pkg/config/parser/parse.go index 1dd9a8bd31e..2dfb6f6b56d 100644 --- a/services/users/pkg/config/parser/parse.go +++ b/services/users/pkg/config/parser/parse.go @@ -42,5 +42,9 @@ func Validate(cfg *config.Config) error { return shared.MissingLDAPBindPassword(cfg.Service.Name) } + if cfg.Driver == "ldap" && cfg.Drivers.LDAP.MasterID != "" && cfg.Drivers.LDAP.UserFilter == "" { + return shared.MissingLDAPUserFilterForMasterID(cfg.Service.Name) + } + return nil } diff --git a/services/users/pkg/config/parser/parse_test.go b/services/users/pkg/config/parser/parse_test.go new file mode 100644 index 00000000000..4bcd70c9be0 --- /dev/null +++ b/services/users/pkg/config/parser/parse_test.go @@ -0,0 +1,63 @@ +package parser + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/owncloud/ocis/v2/services/users/pkg/config" +) + +func TestValidateMasterIDRequiresUserFilter(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "users"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.Error(t, err) +} + +func TestValidateMasterIDWithUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "users"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "master-1", + UserFilter: "(ownCloudMemberOf=instance-1)", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +} + +func TestValidateNoMasterIDWithEmptyUserFilterPasses(t *testing.T) { + cfg := &config.Config{ + Service: config.Service{Name: "users"}, + TokenManager: &config.TokenManager{JWTSecret: "secret"}, + Driver: "ldap", + Drivers: config.Drivers{ + LDAP: config.LDAPDriver{ + BindPassword: "secret", + MasterID: "", + UserFilter: "", + }, + }, + } + + err := Validate(cfg) + assert.NoError(t, err) +}