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 0288e6f98474..e4ca30801b62 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 @@ -309,3 +309,21 @@ JOIN ( GROUP BY object_store_id ) buckets_quota_sum_view ON `object_store`.id = buckets_quota_sum_view.object_store_id 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 +WHERE rule = 'createAccount' AND NOT EXISTS(SELECT 1 FROM cloud.role_permissions rp_ WHERE rp.role_id = rp_.role_id AND rp_.rule = 'listLdapUsers'); + +INSERT INTO cloud.role_permissions (uuid, role_id, rule, permission, sort_order) +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, 'importLdapUsers', 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 = 'importLdapUsers'); diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java index 3aa3e0502268..8835c9fe4b0b 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java @@ -44,7 +44,8 @@ import java.security.SecureRandom; import java.util.Map; -@APICommand(name = "ldapCreateAccount", description = "Creates an account from an LDAP user", responseObject = AccountResponse.class, since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) +@APICommand(name = "ldapCreateAccount", description = "Creates an account from an LDAP user", responseObject = AccountResponse.class, since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, + authorized = {RoleType.Admin, RoleType.DomainAdmin}) public class LdapCreateAccountCmd extends BaseCmd { private static final String s_name = "createaccountresponse"; diff --git a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapImportUsersCmd.java b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapImportUsersCmd.java index d6df27ccfcb9..f497e49d6a01 100644 --- a/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapImportUsersCmd.java +++ b/plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapImportUsersCmd.java @@ -56,7 +56,8 @@ import com.cloud.user.UserAccount; import com.cloud.utils.StringUtils; -@APICommand(name = "importLdapUsers", description = "Import LDAP users", responseObject = LdapUserResponse.class, since = "4.3.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) +@APICommand(name = "importLdapUsers", description = "Import LDAP users", responseObject = LdapUserResponse.class, since = "4.3.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, + authorized = {RoleType.Admin, RoleType.DomainAdmin}) public class LdapImportUsersCmd extends BaseListCmd { 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 b96a00705852..4a56018c6906 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 @@ 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.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 void failedCreationDueToANullResponseFromCloudstackAccountCreator() throw 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 3dc9d4054b90..a5480ce7787c 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.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.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 void successfulResponseFromExecute() throws Exception { ListResponse resp = (ListResponse)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/src/store/modules/user.js b/ui/src/store/modules/user.js index 4103ff920d63..d3835afea94a 100644 --- a/ui/src/store/modules/user.js +++ b/ui/src/store/modules/user.js @@ -451,6 +451,9 @@ const user = { commit('SET_NAME', result.firstname + ' ' + result.lastname) commit('SET_AVATAR', result.icon?.base64image || '') store.dispatch('SetCsLatestVersion', result.rolename) + store.dispatch('UpdateConfiguration').catch(error => { + reject(error) + }) }).catch(error => { reject(error) }) @@ -464,13 +467,6 @@ const user = { }).catch(ignored => { }) - getAPI('listLdapConfigurations').then(response => { - const ldapEnable = (response.ldapconfigurationresponse.count > 0) - commit('SET_LDAP', ldapEnable) - }).catch(error => { - reject(error) - }) - getAPI('cloudianIsEnabled').then(response => { const cloudian = response.cloudianisenabledresponse.cloudianisenabled || {} commit('SET_CLOUDIAN', cloudian) @@ -601,13 +597,15 @@ const user = { }) }, UpdateConfiguration ({ commit }) { - return new Promise((resolve, reject) => { - getAPI('listLdapConfigurations').then(response => { - const ldapEnable = (response.ldapconfigurationresponse.count > 0) - commit('SET_LDAP', ldapEnable) - }).catch(error => { - reject(error) - }) + // Root admins see LDAP as enabled if any configuration exists; everyone else + // only when a global configuration or one linked to their own domain exists + const userInfo = store.getters.userInfo + const queries = userInfo.roletype === 'Admin' + ? [{ listall: true }] + : [{}, { domainid: userInfo.domainid }] + return Promise.all(queries.map(params => getAPI('listLdapConfigurations', params))).then(responses => { + const ldapEnable = responses.some(response => response.ldapconfigurationresponse.count > 0) + commit('SET_LDAP', ldapEnable) }) }, SetDomainStore ({ commit }, domainStore) { 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 000000000000..10dbb8016e8f --- /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) + }) +})