Skip to content

Commit

Permalink
fix: adapt grpc token
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar committed Dec 11, 2024
1 parent 2eb56f1 commit 8f50850
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ PORT=3000
GRPC_PORT=5000
ENABLE_ORM_LOGS=true
ENABLE_DOCUMENTATION=true
SERVICE_AUTH_TOKEN="abc"


#== MONGO
Expand All @@ -28,4 +27,4 @@ REDIS_URI=""

#== TELEGRAM
TELEGRAM_BOT_TOKEN=""
TELEGRAM_CHAT_ID=""
TELEGRAM_CHAT_ID=""
2 changes: 1 addition & 1 deletion src/modules/grpc/gen/ts/immortal.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/modules/grpc/gen/ts/kraken.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/modules/grpc/proto/kraken.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ message registerServiceRequest {
message registerServiceResponse {
bool success = 1;
optional string message = 2;
string token = 3;
}

message EmptyRequest {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,29 @@ export class ServiceRegistryGrpcController {
private readonly apiConfig: ApiConfigService,
) {}

async registerService(
{ heartbeatDurationInSec, type, url, region }: registerServiceRequest,
metadata?: Metadata,
call?: ServerUnaryCall<unknown, unknown>,
): Promise<registerServiceResponse> {
async registerService({
heartbeatDurationInSec,
type,
url,
region,
}: registerServiceRequest): Promise<registerServiceResponse> {
try {
const token = metadata?.getMap().token?.toString();

if (!token) {
throw new Error('Missing authentication token in metadata.');
}

const isValidServiceAuthToken = this.serviceRegistryService.isValidServiceAuthToken(token);

if (!isValidServiceAuthToken) {
throw new Error('Invalid authentication token.');
}

const serviceTypeKey = ServiceTypeEnum[type];

if (!serviceTypeKey || !(serviceTypeKey in ServiceType)) {
throw new Error(`Invalid service type: ${type}`);
}

const { token: newToken } = await this.serviceRegistryService.register({
const { token } = await this.serviceRegistryService.register({
heartbeatDurationInSec,
url,
type: ServiceType[serviceTypeKey as keyof typeof ServiceType],
region,
});

const responseMetadata = new Metadata();
responseMetadata.add('token', newToken);

if (call) {
call.sendMetadata(responseMetadata);
}

return {
success: true,
token: token,

Check failure on line 41 in src/modules/service-registry/controllers/service-registry-grpc.controller.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/service-registry/controllers/service-registry-grpc.controller.ts#L41

Expected property shorthand (object-shorthand)
};
} catch (error) {
const err = error as { message: string; stack: string };
Expand All @@ -64,6 +47,7 @@ export class ServiceRegistryGrpcController {
success: false,
message: err.message || 'An unknown error occurred.',
...(this.apiConfig.isDevelopment && { details: err.stack }),
token: '',
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ export default class ServiceRegistryService extends EventEmitter {
return this.serviceRegistryRepository.findOne({ where: { token } });
}

isValidServiceAuthToken(token: string) {
return token === this.apiConfig.serviceAuthToken;
}

Check failure on line 39 in src/modules/service-registry/services/service-registry.service.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/service-registry/services/service-registry.service.ts#L39

Delete `⏎` (prettier/prettier)

Check failure on line 39 in src/modules/service-registry/services/service-registry.service.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/service-registry/services/service-registry.service.ts#L39

More than 1 blank line not allowed (no-multiple-empty-lines)
generateApiKey(serviceType: string, region: string): string {
const timestamp = Date.now().toString(36);
Expand Down
4 changes: 0 additions & 4 deletions src/shared/services/api-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ export class ApiConfigService {
};
}

get serviceAuthToken() {
return this.getString('SERVICE_AUTH_TOKEN');
}

private get(key: string): string {
const value = this.configService.get<string>(key);

Expand Down

0 comments on commit 8f50850

Please sign in to comment.