diff --git a/api/src/main/java/org/terrakube/api/plugin/vcs/provider/github/GitHubTokenService.java b/api/src/main/java/org/terrakube/api/plugin/vcs/provider/github/GitHubTokenService.java index 813dbcbbc..caaa980f9 100644 --- a/api/src/main/java/org/terrakube/api/plugin/vcs/provider/github/GitHubTokenService.java +++ b/api/src/main/java/org/terrakube/api/plugin/vcs/provider/github/GitHubTokenService.java @@ -1,11 +1,12 @@ package org.terrakube.api.plugin.vcs.provider.github; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.RSAPrivateKeySpec; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Base64; @@ -19,6 +20,8 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; @@ -37,6 +40,8 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import lombok.extern.slf4j.Slf4j; +import reactor.netty.http.client.HttpClient; +import reactor.netty.transport.ProxyProvider; @Slf4j @Service @@ -52,17 +57,38 @@ public class GitHubTokenService implements GetAccessToken { ScheduleGitHubAppTokenService scheduleGitHubAppTokenService; public GitHubToken getAccessToken(String clientId, String clientSecret, String tempCode, String callback, - String endpoint) throws TokenException { - WebClient client = WebClient.builder() - .baseUrl((endpoint != null) ? endpoint : DEFAULT_ENDPOINT) - .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) - .build(); + String endpoint) throws TokenException { + HttpClient httpClient; + WebClient client; + if(System.getProperty("http.proxyHost") != null) { + log.info("Using proxy host: {} port: {}", System.getProperty("http.proxyHost"), System.getProperty("http.proxyPort")); + + httpClient = HttpClient.create() + .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP) + .host(System.getProperty("http.proxyHost")) + .port(Integer.parseInt(System.getProperty("http.proxyPort")))); + + client = WebClient.builder() + .baseUrl((endpoint != null)? endpoint : DEFAULT_ENDPOINT) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .clientConnector(new ReactorClientHttpConnector(httpClient)) + .build(); + } else { + log.info("No proxy host specified, using default proxy"); + client = WebClient.builder() + .baseUrl((endpoint != null)? endpoint : DEFAULT_ENDPOINT) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .build(); + } + + + log.info("Calling Github API"); GitHubToken gitHubToken = client.post().uri(uriBuilder -> uriBuilder.path("/login/oauth/access_token") - .queryParam("client_id", clientId) - .queryParam("client_secret", clientSecret) - .queryParam("code", tempCode) - .build()) + .queryParam("client_id", clientId) + .queryParam("client_secret", clientSecret) + .queryParam("code", tempCode) + .build()) .retrieve().bodyToMono(GitHubToken.class).block(); if (gitHubToken != null) @@ -175,8 +201,23 @@ private ResponseEntity callGithubAPI(String body, String apiUrl, HttpMet headers.set("Accept", "application/vnd.github+json"); headers.set("Authorization", "Bearer " + jws); headers.set("X-GitHub-Api-Version", "2022-11-28"); - RestTemplate restTemplate = new RestTemplate(); + RestTemplate restTemplate = getRestTemplateWithProxy(); HttpEntity entity = new HttpEntity<>(body, headers); return restTemplate.exchange(apiUrl, method, entity, String.class); } -} + + public RestTemplate getRestTemplateWithProxy() { + if (System.getProperty("http.proxyHost") != null) { + log.info("RestTemplate proxy host: {} port: {}", System.getProperty("http.proxyHost"), System.getProperty("http.proxyPort")); + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + String proxyHost = System.getProperty("http.proxyHost"); + int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort")); + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); + requestFactory.setProxy(proxy); + return new RestTemplate(requestFactory); + } else { + log.info("No proxy setup"); + return new RestTemplate(); + } + } +} \ No newline at end of file