Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -82,4 +86,10 @@ public void successfulResponseFromExecute() throws Exception {
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)));
}
}
26 changes: 12 additions & 14 deletions ui/src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
83 changes: 83 additions & 0 deletions ui/tests/unit/store/modules/user.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading