Skip to content

Commit 4c466f7

Browse files
committed
add debugging messges
1 parent da8c284 commit 4c466f7

File tree

2 files changed

+164
-5
lines changed

2 files changed

+164
-5
lines changed

src/test/java/app/component/Operator.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import lombok.Getter;
1212
import okhttp3.Request;
1313
import okhttp3.RequestBody;
14+
import org.junit.platform.commons.logging.Logger;
15+
import org.junit.platform.commons.logging.LoggerFactory;
1416

1517
import javax.crypto.*;
1618
import javax.crypto.spec.GCMParameterSpec;
@@ -68,6 +70,7 @@ private record V2Envelope(String envelope, byte[] nonce) {
6870
// When running via IntelliJ, environment variables are defined in the uid2-dev-workspace repo under .idea/runConfigurations.
6971
// Test data is defined in the uid2-admin repo.
7072

73+
private static final Logger LOGGER = LoggerFactory.getLogger(Operator.class);
7174
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
7275
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
7376
private static final int TIMESTAMP_LENGTH = 8;
@@ -78,6 +81,27 @@ private record V2Envelope(String envelope, byte[] nonce) {
7881

7982
public static final String CLIENT_API_KEY = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_KEY);
8083
public static final String CLIENT_API_SECRET = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_SECRET);
84+
85+
static {
86+
// Log operator configuration at class initialization
87+
String maskedKey = CLIENT_API_KEY != null && CLIENT_API_KEY.length() > 20
88+
? CLIENT_API_KEY.substring(0, 10) + "..." + CLIENT_API_KEY.substring(CLIENT_API_KEY.length() - 10)
89+
: "[null or too short]";
90+
String maskedSecret = CLIENT_API_SECRET != null && CLIENT_API_SECRET.length() > 20
91+
? CLIENT_API_SECRET.substring(0, 10) + "..." + CLIENT_API_SECRET.substring(CLIENT_API_SECRET.length() - 10)
92+
: "[null or too short]";
93+
LOGGER.info(() -> String.format(
94+
"[OPERATOR CONFIG] Initialized with:%n" +
95+
" CLIENT_API_KEY: %s (length: %d)%n" +
96+
" CLIENT_API_SECRET: %s (length: %d)%n" +
97+
" E2E_ENV: %s%n" +
98+
" IDENTITY_SCOPE: %s",
99+
maskedKey, CLIENT_API_KEY != null ? CLIENT_API_KEY.length() : 0,
100+
maskedSecret, CLIENT_API_SECRET != null ? CLIENT_API_SECRET.length() : 0,
101+
EnvUtil.getEnv(Const.Config.ENV, false),
102+
EnvUtil.getEnv(Const.Config.IDENTITY_SCOPE, false)
103+
));
104+
}
81105

82106
// Local only - Sharing
83107
public static final String CLIENT_API_KEY_SHARING_RECIPIENT = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_KEY_SHARING_RECIPIENT, EnabledCondition.isLocal());
@@ -274,9 +298,55 @@ public IdentityMapResponse v2IdentityMap(IdentityMapInput input) {
274298

275299
// Need to use the manual mapping for error cases - SDK won't allow creating input with bad emails
276300
public JsonNode v3IdentityMap(String payload) throws Exception {
301+
String baseUrl = getBaseUrl();
302+
String endpoint = baseUrl + "/v3/identity/map";
303+
304+
LOGGER.info(() -> String.format(
305+
"[v3IdentityMap] Preparing request:%n" +
306+
" Operator Name: %s%n" +
307+
" Operator Type: %s%n" +
308+
" Base URL: %s%n" +
309+
" Full Endpoint: %s%n" +
310+
" CLIENT_API_KEY: %s (length: %d)%n" +
311+
" CLIENT_API_SECRET: %s (length: %d)%n" +
312+
" Payload (raw): %s",
313+
getName(), type, baseUrl, endpoint,
314+
CLIENT_API_KEY != null ? CLIENT_API_KEY.substring(0, Math.min(20, CLIENT_API_KEY.length())) + "..." : "[null]",
315+
CLIENT_API_KEY != null ? CLIENT_API_KEY.length() : 0,
316+
CLIENT_API_SECRET != null ? CLIENT_API_SECRET.substring(0, Math.min(20, CLIENT_API_SECRET.length())) + "..." : "[null]",
317+
CLIENT_API_SECRET != null ? CLIENT_API_SECRET.length() : 0,
318+
payload != null && payload.length() > 200 ? payload.substring(0, 200) + "..." : payload
319+
));
320+
277321
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
278-
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v3/identity/map", envelope.envelope(), CLIENT_API_KEY);
279-
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
322+
323+
LOGGER.info(() -> String.format(
324+
"[v3IdentityMap] Created envelope:%n" +
325+
" Envelope length: %d%n" +
326+
" Nonce length: %d",
327+
envelope.envelope().length(),
328+
envelope.nonce().length
329+
));
330+
331+
try {
332+
String encryptedResponse = HttpClient.post(endpoint, envelope.envelope(), CLIENT_API_KEY);
333+
LOGGER.info(() -> String.format(
334+
"[v3IdentityMap] Request successful, response length: %d",
335+
encryptedResponse != null ? encryptedResponse.length() : 0
336+
));
337+
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
338+
} catch (Exception e) {
339+
final String errorMsg = e.getMessage();
340+
final String errorType = e.getClass().getName();
341+
LOGGER.error(() -> String.format(
342+
"[v3IdentityMap] Request failed:%n" +
343+
" Endpoint: %s%n" +
344+
" Error: %s%n" +
345+
" Error Type: %s",
346+
endpoint, errorMsg, errorType
347+
));
348+
throw e;
349+
}
280350
}
281351

282352
public IdentityMapV3Response v3IdentityMap(IdentityMapV3Input input) {

src/test/java/common/HttpClient.java

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.uid2.shared.util.Mapper;
66
import lombok.Getter;
77
import okhttp3.*;
8+
import org.junit.platform.commons.logging.Logger;
9+
import org.junit.platform.commons.logging.LoggerFactory;
810

911
import java.util.Map;
10-
import java.util.Objects;
1112

1213
public final class HttpClient {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);
1315
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
1416

1517
public static final OkHttpClient RAW_CLIENT = new OkHttpClient();
@@ -71,11 +73,98 @@ public static String post(String url, String body, String bearerToken) throws Ex
7173
}
7274

7375
public static String execute(Request request, HttpMethod method) throws Exception {
76+
final String url = request.url().toString();
77+
final String requestBody = extractRequestBody(request);
78+
final String authHeader = extractAuthHeader(request);
79+
final Headers headers = request.headers();
80+
81+
LOGGER.info(() -> String.format(
82+
"[HTTP REQUEST] %s %s%n" +
83+
" Authorization: %s%n" +
84+
" Request Body: %s%n" +
85+
" Headers: %s",
86+
method, url,
87+
authHeader,
88+
requestBody,
89+
headers.toString()
90+
));
91+
7492
try (Response response = RAW_CLIENT.newCall(request).execute()) {
93+
final ResponseBody body = response.body();
94+
final String responseBody = extractResponseBody(body);
95+
final int statusCode = response.code();
96+
final String statusMessage = response.message();
97+
final Headers responseHeaders = response.headers();
98+
99+
LOGGER.info(() -> String.format(
100+
"[HTTP RESPONSE] %s %s%n" +
101+
" Status: %d %s%n" +
102+
" Response Headers: %s%n" +
103+
" Response Body: %s",
104+
method, url,
105+
statusCode, statusMessage,
106+
responseHeaders.toString(),
107+
responseBody
108+
));
109+
75110
if (!response.isSuccessful()) {
76-
throw new HttpException(method, request.url().toString(), response.code(), response.message(), Objects.requireNonNull(response.body()).string());
111+
LOGGER.error(() -> String.format(
112+
"[HTTP ERROR] Request failed: %s %s - Status: %d %s - Response: %s",
113+
method, url, statusCode, statusMessage, responseBody
114+
));
115+
throw new HttpException(method, url, statusCode, statusMessage, responseBody);
77116
}
78-
return Objects.requireNonNull(response.body()).string();
117+
return responseBody;
118+
}
119+
}
120+
121+
private static String extractRequestBody(Request request) {
122+
if (request.body() == null) {
123+
return "[empty]";
124+
}
125+
try {
126+
okio.Buffer buffer = new okio.Buffer();
127+
request.body().writeTo(buffer);
128+
String body = buffer.readUtf8();
129+
// Truncate very long bodies for readability
130+
if (body.length() > 500) {
131+
return body.substring(0, 500) + "... [truncated, length=" + body.length() + "]";
132+
}
133+
return body;
134+
} catch (Exception e) {
135+
return "[unable to read request body: " + e.getMessage() + "]";
136+
}
137+
}
138+
139+
private static String extractAuthHeader(Request request) {
140+
Headers headers = request.headers();
141+
for (int i = 0; i < headers.size(); i++) {
142+
if ("Authorization".equalsIgnoreCase(headers.name(i))) {
143+
String authValue = headers.value(i);
144+
// Mask the token for security, but show first/last few chars
145+
if (authValue.length() > 20) {
146+
return authValue.substring(0, 10) + "..." + authValue.substring(authValue.length() - 10);
147+
} else {
148+
return "[masked]";
149+
}
150+
}
151+
}
152+
return "[none]";
153+
}
154+
155+
private static String extractResponseBody(ResponseBody body) {
156+
if (body == null) {
157+
return "[empty]";
158+
}
159+
try {
160+
String bodyString = body.string();
161+
// Truncate very long responses
162+
if (bodyString.length() > 1000) {
163+
return bodyString.substring(0, 1000) + "... [truncated, length=" + bodyString.length() + "]";
164+
}
165+
return bodyString;
166+
} catch (Exception e) {
167+
return "[unable to read response body: " + e.getMessage() + "]";
79168
}
80169
}
81170

0 commit comments

Comments
 (0)