Skip to content

Commit 8be55af

Browse files
author
Mateusz Czeladka
committed
chore: added debug for token fetching.
1 parent 4384abe commit 8be55af

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

.env.docker-compose

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ PEER_DISCOVERY=false
114114
## Token Registry
115115
# Token registry is enabled for mainnet
116116
TOKEN_REGISTRY_ENABLED=true
117-
TOKEN_REGISTRY_BASE_URL=https://tokens.cardano.org/api
117+
#TOKEN_REGISTRY_BASE_URL=https://tokens.cardano.org/api
118118
TOKEN_REGISTRY_CACHE_TTL_HOURS=12
119119
TOKEN_REGISTRY_LOGO_FETCH=false
120120
TOKEN_REGISTRY_REQUEST_TIMEOUT_SECONDS=2
121+
122+
# For testing only - disable SSL verification
123+
cardano.rosetta.HTTP_SSL_VERIFY=false
124+
125+
# Use IP address instead of hostname
126+
cardano.rosetta.TOKEN_REGISTRY_BASE_URL=https://35.158.243.250:30443/api
127+
128+
# Keep the Host header pointing to the actual hostname
129+
cardano.rosetta.TOKEN_REGISTRY_HOST=tokens.cardano.org

api/src/main/java/org/cardanofoundation/rosetta/RosettaApiApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
public class RosettaApiApplication {
3434

3535
public static void main(String[] args) {
36+
System.setProperty("jdk.httpclient.allowRestrictedHeaders", "host");
37+
3638
SpringApplication.run(RosettaApiApplication.class, args);
3739
}
3840

api/src/main/java/org/cardanofoundation/rosetta/client/CachingTokenRegistryHttpGatewayImpl.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class CachingTokenRegistryHttpGatewayImpl implements TokenRegistryHttpGat
3939
@Value("${cardano.rosetta.TOKEN_REGISTRY_BASE_URL:https://tokens.cardano.org/api}")
4040
protected String tokenRegistryBaseUrl;
4141

42+
@Value("${cardano.rosetta.TOKEN_REGISTRY_HOST:tokens.cardano.org}")
43+
protected String tokenRegistryHost;
44+
4245
@Value("${cardano.rosetta.TOKEN_REGISTRY_REQUEST_TIMEOUT_SECONDS:2}") // aggressive timeout as we do not want to block the request
4346
protected int httpRequestTimeoutSeconds;
4447

@@ -65,6 +68,9 @@ public Map<String, Optional<TokenSubject>> getTokenMetadataBatch(@NonNull Set<St
6568
return Collections.emptyMap();
6669
}
6770

71+
72+
73+
6874
// Check cache for existing entries
6975
Set<String> subjectsToFetch = new HashSet<>();
7076
Map<String, Optional<TokenSubject>> result = new HashMap<>();
@@ -88,8 +94,7 @@ public Map<String, Optional<TokenSubject>> getTokenMetadataBatch(@NonNull Set<St
8894
return result;
8995
}
9096

91-
log.info("Initiating remote token registry HTTP POST request: {} for {} subjects", batchEndpointUrl, subjectsToFetch.size());
92-
log.debug("Subjects to fetch from token registry: {}", subjectsToFetch);
97+
log.info("Initiating remote token registry HTTP POST request: {} for {} subjects: {}", batchEndpointUrl, subjectsToFetch.size(), subjectsToFetch);
9398

9499
Stopwatch stopwatch = Stopwatch.createStarted();
95100

@@ -102,13 +107,16 @@ public Map<String, Optional<TokenSubject>> getTokenMetadataBatch(@NonNull Set<St
102107

103108
// Prepare HTTP request
104109
String requestBody = objectMapper.writeValueAsString(request);
110+
int requestBodyBytes = requestBody.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
111+
log.info("HTTP POST request body size: {} bytes ({} KB)", requestBodyBytes, String.format("%.2f", requestBodyBytes / 1024.0));
105112

106113
HttpRequest httpRequest = HttpRequest.newBuilder()
107114
.uri(URI.create(batchEndpointUrl))
108115
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
109116
.timeout(Duration.ofSeconds(httpRequestTimeoutSeconds))
110117
.header("Content-Type", "application/json")
111118
.header("Accept", "application/json")
119+
.header("Host", tokenRegistryHost)
112120
.build();
113121

114122
// Execute request
@@ -127,12 +135,16 @@ public Map<String, Optional<TokenSubject>> getTokenMetadataBatch(@NonNull Set<St
127135
TokenRegistryBatchResponse batchResponse = objectMapper.readValue(response.body(), TokenRegistryBatchResponse.class);
128136

129137
if (batchResponse.getSubjects() != null) {
138+
List<String> receivedSubjects = new ArrayList<>();
130139
for (TokenSubject tokenSubject : batchResponse.getSubjects()) {
131140
// If subject is in response, cache it as found
132141
result.put(tokenSubject.getSubject(), Optional.of(tokenSubject));
133142
tokenMetadataCache.put(tokenSubject.getSubject(), TokenCacheEntry.found(tokenSubject));
134-
log.debug("Cached token metadata for subject: {}", tokenSubject.getSubject());
143+
receivedSubjects.add(tokenSubject.getSubject());
135144
}
145+
log.info("Token registry returned {} subjects: {}", receivedSubjects.size(), receivedSubjects);
146+
} else {
147+
log.info("Token registry returned no subjects");
136148
}
137149

138150
// Cache empty results for subjects that were requested but not found in the response
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
11
package org.cardanofoundation.rosetta.config;
22

33
import java.net.http.HttpClient;
4+
import java.security.KeyManagementException;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.cert.X509Certificate;
47
import java.time.Duration;
58
import java.util.concurrent.Executors;
9+
import javax.net.ssl.SSLContext;
10+
import javax.net.ssl.TrustManager;
11+
import javax.net.ssl.X509TrustManager;
612

13+
import lombok.extern.slf4j.Slf4j;
714
import org.springframework.beans.factory.annotation.Value;
815
import org.springframework.context.annotation.Bean;
916
import org.springframework.context.annotation.Configuration;
1017

1118
@Configuration
19+
@Slf4j
1220
public class HttpConfig {
1321

1422
@Value("${cardano.rosetta.HTTP_CONNECT_TIMEOUT_SECONDS}")
1523
private int httpConnectTimeoutSeconds;
1624

25+
@Value("${cardano.rosetta.HTTP_SSL_VERIFY:true}")
26+
private boolean sslVerify;
27+
1728
@Bean
1829
public HttpClient httpClient() {
19-
return HttpClient.newBuilder()
30+
HttpClient.Builder builder = HttpClient.newBuilder()
2031
.version(HttpClient.Version.HTTP_2)
2132
.followRedirects(HttpClient.Redirect.NORMAL)
2233
.connectTimeout(Duration.ofSeconds(httpConnectTimeoutSeconds))
23-
.executor(Executors.newVirtualThreadPerTaskExecutor())
24-
.build();
34+
.executor(Executors.newVirtualThreadPerTaskExecutor());
35+
36+
// For testing only - disable SSL verification if configured
37+
if (!sslVerify) {
38+
log.warn("SSL verification is DISABLED - this should only be used for testing!");
39+
try {
40+
SSLContext sslContext = SSLContext.getInstance("TLS");
41+
sslContext.init(null, new TrustManager[]{new InsecureTrustManager()}, new java.security.SecureRandom());
42+
builder.sslContext(sslContext);
43+
} catch (NoSuchAlgorithmException | KeyManagementException e) {
44+
log.error("Failed to create insecure SSL context", e);
45+
}
46+
}
47+
48+
return builder.build();
49+
}
50+
51+
// Trust manager that accepts all certificates - FOR TESTING ONLY
52+
private static class InsecureTrustManager implements X509TrustManager {
53+
@Override
54+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
55+
// Accept all
56+
}
57+
58+
@Override
59+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
60+
// Accept all
61+
}
62+
63+
@Override
64+
public X509Certificate[] getAcceptedIssuers() {
65+
return new X509Certificate[0];
66+
}
2567
}
2668

2769
}

0 commit comments

Comments
 (0)