The server
directory houses a Nest.js server constructed to operate using the gRPC (Google Remote Procedure Call) transport protocol. Within this server, there are two applications: api-gateway
and keymanager.
The api-gateway
application functions as the entry point for all incoming HTTP traffic within our application ecosystem. Its primary role is to act as a reverse proxy, forwarding these incoming HTTP requests to the keymanager
microservice utilizing the gRPC transport protocol.
The keymanager
microservice is responsible for managing cryptographic keys within our application. It serves as the host for the key management application, handling operations related to key generation, storage, retrieval, and any other cryptographic functions required by our application.
In addition to the applications, there is a common
library present in the server directory. This library houses shared or common assets and functionality utilized by both api-gateway
and keymanager.
Among the assets in the common
library is the keymanager.ts
file, which is generated from the protobuf (protocol buffer) definition file. Protocol buffers are a language-agnostic data serialization format used for efficiently encoding and decoding structured data, making it an excellent choice for inter-service communication in microservices architectures.
$ pnpm install
$ cd server
# watch mode
$ pnpm run start:dev api-gateway
# open another terminal
$ pnpm run start:dev keymanager
# After running two servers, you can test the APIs using the following methods.
graph TD
subgraph "api-gateway"
A[HTTP Request] -->|gRPC Request| B[keys.controller.ts]
B[keys.controller.ts] -->|gRPC Request| C[keys.service.ts]
C[keys.service.ts] -->|gRPC Request| D["KeyManager Service"]
D["KeyManager Service"] -->|gRPC Response| C[keys.service.ts]
C[keys.service.ts] -->|gRPC Response| B[keys.controller.ts]
B[keys.controller.ts] -->|HTTP Response| A[HTTP Client]
end
subgraph "keymanager"
E["KeyManager Service"] -->|gRPC Request| F[keys.controller.ts]
F[keys.controller.ts] -->|gRPC Request| G[keys.service.ts]
G[keys.service.ts] -->|gRPC Response| F[keys.controller.ts]
F[keys.controller.ts] -->|gRPC Response| E["KeyManager Service"]
end
To test the APIs implemented in the Nest.js application using curl
, follow these steps. Ensure that api-gateway
application is running at the appropriate address and port (e.g., http://localhost:3000
).
Use curl
to create a new key with the Create Key
endpoint:
curl -X POST http://localhost:3000/keys -H "Content-Type: application/json" -d '{
"keyName": "myKey",
"keyAlgo": 2
}'
Expected Result: If successful, you should receive a JSON response indicating success. An error message will be returned if the key name already exists or if the key algorithm is unsupported.
Use curl
to delete a key with the Delete Key endpoint:
curl -X DELETE http://localhost:3000/keys/myKey
Expected Result: If successful, you should receive a JSON response indicating success. An error message will be returned if the key name is not found.
curl http://localhost:3000/keys/myKey/secret
Expected Result: If successful, you should receive a JSON response containing the private key for the specified key name. An error message will be returned if the key name is not found.
curl http://localhost:3000/keys
Expected Result: You should receive a JSON response containing an array of key names currently stored in memory.
Nest is MIT licensed.