@kirschbaum-development/sst-laravel 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/laravel-sst.ts +761 -614
- package/package.json +1 -1
- package/src/remote-env-file.ts +88 -4
package/package.json
CHANGED
package/src/remote-env-file.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
1
|
import { CustomResourceOptions, Input, dynamic } from '@pulumi/pulumi';
|
|
4
|
-
import { pullSecrets, toEnvFileContent } from './secrets-manager';
|
|
5
2
|
|
|
6
3
|
export interface RemoteEnvFileLinkedSecret {
|
|
7
4
|
name: Input<string>;
|
|
@@ -67,7 +64,9 @@ export class RemoteEnvFile extends dynamic.Resource {
|
|
|
67
64
|
}
|
|
68
65
|
|
|
69
66
|
async function writeRemoteEnvironmentFile(inputs: ResolvedRemoteEnvFileInputs) {
|
|
70
|
-
const
|
|
67
|
+
const fs = await import('node:fs');
|
|
68
|
+
const path = await import('node:path');
|
|
69
|
+
const secrets = await pullSecretsFromAws(inputs.secretPath);
|
|
71
70
|
|
|
72
71
|
if (!secrets) {
|
|
73
72
|
throw new Error(`RemoteEnvVault secret not found at ${inputs.secretPath}.`);
|
|
@@ -84,6 +83,74 @@ async function writeRemoteEnvironmentFile(inputs: ResolvedRemoteEnvFileInputs) {
|
|
|
84
83
|
};
|
|
85
84
|
}
|
|
86
85
|
|
|
86
|
+
async function pullSecretsFromAws(secretPath: string): Promise<Record<string, string> | null> {
|
|
87
|
+
const secretValue = await getSecretValue(secretPath);
|
|
88
|
+
|
|
89
|
+
if (!secretValue) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const data = JSON.parse(secretValue);
|
|
94
|
+
|
|
95
|
+
if (isChunkedSecret(data)) {
|
|
96
|
+
return pullChunkedSecrets(secretPath, data.chunks);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return data;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function pullChunkedSecrets(basePath: string, chunkCount: number): Promise<Record<string, string>> {
|
|
103
|
+
const allVars: Record<string, string> = {};
|
|
104
|
+
const chunkPromises = Array.from({ length: chunkCount }, (_, i) =>
|
|
105
|
+
getSecretValue(getChunkPath(basePath, i + 1))
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const chunkValues = await Promise.all(chunkPromises);
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < chunkValues.length; i++) {
|
|
111
|
+
const chunkValue = chunkValues[i];
|
|
112
|
+
|
|
113
|
+
if (chunkValue) {
|
|
114
|
+
Object.assign(allVars, JSON.parse(chunkValue));
|
|
115
|
+
} else {
|
|
116
|
+
console.warn(`Warning: Chunk ${i + 1} not found at ${getChunkPath(basePath, i + 1)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return allVars;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function getSecretValue(secretPath: string): Promise<string | null> {
|
|
124
|
+
const { SecretsManagerClient, GetSecretValueCommand } = await import('@aws-sdk/client-secrets-manager');
|
|
125
|
+
const client = new SecretsManagerClient({});
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const response = await client.send(new GetSecretValueCommand({
|
|
129
|
+
SecretId: secretPath,
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
return response.SecretString || null;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (isResourceNotFound(error)) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isChunkedSecret(data: any): data is { chunked: true; chunks: number } {
|
|
143
|
+
return data && typeof data === 'object' && data.chunked === true && typeof data.chunks === 'number';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function isResourceNotFound(error: unknown): boolean {
|
|
147
|
+
return !!error && typeof error === 'object' && 'name' in error && error.name === 'ResourceNotFoundException';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function getChunkPath(basePath: string, chunkIndex: number): string {
|
|
151
|
+
return `${basePath}/${chunkIndex}`;
|
|
152
|
+
}
|
|
153
|
+
|
|
87
154
|
function buildEnvFileContent(
|
|
88
155
|
secrets: Record<string, string>,
|
|
89
156
|
inputs: ResolvedRemoteEnvFileInputs,
|
|
@@ -125,6 +192,23 @@ function buildEnvFileContent(
|
|
|
125
192
|
].filter(Boolean).join('\n\n');
|
|
126
193
|
}
|
|
127
194
|
|
|
195
|
+
function toEnvFileContent(vars: Record<string, string>): string {
|
|
196
|
+
const sortedKeys = Object.keys(vars).sort();
|
|
197
|
+
|
|
198
|
+
return sortedKeys
|
|
199
|
+
.map((key) => {
|
|
200
|
+
const value = vars[key];
|
|
201
|
+
|
|
202
|
+
if (value.includes(' ') || value.includes('"') || value.includes("'") || value.includes('\n')) {
|
|
203
|
+
const escaped = value.replace(/"/g, '\\"');
|
|
204
|
+
return `${key}="${escaped}"`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return `${key}=${value}`;
|
|
208
|
+
})
|
|
209
|
+
.join('\n');
|
|
210
|
+
}
|
|
211
|
+
|
|
128
212
|
function hasOwnVariable(vars: Record<string, string>, key: string) {
|
|
129
213
|
return Object.prototype.hasOwnProperty.call(vars, key);
|
|
130
214
|
}
|