This is an automated email from the ASF dual-hosted git repository. DaanHoogland pushed a commit to branch ghi3687-domain-admin-ldap-access in repository https://gitbox.apache.org/repos/asf/cloudstack.git
commit 37c42dbbe368c7116d024bb432a509c255dc9c29 Author: Daan Hoogland <[email protected]> AuthorDate: Wed Dec 2 14:33:38 2026 +0100 ldap: drop linkAccountToLdap backfill, add tests for domain admin LDAP access --- .../resources/META-INF/db/schema-42300to2400.sql | 6 +- .../api/command/LdapCreateAccountCmdTest.java | 12 ++++ .../api/command/LdapImportUsersCmdTest.java | 10 +++ ui/tests/unit/store/modules/user.spec.js | 83 ++++++++++++++++++++++ 4 files changed, 106 insertions(+), 5 deletions(-) diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42300to2400.sql b/engine/schema/src/main/resources/META-INF/db/schema-42300to2400.sql index b7ed1e716f6..e4ca30801b6 100644 --- a/engine/schema/src/main/resources/META-INF/db/schema-42300to2400.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-42300to2400.sql @@ -312,6 +312,7 @@ SET `object_store`.allocated_size = buckets_quota_sum_view.total_quota; -- @APICommand(authorized=...) alone doesn't grant access under dynamic RBAC (the default). -- Backfill the LDAP account APIs for any role that can already create accounts natively. +-- These create accounts through the same domain-access and role-escalation checks as createAccount. INSERT INTO cloud.role_permissions (uuid, role_id, rule, permission, sort_order) SELECT uuid(), role_id, 'listLdapUsers', permission, sort_order FROM cloud.role_permissions rp @@ -322,11 +323,6 @@ SELECT uuid(), role_id, 'ldapCreateAccount', permission, sort_order FROM cloud.role_permissions rp WHERE rule = 'createAccount' AND NOT EXISTS(SELECT 1 FROM cloud.role_permissions rp_ WHERE rp.role_id = rp_.role_id AND rp_.rule = 'ldapCreateAccount'); -INSERT INTO cloud.role_permissions (uuid, role_id, rule, permission, sort_order) -SELECT uuid(), role_id, 'linkAccountToLdap', permission, sort_order -FROM cloud.role_permissions rp -WHERE rule = 'createAccount' AND NOT EXISTS(SELECT 1 FROM cloud.role_permissions rp_ WHERE rp.role_id = rp_.role_id AND rp_.rule = 'linkAccountToLdap'); - INSERT INTO cloud.role_permissions (uuid, role_id, rule, permission, sort_order) SELECT uuid(), role_id, 'importLdapUsers', permission, sort_order FROM cloud.role_permissions rp diff --git a/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapCreateAccountCmdTest.java b/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapCreateAccountCmdTest.java index b96a0070585..4a56018c690 100644 --- a/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapCreateAccountCmdTest.java +++ b/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapCreateAccountCmdTest.java @@ -19,6 +19,8 @@ package org.apache.cloudstack.api.command; import com.cloud.user.Account; import com.cloud.user.AccountService; import org.apache.cloudstack.acl.RoleService; +import org.apache.cloudstack.acl.RoleType; +import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.ldap.LdapManager; import org.apache.cloudstack.ldap.LdapUser; @@ -29,6 +31,10 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import java.util.Arrays; +import java.util.EnumSet; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.nullable; @@ -69,4 +75,10 @@ public class LdapCreateAccountCmdTest implements LdapConfigurationChanger { ldapCreateAccountCmd.execute(); fail("An exception should have been thrown: " + ServerApiException.class); } + + @Test + public void isAuthorizedForAdminAndDomainAdminOnly() { + RoleType[] authorized = LdapCreateAccountCmd.class.getAnnotation(APICommand.class).authorized(); + assertEquals(EnumSet.of(RoleType.Admin, RoleType.DomainAdmin), EnumSet.copyOf(Arrays.asList(authorized))); + } } diff --git a/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapImportUsersCmdTest.java b/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapImportUsersCmdTest.java index 3dc9d4054b9..a5480ce7787 100644 --- a/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapImportUsersCmdTest.java +++ b/plugins/user-authenticators/ldap/src/test/java/org/apache/cloudstack/api/command/LdapImportUsersCmdTest.java @@ -22,6 +22,8 @@ import com.cloud.user.Account; import com.cloud.user.AccountService; import com.cloud.user.DomainService; import org.apache.cloudstack.acl.RoleService; +import org.apache.cloudstack.acl.RoleType; +import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.response.ListResponse; import org.junit.Before; import org.junit.Test; @@ -33,6 +35,8 @@ import org.apache.cloudstack.ldap.LdapUser; import org.mockito.junit.MockitoJUnitRunner; import java.util.ArrayList; +import java.util.Arrays; +import java.util.EnumSet; import java.util.List; import java.util.UUID; @@ -82,4 +86,10 @@ public class LdapImportUsersCmdTest implements LdapConfigurationChanger { ListResponse<LdapUserResponse> resp = (ListResponse<LdapUserResponse>)ldapImportUsersCmd.getResponseObject(); assertEquals(" when LdapListUsersCmd is executed, a list of size 2 should be returned", 2, resp.getResponses().size()); } + + @Test + public void isAuthorizedForAdminAndDomainAdminOnly() { + RoleType[] authorized = LdapImportUsersCmd.class.getAnnotation(APICommand.class).authorized(); + assertEquals(EnumSet.of(RoleType.Admin, RoleType.DomainAdmin), EnumSet.copyOf(Arrays.asList(authorized))); + } } diff --git a/ui/tests/unit/store/modules/user.spec.js b/ui/tests/unit/store/modules/user.spec.js new file mode 100644 index 00000000000..10dbb8016e8 --- /dev/null +++ b/ui/tests/unit/store/modules/user.spec.js @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { getAPI } from '@/api' +import store from '@/store' +import user from '@/store/modules/user' + +jest.mock('@/api', () => ({ + getAPI: jest.fn(), + postAPI: jest.fn(), + login: jest.fn(), + logout: jest.fn(), + oauthlogin: jest.fn() +})) +jest.mock('@/store', () => ({ getters: {}, dispatch: jest.fn() })) +jest.mock('@/router', () => ({})) +jest.mock('@/vue-app', () => ({ vueProps: {} })) +jest.mock('@/locales', () => ({ i18n: { global: { t: (key) => key } } })) +jest.mock('@/utils/request', () => ({ axios: {} })) +jest.mock('ant-design-vue/es/message', () => ({})) +jest.mock('ant-design-vue/es/notification', () => ({})) + +const ldapResponse = (count) => Promise.resolve({ ldapconfigurationresponse: { count } }) + +const updateConfiguration = async (userInfo, countsByQuery) => { + store.getters.userInfo = userInfo + getAPI.mockImplementation((api, params) => ldapResponse(countsByQuery(params))) + const commit = jest.fn() + await user.actions.UpdateConfiguration({ commit }) + return commit +} + +describe('UpdateConfiguration sets isLdapEnabled', () => { + beforeEach(() => { + getAPI.mockReset() + }) + + it('counts any LDAP configuration for root admins', async () => { + const commit = await updateConfiguration({ roletype: 'Admin', domainid: 'root' }, + (params) => (params.listall ? 1 : 0)) + + expect(getAPI).toHaveBeenCalledTimes(1) + expect(getAPI).toHaveBeenCalledWith('listLdapConfigurations', { listall: true }) + expect(commit).toHaveBeenCalledWith('SET_LDAP', true) + }) + + it('counts a configuration linked to the domain admin\'s own domain', async () => { + const commit = await updateConfiguration({ roletype: 'DomainAdmin', domainid: 'd1' }, + (params) => (params.domainid === 'd1' ? 1 : 0)) + + expect(getAPI).toHaveBeenCalledWith('listLdapConfigurations', {}) + expect(getAPI).toHaveBeenCalledWith('listLdapConfigurations', { domainid: 'd1' }) + expect(getAPI).not.toHaveBeenCalledWith('listLdapConfigurations', { listall: true }) + expect(commit).toHaveBeenCalledWith('SET_LDAP', true) + }) + + it('counts a global configuration for domain admins', async () => { + const commit = await updateConfiguration({ roletype: 'DomainAdmin', domainid: 'd1' }, + (params) => (params.domainid === undefined ? 1 : 0)) + + expect(commit).toHaveBeenCalledWith('SET_LDAP', true) + }) + + it('is false when there is neither a global nor an own-domain configuration', async () => { + const commit = await updateConfiguration({ roletype: 'User', domainid: 'd2' }, () => 0) + + expect(commit).toHaveBeenCalledWith('SET_LDAP', false) + }) +})
