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

feat: Proxy support in GitHubTokenService #931

Merged
merged 2 commits into from
Oct 15, 2024
Merged
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
@@ -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();
}
}
}
Loading