|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/md5" |
| 6 | + "crypto/tls" |
| 7 | + "encoding/hex" |
| 8 | + . "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/client" |
| 9 | + . "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/common" |
| 10 | + . "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/server" |
| 11 | + "github.com/testcontainers/testcontainers-go" |
| 12 | + "github.com/testcontainers/testcontainers-go/wait" |
| 13 | + "github.com/wiremock/go-wiremock" |
| 14 | + . "github.com/wiremock/wiremock-testcontainers-go" |
| 15 | + "io" |
| 16 | + "math/rand" |
| 17 | + "net/http" |
| 18 | + "net/url" |
| 19 | + "os" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + WireMockHttpPort = "8080" |
| 28 | + WireMockHttpsPort = "8443" |
| 29 | + WireMockProtocol = "/tcp" |
| 30 | + ProxyUrl = "http://" + Localhost + ":8081" |
| 31 | + ContentType = "text/xml" |
| 32 | + Md5Hash = "ea3ca57f8f99d1d210d1b438c9841440" |
| 33 | + ContentLength = "403" |
| 34 | +) |
| 35 | + |
| 36 | +type containerCustomizer struct{} |
| 37 | + |
| 38 | +func (c containerCustomizer) Customize(req *testcontainers.GenericContainerRequest) error { |
| 39 | + req.ExposedPorts = []string{WireMockHttpPort + WireMockProtocol, WireMockHttpsPort + WireMockProtocol} |
| 40 | + req.WaitingFor = wait.ForListeningPort(WireMockHttpPort) |
| 41 | + req.Cmd = []string{"--port", WireMockHttpPort, "--https-port", WireMockHttpsPort} |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func createClient(t *testing.T) *http.Client { |
| 46 | + proxy, err := url.Parse(ProxyUrl) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + transport := &http.Transport{ |
| 51 | + Proxy: http.ProxyURL(proxy), |
| 52 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 53 | + } |
| 54 | + return &http.Client{ |
| 55 | + Transport: transport, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func getMd5Hash(bytes []byte) string { |
| 60 | + hash := md5.Sum(bytes) |
| 61 | + return hex.EncodeToString(hash[:]) |
| 62 | +} |
| 63 | + |
| 64 | +func head(urlMatchingPair wiremock.URLMatcher) *wiremock.StubRule { |
| 65 | + return wiremock.NewStubRule(http.MethodHead, urlMatchingPair) |
| 66 | +} |
| 67 | + |
| 68 | +func TestDomainProxy(t *testing.T) { |
| 69 | + // Start Wiremock container |
| 70 | + ctx := context.Background() |
| 71 | + container, err := RunContainerAndStopOnCleanup(ctx, t, containerCustomizer{}) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + // HTTP Get stub |
| 76 | + pom, err := os.ReadFile("testdata/bar-1.0.pom") |
| 77 | + err = container.Client.StubFor( |
| 78 | + wiremock.Get(wiremock.URLEqualTo("/com/foo/bar/1.0/bar-1.0.pom")). |
| 79 | + WillReturnResponse( |
| 80 | + wiremock.NewResponse(). |
| 81 | + WithHeader("Content-Type", ContentType). |
| 82 | + WithBody(string(pom)). |
| 83 | + WithStatus(http.StatusOK), |
| 84 | + ), |
| 85 | + ) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + // HTTP Head stub |
| 90 | + err = container.Client.StubFor( |
| 91 | + head(wiremock.URLEqualTo("/com/foo/bar/1.0/bar-1.0.pom")). |
| 92 | + WillReturnResponse( |
| 93 | + wiremock.NewResponse(). |
| 94 | + WithHeader("Content-Type", ContentType). |
| 95 | + WithHeader("Content-Length", ContentLength). |
| 96 | + WithStatus(http.StatusOK), |
| 97 | + ), |
| 98 | + ) |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + // Set env variables |
| 103 | + os.Setenv(DomainSocketKey, "/tmp/domain-socket-"+strconv.Itoa(rand.Int())+".sock") |
| 104 | + os.Setenv(ServerHttpPortKey, "8081") |
| 105 | + os.Setenv(ProxyTargetWhitelistKey, "localhost,foo.bar") |
| 106 | + // Start services |
| 107 | + domainProxyServer := NewDomainProxyServer() |
| 108 | + go domainProxyServer.Start() |
| 109 | + domainProxyClient := NewDomainProxyClient() |
| 110 | + go domainProxyClient.Start() |
| 111 | + time.Sleep(1 * time.Second) |
| 112 | + defer domainProxyServer.Stop() |
| 113 | + defer domainProxyClient.Stop() |
| 114 | + // Get Wiremock container details |
| 115 | + mappedHttpPort, err := container.MappedPort(ctx, WireMockHttpPort) |
| 116 | + mappedHttpsPort, err := container.MappedPort(ctx, WireMockHttpsPort) |
| 117 | + wireMockHttpUrl := "http://" + Localhost + ":" + mappedHttpPort.Port() |
| 118 | + wireMockHttpsUrl := "https://" + Localhost + ":" + mappedHttpsPort.Port() |
| 119 | + // Create HTTP client |
| 120 | + httpClient := createClient(t) |
| 121 | + |
| 122 | + t.Run("Test HTTP GET dependency", func(t *testing.T) { |
| 123 | + response, err := httpClient.Get(wireMockHttpUrl + "/com/foo/bar/1.0/bar-1.0.pom") |
| 124 | + if err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + if response.StatusCode != http.StatusOK { |
| 128 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusOK) |
| 129 | + } |
| 130 | + defer response.Body.Close() |
| 131 | + pom, err := io.ReadAll(response.Body) |
| 132 | + if err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + hash := getMd5Hash(pom) |
| 136 | + if hash != Md5Hash { |
| 137 | + t.Fatalf("Actual MD5 hash %s did not match expected MD5 hash %s", hash, Md5Hash) |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + t.Run("Test HTTPS GET dependency", func(t *testing.T) { |
| 142 | + response, err := httpClient.Get(wireMockHttpsUrl + "/com/foo/bar/1.0/bar-1.0.pom") |
| 143 | + if err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | + if response.StatusCode != http.StatusOK { |
| 147 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusOK) |
| 148 | + } |
| 149 | + defer response.Body.Close() |
| 150 | + pom, err := io.ReadAll(response.Body) |
| 151 | + if err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + hash := getMd5Hash(pom) |
| 155 | + if hash != Md5Hash { |
| 156 | + t.Fatalf("Actual MD5 hash %s did not match expected MD5 hash %s", hash, Md5Hash) |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("Test HTTP GET non-existent dependency", func(t *testing.T) { |
| 161 | + response, err := httpClient.Get(wireMockHttpUrl + "/com/foo/bar/1.0/bar-2.0.pom") |
| 162 | + if err != nil { |
| 163 | + t.Fatal(err) |
| 164 | + } |
| 165 | + if response.StatusCode != http.StatusNotFound { |
| 166 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusNotFound) |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + t.Run("Test HTTPS GET non-existent dependency", func(t *testing.T) { |
| 171 | + response, err := httpClient.Get(wireMockHttpsUrl + "/com/foo/bar/1.0/bar-2.0.pom") |
| 172 | + if err != nil { |
| 173 | + t.Fatal(err) |
| 174 | + } |
| 175 | + if response.StatusCode != http.StatusNotFound { |
| 176 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusNotFound) |
| 177 | + } |
| 178 | + }) |
| 179 | + |
| 180 | + t.Run("Test HTTP non-whitelisted host", func(t *testing.T) { |
| 181 | + response, err := httpClient.Get("http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.4.1/maven-jar-plugin-3.4.1.jar") |
| 182 | + if err != nil { |
| 183 | + t.Fatal(err) |
| 184 | + } |
| 185 | + if response.StatusCode != http.StatusForbidden { |
| 186 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusForbidden) |
| 187 | + } |
| 188 | + }) |
| 189 | + |
| 190 | + t.Run("Test HTTPS non-whitelisted host", func(t *testing.T) { |
| 191 | + _, err := httpClient.Get("https://repo1.maven.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.4.1/maven-jar-plugin-3.4.1.jar") |
| 192 | + statusText := http.StatusText(http.StatusForbidden) |
| 193 | + if !strings.Contains(err.Error(), statusText) { |
| 194 | + t.Fatalf("Actual error %s did not contain expected HTTP status text %s", err.Error(), statusText) |
| 195 | + } |
| 196 | + }) |
| 197 | + |
| 198 | + t.Run("Test HTTP non-existent host", func(t *testing.T) { |
| 199 | + response, err := httpClient.Get("http://foo.bar") |
| 200 | + if err != nil { |
| 201 | + t.Fatal(err) |
| 202 | + } |
| 203 | + if response.StatusCode != http.StatusBadGateway { |
| 204 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusBadGateway) |
| 205 | + } |
| 206 | + }) |
| 207 | + |
| 208 | + t.Run("Test HTTPS non-existent host", func(t *testing.T) { |
| 209 | + _, err := httpClient.Get("https://foo.bar") |
| 210 | + statusText := http.StatusText(http.StatusBadGateway) |
| 211 | + if !strings.Contains(err.Error(), statusText) { |
| 212 | + t.Fatalf("Actual error %s did not contain expected HTTP status text %s", err.Error(), statusText) |
| 213 | + } |
| 214 | + }) |
| 215 | + |
| 216 | + t.Run("Test HTTP HEAD dependency", func(t *testing.T) { |
| 217 | + response, err := httpClient.Head(wireMockHttpUrl + "/com/foo/bar/1.0/bar-1.0.pom") |
| 218 | + if err != nil { |
| 219 | + t.Fatal(err) |
| 220 | + } |
| 221 | + actualContentLength := response.Header.Get("Content-Length") |
| 222 | + if actualContentLength != ContentLength { |
| 223 | + t.Fatalf("Actual content length %s did not match expected content length %s", actualContentLength, ContentLength) |
| 224 | + } |
| 225 | + }) |
| 226 | + |
| 227 | + t.Run("Test HTTPS HEAD dependency", func(t *testing.T) { |
| 228 | + response, err := httpClient.Head(wireMockHttpsUrl + "/com/foo/bar/1.0/bar-1.0.pom") |
| 229 | + if err != nil { |
| 230 | + t.Fatal(err) |
| 231 | + } |
| 232 | + actualContentLength := response.Header.Get("Content-Length") |
| 233 | + if actualContentLength != ContentLength { |
| 234 | + t.Fatalf("Actual content length %s did not match expected content length %s", actualContentLength, ContentLength) |
| 235 | + } |
| 236 | + }) |
| 237 | + |
| 238 | + t.Run("Test HTTP HEAD non-existent dependency", func(t *testing.T) { |
| 239 | + response, err := httpClient.Head(wireMockHttpUrl + "/com/foo/bar/1.0/bar-2.0.pom") |
| 240 | + if err != nil { |
| 241 | + t.Fatal(err) |
| 242 | + } |
| 243 | + if response.StatusCode != http.StatusNotFound { |
| 244 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusNotFound) |
| 245 | + } |
| 246 | + }) |
| 247 | + |
| 248 | + t.Run("Test HTTPS HEAD non-existent dependency", func(t *testing.T) { |
| 249 | + response, err := httpClient.Head(wireMockHttpsUrl + "/com/foo/bar/1.0/bar-2.0.pom") |
| 250 | + if err != nil { |
| 251 | + t.Fatal(err) |
| 252 | + } |
| 253 | + if response.StatusCode != http.StatusNotFound { |
| 254 | + t.Fatalf("Actual HTTP status %d did not match expected HTTP status %d", response.StatusCode, http.StatusNotFound) |
| 255 | + } |
| 256 | + }) |
| 257 | +} |
0 commit comments