Skip to content

Commit 7ce4e80

Browse files
authored
fix: set user agent (#402)
* fix: set user agent Signed-off-by: Tomasz Janiszewski <tomek@redhat.com> * replace whitespace with _ Signed-off-by: Tomasz Janiszewski <tomek@redhat.com> * fix Signed-off-by: Tomasz Janiszewski <tomek@redhat.com> * fix Signed-off-by: Tomasz Janiszewski <tomek@redhat.com> --------- Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>
1 parent 94ca0a8 commit 7ce4e80

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

stackrox-container-image-scanner/src/main/java/com/stackrox/jenkins/plugins/services/ApiClientFactory.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
import com.google.common.cache.CacheBuilder;
2525
import com.google.common.cache.CacheLoader;
2626
import com.google.common.cache.LoadingCache;
27+
import hudson.PluginWrapper;
28+
import jenkins.model.Jenkins;
2729
import lombok.Data;
30+
import okhttp3.Interceptor;
2831
import okhttp3.OkHttpClient;
2932

3033
import com.stackrox.invoker.ApiClient;
3134

35+
import okhttp3.Response;
36+
import org.jetbrains.annotations.NotNull;
37+
3238
public class ApiClientFactory {
3339

3440
public enum StackRoxTlsValidationMode {
@@ -96,6 +102,7 @@ private static OkHttpClient newHttpClient(@Nullable String caCert, StackRoxTlsVa
96102
builder.connectTimeout(TIMEOUT);
97103
builder.readTimeout(TIMEOUT);
98104
builder.writeTimeout(TIMEOUT);
105+
builder.addNetworkInterceptor(new UserAgentInterceptor());
99106
return builder.build();
100107
}
101108

@@ -156,4 +163,39 @@ private static SSLContext getSslContext(TrustManager[] trustManagers) throws NoS
156163
sslContext.init(null, trustManagers, null);
157164
return sslContext;
158165
}
166+
167+
public static class UserAgentInterceptor implements Interceptor {
168+
private static final String STACKROX_CONTAINER_IMAGE_SCANNER = "stackrox-container-image-scanner";
169+
private static final String VALUE = String.format("%s%s (%s; %s) %s",
170+
STACKROX_CONTAINER_IMAGE_SCANNER,
171+
getVersion(),
172+
System.getProperty("os.name"),
173+
System.getProperty("os.arch"),
174+
"CI"
175+
);
176+
177+
@NotNull
178+
@Override
179+
public Response intercept(@NotNull Chain chain) throws IOException {
180+
return chain.proceed(
181+
chain.request()
182+
.newBuilder()
183+
.header("User-Agent", VALUE)
184+
.build()
185+
);
186+
}
187+
188+
static String getVersion() {
189+
Jenkins jenkins = Jenkins.getInstanceOrNull();
190+
if (jenkins == null) {
191+
return "";
192+
}
193+
194+
PluginWrapper plugin = jenkins.pluginManager.getPlugin(STACKROX_CONTAINER_IMAGE_SCANNER);
195+
if (plugin == null) {
196+
return "";
197+
}
198+
return String.format("/%s:%s", plugin.getVersion(), Jenkins.getVersion()).replaceAll("\\s+", "_");
199+
}
200+
}
159201
}

stackrox-container-image-scanner/src/test/java/com/stackrox/jenkins/plugins/services/ApiClientFactoryTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
44
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
56
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
67
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
78
import static com.stackrox.jenkins.plugins.services.ApiClientFactory.StackRoxTlsValidationMode.VALIDATE;
@@ -36,12 +37,13 @@ class ApiClientFactoryTest {
3637

3738
@BeforeAll
3839
static void beforeAll() {
39-
SERVER.stubFor(get(anyUrl()).willReturn(ok().withBody("{}")));
40+
SERVER.stubFor(get(anyUrl()).withHeader("User-Agent", matching("stackrox-container-image-scanner .* CI")).willReturn(ok().withBody("{}")));
4041
SERVER.start();
4142
}
4243

4344
@AfterAll
4445
static void cleanup() {
46+
4547
SERVER.stop();
4648
}
4749

0 commit comments

Comments
 (0)