Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getAllRemoteRepositoryHosts api for setting up egress network policy #147

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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 @@ -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
Loading