Commit 50a738dd authored by Ahmad's avatar Ahmad

inikt

parent d3891be3
Pipeline #211 canceled with stages
const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');
const AWS = require('aws-sdk');
const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads');
......@@ -12,33 +11,20 @@ if (!fs.existsSync(rootKeyFilePath)) {
process.exit(1);
}
function getAWSCredentials() {
return new Promise((resolve, reject) => {
let credentials = null;
// Function to extract AWS credentials using regex
function extractAWSCredentials(filePath) {
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)
.pipe(csv())
.on('data', (row) => {
console.log('Parsed row:', row); // Log the parsed row for debugging
// Adjust for BOM in the header
const accessKeyIdHeader = Object.keys(row)[0].replace(/^\uFEFF/, ''); // Removes BOM if present
if (accessKeyIdHeader === 'Access key ID' && row['Secret access key']) {
credentials = {
accessKeyId: row[accessKeyIdHeader].trim(),
secretAccessKey: row['Secret access key'].trim(),
if (accessKeyIdMatch && secretAccessKeyMatch) {
return {
accessKeyId: accessKeyIdMatch[1],
secretAccessKey: secretAccessKeyMatch[1],
};
}
})
.on('end', () => {
if (credentials) {
resolve(credentials);
} else {
reject(new Error('Failed to load AWS credentials from rootkey.csv'));
throw new Error('Failed to extract AWS credentials from rootkey.csv');
}
})
.on('error', (error) => reject(error));
});
}
function getPemFilesAndRegions() {
......@@ -73,7 +59,7 @@ async function getLightsailInstances(region, credentials) {
async function main() {
try {
const credentials = await getAWSCredentials();
const credentials = extractAWSCredentials(rootKeyFilePath);
console.log('AWS Credentials Loaded:', credentials); // Log loaded credentials for debugging
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