From dc53e05fc21f35169b76556a6a46cfa84a7815bf Mon Sep 17 00:00:00 2001 From: yma Date: Tue, 29 Apr 2025 09:38:02 +0800 Subject: [PATCH] Remove checksum validation from Sidecar --- .../jaxrs/FoloContentAccessResource.java | 39 +----- .../util/sidecar/services/ProxyService.java | 118 ------------------ .../util/sidecar/services/ReportService.java | 5 - 3 files changed, 5 insertions(+), 157 deletions(-) diff --git a/src/main/java/org/commonjava/util/sidecar/jaxrs/FoloContentAccessResource.java b/src/main/java/org/commonjava/util/sidecar/jaxrs/FoloContentAccessResource.java index 8bfd758..c5001e8 100644 --- a/src/main/java/org/commonjava/util/sidecar/jaxrs/FoloContentAccessResource.java +++ b/src/main/java/org/commonjava/util/sidecar/jaxrs/FoloContentAccessResource.java @@ -42,7 +42,6 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.util.Optional; @@ -92,39 +91,11 @@ public Uni get( @Parameter( in = PATH, required = true ) @PathParam( " Optional download = archiveService.getLocally( path ); if ( download.isPresent() && download.get().isFile() && reportService.checkValidHistoricalEntryMeta( path ) ) { - Uni checksumValidation = - proxyService.validateChecksum( id, packageType, type, name, path, request ); - return checksumValidation.onItem().transform( result -> { - if ( result != null && result ) - { - try - { - InputStream inputStream = FileUtils.openInputStream( download.get() ); - final Response.ResponseBuilder builder = - Response.ok( new TransferStreamingOutput( inputStream ) ); - logger.debug( "Download path: {} from historical archive.", path ); - publishTrackingEvent( path, id ); - return Uni.createFrom().item( builder.build() ); - } - catch ( IOException e ) - { - logger.error( "IO error for local file, path {}.", path, e ); - } - } - else - { - try - { - logger.debug( "Checksum validation failed, download from proxy: {}.", path ); - return proxyService.doGet( id, packageType, type, name, path, request ); - } - catch ( Exception e ) - { - logger.error( "Error for proxy download, path {}.", path, e ); - } - } - return null; - } ).flatMap( response -> response ); + InputStream inputStream = FileUtils.openInputStream( download.get() ); + final Response.ResponseBuilder builder = Response.ok( new TransferStreamingOutput( inputStream ) ); + logger.debug( "Download path: {} from historical archive.", path ); + publishTrackingEvent( path, id ); + return Uni.createFrom().item( builder.build() ); } else { diff --git a/src/main/java/org/commonjava/util/sidecar/services/ProxyService.java b/src/main/java/org/commonjava/util/sidecar/services/ProxyService.java index ea38db6..d600a0b 100644 --- a/src/main/java/org/commonjava/util/sidecar/services/ProxyService.java +++ b/src/main/java/org/commonjava/util/sidecar/services/ProxyService.java @@ -15,14 +15,12 @@ */ package org.commonjava.util.sidecar.services; -import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerRequest; import kotlin.Pair; import org.commonjava.util.sidecar.config.ProxyConfiguration; import org.commonjava.util.sidecar.interceptor.ExceptionHandler; -import org.commonjava.util.sidecar.model.dto.HistoricalEntryDTO; import org.commonjava.util.sidecar.util.OtelAdapter; import org.commonjava.util.sidecar.util.ProxyStreamingOutput; import org.commonjava.util.sidecar.util.UrlUtils; @@ -33,12 +31,7 @@ import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.ws.rs.core.Response; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; import static io.vertx.core.http.HttpMethod.HEAD; import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; @@ -135,76 +128,6 @@ public Uni wrapAsyncCall( WebClientAdapter.CallAdapter asyncCall, Http return ret.onFailure().recoverWithItem( this::handleProxyException ); } - public Uni validateChecksum( String trackingId, String packageType, String type, String name, String path, - HttpServerRequest request ) - { - Map localChecksums = getChecksums( path ); - Multi multiResults = Multi.createFrom().iterable( localChecksums.keySet() ).flatMap( checksumType -> { - String localChecksum = localChecksums.get( checksumType ); - if ( localChecksum == null ) - { - return Multi.createFrom().item( false ); - } - String checksumUrl = path + "." + checksumType; - try - { - return downloadAndCompareChecksum( trackingId, packageType, type, name, checksumUrl, localChecksum, - request ).onItem().invoke( result -> { - if ( result != null && result ) - { - // This is just used to skip loop to avoid unnecessary checksum download - logger.debug( - "Found the valid checksum compare result, stopping further checks, remote path {}", - checksumUrl ); - throw new FoundValidChecksumException(); - } - } ).onFailure().recoverWithItem( true ).toMulti(); - } - catch ( Exception e ) - { - logger.error( "Checksum download compare error for path: {}", checksumUrl, e ); - } - return Multi.createFrom().item( false ); - } ); - - Uni> collectedResults = multiResults.collect().asList(); - return collectedResults.onItem().transform( results -> { - boolean finalResult = results.stream().anyMatch( res -> res ); - logger.debug( "FinalResult:{}", finalResult ); - return finalResult; - } ); - } - - private Uni downloadAndCompareChecksum( String trackingId, String packageType, String type, String name, - String checksumUrl, String localChecksum, - HttpServerRequest request ) - throws Exception - { - return doGet( trackingId, packageType, type, name, checksumUrl, request ).onItem().transform( response -> { - if ( response.getStatus() == Response.Status.OK.getStatusCode() ) - { - ProxyStreamingOutput streamingOutput = (ProxyStreamingOutput) response.getEntity(); - try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) - { - streamingOutput.write( outputStream ); - String remoteChecksum = outputStream.toString(); - return localChecksum.equals( remoteChecksum ); - } - catch ( IOException e ) - { - logger.error( "Error to read remote checksum, path:{}.", checksumUrl, e ); - return null; - } - } - else - { - logger.error( "Failed to download remote checksum for {}: HTTP {}.", checksumUrl, - response.getStatus() ); - return null; - } - } ); - } - /** * Send status 500 with error message body. * @param t error @@ -245,45 +168,4 @@ private boolean isHeaderAllowed( Pair header return !FORBIDDEN_HEADERS.contains( key.toLowerCase() ); } - private Map getChecksums( String path ) - { - Map result = new LinkedHashMap<>(); - HistoricalEntryDTO entryDTO = reportService.getHistoricalContentMap().get( path ); - if ( entryDTO != null ) - { - result.put( ChecksumType.SHA1.getValue(), entryDTO.getSha1() ); - result.put( ChecksumType.SHA256.getValue(), entryDTO.getSha256() ); - result.put( ChecksumType.MD5.getValue(), entryDTO.getMd5() ); - } - - return result; - } - - enum ChecksumType - { - SHA1( "sha1" ), - SHA256( "sha256" ), - MD5( "md5" ); - - private final String value; - - ChecksumType( String value ) - { - this.value = value; - } - - public String getValue() - { - return value; - } - } - - class FoundValidChecksumException - extends RuntimeException - { - public FoundValidChecksumException() - { - super( "Found a valid checksum, stopping further checks." ); - } - } } \ No newline at end of file diff --git a/src/main/java/org/commonjava/util/sidecar/services/ReportService.java b/src/main/java/org/commonjava/util/sidecar/services/ReportService.java index 6959ad7..c1bcfbb 100644 --- a/src/main/java/org/commonjava/util/sidecar/services/ReportService.java +++ b/src/main/java/org/commonjava/util/sidecar/services/ReportService.java @@ -137,11 +137,6 @@ public void storeTrackedDownload( JsonObject message ) } } - public HashMap getHistoricalContentMap() - { - return historicalContentMap; - } - public boolean checkValidHistoricalEntryMeta( String trackingPath ) { HistoricalEntryDTO entryDTO = historicalContentMap.get( trackingPath );