Skip to content

Commit

Permalink
Add getAllRemoteRepositoryHosts api for setting up egress network pol…
Browse files Browse the repository at this point in the history
…icy (#147)
  • Loading branch information
ruhan1 authored Apr 18, 2024
1 parent de1148f commit 6aa9a8f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
import static org.commonjava.indy.service.repository.model.StoreKey.fromString;
import static org.commonjava.indy.service.repository.model.pkg.MavenPackageTypeDescriptor.MAVEN_PKG_KEY;
import static org.commonjava.indy.service.repository.model.pkg.PackageTypeConstants.isValidPackageType;
import static org.commonjava.indy.service.repository.model.pkg.PackageTypeConstants.*;

@ApplicationScoped
public class QueryController
Expand Down Expand Up @@ -204,6 +204,22 @@ public List<RemoteRepository> getAllRemoteRepositories( final String packageType
"Failed to get all remote repos for package type {}", packageType );
}

/**
* Get all remote hostnames in known package types [maven, npm, generic-http].
*/
public String getAllRemoteRepositoryHosts()
throws IndyDataException
{
final Set<String> ret = new HashSet<>();
storeManager.query().getAllRemoteRepositories(PKG_TYPE_MAVEN).forEach( r -> ret.add( r.getHost() ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_NPM).forEach( r -> ret.add( r.getHost() ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_GENERIC_HTTP).forEach( r -> ret.add( r.getHost() ) );

final StringBuilder sb = new StringBuilder();
ret.stream().sorted().forEach( s -> sb.append(s).append(","));
return sb.toString();
}

public List<HostedRepository> getAllHostedRepositories( final String packageType, final String enabled )
throws IndyWorkflowException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;

import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
import static jakarta.ws.rs.core.Response.ok;
Expand Down Expand Up @@ -118,6 +119,17 @@ public Response getAllRemoteRepositories(
return generateStoreListingResponse( () -> queryController.getAllRemoteRepositories( packageType, enabled ) );
}

@Operation( description = "Retrieve all remote repository hostname for setting up egress network policy" )
@GET
@Path( "/remotes/hosts" )
@Produces( TEXT_PLAIN )
public Response getAllRemoteRepositoryHosts()
throws Exception
{
return responseHelper.formatOkResponseWithEntity(
queryController.getAllRemoteRepositoryHosts(), TEXT_PLAIN, null);
}

@Operation( description = "Retrieve all hosted repository definitions by specified package type" )
@APIResponse( responseCode = "200",
content = @Content( schema = @Schema( implementation = StoreListingDTO.class ) ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.Response.Status.OK;
import static org.commonjava.indy.service.repository.util.PathUtils.normalize;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
@TestProfile( MockTestProfile.class )
Expand All @@ -44,6 +51,18 @@ public void testGetAll()
.body( "items.size()", greaterThan( 1 ) );
}

@Test
public void testGetAllRemoteHosts() throws IOException
{
Response response = given().when()
.get(normalize(BASE_QUERY_PATH, "remotes/hosts"));
try (InputStream in = response.getBody().asInputStream())
{
final String ret = IOUtils.toString( in, Charset.defaultCharset() );
assertTrue( ret.contains("repo.maven.apache.org") );
}
}

@Test
public void testGetAllByDefaultPackageTypes()
{
Expand Down

0 comments on commit 6aa9a8f

Please sign in to comment.