Skip to content

Commit 584718c

Browse files
committed
bug 9571: respect allocatedOnly flag in listPublicIpAddresses API
status 9571: resolved fixed
1 parent 9909371 commit 584718c

1 file changed

Lines changed: 25 additions & 33 deletions

File tree

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,18 +2462,20 @@ public HostVO findSecondaryStorageHosT(long zoneId) {
24622462

24632463
@Override
24642464
public List<IPAddressVO> searchForIPAddresses(ListPublicIpAddressesCmd cmd) {
2465-
Account account = UserContext.current().getCaller();
2465+
Account caller = UserContext.current().getCaller();
24662466
Long domainId = cmd.getDomainId();
24672467
String accountName = cmd.getAccountName();
24682468
Object keyword = cmd.getKeyword();
24692469
Long accountId = null;
24702470

2471-
if ((account == null) || isAdmin(account.getType())) {
2471+
if (isAdmin(caller.getType())) {
24722472
// validate domainId before proceeding
24732473
if (domainId != null) {
2474-
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
2475-
throw new PermissionDeniedException("Unable to list IP addresses for domain " + domainId + ", permission denied.");
2474+
Domain domain = _domainDao.findById(domainId);
2475+
if (domain == null) {
2476+
throw new InvalidParameterValueException("Unable to find domain by id " + domainId);
24762477
}
2478+
_accountMgr.checkAccess(caller, domain);
24772479

24782480
if (accountName != null) {
24792481
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
@@ -2483,11 +2485,11 @@ public List<IPAddressVO> searchForIPAddresses(ListPublicIpAddressesCmd cmd) {
24832485
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
24842486
}
24852487
}
2486-
} else {
2487-
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
2488+
} else if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
2489+
domainId = caller.getDomainId();
24882490
}
24892491
} else {
2490-
accountId = account.getId();
2492+
accountId = caller.getId();
24912493
}
24922494

24932495
if (accountId == null && keyword != null) {
@@ -3280,7 +3282,7 @@ protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsC
32803282

32813283
// Input validation
32823284
Long id = cmd.getId();
3283-
Account account = UserContext.current().getCaller();
3285+
Account caller = UserContext.current().getCaller();
32843286
List<String> accountNames = cmd.getAccountNames();
32853287
Long userId = UserContext.current().getCallerUserId();
32863288
Boolean isFeatured = cmd.isFeatured();
@@ -3307,7 +3309,7 @@ protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsC
33073309
}
33083310
}
33093311

3310-
_accountMgr.checkAccess(account, template);
3312+
_accountMgr.checkAccess(caller, template);
33113313

33123314
// If command is executed via 8096 port, set userId to the id of System account (1)
33133315
if (userId == null) {
@@ -3324,7 +3326,7 @@ protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsC
33243326
throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id);
33253327
}
33263328

3327-
boolean isAdmin = ((account == null) || isAdmin(account.getType()));
3329+
boolean isAdmin = isAdmin(caller.getType());
33283330
boolean allowPublicUserTemplates = Boolean.parseBoolean(getConfigurationValue("allow.public.user.templates"));
33293331
if (!isAdmin && !allowPublicUserTemplates && isPublic != null && isPublic) {
33303332
throw new InvalidParameterValueException("Only private " + mediaType + "s can be created.");
@@ -3363,14 +3365,13 @@ protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsC
33633365
_templateDao.update(template.getId(), updatedTemplate);
33643366

33653367
Long domainId;
3366-
domainId = (null == account) ? DomainVO.ROOT_DOMAIN : account.getDomainId(); // Account == null for 8096 and so its safe
3367-
// for domainid = ROOT
3368+
domainId = caller.getDomainId();
33683369
if ("add".equalsIgnoreCase(operation)) {
33693370
txn.start();
33703371
for (String accountName : accountNames) {
33713372
Account permittedAccount = _accountDao.findActiveAccount(accountName, domainId);
33723373
if (permittedAccount != null) {
3373-
if (permittedAccount.getId() == account.getId()) {
3374+
if (permittedAccount.getId() == caller.getId()) {
33743375
continue; // don't grant permission to the template owner, they implicitly have permission
33753376
}
33763377
LaunchPermissionVO existingPermission = _launchPermissionDao.findByTemplateAndAccount(id, permittedAccount.getId());
@@ -3408,16 +3409,16 @@ protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsC
34083409

34093410
@Override
34103411
public List<String> listTemplatePermissions(ListTemplateOrIsoPermissionsCmd cmd) {
3411-
Account account = UserContext.current().getCaller();
3412+
Account caller = UserContext.current().getCaller();
34123413
Long domainId = cmd.getDomainId();
34133414
String acctName = cmd.getAccountName();
34143415
Long id = cmd.getId();
34153416
Long accountId = null;
34163417

3417-
if ((account == null) || account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
3418+
if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
34183419
// validate domainId before proceeding
34193420
if (domainId != null) {
3420-
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
3421+
if ((caller != null) && !_domainDao.isChildDomain(caller.getDomainId(), domainId)) {
34213422
throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list " + cmd.getMediaType() + " permissions.");
34223423
}
34233424
if (acctName != null) {
@@ -3429,30 +3430,21 @@ public List<String> listTemplatePermissions(ListTemplateOrIsoPermissionsCmd cmd)
34293430
}
34303431
}
34313432
}
3432-
} else {
3433-
accountId = account.getId();
3433+
} else if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
3434+
accountId = caller.getId();
3435+
}
3436+
3437+
if (id == Long.valueOf(1)) {
3438+
throw new PermissionDeniedException("unable to list permissions for " + cmd.getMediaType() + " with id " + id);
34343439
}
34353440

34363441
VMTemplateVO template = _templateDao.findById(id.longValue());
34373442
if (template == null || !templateIsCorrectType(template)) {
34383443
throw new InvalidParameterValueException("unable to find " + cmd.getMediaType() + " with id " + id);
34393444
}
34403445

3441-
if (accountId != null && !template.isPublicTemplate()) {
3442-
if (account.getType() == Account.ACCOUNT_TYPE_NORMAL && template.getAccountId() != accountId) {
3443-
throw new PermissionDeniedException("unable to list permissions for " + cmd.getMediaType() + " with id " + id);
3444-
} else if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || account.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
3445-
DomainVO accountDomain = _domainDao.findById(account.getDomainId());
3446-
Account templateAccount = _accountDao.findById(template.getAccountId());
3447-
DomainVO templateDomain = _domainDao.findById(templateAccount.getDomainId());
3448-
if (!templateDomain.getPath().contains(accountDomain.getPath())) {
3449-
throw new PermissionDeniedException("unable to list permissions for " + cmd.getMediaType() + " with id " + id);
3450-
}
3451-
}
3452-
}
3453-
3454-
if (id == Long.valueOf(1)) {
3455-
throw new PermissionDeniedException("unable to list permissions for " + cmd.getMediaType() + " with id " + id);
3446+
if (!template.isPublicTemplate()) {
3447+
_accountMgr.checkAccess(caller, template);
34563448
}
34573449

34583450
List<String> accountNames = new ArrayList<String>();

0 commit comments

Comments
 (0)