Skip to content

Commit

Permalink
fix header stuff for gcp cloud task (#171)
Browse files Browse the repository at this point in the history
* fix header stuff for gcp cloud task

* fixes for preconditions check

---------

Co-authored-by: Dean Hiller <[email protected]>
  • Loading branch information
deanhiller and deantray authored Apr 10, 2023
1 parent cd72168 commit 05a8a9d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public HttpsJsonClient(
this.schedulerSvc = schedulerSvc;
this.masker = masker;

Context.checkForDuplicates(listHeaders);

for(PlatformHeaders header : listHeaders) {
if(header.isSecured()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,39 @@ public class Context {

private static ThreadLocal<Map<String,Object>> context = ThreadLocal.withInitial(() -> new HashMap<>());

public static boolean wasDupsChecked = false;
/**
* We use header as the key unless null and then use mdc as the key. Do not allow duplication
* including mdc can't use a header name if used as the key.
*/
public static void checkForDuplicates(List<PlatformHeaders> platformHeaders) {
Map<String, PlatformHeaders> keyToHeader = new HashMap<>();
public synchronized static void checkForDuplicates(List<PlatformHeaders> platformHeaders) {
if(wasDupsChecked) //only check once
return;

Map<String, PlatformHeaders> headerKeyToHeader = new HashMap<>();
Map<String, PlatformHeaders> mdcKeyToHeader = new HashMap<>();

for(PlatformHeaders header : platformHeaders) {
if(header.getHeaderName() == null && header.getLoggerMDCKey() == null) {
throw new IllegalArgumentException("Either header or MDC must contain a value. both cannot be null");
}
PlatformHeaders existingFromHeader = keyToHeader.get(header.getHeaderName());
PlatformHeaders existingFromMdc = keyToHeader.get(header.getLoggerMDCKey());
PlatformHeaders existingFromHeader = null;
if(header.getHeaderName() != null)
existingFromHeader = headerKeyToHeader.get(header.getHeaderName());
PlatformHeaders existingFromMdc = null;
if(header.getLoggerMDCKey() != null)
existingFromMdc = mdcKeyToHeader.get(header.getLoggerMDCKey());

if(existingFromHeader != null) {
throw new IllegalArgumentException("Duplicate in Platform headers not allowed. key="
+header.getHeaderName()+" exists in "+tuple(existingFromHeader)+" and in "+tuple(header));
} else if(existingFromMdc != null) {
throw new IllegalArgumentException("Duplicate in Platform headers not allowed. key="
+header.getHeaderName()+" exists in "+tuple(existingFromHeader)+" and in "+tuple(header));
+header.getHeaderName()+" exists in "+tuple(existingFromMdc)+" and in "+tuple(header));
}

headerKeyToHeader.put(header.getHeaderName(), header);
mdcKeyToHeader.put(header.getLoggerMDCKey(), header);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,25 @@ public class GCPTaskClient {

private static Logger log = LoggerFactory.getLogger(GCPTaskClient.class);

@Inject(optional = true)
private ClientServiceConfig clientServiceConfig;
private final GCPCloudTaskConfig config;
private CloudTasksClient cloudTasksClient;
private final Set<String> secureList = new HashSet<>();
private final Set<PlatformHeaders> toTransfer = new HashSet<>();
@Inject
public GCPTaskClient(
GCPCloudTaskConfig config,
CloudTasksClient cloudTasksClient) {
CloudTasksClient cloudTasksClient,
ClientServiceConfig clientServiceConfig
) {
this.config = config;
this.cloudTasksClient = cloudTasksClient;

List<PlatformHeaders> listHeaders;
if(clientServiceConfig == null)
listHeaders = Collections.emptyList();
else if(clientServiceConfig.getHcl() == null)
if(clientServiceConfig.getHcl() == null)
throw new IllegalArgumentException("clientServiceConfig.getHcl() cannot be null and was");
else
listHeaders = clientServiceConfig.getHcl().listHeaderCtxPairs();

List<PlatformHeaders> listHeaders = clientServiceConfig.getHcl().listHeaderCtxPairs();

Context.checkForDuplicates(listHeaders);

for(PlatformHeaders header : listHeaders) {
if(header.isSecured())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public class HttpClientWrapper {
protected Http2Client client;
protected ScheduledExecutorService schedulerSvc;

@Inject(optional = true)
private ClientServiceConfig clientServiceConfig;

private FutureHelper futureUtil;

private final Set<String> secureList = new HashSet<>();
Expand All @@ -64,6 +61,7 @@ public class HttpClientWrapper {
@Inject
public HttpClientWrapper(
HttpsConfig httpsConfig,
ClientServiceConfig clientServiceConfig,
Http2Client client,
FutureHelper futureUtil,
Masker masker
Expand All @@ -73,15 +71,12 @@ public HttpClientWrapper(
this.client = client;
this.futureUtil = futureUtil;


List<PlatformHeaders> listHeaders;
if(clientServiceConfig == null)
listHeaders = Collections.emptyList();
else if(clientServiceConfig.getHcl() == null)
if(clientServiceConfig.getHcl() == null)
throw new IllegalArgumentException("clientServiceConfig.getHcl() cannot be null and was");
else
listHeaders = clientServiceConfig.getHcl().listHeaderCtxPairs();

List<PlatformHeaders> listHeaders = clientServiceConfig.getHcl().listHeaderCtxPairs();

Context.checkForDuplicates(listHeaders);

for(PlatformHeaders header : listHeaders) {
if(header.isSecured())
Expand Down

0 comments on commit 05a8a9d

Please sign in to comment.