Accept a role ID on linking an account to LDAP (#8236)

* accept role on link account to ldap

* reformat tests

* validation

* Update plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LinkAccountToLdapCmd.java

Co-authored-by: Suresh Kumar Anaparti <sureshkumar.anaparti@gmail.com>
This commit is contained in:
dahn 2024-06-25 21:56:28 +02:00 committed by GitHub
parent 7f0d9a0304
commit 6d7c042bc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 21 deletions

View File

@ -33,6 +33,7 @@ import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DomainResponse; import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.LinkAccountToLdapResponse; import org.apache.cloudstack.api.response.LinkAccountToLdapResponse;
import org.apache.cloudstack.api.response.LinkDomainToLdapResponse; import org.apache.cloudstack.api.response.LinkDomainToLdapResponse;
import org.apache.cloudstack.api.response.RoleResponse;
import org.apache.cloudstack.ldap.LdapManager; import org.apache.cloudstack.ldap.LdapManager;
import org.apache.cloudstack.ldap.LdapUser; import org.apache.cloudstack.ldap.LdapUser;
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException; import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
@ -63,9 +64,12 @@ public class LinkAccountToLdapCmd extends BaseCmd {
@Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ") @Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ")
private String admin; private String admin;
@Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.INTEGER, required = true, description = "Type of the account to auto import. Specify 0 for user and 2 for " @Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.INTEGER, required = false, description = "Type of the account to auto import. Specify 0 for user and 2 for "
+ "domain admin") + "domain admin")
private int accountType; private Integer accountType;
@Parameter(name = ApiConstants.ROLE_ID, type = CommandType.UUID, entityType = RoleResponse.class, required = false, description = "Creates the account under the specified role.", since="4.19.1")
private Long roleId;
@Inject @Inject
private LdapManager _ldapManager; private LdapManager _ldapManager;
@ -134,7 +138,14 @@ public class LinkAccountToLdapCmd extends BaseCmd {
} }
public Account.Type getAccountType() { public Account.Type getAccountType() {
return Account.Type.getFromValue(accountType); if (accountType == null) {
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
}
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
}
public Long getRoleId() {
return RoleType.getRoleByAccountType(roleId, getAccountType());
} }
@Override @Override

View File

@ -451,11 +451,12 @@ public class LdapManagerImpl extends ComponentLifecycleBase implements LdapManag
Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name"); Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");
Validate.notNull(cmd.getType(), "type cannot be null. It should either be GROUP or OU"); Validate.notNull(cmd.getType(), "type cannot be null. It should either be GROUP or OU");
Validate.notEmpty(cmd.getLdapDomain(), "GROUP or OU name cannot be empty"); Validate.notEmpty(cmd.getLdapDomain(), "GROUP or OU name cannot be empty");
Validate.isTrue(cmd.getAccountType() != null || cmd.getRoleId() != null, "Either account type or role ID must be given");
LinkType linkType = LdapManager.LinkType.valueOf(cmd.getType().toUpperCase()); LinkType linkType = LdapManager.LinkType.valueOf(cmd.getType().toUpperCase());
Account account = accountDao.findActiveAccount(cmd.getAccountName(),cmd.getDomainId()); Account account = accountDao.findActiveAccount(cmd.getAccountName(),cmd.getDomainId());
if (account == null) { if (account == null) {
account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), UUID.randomUUID().toString()); account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), cmd.getRoleId(), UUID.randomUUID().toString());
accountDao.persist((AccountVO)account); accountDao.persist((AccountVO)account);
} }

View File

@ -52,12 +52,7 @@ class TestLdap(cloudstackTestCase):
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
try: super(TestLdap, cls).tearDownClass()
cleanup_resources(cls.api_client, cls._cleanup)
except Exception as tde:
raise Exception("Warning: Exception during cleanup : %s" % tde)
return
def setUp(self): def setUp(self):

View File

@ -101,11 +101,13 @@ class TestLDAP(cloudstackTestCase):
def tearDownClass(cls): def tearDownClass(cls):
cls.logger.info("Tearing Down Class") cls.logger.info("Tearing Down Class")
try: try:
cleanup_resources(cls.apiclient, reversed(cls._cleanup)) super(TestLDAP, cls).tearDownClass()
cls.remove_ldap_configuration_for_domains() finally:
cls.logger.debug("done cleaning up resources in tearDownClass(cls) %s") try:
except Exception as e: cls.remove_ldap_configuration_for_domains()
cls.logger.debug("Exception in tearDownClass(cls): %s" % e) cls.logger.debug("done cleaning up resources in tearDownClass(cls) %s")
except Exception as e:
cls.logger.debug("Exception in tearDownClass(cls): %s" % e)
def setUp(self): def setUp(self):
self.cleanup = [] self.cleanup = []
@ -116,11 +118,7 @@ class TestLDAP(cloudstackTestCase):
return return
def tearDown(self): def tearDown(self):
try: super(TestLDAP, self).tearDown()
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@attr(tags=["smoke", "advanced"], required_hardware="false") @attr(tags=["smoke", "advanced"], required_hardware="false")
def test_01_manual(self): def test_01_manual(self):
@ -349,8 +347,8 @@ class TestLDAP(cloudstackTestCase):
if parent_domain: if parent_domain:
domain_to_create["parentdomainid"] = parent_domain domain_to_create["parentdomainid"] = parent_domain
tmpDomain = Domain.create(cls.apiclient, domain_to_create) tmpDomain = Domain.create(cls.apiclient, domain_to_create)
cls.logger.debug("Created domain %s with id %s " % (tmpDomain.name, tmpDomain.id))
cls._cleanup.append(tmpDomain) cls._cleanup.append(tmpDomain)
cls.logger.debug("Created domain %s with id %s " % (tmpDomain.name, tmpDomain.id))
return tmpDomain return tmpDomain
@classmethod @classmethod