Skip to content

Commit c0f35b9

Browse files
Merge branch 'Flusinerd-feat/microservice-tcp-tls'
2 parents 467c189 + d4cb393 commit c0f35b9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

content/microservices/basics.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,67 @@ this.client
410410
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
411411
412412
After 5 seconds, if the microservice isn't responding, it will throw an error.
413+
414+
#### TLS support
415+
416+
WWhen communicating outside of a private network, it’s important to encrypt traffic to ensure security. In NestJS, this can be achieved with TLS over TCP using Node's built-in [TLS](https://nodejs.org/api/tls.html) module. Nest provides built-in support for TLS in its TCP transport, allowing us to encrypt communication between microservices or clients.
417+
418+
To enable TLS for a TCP server, you'll need both a private key and a certificate in PEM format. These are added to the server's options by setting the `tlsOptions` and specifying the key and cert files, as shown below:
419+
420+
```typescript
421+
import * as fs from 'fs';
422+
import { NestFactory } from '@nestjs/core';
423+
import { AppModule } from './app.module';
424+
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
425+
426+
async function bootstrap() {
427+
const key = fs.readFileSync('<pathToKeyFile>', 'utf8').toString();
428+
const cert = fs.readFileSync('<pathToCertFile>', 'utf8').toString();
429+
430+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
431+
AppModule,
432+
{
433+
transport: Transport.TCP,
434+
options: {
435+
tlsOptions: {
436+
key,
437+
cert,
438+
},
439+
},
440+
},
441+
);
442+
443+
await app.listen();
444+
}
445+
bootstrap();
446+
```
447+
448+
For a client to communicate securely over TLS, we also define the `tlsOptions` object but this time with the CA certificate. This is the certificate of the authority that signed the server's certificate. This ensures that the client trusts the server's certificate and can establish a secure connection.
449+
450+
```typescript
451+
import { Module } from '@nestjs/common';
452+
import { ClientsModule, Transport } from '@nestjs/microservices';
453+
454+
@Module({
455+
imports: [
456+
ClientsModule.register([
457+
{
458+
name: 'MATH_SERVICE',
459+
transport: Transport.TCP,
460+
options: {
461+
tlsOptions: {
462+
ca: [fs.readFileSync('<pathToCaFile>', 'utf-8').toString()],
463+
},
464+
},
465+
},
466+
]),
467+
],
468+
})
469+
export class AppModule {}
470+
```
471+
472+
You can also pass an array of CAs if your setup involves multiple trusted authorities.
473+
474+
Once everything is set up, you can inject the `ClientProxy` as usual using the `@Inject()` decorator to use the client in your services. This ensures encrypted communication across your NestJS microservices, with Node's `TLS` module handling the encryption details.
475+
476+
For more information, refer to Node’s [TLS documentation](https://nodejs.org/api/tls.html).

0 commit comments

Comments
 (0)