Skip to content

Commit

Permalink
Accept a role ID on linking an account to LDAP (#8236)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
DaanHoogland and sureshanaparti authored Jun 25, 2024
1 parent 7f0d9a0 commit 6d7c042
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.LinkAccountToLdapResponse;
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.LdapUser;
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
Expand Down Expand Up @@ -63,9 +64,12 @@ public class LinkAccountToLdapCmd extends BaseCmd {
@Parameter(name = ApiConstants.ADMIN, type = CommandType.STRING, required = false, description = "domain admin username in LDAP ")
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")
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
private LdapManager _ldapManager;
Expand Down Expand Up @@ -134,7 +138,14 @@ public String getAdmin() {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,12 @@ public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
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.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());
Account account = accountDao.findActiveAccount(cmd.getAccountName(),cmd.getDomainId());
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);
}

Expand Down
7 changes: 1 addition & 6 deletions test/integration/component/test_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
try:
cleanup_resources(cls.api_client, cls._cleanup)

except Exception as tde:
raise Exception("Warning: Exception during cleanup : %s" % tde)
return
super(TestLdap, cls).tearDownClass()

def setUp(self):

Expand Down
20 changes: 9 additions & 11 deletions test/integration/plugins/ldap/test_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ def setUpClass(cls):
def tearDownClass(cls):
cls.logger.info("Tearing Down Class")
try:
cleanup_resources(cls.apiclient, reversed(cls._cleanup))
cls.remove_ldap_configuration_for_domains()
cls.logger.debug("done cleaning up resources in tearDownClass(cls) %s")
except Exception as e:
cls.logger.debug("Exception in tearDownClass(cls): %s" % e)
super(TestLDAP, cls).tearDownClass()
finally:
try:
cls.remove_ldap_configuration_for_domains()
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):
self.cleanup = []
Expand All @@ -116,11 +118,7 @@ def setUp(self):
return

def tearDown(self):
try:
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
super(TestLDAP, self).tearDown()

@attr(tags=["smoke", "advanced"], required_hardware="false")
def test_01_manual(self):
Expand Down Expand Up @@ -349,8 +347,8 @@ def create_domain(cls, domain_to_create, parent_domain = None):
if parent_domain:
domain_to_create["parentdomainid"] = parent_domain
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.logger.debug("Created domain %s with id %s " % (tmpDomain.name, tmpDomain.id))
return tmpDomain

@classmethod
Expand Down

0 comments on commit 6d7c042

Please sign in to comment.