Skip to content

Commit 7cac162

Browse files
fixes in the listIacTemplates API
1 parent 4a121d6 commit 7cac162

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

plugins/iac/nimble/src/main/java/org/apache/cloudstack/api/command/ListIacTemplatesCmd.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class ListIacTemplatesCmd extends BaseListCmd {
2424
@Inject
2525
private NimbleService nimbleService;
2626

27-
@ACL
2827
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = IacTemplateResponse.class, description = "ID of the IaC template.")
2928
private Long id;
3029

@@ -87,13 +86,6 @@ public boolean isShowSharedIacTemplates() {
8786

8887
@Override
8988
public long getEntityOwnerId() {
90-
if (getId() != null) {
91-
IacTemplate iacTemplate = nimbleService.findIacTemplateById(id);
92-
if (iacTemplate != null) {
93-
return iacTemplate.getAccountId();
94-
}
95-
}
96-
9789
if (getAccountId() != null) {
9890
return getAccountId();
9991
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/service/NimbleManagerImpl.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ public ListResponse<IacResourceTypeResponse> listIacResourceTypes(ListIacResourc
121121

122122
@Override
123123
public ListResponse<IacTemplateResponse> listIacTemplates(ListIacTemplatesCmd cmd) {
124-
Account caller = CallContext.current().getCallingAccount();
124+
CallContext currentCallContext = CallContext.current();
125+
Account caller = currentCallContext.getCallingAccount();
126+
if (cmd.getId() != null) {
127+
checkCallerAccessToIacTemplate(currentCallContext, cmd.getId());
128+
}
125129
long domainId = getBaseDomainIdToListIacTemplatesFrom(cmd.getDomainId(), caller);
126130
List<Long> domainIds = cmd.isRecursive() ? domainDao.getDomainAndChildrenIds(domainId) : List.of(domainId);
127131
Long accountId = getAccountIdToListIacTemplatesFor(cmd.getAccountId(), cmd.getProjectId(), caller);
@@ -138,6 +142,35 @@ public ListResponse<IacTemplateResponse> listIacTemplates(ListIacTemplatesCmd cm
138142
return response;
139143
}
140144

145+
private void checkCallerAccessToIacTemplate(CallContext callContext, long iacTemplateId) {
146+
IacTemplateVO iacTemplate = iacTemplateDao.findById(iacTemplateId);
147+
if (iacTemplate == null) {
148+
throw new InvalidParameterValueException("Unable to find IaC template with the specified ID.");
149+
}
150+
151+
boolean hasAccess = false;
152+
Account iacTemplateOwner = accountService.getActiveAccountById(iacTemplate.getAccountId());
153+
if (iacTemplateOwner != null) {
154+
try {
155+
accountService.checkAccess(callContext.getCallingUser(), iacTemplateOwner);
156+
hasAccess = true;
157+
} catch (PermissionDeniedException ignored) {}
158+
}
159+
160+
Account caller = callContext.getCallingAccount();
161+
boolean isIacTemplateSharedWithCallingAccount = iacTemplate.getAccountMappings()
162+
.stream().anyMatch((accountMap) -> accountMap.getAccountId() == caller.getId());
163+
boolean isIacTemplateSharedWithCallingAccountDomain = iacTemplate.getDomainMappings()
164+
.stream().anyMatch((domainMap) -> domainMap.getDomainId() == caller.getDomainId());
165+
if (isIacTemplateSharedWithCallingAccount || isIacTemplateSharedWithCallingAccountDomain) {
166+
hasAccess = true;
167+
}
168+
169+
if (!hasAccess) {
170+
throw new PermissionDeniedException(String.format("Account [%s] does not have permission to operate over the requested IaC template.", caller.getAccountName()));
171+
}
172+
}
173+
141174
Set<Long> getListOfSharedIacTemplatesIds(long domainId, Long accountId) {
142175
Set<Long> sharedIacTemplateIds = new HashSet<>();
143176
iacTemplateAccountMapDao.listByAccountId(accountId)
@@ -295,7 +328,8 @@ private IacTemplate persistIacTemplate(BaseIacTemplateRegistrationCmd cmd, Accou
295328
if (iacTemplateUpdate) {
296329
iacTemplateDomainMapDao.removeByIacTemplateId(iacTemplate.getId());
297330
}
298-
Set<Long> sharedDomainIds = cmd.isRecursiveDomains() ? getSharedDomainIdsRecursively(cmd.getSharedDomainIds()) : cmd.getSharedDomainIds();
331+
Set<Long> sharedDomainIds = BooleanUtils.toBoolean(cmd.isRecursiveDomains()) ?
332+
getSharedDomainIdsRecursively(cmd.getSharedDomainIds()) : cmd.getSharedDomainIds();
299333
persistDomainMappings(sharedDomainIds, persistedTemplate.getId(), owner);
300334
}
301335

@@ -411,6 +445,7 @@ public IacTemplate findIacTemplateById(Long id) {
411445

412446
@Override
413447
public void cleanUpAccountIacTemplates(long accountId) {
448+
// not cleaning up relationships here?
414449
iacTemplateDao.removeByAccountId(accountId);
415450
}
416451

0 commit comments

Comments
 (0)