Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 10 additions & 48 deletions src/artifactexporter/ArtifactExporter.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
import { readFileSync } from 'fs';
import { resolve, dirname, isAbsolute } from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import { load } from 'js-yaml';
import { TopLevelSection, IntrinsicFunction } from '../context/ContextType';
import { Document, DocumentType } from '../document/Document';
import { detectDocumentType } from '../document/DocumentUtils';
import { fileURLToPath } from 'url';
import { TopLevelSection } from '../context/ContextType';
import { normalizeIntrinsicFunctionAndCondition } from '../context/semantic/Intrinsics';
import { DocumentType } from '../document/Document';
import { parseDocumentContent } from '../document/DocumentUtils';
import { S3Service } from '../services/S3Service';
import { Artifact } from '../stacks/actions/StackActionRequestType';
import { isS3Url, RESOURCE_EXPORTER_MAP } from './ResourceExporters';

const INTRINSIC_FUNCTION_MAP = new Map<string, string>([
['!Ref', IntrinsicFunction.Ref],
['!GetAtt', IntrinsicFunction.GetAtt],
['!Join', IntrinsicFunction.Join],
['!Sub', IntrinsicFunction.Sub],
['!Base64', IntrinsicFunction.Base64],
['!GetAZs', IntrinsicFunction.GetAZs],
['!ImportValue', IntrinsicFunction.ImportValue],
['!Select', IntrinsicFunction.Select],
['!Split', IntrinsicFunction.Split],
['!FindInMap', IntrinsicFunction.FindInMap],
['!Equals', IntrinsicFunction.Equals],
['!If', IntrinsicFunction.If],
['!Not', IntrinsicFunction.Not],
['!And', IntrinsicFunction.And],
['!Or', IntrinsicFunction.Or],
['!Cidr', IntrinsicFunction.Cidr],
['!Transform', IntrinsicFunction.Transform],
['!Condition', 'Condition'],
]);

export type ArtifactWithProperty = {
resourceType: string;
resourcePropertyDict: Record<string, unknown>;
Expand All @@ -39,30 +17,14 @@ export type ArtifactWithProperty = {

export class ArtifactExporter {
private readonly templateDict: unknown;
private readonly templateUri: string;
private readonly templateType: DocumentType;

constructor(
private readonly s3Service: S3Service,
private readonly document?: Document,
private readonly templateAbsPath?: string,
private readonly templateType: DocumentType,
private readonly templateUri: string,
templateContent: string,
) {
if (this.document) {
this.templateDict = this.document.getParsedDocumentContent();
this.templateUri = this.document.uri;
this.templateType = this.document.documentType;
} else if (this.templateAbsPath) {
const content = readFileSync(this.templateAbsPath, 'utf8');
this.templateUri = pathToFileURL(this.templateAbsPath).href;
this.templateType = detectDocumentType(this.templateUri, content).type;
if (this.templateType === DocumentType.YAML) {
this.templateDict = load(content);
} else {
this.templateDict = JSON.parse(content);
}
} else {
throw new Error('Either document or absolutePath must be provided');
}
this.templateDict = parseDocumentContent(templateUri, templateContent);
}

private getResourceMapWithArtifact(): Record<string, ArtifactWithProperty[]> {
Expand Down Expand Up @@ -148,7 +110,7 @@ export class ArtifactExporter {
const objDict = obj as Record<string, unknown>;

for (const [key, value] of Object.entries(objDict)) {
const newKey = INTRINSIC_FUNCTION_MAP.get(key) ?? key;
const newKey = normalizeIntrinsicFunctionAndCondition(key);
result[newKey] = this.convertIntrinsicFunctionKeys(value);
}

Expand Down
57 changes: 18 additions & 39 deletions src/artifactexporter/ResourceExporters.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import {
existsSync,
mkdtempSync,
copyFileSync,
rmSync,
createWriteStream,
statSync,
openSync,
readSync,
closeSync,
} from 'fs';
import { existsSync, mkdtempSync, copyFileSync, rmSync, createWriteStream, statSync, readFileSync } from 'fs';
import { tmpdir } from 'os';
import path, { join, basename } from 'path';
import { pathToFileURL } from 'url';
import archiver from 'archiver';
import { dump } from 'js-yaml';
import { detectDocumentType } from '../document/DocumentUtils';
import { S3Service } from '../services/S3Service';
import { ArtifactExporter } from './ArtifactExporter';

export function isS3Url(url: string): boolean {
return typeof url === 'string' && /^s3:\/\/[^/]+\/.+/.test(url);
return /^s3:\/\/[^/]+\/.+/.test(url);
}

export function isLocalFile(filePath: string): boolean {
Expand All @@ -31,26 +23,9 @@ function isLocalFolder(path: string): boolean {
function isArchiveFile(filePath: string) {
// Quick extension check
const ext = path.extname(filePath).toLowerCase();
const archiveExts = ['.zip', '.rar', '.7z', '.tar', '.gz', '.tgz'];

if (!archiveExts.includes(ext)) return false;

// Verify with magic numbers
try {
const fd = openSync(filePath, 'r');
const buffer = Buffer.alloc(8);
readSync(fd, buffer, 0, 8, 0);
closeSync(fd);

return (
(buffer[0] === 0x50 && buffer[1] === 0x4b) || // ZIP
buffer.toString('ascii', 0, 4) === 'Rar!' || // RAR
(buffer[0] === 0x37 && buffer[1] === 0x7a) || // 7Z
(buffer[0] === 0x1f && buffer[1] === 0x8b) // GZIP
);
} catch {
return false;
}
const archiveExts = ['.zip', '.rar', '.7z', '.tar', '.gz', '.tgz', '.zst', '.war'];

return archiveExts.includes(ext);
}

function copyToTempDir(filePath: string): string {
Expand Down Expand Up @@ -228,25 +203,25 @@ export abstract class ResourceWithS3UrlDict extends Resource {
}
}

export class ServerlessFunctionResource extends Resource {
class ServerlessFunctionResource extends Resource {
public override resourceType = 'AWS::Serverless::Function';
public override propertyName = 'CodeUri';
protected override forceZip = true;
}

export class ServerlessApiResource extends Resource {
class ServerlessApiResource extends Resource {
public override resourceType = 'AWS::Serverless::Api';
public override propertyName = 'DefinitionUri';
protected override packageNullProperty = false;
}

export class GraphQLSchemaResource extends Resource {
class GraphQLSchemaResource extends Resource {
public override resourceType = 'AWS::AppSync::GraphQLSchema';
public override propertyName = 'DefinitionS3Location';
protected override packageNullProperty = false;
}

export class LambdaFunctionResource extends ResourceWithS3UrlDict {
class LambdaFunctionResource extends ResourceWithS3UrlDict {
public override resourceType = 'AWS::Lambda::Function';
public override propertyName = 'Code';
protected override bucketNameProperty = 'S3Bucket';
Expand All @@ -255,7 +230,7 @@ export class LambdaFunctionResource extends ResourceWithS3UrlDict {
protected override forceZip = true;
}

export class ApiGatewayRestApiResource extends ResourceWithS3UrlDict {
class ApiGatewayRestApiResource extends ResourceWithS3UrlDict {
public override resourceType = 'AWS::ApiGateway::RestApi';
public override propertyName = 'BodyS3Location';
protected override packageNullProperty = false;
Expand All @@ -264,7 +239,7 @@ export class ApiGatewayRestApiResource extends ResourceWithS3UrlDict {
protected override versionProperty = 'Version';
}

export class CloudFormationStackResource extends Resource {
class CloudFormationStackResource extends Resource {
public override resourceType = 'AWS::CloudFormation::Stack';
public override propertyName = 'TemplateURL';

Expand All @@ -278,7 +253,11 @@ export class CloudFormationStackResource extends Resource {
throw new Error(`Invalid template path: ${templateAbsPath}`);
}

const template = new ArtifactExporter(this.s3Service, undefined, templateAbsPath);
const templateUri = pathToFileURL(templateAbsPath).href;
const content = readFileSync(templateAbsPath, 'utf8');
const templateType = detectDocumentType(templateUri, content).type;

const template = new ArtifactExporter(this.s3Service, templateType, templateUri, content);
const exportedTemplateDict = await template.export(bucketName, s3KeyPrefix);
const exportedTemplateStr = dump(exportedTemplateDict);

Expand Down
4 changes: 4 additions & 0 deletions src/context/semantic/Intrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export function normalizeIntrinsicFunction(text: string): string {
}
return text;
}

export function normalizeIntrinsicFunctionAndCondition(text: string): string {
return text === '!Condition' ? 'Condition' : normalizeIntrinsicFunction(text);
}
9 changes: 9 additions & 0 deletions src/document/DocumentUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extname, parse } from 'path';
import { Edit, Point } from 'tree-sitter';
import { DocumentType } from './Document';
import { parseValidYaml } from './YamlParser';

export function getIndexFromPoint(content: string, point: Point): number {
const contentInLines = content.split('\n');
Expand Down Expand Up @@ -91,3 +92,11 @@ export function detectDocumentType(uri: string, content: string): { extension: s
export function uriToPath(uri: string) {
return parse(uri);
}

export function parseDocumentContent(uri: string, content: string): unknown {
const documentType = detectDocumentType(uri, content).type;
if (documentType === DocumentType.JSON) {
return JSON.parse(content);
}
return parseValidYaml(content);
}
7 changes: 6 additions & 1 deletion src/handlers/StackHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ export function getTemplateArtifactsHandler(
throw new Error(`Cannot retrieve file with uri: ${params}`);
}

const template = new ArtifactExporter(components.s3Service, document);
const template = new ArtifactExporter(
components.s3Service,
document.documentType,
document.uri,
document.contents(),
);
const artifacts = template.getTemplateArtifacts();
return { artifacts };
} catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/stacks/actions/StackActionOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ export async function processChangeSet(
let expectedETag: string | undefined;
try {
if (params.s3Bucket) {
const s3KeyPrefix = params.s3Key ? params.s3Key.slice(0, params.s3Key.lastIndexOf('/')) : undefined;
const template = new ArtifactExporter(s3Service, document);
const s3KeyPrefix = params.s3Key?.includes('/')
? params.s3Key.slice(0, params.s3Key.lastIndexOf('/'))
: undefined;
const template = new ArtifactExporter(s3Service, document.documentType, document.uri, document.contents());

const exportedTemplate = await template.export(params.s3Bucket, s3KeyPrefix);

Expand Down
Loading
Loading