Skip to content

Commit

Permalink
fix: broker account token events (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbystedt authored Sep 18, 2024
1 parent 4483caf commit 5d93c28
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
15 changes: 6 additions & 9 deletions src/collection/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ export class AccountService {
['token', 'generated'],
);

this.redisService.publish(REDIS_PUBSUB.BROKER_ACCOUNT_TOKEN, {
data: {
clientId: account.clientId,
},
});

return {
token,
};
Expand Down Expand Up @@ -275,15 +281,6 @@ export class AccountService {
await this.addTokenToServiceTools(projectName, serviceName, {
[`broker-jwt:${account.clientId}`]: token,
});
this.redisService.publish(REDIS_PUBSUB.VAULT_SERVICE_TOKEN, {
data: {
clientId: account.clientId,
environment: 'tools',
project: projectName,
service: serviceName,
scmUrl: service.collection.scmUrl,
},
});
} catch (err) {
// Log?
}
Expand Down
2 changes: 1 addition & 1 deletion src/collection/collection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class CollectionController {
@UseGuards(BrokerCombinedAuthGuard)
@ApiBearerAuth()
tokenUpdatedEvents(): Observable<MessageEvent> {
return this.redis.getEventSource(REDIS_PUBSUB.VAULT_SERVICE_TOKEN);
return this.redis.getEventSource(REDIS_PUBSUB.BROKER_ACCOUNT_TOKEN);
}

@Get('service/:id/details')
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const OAUTH2_CLIENT_MAP_USERNAME =

export const REDIS_PUBSUB = {
GRAPH: 'graph',
VAULT_SERVICE_TOKEN: 'vault-service-token',
BROKER_ACCOUNT_TOKEN: 'broker-account-token',
} as const;

export const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ export class InspectorAccountComponent implements OnChanges, OnInit, OnDestroy {
.createAccountTokenEventSource()
.subscribe({
next: (data: any) => {
console.log(data);
// TODO: only update if necessary
this.updateAccount();
if (data.clientId === this.account.clientId) {
this.updateAccount();
}
},
error: (error: any) => {
console.error('Error receiving token update events:', error);
Expand Down Expand Up @@ -105,7 +105,7 @@ export class InspectorAccountComponent implements OnChanges, OnInit, OnDestroy {
.afterClosed()
.subscribe(() => {
this.requestedAccountId = undefined;
this.updateAccount();
//this.updateAccount();
});
}

Expand Down
55 changes: 32 additions & 23 deletions ui/src/app/service/system-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { filter, map, Observable } from 'rxjs';
import { filter, finalize, map, Observable, share } from 'rxjs';
import { SseClient } from 'ngx-sse-client';

import { environment } from '../../environments/environment';
Expand All @@ -16,6 +16,8 @@ export class SystemApiService {
private sseClient: SseClient,
) {}

static accountEventObserver: Observable<any> | null = null;

getAccountTokens(accountId: string) {
return this.http.get<JwtRegistryDto[]>(
`${environment.apiUrl}/v1/collection/broker-account/${accountId}/token`,
Expand Down Expand Up @@ -52,29 +54,36 @@ export class SystemApiService {
}

createAccountTokenEventSource(): Observable<any> {
return this.sseClient
.stream(`${environment.apiUrl}/v1/graph/token-updated`)
.pipe(
filter((event) => {
if (event.type === 'error') {
const errorEvent = event as ErrorEvent;
if (errorEvent.error) {
console.error(errorEvent.error, errorEvent.message);
if (!SystemApiService.accountEventObserver) {
SystemApiService.accountEventObserver = this.sseClient
.stream(`${environment.apiUrl}/v1/collection/broker-account/events`)
.pipe(
filter((event) => {
if (event.type === 'error') {
const errorEvent = event as ErrorEvent;
if (errorEvent.error) {
console.error(errorEvent.error, errorEvent.message);
}
return false;
}
return true;
}),
map((event) => {
if (event.type !== 'error') {
const messageEvent = event as MessageEvent;
//console.info(
// `SSE request with type "${messageEvent.type}" and data "${messageEvent.data}"`,
//);
return JSON.parse(messageEvent.data);
}
return false;
}
return true;
}),
map((event) => {
if (event.type !== 'error') {
const messageEvent = event as MessageEvent;
//console.info(
// `SSE request with type "${messageEvent.type}" and data "${messageEvent.data}"`,
//);
return JSON.parse(messageEvent.data);
}
}),
);
}),
finalize(() => {
SystemApiService.accountEventObserver = null;
}),
share(),
);
}
return SystemApiService.accountEventObserver;
}

getConnectionConfig() {
Expand Down

0 comments on commit 5d93c28

Please sign in to comment.