Skip to content

Commit

Permalink
tf-aws working
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanaburayyan committed Apr 17, 2024
1 parent f8a83b2 commit 744ff53
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
1 change: 0 additions & 1 deletion apps/wing/src/commands/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe(
[
".wing",
"connections.json",
"secrets.json",
"simulator.json",
"tree.json",
]
Expand Down
1 change: 0 additions & 1 deletion libs/awscdk/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { IAwsFunction, PolicyStatement } from "@winglang/sdk/lib/shared-aws";
import { resolve } from "path";
import { renameSync, rmSync, writeFileSync } from "fs";
import { App } from "./app";
import * as ec2 from "aws-cdk-lib/aws-ec2";

/**
* Implementation of `awscdk.Function` are expected to implement this
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-sim/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export class Platform implements IPlatform {

fs.writeFileSync('./.env', updatedContent);

console.log("Secrets created/updated for sim platform");
console.log(`${Object.keys(secrets).length} secret(s) stored in .env`);
}
}
20 changes: 17 additions & 3 deletions libs/wingsdk/src/target-sim/secret.inflight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { SecretAttributes, SecretSchema } from "./schema-resources";
import { ISecretClient, SECRET_FQN } from "../cloud";
Expand All @@ -16,7 +15,8 @@ export class Secret implements ISecretClient, ISimulatorResourceInstance {
private readonly name: string;

constructor(props: SecretSchema) {
this.secretsFile = path.join(os.homedir(), ".wing", "secrets.json");
this.secretsFile = path.join(process.cwd(), ".env");

if (!fs.existsSync(this.secretsFile)) {
throw new Error(
`No secrets file found at ${this.secretsFile} while looking for secret ${props.name}`
Expand Down Expand Up @@ -57,7 +57,8 @@ export class Secret implements ISecretClient, ISimulatorResourceInstance {
timestamp: new Date().toISOString(),
});

const secrets = JSON.parse(fs.readFileSync(this.secretsFile, "utf-8"));
const secretsContent = fs.readFileSync(this.secretsFile, "utf-8");
const secrets = this.parseEnvFile(secretsContent);

if (!secrets[this.name]) {
throw new Error(`No secret value for secret ${this.name}`);
Expand All @@ -69,4 +70,17 @@ export class Secret implements ISecretClient, ISimulatorResourceInstance {
public async valueJson(): Promise<Json> {
return JSON.parse(await this.value());
}

private parseEnvFile(contents: string): { [key: string]: string } {
return contents.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#')) // Ignore empty lines and comments
.reduce((acc, line) => {
const [key, value] = line.split('=', 2);
if (key) {
acc[key.trim()] = value.trim();
}
return acc;
}, {} as { [key: string]: string });
}
}
34 changes: 34 additions & 0 deletions libs/wingsdk/src/target-tf-aws/platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { App } from "./app";
import { IPlatform } from "../platform";
import { SecretsManagerClient, GetSecretValueCommand, CreateSecretCommand, UpdateSecretCommand } from "@aws-sdk/client-secrets-manager";

/**
* AWS Terraform Platform
Expand Down Expand Up @@ -67,4 +68,37 @@ export class Platform implements IPlatform {
public newApp?(appProps: any): any {
return new App(appProps);
}

public async storeSecrets(secrets: { [name: string]: string; }): Promise<void> {
console.log("Storing secrets in AWS Secrets Manager");
const client = new SecretsManagerClient({});

for (const [name, value] of Object.entries(secrets)) {
try {
// Attempt to retrieve the secret to check if it exists
await client.send(new GetSecretValueCommand({ SecretId: name }));
console.log(`Secret ${name} exists, updating it.`);
// Update the secret if it exists
await client.send(new UpdateSecretCommand({
SecretId: name,
SecretString: JSON.stringify({ [name]: value }),
}));
} catch (error: any) {
if (error.name === 'ResourceNotFoundException') {
// If the secret does not exist, create it
console.log(`Secret ${name} does not exist, creating it.`);
await client.send(new CreateSecretCommand({
Name: name,
SecretString: JSON.stringify({ [name]: value }),
}));
} else {
// Log other errors
console.error(`Failed to store secret ${name}:`, error);
throw error; // Re-throw the error if it is not related to the secret not existing
}
}
}

console.log(`${Object.keys(secrets).length} secret(s) stored AWS Secrets Manager`);
}
}

0 comments on commit 744ff53

Please sign in to comment.