Nestjs构建Certeasy证书自动化平台 - 业务实现(云资源模块)

91 阅读3分钟

Certeasy

预览

摘要

申请颁发的证书,自然是要部署到对应云资源内容的,因此需要一个云资源模块来配合相关工作记录。

app.certeasy.cn_cloud_guide.png

要点

  • 要实现证书的自动化部署,第一就是要对接各大厂商的API, 获取其提供的 AK/SK 等授权码
  • 确认厂商,构建一个工厂方法,统一操作方法。之后就说各个厂商的SDK接入实现
  • 因部分厂商的部署周期较长,需要实现一个校验能力,以间隔 ns 的方式主动校验部署状态

模块初始化

执行 NestJS CLI三件套

    nest g mo modules/cloud
    nest g co modules/cloud --no-spec
    nest g s modules/cloud --no-spec

image.png

业务实现

1. 控制器

  • 资源 CURD
 @Get('')
 @Post('')
 @Get(':id')
 @Put(':id')
 @Delete(':id')
  • 资源部署 :id/deploy 指定某个资源部署证书
@Post(':id/deploy')
@ApiOperation({
  summary: '云资源部署',
})
@ApiResponse({
  status: HttpStatus.BAD_REQUEST,
  type: BaseApiErrorResponse,
})
@ApiParam({ name: 'id', description: 'Cloud id' })
async deploy(
  @IRequestUser() user: IUserPayload,
  @Param('id', ParseIntPipe) id: number,
) {
  return await this.cloudService.deploy(user, id);
}

2. 实体

  • 资源实体 cloud.entity,记录用户的云资源数据
@Entity('cloud')
export class CloudEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Expose()
  @Column({ type: 'int', name: 'user_id', comment: '所属用户' })
  userId: number;

  @Expose()
  @Column({ type: 'int', name: 'provider_id', comment: '提供商ID' })
  providerId: number;

  @OneToOne(() => CloudProviderEntity, (provider) => provider.id)
  @JoinColumn({ name: 'provider_id' })
  provider: CloudProviderEntity;

  @Expose()
  @Column({ type: 'int', name: 'provider_product_id', comment: '提供商产品ID' })
  providerProductId: number;

  @OneToOne(() => CloudProviderProductEntity, (product) => product.id)
  @JoinColumn({ name: 'provider_product_id' })
  providerProduct: CloudProviderProductEntity;

  @Expose()
  @Column({ type: 'varchar', name: 'name', comment: '名称' })
  name: string;

  @Expose()
  @Column({ type: 'varchar', name: 'alias', comment: '别名' })
  alias: string;

  @Expose()
  @Column({ type: 'json', name: 'accessJson', comment: '配置内容' })
  accessJson: any;

  @Expose()
  @Column({ type: 'tinyint', name: 'status' })
  status: number;

  @CreateDateColumn({ type: 'datetime', name: 'create_time' })
  createTime: Date | string;

  @UpdateDateColumn({ type: 'datetime', name: 'update_time' })
  updateTime: Date | string;
  // save才有效
  @BeforeInsert()
  updateName() {
    this.name = `cloud-${randomString(16, cryptoMd5(JSON.stringify(this.accessJson)))}`;
  }

  @OneToOne(() => CloudCertificateEntity, (certificate) => certificate.cloud)
  certificate: CloudCertificateEntity;

  @ManyToMany(() => CertificateEntity, (certificate) => certificate.clouds)
  @JoinTable({
    name: 'cloud_certificate',
    joinColumn: { name: 'cloud_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'certificate_id', referencedColumnName: 'id' },
  })
  certificates: CertificateEntity[];
}
  • 资源证书关联 cloud-certificate.entity 云资源绑定证书数据
@Entity('cloud_certificate')
export class CloudCertificateEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Expose()
  @Column({ type: 'int', name: 'cloud_id', comment: '提供商ID' })
  cloudId: number;

  @OneToOne(() => CloudEntity, (cloud) => cloud.id)
  @JoinColumn({ name: 'cloud_id' })
  cloud: CloudEntity;

  @Expose()
  @Column({ type: 'int', name: 'certificate_id', comment: '提供商ID' })
  certificateId: number;

  @OneToOne(() => CertificateEntity, (certificate) => certificate.id)
  @JoinColumn({ name: 'certificate_id' })
  certificate: CertificateEntity;

  @CreateDateColumn({ type: 'datetime', name: 'create_time' })
  createTime: Date | string;

  @UpdateDateColumn({ type: 'datetime', name: 'update_time' })
  updateTime: Date | string;

  @OneToOne(
    () => CloudDeployEntity,
    (cloudDeploy) => cloudDeploy.cloudCertificateId,
  )
  @JoinColumn({ name: 'id' })
  deployment: CloudDeployEntity;

  @OneToMany(
    () => CloudDeployEntity,
    (cloudDeploy) => cloudDeploy.cloudCertificate,
  )
  deployments: CloudDeployEntity[];
}
  • 资源部署 cloud-deploy 资证书源部署记录
  • 资源厂商 cloud-provider
  • 资源厂商产品 cloud-provider-product

就不一一概述了,具体请查看 certeasy_nest_open/src/modules/cloud/entities at main · CerteasyTeam/certeasy_nest_open (github.com)

3. 服务

  • 云资源部署 actionDeploy

image.png

更多代码请查看 certeasy_nest_open/src/modules/cloud at main · CerteasyTeam/certeasy_nest_open (github.com)

系列文章

开源

联系

wechat: zuxcloud

Email: zuxing.xu@lettered.cn