Skip to content

Commit 890f3d5

Browse files
jcschaffclaude
andcommitted
Desktop client: identify itself on every API request
ClientIdentification sends VCell-Client (vcell-desktop, or the vcell.client.name property for other tools) and VCell-Client-Version on every /api/v0 request (default headers on the Apache client) and every /api/v1 request (an interceptor that runs before the existing one, so the token-renewing interceptor of an authenticated client is kept). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 31afc40)
1 parent 5d04dd3 commit 890f3d5

3 files changed

Lines changed: 134 additions & 1 deletion

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.vcell.api.client;
2+
3+
import java.net.http.HttpRequest;
4+
import java.util.List;
5+
import java.util.function.Consumer;
6+
7+
import org.apache.http.Header;
8+
import org.apache.http.message.BasicHeader;
9+
import org.vcell.util.document.VCellSoftwareVersion;
10+
11+
/**
12+
* Identifies this client on every request, so the server can log which client and version made each
13+
* call (see docs/api-style-guide.md §6.3 and §7.1). Without it, nothing tells us which desktop versions
14+
* still use an endpoint, which is the evidence needed before retiring one.
15+
*/
16+
public final class ClientIdentification {
17+
18+
public static final String CLIENT_HEADER = "VCell-Client";
19+
public static final String VERSION_HEADER = "VCell-Client-Version";
20+
21+
/** Overrides the client name, e.g. for admin tools that reuse this client. */
22+
public static final String CLIENT_NAME_PROPERTY = "vcell.client.name";
23+
static final String DEFAULT_CLIENT_NAME = "vcell-desktop";
24+
static final String UNKNOWN_VERSION = "unknown";
25+
26+
private ClientIdentification() { }
27+
28+
public static String clientName() {
29+
String name = System.getProperty(CLIENT_NAME_PROPERTY);
30+
return (name == null || name.isBlank()) ? DEFAULT_CLIENT_NAME : name.trim();
31+
}
32+
33+
public static String clientVersion() {
34+
try {
35+
String version = VCellSoftwareVersion.fromSystemProperty().getSoftwareVersionString();
36+
return (version == null || version.isBlank()) ? UNKNOWN_VERSION : version.trim();
37+
} catch (RuntimeException e) {
38+
// a development run without the version property still identifies itself
39+
return UNKNOWN_VERSION;
40+
}
41+
}
42+
43+
/** Default headers for the Apache client used for /api/v0. */
44+
public static List<Header> apacheHeaders() {
45+
return List.of(new BasicHeader(CLIENT_HEADER, clientName()), new BasicHeader(VERSION_HEADER, clientVersion()));
46+
}
47+
48+
/**
49+
* A request interceptor for the generated /api/v1 client that adds the identification headers and
50+
* then runs {@code next} (for example the token-renewing interceptor of an authenticated client).
51+
*/
52+
public static Consumer<HttpRequest.Builder> withIdentification(Consumer<HttpRequest.Builder> next) {
53+
String name = clientName();
54+
String version = clientVersion();
55+
return builder -> {
56+
builder.header(CLIENT_HEADER, name);
57+
builder.header(VERSION_HEADER, version);
58+
if (next != null) {
59+
next.accept(builder);
60+
}
61+
};
62+
}
63+
}

‎vcell-apiclient/src/main/java/org/vcell/api/client/VCellApiClient.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ private void initClient() throws NoSuchAlgorithmException, KeyStoreException, Ke
177177
if (!bSkipSSL){
178178
httpClientBuilder.setSSLSocketFactory(sslsf);
179179
}
180-
httpclient = httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy()).build();
180+
httpclient = httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy())
181+
.setDefaultHeaders(ClientIdentification.apacheHeaders())
182+
.build();
181183
httpClientContext = HttpClientContext.create();
182184
}
183185

@@ -226,12 +228,19 @@ public void createDefaultQuarkusClient(boolean bIgnoreCertProblems){
226228
setBasePath(quarkusURL.getPath());
227229
setScheme(quarkusURL.getProtocol());
228230
}};
231+
identify(apiClient);
232+
}
233+
234+
/** Adds the client identification headers to every /api/v1 request, before any existing interceptor. */
235+
private static void identify(ApiClient client) {
236+
client.setRequestInterceptor(ClientIdentification.withIdentification(client.getRequestInterceptor()));
229237
}
230238

231239
public void authenticate(boolean ignoreSSLCertProblems) throws URISyntaxException, IOException, ParseException, ApiException {
232240
apiClient = InteractiveLogin.login(new URI(InteractiveLogin.authDomain + "/authorize"),
233241
this.quarkusURL.toURI(), ignoreSSLCertProblems);
234242
apiClient.setScheme(this.quarkusURL.getProtocol());
243+
identify(apiClient);
235244
}
236245

237246
public void logOut(){
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.vcell.api.client;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.net.URI;
7+
import java.net.http.HttpRequest;
8+
import java.util.Map;
9+
import java.util.concurrent.atomic.AtomicBoolean;
10+
import java.util.function.Consumer;
11+
import java.util.stream.Collectors;
12+
13+
import org.apache.http.Header;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.Tag;
16+
import org.junit.jupiter.api.Test;
17+
18+
@Tag("Fast")
19+
public class ClientIdentificationTest {
20+
21+
@AfterEach
22+
public void clearOverride() {
23+
System.clearProperty(ClientIdentification.CLIENT_NAME_PROPERTY);
24+
}
25+
26+
@Test
27+
public void v0RequestsCarryTheClientNameAndAVersion() {
28+
Map<String, String> h = ClientIdentification.apacheHeaders().stream()
29+
.collect(Collectors.toMap(Header::getName, Header::getValue));
30+
assertEquals("vcell-desktop", h.get(ClientIdentification.CLIENT_HEADER));
31+
String version = h.get(ClientIdentification.VERSION_HEADER);
32+
assertTrue(version != null && !version.isBlank(), "a version, or 'unknown' without the version property");
33+
}
34+
35+
@Test
36+
public void theClientNameCanBeOverriddenForOtherTools() {
37+
System.setProperty(ClientIdentification.CLIENT_NAME_PROPERTY, " vcell-admin ");
38+
assertEquals("vcell-admin", ClientIdentification.clientName());
39+
}
40+
41+
/** The v1 client already has an interceptor (token renewal); identification must not replace it. */
42+
@Test
43+
public void v1RequestsCarryTheHeadersAndStillRunTheExistingInterceptor() {
44+
AtomicBoolean existingRan = new AtomicBoolean(false);
45+
Consumer<HttpRequest.Builder> existing = b -> { existingRan.set(true); b.header("Authorization", "Bearer t"); };
46+
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("https://example.org/api/v1/x"));
47+
ClientIdentification.withIdentification(existing).accept(builder);
48+
HttpRequest request = builder.build();
49+
assertTrue(existingRan.get(), "the existing interceptor still runs");
50+
assertEquals("vcell-desktop", request.headers().firstValue(ClientIdentification.CLIENT_HEADER).orElse(null));
51+
assertTrue(request.headers().firstValue(ClientIdentification.VERSION_HEADER).isPresent());
52+
assertEquals("Bearer t", request.headers().firstValue("Authorization").orElse(null));
53+
}
54+
55+
@Test
56+
public void v1IdentificationWorksWithoutAnExistingInterceptor() {
57+
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("https://example.org/api/v1/x"));
58+
ClientIdentification.withIdentification(null).accept(builder);
59+
assertTrue(builder.build().headers().firstValue(ClientIdentification.CLIENT_HEADER).isPresent());
60+
}
61+
}

0 commit comments

Comments
 (0)