https://gravatar.com/userimage/16926726/2de44cb9918f08ff7f430244b10bf37b.jpeg?size=256

gowinder's Blog

Resolving Gluster Brick Offline Issues

Resolving Gluster Brick Offline Issues

Offline Issue

I had just finished installing gluster and stored the rootfs of a linux container on it, so that the linux container could achieve high availability within a two-node pve cluster.

When I had some spare time, I checked with the command: sudo gluster vol status VMS, and found that one brick was in offline state.

I looked it up online and installed gstatus:

Notes on Deploying a Two-Node HA Cluster and GlusterFS Distributed File System on Proxmox VE

Deploying a Two-Node HA Cluster and GlusterFS Distributed File System on Proxmox VE

Make sure both pve nodes run the same version

Two proxmox ve nodes, enabling high availability with automatic vm and lxc migration

Edit the hosts file

In /etc/hosts on both pve machines, add the following host entries:

1
2
3
4
192.168.1.229 pve1.local pve1
192.168.1.230 pve2.local pve2
192.168.1.229 gluster1
192.168.1.230 gluster2

Install glusterfs

The following operations are performed on both machines, referred to here as pve1 and pve2

JwtService Error in NestJS: Missing SECRET

JwtService Error in NestJS: “JsonWebTokenError: secret or public key must be provided”

Cause

This error keeps appearing recently: JsonWebTokenError: secret or public key must be provided,

  • I have already added imports: [JwtService] in the environment variables,

  • And I also added the following in the underlying library shared.module.ts:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
      imports: [
        JwtModule.registerAsync({
          imports: [ConfigModule],
          useFactory: (configService: ConfigService) => ({
            secret: configService.get<string>('JWT_SECRET') as string,
            signOptions: { expiresIn: '1d' },
          }),
          inject: [ConfigService],
        }),
    ]
    

But JwtService inside AuthGurad still can’t find secret; based on past experience, it should be that AuthModule did not import it correctly.

NestJS: AuthGuard Cannot Use Inject in Its canActivate Function

NestJS: AuthGuard Cannot Use Inject in Its canActivate Function

Cause

Recently I was writing a nestjs project. Its UserGuard is very convenient — it is like, when a route handles a request, the verification code can be handled by AuthGuard.

But while writing it, I ran into a problem that took me a very long time to solve, for example the following StaffAuthGuard.ts code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { ExecutionContext, Inject, Injectable } from '@nestjs/common';
import { StaffAuthService } from './staff-auth.service';
import { of } from 'rxjs';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class StaffJwtAuthGuard extends AuthGuard('jwt') {
  constructor(private authService: StaffAuthService) {
    super(authService);
    this.authService = authService;
    console.log(
      '🚀 ~ file: staff-auth.guard.ts:11 ~ StaffJwtAuthGuard ~ constructor ~ authService',
      authService,
      ', this.authService: ',
      this.authService,
    );
  }
  canActivate(context: ExecutionContext) {
    if (context.getType() != 'http') {
      return false;
    }
    const authHeader = context.switchToHttp().getRequest()
      .headers.authorization;
    if (!authHeader) {
      return false;
    }
    const authHeaderParts = authHeader.split(' ');
    if (authHeaderParts.length != 2) {
      return false;
    }
    const [, jwt] = authHeaderParts;

    this.authService
      .verifyJwt(jwt)
      .then((res) => {
        const exp = res.exp;
        if (!exp) return of(false);
        const TOKEN_EXP_MS = exp * 1000;
        const isJwtValid = Date.now() < TOKEN_EXP_MS;
        // return of(isJwtValid);
        return isJwtValid;
      })
      .catch((err) => {
        console.log(
          '🚀 ~ file: staff-auth.guard.ts:35 ~ StaffJwtAuthGuard ~ canActivate ~ err',
          err,
        );
        // return of(false);
        return false;
      });
    return super.canActivate(context);
  }
}

When calling the api, when it went through StaffAuthGuard.canActivate, it reported undefined. After adding logs, I found it was this line:

Fixing 'nestjsx/crud xxxRepository not found'

Fixing ’nestjsx/crud xxxRepository not found'

nestjsx/crud can quickly generate the CRUD operation APIs of the corresponding service and controller for the typeorm objects defined with @Entity.

However, when using nestjsx/crud, there are always errors such as: Nest can't resolve dependencies of the XXXService (?). Please make sure that the argument UtilsService at index [0] is available in the UtilsModule context.

The usual cause is that XXXModule has not imported the corresponding TypeOrm.forFeature([XXXEntity]), and has not exported XXXService in exports and providers.

Mutual References Between Entities in NestJS and TypeORM

Mutual References Between Entities in NestJS and TypeORM

20230131-152600

When using nestjs with typeorm, entities that reference each other keep throwing errors; following the official docs, I always ended up with an entity metadata not found error.

The approach that fails

After many attempts, I found it needs to be written like this. Suppose there are two entitys, one UserEntity and one CarEntity, referencing each other. Writing them the following way throws circular import errors.