Skip to content

Commit 5aec8b7

Browse files
committed
Final update
1 parent f459968 commit 5aec8b7

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

.local.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
SENTRIUS_VERSION=1.1.188
1+
SENTRIUS_VERSION=1.1.193
22
SENTRIUS_SSH_VERSION=1.1.35
33
SENTRIUS_KEYCLOAK_VERSION=1.1.47
44
SENTRIUS_AGENT_VERSION=1.1.34
5-
SENTRIUS_AI_AGENT_VERSION=1.1.63
5+
SENTRIUS_AI_AGENT_VERSION=1.1.64
66
LLMPROXY_VERSION=1.0.46
77
LAUNCHER_VERSION=1.0.51
88
AGENTPROXY_VERSION=1.0.66

.local.env.bak

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
SENTRIUS_VERSION=1.1.188
1+
SENTRIUS_VERSION=1.1.193
22
SENTRIUS_SSH_VERSION=1.1.35
33
SENTRIUS_KEYCLOAK_VERSION=1.1.47
44
SENTRIUS_AGENT_VERSION=1.1.34
5-
SENTRIUS_AI_AGENT_VERSION=1.1.63
5+
SENTRIUS_AI_AGENT_VERSION=1.1.64
66
LLMPROXY_VERSION=1.0.46
77
LAUNCHER_VERSION=1.0.51
88
AGENTPROXY_VERSION=1.0.66

ai-agent/src/main/java/io/sentrius/agent/analysis/agents/agents/RegisteredAgent.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,6 @@ public void onApplicationEvent(final ApplicationReadyEvent event) {
102102

103103
log.error(e.getMessage());
104104
log.info("Registering v1.0.2 agent failed. Retrying in 10 seconds...");
105-
106-
try {
107-
var agentName = execution.getUser().getUsername();
108-
var base64PublicKey = agentKeyService.getBase64PublicKey(keyPair.getPublic());
109-
var agentRegistrationDTO = agentClientService.bootstrap(
110-
agentName, base64PublicKey
111-
, keyPair.getPublic().getAlgorithm()
112-
);
113-
114-
var encryptedSecret = agentRegistrationDTO.getClientSecret();
115-
var decryptedSecret = agentKeyService.
116-
decryptWithPrivateKey(encryptedSecret, keyPair.getPrivate());
117-
keycloakService.createKeycloakClient(
118-
agentName,
119-
decryptedSecret
120-
);
121-
122-
user = UserDTO.builder()
123-
.username(zeroTrustClientService.getUsername())
124-
.build();
125-
126-
execution = agentExecutionService.getAgentExecution(user);
127-
} catch (Exception e1) {
128-
log.error("Failed to bootstrap agent", e1);
129-
} catch (ZtatException ex) {
130-
log.error("Failed to bootstrap agent", ex);
131-
}
132105
try {
133106
Thread.sleep(10_000);
134107
} catch (InterruptedException ex) {

core/src/main/java/io/sentrius/sso/core/services/security/JwtUtil.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentrius.sso.core.services.security;
22

3+
import java.nio.charset.StandardCharsets;
34
import java.util.Base64;
45
import java.util.Optional;
56
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -87,26 +88,33 @@ public static Optional<String> getUserTypeName(ObjectNode jwt) {
8788

8889
}
8990

90-
/**
91-
* Extract the 'kid' (Key ID) from a JWT header.
92-
*/
93-
public static String extractKid(String jwt) {
94-
// JWT structure: header.payload.signature
91+
public static String extractKid(String jwt) {
9592
try {
93+
// Strip "Bearer " prefix if present
94+
if (jwt.startsWith("Bearer ")) {
95+
jwt = jwt.substring(7);
96+
}
9697
String[] parts = jwt.split("\\.");
98+
log.info("JWT parts: header={}, payload={}, signaturePresent={}",
99+
parts.length > 0 ? parts[0] : "null",
100+
parts.length > 1 ? parts[1] : "null",
101+
parts.length == 3);
97102
if (parts.length != 3) {
98103
throw new IllegalArgumentException("Invalid JWT token format");
99104
}
100105

101-
var part = parts[0].trim();
102-
String headerJson = new String(Base64.getDecoder().decode(part));
106+
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
103107
var headerNode = JsonUtil.MAPPER.readTree(headerJson);
104108

105-
return headerNode.has("kid") ? headerNode.get("kid").asText() : null;
109+
if (!headerNode.has("kid")) {
110+
throw new RuntimeException("Missing 'kid' in JWT header");
111+
}
112+
113+
return headerNode.get("kid").asText();
106114
} catch (Exception e) {
107-
e.printStackTrace();
108-
log.info("Failed to extract 'kid' from JWT {}", jwt);
115+
log.error("Failed to extract 'kid' from JWT: {}", jwt, e);
109116
throw new RuntimeException("Failed to extract 'kid' from JWT", e);
110117
}
111118
}
119+
112120
}

core/src/main/java/io/sentrius/sso/core/services/security/KeycloakService.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public Map<String, List<String>> getUserAttributes(String userId) {
6060
*/
6161
public boolean validateJwt(String token) {
6262
try {
63+
if (token.startsWith("Bearer ")) {
64+
token = token.substring(7);
65+
}
66+
67+
token = token.trim().replaceAll("\\s+", ""); // remove all whitespace
6368
var kid = JwtUtil.extractKid(token);
6469
Objects.requireNonNull(kid, "No 'kid' found in JWT header");
6570
var publicKey = keycloak.getPublicKey(kid);
@@ -79,6 +84,11 @@ public boolean validateJwt(String token) {
7984
* Extract the client ID (agent identity) from a valid JWT.
8085
*/
8186
public String extractAgentId(String token) {
87+
if (token.startsWith("Bearer ")) {
88+
token = token.substring(7);
89+
}
90+
91+
token = token.trim().replaceAll("\\s+", ""); // remove all whitespace
8292
var kid = JwtUtil.extractKid(token);
8393
Objects.requireNonNull(kid, "No 'kid' found in JWT header");
8494
var publicKey = keycloak.getPublicKey(kid);
@@ -93,6 +103,11 @@ public String extractAgentId(String token) {
93103
}
94104

95105
public String extractUsername(String token) {
106+
if (token.startsWith("Bearer ")) {
107+
token = token.substring(7);
108+
}
109+
110+
token = token.trim().replaceAll("\\s+", ""); // remove all whitespace
96111
var kid = JwtUtil.extractKid(token);
97112
Objects.requireNonNull(kid, "No 'kid' found in JWT header");
98113
var publicKey = keycloak.getPublicKey(kid);
@@ -119,6 +134,13 @@ public void removeAgentClient(String clientId) {
119134
public AgentRegistrationDTO registerAgentClient(AgentRegistrationDTO agent) {
120135
ClientsResource clients = keycloak.getKeycloak().realm(realm).clients();
121136

137+
List<ClientRepresentation> existingClients = clients.findByClientId(agent.getAgentName());
138+
if (!existingClients.isEmpty()) {
139+
String existingClientId = existingClients.get(0).getId();
140+
log.warn("Client with ID '{}' already exists. Removing before re-registration.", agent.getAgentName());
141+
clients.get(existingClientId).remove();
142+
}
143+
122144
// Step 1: Build client representation
123145
ClientRepresentation client = new ClientRepresentation();
124146
client.setClientId(agent.getAgentName());

0 commit comments

Comments
 (0)