|
| 1 | +package io.swagger.codegen.v3.utils; |
| 2 | + |
| 3 | + |
| 4 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 5 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 6 | +import com.github.tomakehurst.wiremock.verification.LoggedRequest; |
| 7 | +import io.swagger.codegen.v3.service.GenerationRequest; |
| 8 | +import io.swagger.codegen.v3.service.GeneratorService; |
| 9 | +import io.swagger.codegen.v3.service.HostAccessControl; |
| 10 | +import io.swagger.codegen.v3.service.Options; |
| 11 | +import io.swagger.v3.parser.core.models.AuthorizationValue; |
| 12 | +import org.testng.annotations.AfterMethod; |
| 13 | +import org.testng.annotations.BeforeMethod; |
| 14 | +import org.testng.annotations.Test; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 26 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 27 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 28 | +import static org.testng.Assert.assertEquals; |
| 29 | +import static org.testng.Assert.assertFalse; |
| 30 | + |
| 31 | +public class AllowDeniedHostTest { |
| 32 | + |
| 33 | + private static final int WIRE_MOCK_PORT = 9999; |
| 34 | + private static final String EXPECTED_ACCEPTS_HEADER = "application/json, application/yaml, */*"; |
| 35 | + private static final String LOCALHOST = "localhost"; |
| 36 | + private WireMockServer wireMockServer; |
| 37 | + |
| 38 | + |
| 39 | + @AfterMethod |
| 40 | + public void tearDown() throws Exception { |
| 41 | + wireMockServer.stop(); |
| 42 | + } |
| 43 | + |
| 44 | + @BeforeMethod |
| 45 | + public void setUp() throws Exception { |
| 46 | + wireMockServer = new WireMockServer(WIRE_MOCK_PORT); |
| 47 | + wireMockServer.start(); |
| 48 | + WireMock.configureFor(WIRE_MOCK_PORT); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testAuthorizationHeaderAllowedHost() throws Exception { |
| 53 | + |
| 54 | + HostAccessControl allowedHostAccessControl = new HostAccessControl(); |
| 55 | + allowedHostAccessControl.setHost("localhost"); |
| 56 | + |
| 57 | + setupStub(); |
| 58 | + |
| 59 | + final String headerValue = "foobar"; |
| 60 | + final String headerName = "Authorization"; |
| 61 | + final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header", |
| 62 | + url -> url.toString().startsWith("http://localhost")); |
| 63 | + |
| 64 | + GenerationRequest request = new GenerationRequest(); |
| 65 | + request |
| 66 | + .codegenVersion(GenerationRequest.CodegenVersion.V3) |
| 67 | + .type(GenerationRequest.Type.SERVER) |
| 68 | + .lang("java") |
| 69 | + .specURL(getUrl()) |
| 70 | + .options( |
| 71 | + new Options() |
| 72 | + .outputDir(getTmpFolder().getAbsolutePath()) |
| 73 | + .authorizationValue(authorizationValue) |
| 74 | + .allowedAuthHosts(Arrays.asList(allowedHostAccessControl)) |
| 75 | + ); |
| 76 | + |
| 77 | + new GeneratorService().generationRequest(request).generate(); |
| 78 | + |
| 79 | + verify(getRequestedFor(urlEqualTo("/v2/pet/1")) |
| 80 | + .withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)) |
| 81 | + .withHeader(headerName, equalTo(headerValue)) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testAuthorizationHeaderWithNonAllowedHost() throws Exception { |
| 87 | + |
| 88 | + HostAccessControl deniedHostAccessControl = new HostAccessControl(); |
| 89 | + deniedHostAccessControl.setHost("localhost"); |
| 90 | + |
| 91 | + setupStub(); |
| 92 | + |
| 93 | + final String headerValue = "foobar"; |
| 94 | + String authorization = "Authorization"; |
| 95 | + final AuthorizationValue authorizationValue = new AuthorizationValue(authorization, |
| 96 | + headerValue, "header", u -> false); |
| 97 | + |
| 98 | + GenerationRequest request = new GenerationRequest(); |
| 99 | + request |
| 100 | + .codegenVersion(GenerationRequest.CodegenVersion.V3) |
| 101 | + .type(GenerationRequest.Type.SERVER) |
| 102 | + .lang("java") |
| 103 | + .specURL(getUrl()) |
| 104 | + .options( |
| 105 | + new Options() |
| 106 | + .outputDir(getTmpFolder().getAbsolutePath()) |
| 107 | + .authorizationValue(authorizationValue) |
| 108 | + .deniedAuthHosts(Arrays.asList(deniedHostAccessControl)) |
| 109 | + ); |
| 110 | + |
| 111 | + new GeneratorService().generationRequest(request).generate(); |
| 112 | + |
| 113 | + List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1"))); |
| 114 | + assertFalse(requests.get(0).containsHeader(authorization)); |
| 115 | + assertEquals(requests.size(),2); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + private String getUrl() { |
| 120 | + return String.format("http://%s:%d/v2/pet/1", LOCALHOST, WIRE_MOCK_PORT); |
| 121 | + } |
| 122 | + |
| 123 | + private String setupStub() { |
| 124 | + final String expectedBody = "openapi: 3.0.0\n" + |
| 125 | + "info:\n" + |
| 126 | + " title: test\n" + |
| 127 | + " version: \"0.0.1\"\n" + |
| 128 | + "\n" + |
| 129 | + "paths:\n" + |
| 130 | + " '/contents/{id}':\n" + |
| 131 | + " parameters:\n" + |
| 132 | + " - name: id\n" + |
| 133 | + " in: path\n" + |
| 134 | + " description: test\n" + |
| 135 | + " required: true\n" + |
| 136 | + " schema:\n" + |
| 137 | + " type: integer\n" + |
| 138 | + " get:\n" + |
| 139 | + " description: test\n" + |
| 140 | + " responses:\n" + |
| 141 | + " '200':\n" + |
| 142 | + " description: OK\n" + |
| 143 | + " schema: null\n" + |
| 144 | + " $ref: '#/components/schemas/Content'\n" + |
| 145 | + "components:\n" + |
| 146 | + " schemas:\n" + |
| 147 | + " Content:\n" + |
| 148 | + " type: object\n" + |
| 149 | + " title: \t\ttest"; |
| 150 | + |
| 151 | + stubFor(get(urlEqualTo("/v2/pet/1")) |
| 152 | + .willReturn(aResponse() |
| 153 | + .withBody(expectedBody) |
| 154 | + .withHeader("Content-Type", "application/json") |
| 155 | + )); |
| 156 | + return expectedBody; |
| 157 | + } |
| 158 | + |
| 159 | + protected static File getTmpFolder() { |
| 160 | + try { |
| 161 | + File outputFolder = Files.createTempFile("codegentest-", "-tmp").toFile(); |
| 162 | + outputFolder.delete(); |
| 163 | + outputFolder.mkdir(); |
| 164 | + outputFolder.deleteOnExit(); |
| 165 | + return outputFolder; |
| 166 | + } catch (Exception e) { |
| 167 | + e.printStackTrace(); |
| 168 | + return null; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
0 commit comments