Skip to content

Commit

Permalink
Add Auth header for heartbeat and don't wait for access token (#548) (#…
Browse files Browse the repository at this point in the history
…549)

Co-authored-by: Dustin Kut <thescouser89@users.noreply.github.com>
  • Loading branch information
vibe13 and thescouser89 authored Nov 7, 2023
1 parent 76f2727 commit 6b9eb45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -166,11 +167,15 @@ private AnalyzeResponse createAnalyzeResponse(String id) throws URISyntaxExcepti
}

private boolean performCallback(org.jboss.pnc.api.dto.Request callback, AnalysisReport result) {

addAuthenticationHeaderToCallback(callback);

try {
addAuthenticationHeaderToCallback(callback);
httpClient.performHttpRequest(callback, result);
return true;
} catch (Exception e) {
LOGGER.warn("Exception when performing Callback with: " + e.toString());
LOGGER.warn("Retrying");
try {
httpClient.performHttpRequest(callback, result);
return true;
Expand Down Expand Up @@ -236,7 +241,7 @@ private static void mergeHttpHeaders(Request request, Map<String, String> httpHe
}

private void addAuthenticationHeaderToCallback(org.jboss.pnc.api.dto.Request callback) {
String accessToken = oidcClient.getTokens().await().indefinitely().getAccessToken();
String accessToken = oidcClient.getTokens().await().atMost(Duration.ofMinutes(1)).getAccessToken();
List<Request.Header> headers = callback.getHeaders();
headers.add(new Request.Header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package org.jboss.pnc.deliverablesanalyzer.rest;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.core.HttpHeaders;

import org.jboss.pnc.api.dto.HeartbeatConfig;
import org.jboss.pnc.api.dto.Request;
Expand All @@ -30,6 +34,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.quarkus.oidc.client.OidcClient;

/**
* Service, which performs regular heartbeat requests based using subscription style
*
Expand All @@ -48,6 +54,9 @@ public class HeartbeatScheduler {
@Inject
HttpClient httpClient;

@Inject
OidcClient oidcClient;

private Map<String, Future<?>> subscribedRequests = new ConcurrentHashMap<>();

public void subscribeRequest(String id, HeartbeatConfig heartbeatConfig) {
Expand All @@ -67,7 +76,24 @@ public void unsubscribeRequest(String id) {
}

private void sendHeartbeat(Request heartbeatRequest) {

try {
List<Request.Header> headers = heartbeatRequest.getHeaders();
List<Request.Header> existingAuthHeaders = new ArrayList<>();

for (Request.Header header : headers) {
if (header.getName().equals(HttpHeaders.AUTHORIZATION)) {
existingAuthHeaders.add(header);
}
}

// remove any Authorization headers
headers.removeAll(existingAuthHeaders);

// Add an authorization header with a fresh token
String accessToken = oidcClient.getTokens().await().atMost(Duration.ofMinutes(1)).getAccessToken();
headers.add(new Request.Header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken));

this.httpClient.performHttpRequest(heartbeatRequest);
} catch (Exception e) {
LOGGER.warn("Heartbeat failed with an exception!", e);
Expand Down

0 comments on commit 6b9eb45

Please sign in to comment.