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

fixes #343 Fix all test cases with another server2 for the Jwks #344

Merged
merged 1 commit into from
Jan 6, 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 @@ -32,43 +32,62 @@
* This is a test class that focuses the multiple specifications with scope verification. It has a customized
* openapi-handler.yml config file injected during the setup.
*
* This test is disabled as it has to access jwk endpoint during the middle of the request.
*
*/
@Ignore
public class JwtVerifierHandlerMultipleSpecsTest {
static final Logger logger = LoggerFactory.getLogger(JwtVerifierHandlerMultipleSpecsTest.class);

static Undertow server = null;

static Undertow server1 = null;
static Undertow server2 = null;
@BeforeClass
public static void setUp() {
if(server == null) {
logger.info("starting server");
if (server1 == null) {
logger.info("starting server1");
HttpHandler handler = getTestHandler();
JwtVerifyHandler jwtVerifyHandler = new JwtVerifyHandler();
jwtVerifyHandler.setNext(handler);
OpenApiHandler openApiHandler = new OpenApiHandler(OpenApiHandlerConfig.load("openapi-handler-multiple"));
openApiHandler.setNext(jwtVerifyHandler);
server = Undertow.builder()
server1 = Undertow.builder()
.addHttpListener(7081, "localhost")
.setHandler(openApiHandler)
.build();
server.start();
server1.start();
}

if (server2 == null) {
logger.info("starting server2");
HttpHandler handler = getJwksHandler();
server2 = Undertow.builder()
.addHttpListener(7082, "localhost")
.setHandler(handler)
.build();
server2.start();
}

}

@AfterClass
public static void tearDown() throws Exception {
if(server != null) {
if (server1 != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
logger.info("The server is stopped.");
server1.stop();
logger.info("The server1 is stopped.");
}
if (server2 != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server2.stop();
logger.info("The server2 is stopped.");
}

}

static RoutingHandler getTestHandler() {
Expand All @@ -84,11 +103,16 @@ static RoutingHandler getTestHandler() {
exchange.endExchange();
}
})
.add(Methods.GET, "/petstore/pets", exchange -> exchange.getResponseSender().send("get"));
}

static RoutingHandler getJwksHandler() {
return Handlers.routing()
.add(Methods.GET, "/oauth2/N2CMw0HGQXeLvC1wBfln2A/keys", exchange -> {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
exchange.getResponseSender().send("{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"Tj_l_tIBTginOtQbL0Pv5w\",\"n\":\"0YRbWAb1FGDpPUUcrIpJC6BwlswlKMS-z2wMAobdo0BNxNa7hG_gIHVPkXu14Jfo1JhUhS4wES3DdY3a6olqPcRN1TCCUVHd-1TLd1BBS-yq9tdJ6HCewhe5fXonaRRKwutvoH7i_eR4m3fQ1GoVzVAA3IngpTr4ptnM3Ef3fj-5wZYmitzrRUyQtfARTl3qGaXP_g8pHFAP0zrNVvOnV-jcNMKm8YZNcgcs1SuLSFtUDXpf7Nr2_xOhiNM-biES6Dza1sMLrlxULFuctudO9lykB7yFh3LHMxtIZyIUHuy0RbjuOGC5PmDowLttZpPI_j4ynJHAaAWr8Ddz764WdQ\",\"e\":\"AQAB\"}]}");
})
.add(Methods.GET, "/petstore/pets", exchange -> exchange.getResponseSender().send("get"));
});

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,62 @@
import java.util.concurrent.atomic.AtomicReference;

/**
* Created by steve on 01/09/16. Disabled as it has threading issue as the same request will access the server twice
* to get the token in the middle.
* Created by steve on 01/09/16.
*/
@Ignore
public class JwtVerifyHandlerTest {
static final Logger logger = LoggerFactory.getLogger(JwtVerifyHandlerTest.class);

static Undertow server = null;

static Undertow server1 = null;
static Undertow server2 = null;
@BeforeClass
public static void setUp() {
if (server == null) {
logger.info("starting server");
if (server1 == null) {
logger.info("starting server1");
HttpHandler handler = getTestHandler();
JwtVerifyHandler jwtVerifyHandler = new JwtVerifyHandler();
jwtVerifyHandler.setNext(handler);
OpenApiHandler openApiHandler = new OpenApiHandler();
openApiHandler.setNext(jwtVerifyHandler);
server = Undertow.builder()
server1 = Undertow.builder()
.addHttpListener(7081, "localhost")
.setHandler(openApiHandler)
.build();
server.start();
server1.start();
}

if (server2 == null) {
logger.info("starting server2");
HttpHandler handler = getJwksHandler();
server2 = Undertow.builder()
.addHttpListener(7082, "localhost")
.setHandler(handler)
.build();
server2.start();
}

}

@AfterClass
public static void tearDown() throws Exception {
if (server != null) {
if (server1 != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
logger.info("The server is stopped.");
server1.stop();
logger.info("The server1 is stopped.");
}
if (server2 != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server2.stop();
logger.info("The server2 is stopped.");
}

}

static RoutingHandler getTestHandler() {
Expand All @@ -98,11 +117,16 @@ static RoutingHandler getTestHandler() {
exchange.endExchange();
}
})
.add(Methods.GET, "/v1/pets", exchange -> exchange.getResponseSender().send("get"));
}

static RoutingHandler getJwksHandler() {
return Handlers.routing()
.add(Methods.GET, "/oauth2/N2CMw0HGQXeLvC1wBfln2A/keys", exchange -> {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
exchange.getResponseSender().send("{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"Tj_l_tIBTginOtQbL0Pv5w\",\"n\":\"0YRbWAb1FGDpPUUcrIpJC6BwlswlKMS-z2wMAobdo0BNxNa7hG_gIHVPkXu14Jfo1JhUhS4wES3DdY3a6olqPcRN1TCCUVHd-1TLd1BBS-yq9tdJ6HCewhe5fXonaRRKwutvoH7i_eR4m3fQ1GoVzVAA3IngpTr4ptnM3Ef3fj-5wZYmitzrRUyQtfARTl3qGaXP_g8pHFAP0zrNVvOnV-jcNMKm8YZNcgcs1SuLSFtUDXpf7Nr2_xOhiNM-biES6Dza1sMLrlxULFuctudO9lykB7yFh3LHMxtIZyIUHuy0RbjuOGC5PmDowLttZpPI_j4ynJHAaAWr8Ddz764WdQ\",\"e\":\"AQAB\"}]}");
})
.add(Methods.GET, "/v1/pets", exchange -> exchange.getResponseSender().send("get"));
});

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@

/**
* This is a test case for UnifiedSecurityHandler. It is using the petstore.yaml file in the test resources
* Due to the jwk is in the same server and it is called within the context of the petstore access, there are
* some threading issue that cause some test case failures. So, I have to run the test case one by one to make
* all passed. For build, we have disabled this test case for now.
*/
@Ignore
public class UnifiedSecurityHandlerTest {
static final Logger logger = LoggerFactory.getLogger(UnifiedSecurityHandlerTest.class);

Expand All @@ -50,6 +46,44 @@ public class UnifiedSecurityHandlerTest {
static final int httpsPort = server.getServerConfig().getHttpsPort();
static final String url = enableHttp2 || enableHttps ? "https://localhost:" + httpsPort : "http://localhost:" + httpPort;

static Undertow server2 = null;
@BeforeClass
public static void setUp() {
if (server2 == null) {
logger.info("starting server2");
HttpHandler handler = getJwksHandler();
server2 = Undertow.builder()
.addHttpListener(7082, "localhost")
.setHandler(handler)
.build();
server2.start();
}

}

@AfterClass
public static void tearDown() throws Exception {
if (server2 != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server2.stop();
logger.info("The server2 is stopped.");
}

}

static RoutingHandler getJwksHandler() {
return Handlers.routing()
.add(Methods.GET, "/oauth2/N2CMw0HGQXeLvC1wBfln2A/keys", exchange -> {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
exchange.getResponseSender().send("{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"Tj_l_tIBTginOtQbL0Pv5w\",\"n\":\"0YRbWAb1FGDpPUUcrIpJC6BwlswlKMS-z2wMAobdo0BNxNa7hG_gIHVPkXu14Jfo1JhUhS4wES3DdY3a6olqPcRN1TCCUVHd-1TLd1BBS-yq9tdJ6HCewhe5fXonaRRKwutvoH7i_eR4m3fQ1GoVzVAA3IngpTr4ptnM3Ef3fj-5wZYmitzrRUyQtfARTl3qGaXP_g8pHFAP0zrNVvOnV-jcNMKm8YZNcgcs1SuLSFtUDXpf7Nr2_xOhiNM-biES6Dza1sMLrlxULFuctudO9lykB7yFh3LHMxtIZyIUHuy0RbjuOGC5PmDowLttZpPI_j4ynJHAaAWr8Ddz764WdQ\",\"e\":\"AQAB\"}]}");
});

}

static RoutingHandler getTestHandler() {
return Handlers.routing()
.add(Methods.GET, "/v1/pets/{petId}", exchange -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ multipleSpec: ${openapi-handler.multipleSpec:true}
pathSpecMapping:
/petstore: openapi-petstore
/market: openapi-market
/oauth2: openapi-oauth2
Loading