Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GCP connections for mirroring #22

Merged
merged 12 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions backend/src/api/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import { EC2_CONN } from "suricata_setup/aws-services/create-ec2-instance"
import { VirtualizationType } from "@aws-sdk/client-ec2"
import { save_connection } from "services/connections"
import { deleteKeyFromRedis, getFromRedis } from "suricata_setup/utils"
import {
list_images,
list_machines,
} from "suricata_setup/gcp-services/gcp_setup"

declare module "express-session" {
interface SessionData {
connection_config: Record<
string, // id
{
step?: STEP_RESPONSE["step_number"]
status?: STEP_RESPONSE["status"]
step?: STEP_RESPONSE<ConnectionType>["step_number"]
status?: STEP_RESPONSE<ConnectionType>["status"]
id?: string
type?: ConnectionType
data?: STEP_RESPONSE["data"]
Expand Down Expand Up @@ -77,23 +81,41 @@ export const aws_os_choices = async (
res: Response,
): Promise<void> => {
const { id } = req.body
const { access_id, secret_access_key, region } =
req.session.connection_config[id].data
const { access_id, secret_access_key, region } = req.session
.connection_config[id].data as STEP_RESPONSE<ConnectionType.AWS>["data"]
let conn = new EC2_CONN(access_id, secret_access_key, region)
let choices = await conn.get_latest_image()
await ApiResponseHandler.success(res, [
[choices.Description, choices.ImageId],
])
}

export const gcp_os_choices = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const { id } = req.body
const { key_file, zone, project } = req.session.connection_config[id]
.data as STEP_RESPONSE<ConnectionType.GCP>["data"]

let choices = await list_images({ key_file, project, zone })
let resp = choices.map(v => [v.description, v.selfLink])
await ApiResponseHandler.success(res, resp)
} catch (err) {
await ApiResponseHandler.error(res, err)
}
}

export const aws_instance_choices = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const { id, specs } = req.body
const { access_id, secret_access_key, virtualization_type, region } =
req.session.connection_config[id].data
const { access_id, secret_access_key, virtualization_type, region } = req
.session.connection_config[id]
.data as STEP_RESPONSE<ConnectionType.AWS>["data"]
let conn = new EC2_CONN(access_id, secret_access_key, region)
let choices = await conn.get_valid_types(
virtualization_type as VirtualizationType,
Expand All @@ -107,6 +129,32 @@ export const aws_instance_choices = async (
ApiResponseHandler.error(res, err)
}
}
export const gcp_instance_choices = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const { id, specs } = req.body
const { key_file, zone, project } = req.session.connection_config[id]
.data as STEP_RESPONSE<ConnectionType.GCP>["data"]

let choices = await list_machines({
key_file,
zone,
project,
minCpu: specs.minCpu,
maxCpu: specs.maxCpu,
minMem: specs.minMem,
maxMem: specs.maxMem,
})
await ApiResponseHandler.success(
res,
choices.map(v => [v.name, v.selfLink]),
)
} catch (err) {
ApiResponseHandler.error(res, err)
}
}

export const get_setup_state = async (req: Request, res: Response) => {
const { uuid } = req.params
Expand All @@ -115,6 +163,10 @@ export const get_setup_state = async (req: Request, res: Response) => {
if (["OK", "FAIL"].includes(resp.success)) {
await deleteKeyFromRedis(uuid)
}
req.session.connection_config[resp.data.id] = {
...req.session.connection_config[resp.data.id],
...resp,
}
delete resp.data
await ApiResponseHandler.success(res, resp)
} catch (err) {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { MulterSource } from "multer-source"
import {
aws_instance_choices,
aws_os_choices,
gcp_instance_choices,
gcp_os_choices,
get_setup_state,
setup_connection,
} from "./api/setup"
Expand Down Expand Up @@ -99,6 +101,8 @@ app.post("/api/v1/setup_connection", setup_connection)
app.get("/api/v1/setup_connection/fetch/:uuid", get_setup_state)
app.post("/api/v1/setup_connection/aws/os", aws_os_choices)
app.post("/api/v1/setup_connection/aws/instances", aws_instance_choices)
app.post("/api/v1/setup_connection/gcp/os", gcp_os_choices)
app.post("/api/v1/setup_connection/gcp/instances", gcp_instance_choices)
app.get("/api/v1/list_connections", list_connections)
app.get("/api/v1/list_connections/:uuid", get_connection_for_uuid)
app.get(
Expand Down
8 changes: 6 additions & 2 deletions backend/src/models/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
BeforeInsert,
} from "typeorm"
import { ConnectionType } from "@common/enums"
import { AWS_CONNECTION, ENCRYPTED_AWS_CONNECTION__META } from "@common/types"
import {
AWS_CONNECTION,
ENCRYPTED_AWS_CONNECTION__META,
SSH_INFO,
} from "@common/types"
import { encrypt, generate_iv } from "utils/encryption"

@Entity()
Expand All @@ -29,7 +33,7 @@ export class Connections extends BaseEntity {
name: string

@Column({ nullable: true, type: "jsonb" })
aws?: AWS_CONNECTION
aws?: AWS_CONNECTION & SSH_INFO

@Column({ nullable: true, type: "jsonb" })
aws_meta?: ENCRYPTED_AWS_CONNECTION__META
Expand Down
6 changes: 3 additions & 3 deletions backend/src/services/connections/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConnectionType } from "@common/enums"
import { AWS_CONNECTION } from "@common/types"
import { AWS_CONNECTION, SSH_INFO } from "@common/types"
import { AppDataSource } from "data-source"
import Error500InternalServer from "errors/error-500-internal-server"
import { Connections } from "models"
Expand All @@ -9,7 +9,7 @@ const save_connection = async ({
name,
id,
}: {
conn_meta: AWS_CONNECTION
conn_meta: AWS_CONNECTION & SSH_INFO
name: string
id: string
}) => {
Expand Down Expand Up @@ -52,7 +52,7 @@ const save_connection = async ({
remote_machine_url,
keypair_name,
keypair_id,
} as AWS_CONNECTION
} as AWS_CONNECTION & SSH_INFO
conn.connectionType = ConnectionType.AWS
conn.uuid = id
conn.name = name
Expand Down
21 changes: 12 additions & 9 deletions backend/src/suricata_setup/aws-services/aws_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import {
} from "./utils"

import { STEP_RESPONSE } from "@common/types"
import { ConnectionType } from "@common/enums"

type RESPONSE = STEP_RESPONSE<ConnectionType.AWS>
export async function aws_key_setup({
access_id,
secret_access_key,
region,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let client = new STSClient({
credentials: {
Expand Down Expand Up @@ -77,7 +79,7 @@ export async function aws_source_identification({
source_instance_id,
region: _region,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let client = new EC2Client({
credentials: {
Expand Down Expand Up @@ -144,7 +146,7 @@ export async function aws_os_selection({
ami,
region,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let conn = new EC2_CONN(access_id, secret_access_key, region)
let resp = await conn.image_from_ami(ami)
Expand All @@ -163,6 +165,7 @@ export async function aws_os_selection({
region,
ami,
virtualization_type: resp[0].VirtualizationType,
username: "ubuntu",
...rest,
},
}
Expand Down Expand Up @@ -197,7 +200,7 @@ export async function aws_instance_selection({
selected_instance_type,
ami,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
return {
success: "OK",
Expand Down Expand Up @@ -250,7 +253,7 @@ export async function aws_instance_creation({
selected_instance_type,
id,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let conn = new EC2_CONN(access_id, secret_access_key, region)
let resp = await conn.create_new_instance(ami, selected_instance_type, id)
Expand Down Expand Up @@ -309,7 +312,7 @@ export async function get_public_ip({
region,
destination_eni_id,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let client = new EC2Client({
credentials: {
Expand Down Expand Up @@ -369,7 +372,7 @@ export async function aws_mirror_target_creation({
destination_eni_id,
id,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let client = new EC2Client({
credentials: {
Expand Down Expand Up @@ -428,7 +431,7 @@ export async function aws_mirror_filter_creation({
mirror_rules,
id,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
let client = new EC2Client({
credentials: {
secretAccessKey: secret_access_key,
Expand Down Expand Up @@ -523,7 +526,7 @@ export async function aws_mirror_session_creation({
mirror_target_id,
id,
...rest
}: STEP_RESPONSE["data"]): Promise<STEP_RESPONSE> {
}: RESPONSE["data"]): Promise<RESPONSE> {
try {
let client = new EC2Client({
credentials: {
Expand Down
5 changes: 4 additions & 1 deletion backend/src/suricata_setup/aws-services/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EC2Client } from "@aws-sdk/client-ec2"
import { STEP_RESPONSE } from "@common/types"
import { ConnectionType } from "@common/enums"
import { randomUUID } from "crypto"
import { EC2_CONN } from "./create-ec2-instance"
import {
Expand All @@ -8,7 +9,9 @@ import {
delete_mirror_target,
} from "./mirroring"

export async function delete_aws_data(aws: STEP_RESPONSE["data"]) {
export async function delete_aws_data(
aws: STEP_RESPONSE<ConnectionType.AWS>["data"],
) {
let client = new EC2Client({
credentials: {
accessKeyId: aws.access_id,
Expand Down
Loading