Skip to content

Commit

Permalink
upstream: b=main,r=12788a0065c10cc9d731216f58613be5d3882b03,t=2024-06…
Browse files Browse the repository at this point in the history
…-07-1243-34334
  • Loading branch information
sonatype-zion committed Jun 7, 2024
1 parent d96ad5b commit e8fbdbf
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 10 deletions.
7 changes: 5 additions & 2 deletions buildsupport/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,13 @@
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j</artifactId>
</exclusion>

</exclusions>
</dependency>

<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j-core</artifactId>
<version>0.8.10</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-atom-provider</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public interface CleanupPolicyConstants
String RETAIN_SORT_BY_KEY = "sortBy";

String REGEX_KEY = "regex";

String MAVEN2_FORMAT = "maven2";

String DOCKER_FORMAT = "docker";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

public enum SortType
{
VERSION,
DATE
VERSION("version"),
DATE("date");

public final String value;

SortType(final String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public HikariDataSource upgradeH2Database(String storeName, HikariConfig hikariC

private boolean validateH2SqlFileExists(Optional<Path> sqlFile) throws Exception {
if (!sqlFile.isPresent()) {
log.error("You are trying to start Nexus Repository with an outdated H2 database, " +
"please use Nexus with version 3.69, run the 'Export H2 database to SQL file' task, " +
"put the Sql file into the database work-directory, go to Nexus version 3.70 " +
"and start Nexus repository again");//TODO replace for the correct message when NEXUS-42220 is done
StringBuilder buf = new StringBuilder();
buf.append("\n-------------------------------------------------------------------------------------------" +
"----------------------------------------------------------------------------------\n\n");
buf.append("Your H2 database version is no longer supported. Upgrade to a supported version using upgrade " +
"instructions at https://links.sonatype.com/products/nxrm3/docs/upgrade-h2.html.");
buf.append("\n\n-----------------------------------------------------------------------------------------" +
"------------------------------------------------------------------------------------\n\n");
log.error(buf.toString());
managedLifecycleManager.shutdownWithExitCode(1);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Optional;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
Expand All @@ -32,8 +33,10 @@
import org.sonatype.nexus.blobstore.s3.rest.internal.model.S3BlobStoreApiModel;
import org.sonatype.nexus.rapture.PasswordPlaceholder;
import org.sonatype.nexus.rest.Resource;
import org.sonatype.nexus.rest.ValidationErrorsException;
import org.sonatype.nexus.rest.WebApplicationMessageException;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

Expand Down Expand Up @@ -79,13 +82,15 @@ public S3BlobStoreApiResource(
@RequiresPermissions("nexus:blobstores:create")
public Response createBlobStore(@Valid final S3BlobStoreApiModel request) {
try {
s3BlobStoreApiUpdateValidation.validateCreateRequest(request);
final BlobStoreConfiguration blobStoreConfiguration = map(blobStoreManager.newConfiguration(), request);
blobStoreManager.create(blobStoreConfiguration);
return status(CREATED).build();
}
catch (Exception e) {
throw new WebApplicationMessageException(INTERNAL_SERVER_ERROR, e.getMessage());
throw new WebApplicationMessageException(BAD_REQUEST, e.getMessage());
}

}

@PUT
Expand Down Expand Up @@ -155,4 +160,28 @@ private BlobStoreConfiguration ensureBlobStoreTypeIsS3(final BlobStoreConfigurat
}
return configuration;
}

@DELETE
@RequiresAuthentication
@Path("/s3")
@RequiresPermissions("nexus:blobstores:delete")
@ApiOperation(value = "Delete a blob store with an empty name", hidden = true)
public Response deleteBlobStoreWithEmptyName() {
String blobStoreName = "";
try {
BlobStore blobStore = blobStoreManager.get(blobStoreName);
if (blobStore == null) {
return Response.status(Response.Status.NOT_FOUND)
.entity("Blob store not found")
.build();
}
blobStoreManager.delete(blobStoreName);
return Response.status(Response.Status.NO_CONTENT).build();
}
catch (Exception e) {
throw new WebApplicationMessageException(BAD_REQUEST, e.getMessage());
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
package org.sonatype.nexus.blobstore.s3.rest.internal;

import javax.validation.Valid;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.sonatype.nexus.blobstore.s3.rest.internal.model.S3BlobStoreApiModel;
Expand All @@ -22,6 +25,8 @@
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import static org.sonatype.nexus.rest.ApiDocConstants.API_BLOB_STORE;
import static org.sonatype.nexus.rest.ApiDocConstants.AUTHENTICATION_REQUIRED;
Expand Down Expand Up @@ -67,4 +72,16 @@ void updateBlobStore(
})
S3BlobStoreApiModel getBlobStore(
@ApiParam(value = "Name of the blob store configuration to fetch") String blobStoreName);


@ApiOperation("Delete an S3 blob store with an empty name")
@ApiResponses(value = {
@ApiResponse(code = 204, message = "Blob store deleted"),
@ApiResponse(code = 400, message = "Unknown S3 blob store"),
@ApiResponse(code = 401, message = "Authentication required"),
@ApiResponse(code = 403, message = "Insufficient permissions"),
@ApiResponse(code = 404, message = "Blob store not found"),
})
Response deleteBlobStoreWithEmptyName();
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sonatype.nexus.blobstore.api.BlobStoreManager;
import org.sonatype.nexus.blobstore.s3.rest.internal.model.S3BlobStoreApiModel;
import org.sonatype.nexus.rest.ValidationErrorsException;
import org.apache.commons.lang.StringUtils;

import static java.lang.String.format;
import static java.lang.String.join;
Expand Down Expand Up @@ -53,6 +54,15 @@ public S3BlobStoreApiUpdateValidation(final BlobStoreManager blobStoreManager) {
this.blobStoreManager = blobStoreManager;
}

void validateCreateRequest(final S3BlobStoreApiModel s3BlobStoreApiModel) {
List<String> errors = new ArrayList<>();
checkBlobStoreNameNotEmpty(s3BlobStoreApiModel.getName(), errors);

if (!errors.isEmpty()) {
throw new ValidationErrorsException(BLOB_STORE_NAME, String.join(COMMA, errors));
}
}

void validateUpdateRequest(final S3BlobStoreApiModel s3BlobStoreApiModel, final String blobStoreName) {
List<String> errors = new ArrayList<>();
final boolean blobStoreExists = checkBlobStoreExists(blobStoreName, errors);
Expand All @@ -66,6 +76,12 @@ void validateUpdateRequest(final S3BlobStoreApiModel s3BlobStoreApiModel, final
}
}

private void checkBlobStoreNameNotEmpty(final String blobStoreName, final List<String> errors) {
if (StringUtils.isBlank(blobStoreName)) {
errors.add("Blob store name cannot be empty");
}
}

private boolean checkBlobStoreExists(final String blobStoreName, final List<String> errors) {
if (!blobStoreManager.exists(blobStoreName)) {
errors.add(format(NON_EXISTENT_BLOB_STORE_ERROR_MESSAGE_FORMAT, blobStoreName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public void shouldHaveNoValidationErrors() {
verify(blobStoreManager).exists(BLOB_STORE_NAME);
}

@Test
public void shouldThrowExceptionWhenBlobStoreNameIsEmptyOnCreate() {
expectedException.expect(ValidationErrorsException.class);
expectedException.expectMessage("Blob store name cannot be empty");

underTest.validateCreateRequest(anS3BlobStoreApiModel(""));
}

private void mockBlobStoreType(final String type) {
final BlobStoreConfiguration blobStoreConfiguration = new MockBlobStoreConfiguration();
blobStoreConfiguration.setType(type);
Expand Down
2 changes: 1 addition & 1 deletion revision.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b=main,r=3418179532e172f1b1a7136357eb107935d8d4f3,t=2024-05-31-1243-23302
b=main,r=12788a0065c10cc9d731216f58613be5d3882b03,t=2024-06-07-1243-34334
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.inject.Provider;

import org.sonatype.nexus.capability.CapabilityContext;
import org.sonatype.nexus.capability.CapabilityIdentity;
import org.sonatype.nexus.capability.CapabilityReference;
import org.sonatype.nexus.capability.CapabilityRegistry;
import org.sonatype.nexus.capability.CapabilityType;
Expand Down Expand Up @@ -49,6 +50,15 @@ public void disableOutreach() {
disable(OUTREACH);
}

public Collection<CapabilityReference> getAll() {
//noinspection unchecked
return (Collection<CapabilityReference>) capabilityRegistryProvider.get().getAll();
}

public void removeById(final CapabilityIdentity id) {
capabilityRegistryProvider.get().remove(id);
}

@Override
public void after() {
capabilitiesToRemove.stream()
Expand Down

0 comments on commit e8fbdbf

Please sign in to comment.