Skip to content

Commit 39c30d4

Browse files
committed
Revert "Avoid socket hangup: enhance Keycloak auth in group service with retry logic; improve error handling in namespace service login"
This reverts commit e964a72.
1 parent 5e8d9c0 commit 39c30d4

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

src/services/keycloak/group-service.ts

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,43 @@ import UserRepresentation from '@keycloak/keycloak-admin-client/lib/defs/userRep
1313
const logger = Logger('kc.group');
1414

1515
export class KeycloakGroupService {
16+
private static instanceCount = 0; // Track total instances created
17+
private instanceId: string;
18+
private createdAt: Date;
1619
private allGroups: any = undefined;
1720
private kcAdminClient: KeycloakAdminClient;
18-
private clientId: string;
19-
private clientSecret: string;
20-
private lastAuthTime: number = 0;
21-
private readonly AUTH_TIMEOUT = 280 * 1000; // 280 seconds (slightly less than typical 5 min token lifetime)
2221

2322
constructor(issuerUrl: string) {
23+
this.instanceId = `kc-group-${++KeycloakGroupService.instanceCount}`;
24+
this.createdAt = new Date();
25+
logger.info('[Instance Created] id=%s, created=%s', this.instanceId, this.createdAt);
26+
2427
const baseUrl = issuerUrl.substr(0, issuerUrl.indexOf('/realms'));
2528
const realmName = issuerUrl.substr(issuerUrl.lastIndexOf('/') + 1);
2629
logger.debug('%s %s', baseUrl, realmName);
2730
this.kcAdminClient = new KcAdminClient({ baseUrl, realmName });
2831
}
2932

30-
private async ensureAuthenticated(): Promise<void> {
31-
if (this.clientId && (Date.now() - this.lastAuthTime > this.AUTH_TIMEOUT)) {
32-
logger.debug('[ensureAuthenticated] Re-authenticating due to timeout');
33-
await this.login(this.clientId, this.clientSecret);
34-
}
35-
}
36-
3733
public async cacheGroups() {
3834
this.allGroups = await this.getAllGroups();
3935
}
4036

4137
public async login(
4238
clientId: string,
43-
clientSecret: string,
44-
retryAttempts: number = 3
39+
clientSecret: string
4540
): Promise<KeycloakGroupService> {
46-
this.clientId = clientId;
47-
this.clientSecret = clientSecret;
48-
49-
const result = await this._login(retryAttempts);
50-
this.lastAuthTime = Date.now();
51-
return result;
52-
}
53-
54-
private async _login(retryAttempts: number): Promise<KeycloakGroupService> {
55-
logger.debug('[login] %s', this.clientId);
41+
logger.debug('[login] %s', clientId);
5642

57-
for (let attempt = 1; attempt <= retryAttempts; attempt++) {
58-
try {
59-
await this.kcAdminClient
60-
.auth({
61-
grantType: 'client_credentials',
62-
clientId: this.clientId,
63-
clientSecret: this.clientSecret,
64-
});
65-
return this;
66-
} catch (err: any) {
67-
if (attempt === retryAttempts) {
68-
logger.error('[login] Login failed after %d attempts: %s', retryAttempts, err);
69-
throw err;
70-
}
71-
logger.warn('[login] Attempt %d failed, retrying: %s', attempt, err);
72-
// Add exponential backoff
73-
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 100));
74-
}
75-
}
43+
await this.kcAdminClient
44+
.auth({
45+
grantType: 'client_credentials',
46+
clientId: clientId,
47+
clientSecret: clientSecret,
48+
})
49+
.catch((err: any) => {
50+
logger.error('[login] Login failed %s', err);
51+
throw err;
52+
});
7653
return this;
7754
}
7855

@@ -100,7 +77,6 @@ export class KeycloakGroupService {
10077
}
10178

10279
public async updateGroup(group: GroupRepresentation): Promise<void> {
103-
await this.ensureAuthenticated();
10480
logger.debug('[updateGroup] %j', group);
10581
await this.kcAdminClient.groups.update({ id: group.id }, group);
10682
}

src/services/org-groups/namespace.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ export class NamespaceService {
1313
}
1414

1515
async login(clientId: string, clientSecret: string) {
16-
try {
17-
await this.groupService.login(clientId, clientSecret);
18-
} catch (err) {
19-
logger.error('[login] Failed to login to Keycloak: %s', err);
20-
throw new Error('Failed to authenticate with Keycloak');
21-
}
16+
await this.groupService.login(clientId, clientSecret);
2217
}
2318

2419
async markNotification(ns: string, viewed: boolean): Promise<void> {

0 commit comments

Comments
 (0)