Commit 50a738dd authored by Ahmad's avatar Ahmad

inikt

parent d3891be3
Pipeline #211 canceled with stages
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const csv = require('csv-parser');
const AWS = require('aws-sdk'); const AWS = require('aws-sdk');
const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads'); const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads');
...@@ -12,33 +11,20 @@ if (!fs.existsSync(rootKeyFilePath)) { ...@@ -12,33 +11,20 @@ if (!fs.existsSync(rootKeyFilePath)) {
process.exit(1); process.exit(1);
} }
function getAWSCredentials() { // Function to extract AWS credentials using regex
return new Promise((resolve, reject) => { function extractAWSCredentials(filePath) {
let credentials = null; const fileContent = fs.readFileSync(filePath, 'utf8');
const accessKeyIdMatch = fileContent.match(/Access key ID\s*,\s*([^,\s]+)/i);
const secretAccessKeyMatch = fileContent.match(/Secret access key\s*,\s*([^,\s]+)/i);
fs.createReadStream(rootKeyFilePath) if (accessKeyIdMatch && secretAccessKeyMatch) {
.pipe(csv()) return {
.on('data', (row) => { accessKeyId: accessKeyIdMatch[1],
console.log('Parsed row:', row); // Log the parsed row for debugging secretAccessKey: secretAccessKeyMatch[1],
};
// Adjust for BOM in the header } else {
const accessKeyIdHeader = Object.keys(row)[0].replace(/^\uFEFF/, ''); // Removes BOM if present throw new Error('Failed to extract AWS credentials from rootkey.csv');
if (accessKeyIdHeader === 'Access key ID' && row['Secret access key']) { }
credentials = {
accessKeyId: row[accessKeyIdHeader].trim(),
secretAccessKey: row['Secret access key'].trim(),
};
}
})
.on('end', () => {
if (credentials) {
resolve(credentials);
} else {
reject(new Error('Failed to load AWS credentials from rootkey.csv'));
}
})
.on('error', (error) => reject(error));
});
} }
function getPemFilesAndRegions() { function getPemFilesAndRegions() {
...@@ -73,7 +59,7 @@ async function getLightsailInstances(region, credentials) { ...@@ -73,7 +59,7 @@ async function getLightsailInstances(region, credentials) {
async function main() { async function main() {
try { try {
const credentials = await getAWSCredentials(); const credentials = extractAWSCredentials(rootKeyFilePath);
console.log('AWS Credentials Loaded:', credentials); // Log loaded credentials for debugging console.log('AWS Credentials Loaded:', credentials); // Log loaded credentials for debugging
const regions = getPemFilesAndRegions(); const regions = getPemFilesAndRegions();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment