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 @@ -46,7 +46,7 @@ protected String getResponseName() {
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = TemplateResponse.class, required = true, description = "The Template ID")
private Long id;

@Parameter(name = ApiConstants.IS_FEATURED, type = CommandType.BOOLEAN, description = "True for featured Template/ISO, false otherwise")
@Parameter(name = ApiConstants.IS_FEATURED, type = CommandType.BOOLEAN, description = "True for featured Template/ISO, false otherwise. Only admins can set it to true")
private Boolean featured;

@Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN, description = "True for public Template/ISO, false for private Templates/ISOs")
Expand Down
11 changes: 11 additions & 0 deletions server/src/main/java/com/cloud/template/TemplateManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,16 @@ public List<String> listTemplatePermissions(BaseListTemplateOrIsoPermissionsCmd
return accountNames;
}

/**
* Only admins can mark a template or ISO as featured, the same as when registering one or creating one from a volume or snapshot.
* Anyone allowed to update the permissions can un-feature it.
*/
protected void checkCallerCanFeature(boolean isFeatured, boolean isAdmin, String mediaType) {
if (isFeatured && !isAdmin) {
throw new InvalidParameterValueException("Only admins are allowed to mark a " + mediaType + " as featured.");
}
}

@DB
@Override
public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissionsCmd cmd) {
Expand Down Expand Up @@ -1652,6 +1662,7 @@ public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissions
}

if (isFeatured != null) {
checkCallerCanFeature(isFeatured, isAdmin, mediaType);
updatedTemplate.setFeatured(isFeatured.booleanValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.cloud.template;


import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.host.Status;
Expand Down Expand Up @@ -66,6 +68,7 @@
import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd;
import org.apache.cloudstack.api.command.user.template.RegisterVnfTemplateCmd;
import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd;
import org.apache.cloudstack.api.command.user.template.UpdateTemplatePermissionsCmd;
import org.apache.cloudstack.api.command.user.template.UpdateVnfTemplateCmd;
import org.apache.cloudstack.api.command.user.userdata.LinkUserDataToTemplateCmd;
import org.apache.cloudstack.context.CallContext;
Expand Down Expand Up @@ -177,6 +180,9 @@
@Mock
AccountManager _accountMgr;

@Mock
DomainDao _domainDao;

Check warning on line 184 in server/src/test/java/com/cloud/template/TemplateManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "_domainDao" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaI85P7P_r-9K1NrlnrF&open=AaI85P7P_r-9K1NrlnrF&pullRequest=14973

@Mock
VnfTemplateManager vnfTemplateManager;

Expand Down Expand Up @@ -773,4 +779,43 @@
Mockito.verify(heuristicRuleHelperMock, Mockito.times(1)).getImageStoreIfThereIsHeuristicRule(1L, HeuristicType.TEMPLATE, vmTemplateVOMock);
}

private UpdateTemplatePermissionsCmd mockOwnTemplateFeaturedUpdate(Boolean featured, boolean callerIsAdmin) {
long callerId = CallContext.current().getCallingAccount().getId();
UpdateTemplatePermissionsCmd cmd = mock(UpdateTemplatePermissionsCmd.class);
when(cmd.getId()).thenReturn(10L);
when(cmd.isFeatured()).thenReturn(featured);
when(cmd.getAccountNames()).thenReturn(null);
when(cmd.getProjectIds()).thenReturn(null);

VMTemplateVO template = mock(VMTemplateVO.class);
when(template.getAccountId()).thenReturn(callerId);
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
when(vmTemplateDao.findById(10L)).thenReturn(template);
when(vmTemplateDao.createForUpdate()).thenReturn(mock(VMTemplateVO.class));

Check warning on line 794 in server/src/test/java/com/cloud/template/TemplateManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this mock creation to a local variable.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaI85P7P_r-9K1NrlnrD&open=AaI85P7P_r-9K1NrlnrD&pullRequest=14973

Account owner = mock(Account.class);
when(owner.getType()).thenReturn(Account.Type.NORMAL);
when(owner.getDomainId()).thenReturn(1L);
when(_domainDao.findById(1L)).thenReturn(mock(DomainVO.class));

Check warning on line 799 in server/src/test/java/com/cloud/template/TemplateManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this mock creation to a local variable.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaI85P7P_r-9K1NrlnrE&open=AaI85P7P_r-9K1NrlnrE&pullRequest=14973
when(_accountMgr.getAccount(callerId)).thenReturn(owner);
when(_accountMgr.isAdmin(callerId)).thenReturn(callerIsAdmin);
return cmd;
}

@Test
public void updateTemplateOrIsoPermissionsTestOwnerCannotFeatureTemplate() {
InvalidParameterValueException e = Assert.assertThrows(InvalidParameterValueException.class,

Check warning on line 807 in server/src/test/java/com/cloud/template/TemplateManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaI85P7P_r-9K1NrlnrG&open=AaI85P7P_r-9K1NrlnrG&pullRequest=14973
() -> templateManager.updateTemplateOrIsoPermissions(mockOwnTemplateFeaturedUpdate(true, false)));
Assert.assertTrue(e.getMessage().contains("featured"));
}

@Test
public void updateTemplateOrIsoPermissionsTestOwnerCanUnfeatureTemplate() {
Assert.assertTrue(templateManager.updateTemplateOrIsoPermissions(mockOwnTemplateFeaturedUpdate(false, false)));
}

@Test
public void updateTemplateOrIsoPermissionsTestAdminCanFeatureTemplate() {
Assert.assertTrue(templateManager.updateTemplateOrIsoPermissions(mockOwnTemplateFeaturedUpdate(true, true)));
}
}
Loading