Skip to content

Commit

Permalink
Add proxy support to GitHubTokenService (#931)
Browse files Browse the repository at this point in the history
Added support for proxy in the GitHubTokenService. The code now checks if a proxy host is set in system properties and if so, uses it to create the HttpClient. If no proxy host is specified, it falls back to the default settings. Additionally, log messages were introduced to aid in debugging.
  • Loading branch information
alfespa17 authored Oct 15, 2024
1 parent 338ff12 commit 383c2c6
Showing 1 changed file with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -52,17 +57,38 @@ public class GitHubTokenService implements GetAccessToken<GitHubToken> {
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)
Expand Down Expand Up @@ -175,8 +201,23 @@ private ResponseEntity<String> 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<String> 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();
}
}
}

0 comments on commit 383c2c6

Please sign in to comment.