import { Injectable, NotFoundException, Version } from '@nestjs/common'; import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler'; import { Action } from '../action.enum'; import * as Joi from 'joi'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { IamPolicy } from './iam-policy.entity'; import { breakdownArn } from '../util/breakdown-arn'; import { IamRolePolicyAttachment } from './iam-role-policy-attachment.entity'; type QueryParams = { PolicyArn: string; VersionId: string; } @Injectable() export class GetPolicyVersionHandler extends AbstractActionHandler { constructor( @InjectRepository(IamPolicy) private readonly policyRepo: Repository, @InjectRepository(IamRolePolicyAttachment) private readonly attachmentRepo: Repository, ) { super(); } format = Format.Xml; action = Action.IamGetPolicyVersion; validator = Joi.object({ PolicyArn: Joi.string().required(), VersionId: Joi.string().required(), }); protected async handle({ PolicyArn, VersionId }: QueryParams, awsProperties: AwsProperties) { const { identifier, accountId } = breakdownArn(PolicyArn); const [_policy, name] = identifier.split('/'); const policy = await this.policyRepo.findOne({ where: { name, accountId, version: +VersionId }}); if (!policy) { throw new NotFoundException('NoSuchEntity', 'The request was rejected because it referenced a resource entity that does not exist. The error message describes the resource.'); } return { PolicyVersion: { Document: policy.document, IsDefaultVersion: policy.isDefault, VersionId: `${policy.version}`, CreateDate: new Date(policy.createdAt).toISOString(), } } } }