Skip to content

Commit 06477dd

Browse files
feat(logs): Add custom header for UI and CLI clients
Add the value of the `X-Client-Type` header for tracking UI and CLI clients to the MDC logs. This can be used to distinguish all API requests originating from the known clients from unknown ones. Configure the CLI as `ort-server-cli` and the UI as `ort-server-ui`. For unknown clients that do not set this header, the value of the `User-Agent` header will be used. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent 9af5da8 commit 06477dd

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

cli/src/commonMain/kotlin/utils/OrtServerClientUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.ktor.client.plugins.auth.Auth
2424
import io.ktor.client.plugins.auth.providers.BearerTokens
2525
import io.ktor.client.plugins.auth.providers.bearer
2626
import io.ktor.client.plugins.defaultRequest
27+
import io.ktor.client.request.header
2728

2829
import org.eclipse.apoapsis.ortserver.cli.COMMAND_NAME
2930
import org.eclipse.apoapsis.ortserver.cli.model.AuthenticationStorage
@@ -35,6 +36,7 @@ import org.eclipse.apoapsis.ortserver.client.auth.AuthService
3536
import org.eclipse.apoapsis.ortserver.client.auth.AuthenticationException
3637
import org.eclipse.apoapsis.ortserver.client.createDefaultHttpClient
3738
import org.eclipse.apoapsis.ortserver.client.createOrtHttpClient
39+
import org.eclipse.apoapsis.ortserver.utils.system.CustomHttpHeaders
3840

3941
/**
4042
* Create an authenticated ORT Server client using the stored authentication. Returns null if the user is not
@@ -53,6 +55,7 @@ fun createUnauthenticatedOrtServerClient(baseUrl: String) = OrtServerClient(crea
5355
private fun createHttpClient(url: String) = createOrtHttpClient(JSON) {
5456
defaultRequest {
5557
url(url)
58+
header(CustomHttpHeaders.ClientType, "ort-server-cli")
5659
}
5760
}
5861

core/src/main/kotlin/plugins/HTTP.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import io.ktor.server.config.ApplicationConfig
2727
import io.ktor.server.plugins.cors.routing.CORS
2828
import io.ktor.server.plugins.defaultheaders.DefaultHeaders
2929

30+
import org.eclipse.apoapsis.ortserver.utils.system.CustomHttpHeaders
31+
3032
import org.koin.ktor.ext.inject
3133

3234
fun Application.configureHTTP() {
@@ -39,6 +41,7 @@ fun Application.configureHTTP() {
3941
install(CORS) {
4042
allowedHosts.split(',').forEach(::allowHost)
4143
allowCredentials = true
44+
allowHeader(CustomHttpHeaders.ClientType)
4245
allowHeader(HttpHeaders.Authorization)
4346
allowHeader(HttpHeaders.ContentType)
4447
allowMethod(HttpMethod.Delete)

core/src/main/kotlin/plugins/Monitoring.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
package org.eclipse.apoapsis.ortserver.core.plugins
2121

22+
import io.ktor.http.HttpHeaders
2223
import io.ktor.server.application.Application
2324
import io.ktor.server.application.install
2425
import io.ktor.server.plugins.calllogging.CallLogging
2526
import io.ktor.server.request.path
2627

2728
import java.util.UUID
2829

30+
import org.eclipse.apoapsis.ortserver.utils.system.CustomHttpHeaders
31+
2932
import org.slf4j.event.Level
3033

3134
fun Application.configureMonitoring() {
@@ -42,5 +45,10 @@ fun Application.configureMonitoring() {
4245
mdc("organizationId") { it.parameters["organizationId"] }
4346
mdc("productId") { it.parameters["productId"] }
4447
mdc("repositoryId") { it.parameters["repositoryId"] }
48+
mdc("clientType") { call ->
49+
call.request.headers[CustomHttpHeaders.ClientType]
50+
?: call.request.headers[HttpHeaders.UserAgent]
51+
?: "unknown"
52+
}
4553
}
4654
}

ui/src/app.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import { routeTree } from '@/routeTree.gen';
4040
client.setConfig({
4141
baseURL: config.API_URL,
4242
auth: () => authRef.current?.user?.access_token,
43+
headers: {
44+
'X-Client-Type': 'ort-server-ui',
45+
},
4346
});
4447

4548
export interface RouterContext {

utils/system/src/commonMain/kotlin/Constants.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ package org.eclipse.apoapsis.ortserver.utils.system
2323
* The version of the ORT Server as a string.
2424
*/
2525
const val ORT_SERVER_VERSION = BuildConfig.ORT_SERVER_VERSION
26+
27+
object CustomHttpHeaders {
28+
/**
29+
* HTTP header name used to identify the client type making API requests.
30+
*/
31+
const val ClientType = "X-Client-Type"
32+
}

0 commit comments

Comments
 (0)