Skip to content

Commit

Permalink
changed dbconnect code
Browse files Browse the repository at this point in the history
  • Loading branch information
tmahmood72 committed Nov 1, 2023
1 parent 18069fe commit 9cf31d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 43 deletions.
53 changes: 14 additions & 39 deletions terraform/environments/apex/dbconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@
// version: 0.1
// auth: phil h
/////////////////////////////////////////////////////////////////////
import { exec } from 'child_process';
const SSH = require("simple-ssh");
const AWS = require("aws-sdk");

const AWS = require("@aws-sdk");
//SSM object with temp parms
const ssm = new AWS.SSM({ apiVersion: "2014-11-06" });

// Environment variables
const pem = "MGMT_EC2_KEY_DEFAULT";
const username = "ec2-user";

//Set date format
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();

if (dd < 10) {
dd = "0" + dd;
}

if (mm < 10) {
mm = "0" + mm;
}
today = dd + "-" + mm + "-" + yyyy;

//EC2 object
let ec2 = new AWS.EC2({ apiVersion: "2014-10-31" });

//Get private IP address for EC2 instances tagged with Name:{ appname }
// May return more than 1 instance if there are multiple instances with the same name
async function getInstances(appname) {
Expand All @@ -43,7 +35,6 @@ async function getInstances(appname) {
.describeInstances({ Filters: [{ Name: "tag:Name", Values: [appname] }] })
.promise();
}

async function getIPaddress(appname) {
var instance_ip_list = [];
var instance_data = await getInstances(appname);
Expand All @@ -55,23 +46,16 @@ async function getIPaddress(appname) {
console.log("Found ", instance_ip_list.length, " instances");
return instance_ip_list;
}


// Get SSH key from param store

async function getSSMparam() {
return await ssm.getParameter({ Name: pem, WithDecryption: true }).promise();
}

// Trigger SSH connection to the EC2 instance
// Run SSH command

async function connSSH(action, appname) {
//get ssm key
const key = await getSSMparam();

const myKey = key["Parameter"]["Value"];

const addresses = await getIPaddress(appname);
// all this config could be passed in via the event
for(var address of addresses){
Expand All @@ -81,25 +65,23 @@ async function connSSH(action, appname) {
user: username,
key: myKey,
});
// const { exec } = require('child_process');
const bashCommand = 'sudo su - oracle -c "sqlplus / as sysdba <<EOFUM' +
"\n" +
"alter system switch logfile;" +
"\n" +
"alter system switch logfile;" +
"\n" +
"alter database begin backup;" +
"\n" +
"exit;" +
"\n" +
'EOFUM"';
let prom = new Promise(function (resolve, reject) {
if (action == "begin") {
console.log("[+] Trying connecting to EC2 ==>> " + address);
console.log(`[+] Running "begin backup commands" as Oracle`);

ssh
exec(bashCommand,
.exec(
'sudo su - oracle -c "sqlplus / as sysdba <<EOFUM' +
"\n" +
"alter system switch logfile;" +
"\n" +
"alter system switch logfile;" +
"\n" +
"alter database begin backup;" +
"\n" +
"exit;" +
"\n" +
'EOFUM"',
{
pty: true,
out: console.log.bind(console),
Expand All @@ -113,7 +95,6 @@ async function connSSH(action, appname) {
.start();
} else if (action == "end"){
console.log(`[+] Running "end backup commands" as Oracle`);

ssh
.exec(
'sudo su - oracle -c "sqlplus / as sysdba <<EOFUM' +
Expand Down Expand Up @@ -142,32 +123,26 @@ async function connSSH(action, appname) {
});
try {
await prom;

const response = {
statusCode: 200,
};

console.log(`[+] Completed DB alter state: ${action} ==>> ` + address);
console.log("[+] Returned response: " + response);

ssh.end();

return response;
} catch (e) {
console.log(e);
context.fail();
}
}
}

exports.handler = async (event, context) => {
try {
console.log("[+} Received event:", JSON.stringify(event, null, 2));
await connSSH(event.action, event.appname);

context.done();
} catch (error) {
console.error(error);
context.fail();
}
};
};
2 changes: 1 addition & 1 deletion terraform/environments/apex/event_triggers.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "aws_cloudwatch_event_rule" "snapshotDBFunctionmon_sun" {
name = "laa-createSnapshotRule-${local.application_name}-${local.environment}-mp"
description = "Daily snapshots of Oracle volumes"
schedule_expression = "cron(46 15 ? * MON-SUN *)"
schedule_expression = "cron(04 16 ? * MON-SUN *)"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_mon_sun" {
statement_id = "AllowExecutionFromCloudWatch"
Expand Down
6 changes: 3 additions & 3 deletions terraform/environments/apex/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ locals {
#Lambda Function creation
snapshotDBFunctionname = "snapshotDBFunction"
snapshotDBFunctionhandler = "snapshot/dbsnapshot.handler"
snapshotDBFunctionruntime = "nodejs14.x"
snapshotDBFunctionruntime = "nodejs18.x"
snapshotDBFunctionfilename = "dbsnapshot.zip"

deletesnapshotFunctionname = "deletesnapshotFunction"
Expand All @@ -23,12 +23,12 @@ locals {

connectDBFunctionname = "connectDBFunction"
connectDBFunctionhandler = "ssh/dbconnect.handler"
connectDBFunctionruntime = "nodejs14.x"
connectDBFunctionruntime = "nodejs18.x"
connectDBFunctionfilename = "dbconnect.zip"

#layer config
s3layerkey = "nodejs.zip"
compatible_runtimes = "nodejs14.x"
compatible_runtimes = "nodejs18.x"

application_test_url = "https://apex.laa-development.modernisation-platform.service.justice.gov.uk/apex/"
}

0 comments on commit 9cf31d8

Please sign in to comment.