|
| 1 | +import AsyncRetry from "async-retry" |
| 2 | +import { v4 as uuidv4, validate } from "uuid" |
| 3 | +import fs from "fs" |
| 4 | +import { prompt } from "enquirer" |
| 5 | +import { GCP_REGIONS_SUPPORTED, wait_for_global_operation, wait_for_regional_operation, wait_for_zonal_operation } from "./gcpUtils" |
| 6 | +import { GCP_CONN } from "./gcp_apis" |
| 7 | +import chalk from "chalk" |
| 8 | +import ora from "ora"; |
| 9 | +import { google } from "@google-cloud/compute/build/protos/protos" |
| 10 | + |
| 11 | +const spinner = ora() |
| 12 | + |
| 13 | +const verifyAccountDetails = async () => { |
| 14 | + const gcp_regions = GCP_REGIONS_SUPPORTED.map(e => ({ |
| 15 | + name: e, |
| 16 | + })) |
| 17 | + const resp = await prompt([ |
| 18 | + { |
| 19 | + type: "input", |
| 20 | + name: "_projectName", |
| 21 | + message: "GCP Project Name", |
| 22 | + }, { |
| 23 | + type: "input", |
| 24 | + initial: "default", |
| 25 | + name: "_networkName", |
| 26 | + message: "GCP Network to mirror", |
| 27 | + }, { |
| 28 | + type: "autocomplete", |
| 29 | + name: "_zoneName", |
| 30 | + message: "Select your GCP zone", |
| 31 | + initial: 1, |
| 32 | + choices: gcp_regions, |
| 33 | + }, { |
| 34 | + type: "input", |
| 35 | + name: "_keyPath", |
| 36 | + message: "Path to GCP key file", |
| 37 | + validate: (path: string) => { |
| 38 | + if (fs.existsSync(path)) { |
| 39 | + return true |
| 40 | + } else { |
| 41 | + // @ts-ignore |
| 42 | + let text = chalk.redBright(`GCP Key file not found at ${path}`) |
| 43 | + return text |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + ]) |
| 48 | + |
| 49 | + spinner.text = "Validating account details" |
| 50 | + spinner.start() |
| 51 | + // @ts-ignore Destructuring is improperly done |
| 52 | + const { _projectName: project, _networkName: network, _zoneName: zone, _keyPath: keyFilePath } = resp; |
| 53 | + |
| 54 | + const key = (fs.readFileSync(keyFilePath)).toString("utf-8"); |
| 55 | + |
| 56 | + let conn = new GCP_CONN(key, zone, project) |
| 57 | + await conn.test_connection() |
| 58 | + await conn.get_zone({ zone }) |
| 59 | + spinner.succeed("Validated account details") |
| 60 | + spinner.stop() |
| 61 | + spinner.clear() |
| 62 | + return { project, network, zone, key } |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +const deletePacketMirroringResources = async ( |
| 67 | + conn: GCP_CONN, |
| 68 | + mirroring: google.cloud.compute.v1.IPacketMirroring[] |
| 69 | +) => { |
| 70 | + if (mirroring.length == 0) { |
| 71 | + throw new Error("No existing packet mirroring instances found") |
| 72 | + } |
| 73 | + |
| 74 | + const instanceChoices = mirroring.flatMap( |
| 75 | + (mirror) => mirror.mirroredResources.instances.map( |
| 76 | + inst => { |
| 77 | + const splits = inst.url.split("/"); |
| 78 | + return splits[splits.length - 1] |
| 79 | + } |
| 80 | + ) |
| 81 | + ) |
| 82 | + const subnetChoices = mirroring.flatMap( |
| 83 | + (mirror) => mirror.mirroredResources.subnetworks.map( |
| 84 | + inst => { |
| 85 | + const splits = inst.url.split("/"); |
| 86 | + return splits[splits.length - 1] |
| 87 | + } |
| 88 | + ) |
| 89 | + ) |
| 90 | + const tagChoices = mirroring.flatMap( |
| 91 | + (mirror) => mirror.mirroredResources.tags |
| 92 | + ) |
| 93 | + |
| 94 | + const availableChoices = [] |
| 95 | + if (instanceChoices.length > 0) { |
| 96 | + availableChoices.push("INSTANCE") |
| 97 | + } |
| 98 | + if (subnetChoices.length > 0) { |
| 99 | + availableChoices.push("SUBNET") |
| 100 | + } |
| 101 | + if (tagChoices.length > 0) { |
| 102 | + availableChoices.push("TAG") |
| 103 | + } |
| 104 | + |
| 105 | + const sourceTypeResp = await prompt([ |
| 106 | + { |
| 107 | + type: "autocomplete", |
| 108 | + name: "_packetMirrorName", |
| 109 | + message: "Select Packet Mirroring instance", |
| 110 | + initial: 0, |
| 111 | + choices: mirroring.filter((inst) => inst.name.startsWith("metlo")).map((inst) => inst.name) |
| 112 | + }, { |
| 113 | + type: "select", |
| 114 | + name: "_sourceType", |
| 115 | + message: "Select your mirror source type", |
| 116 | + initial: 0, |
| 117 | + choices: availableChoices, |
| 118 | + }, |
| 119 | + ]) |
| 120 | + let sourceType = sourceTypeResp["_sourceType"] |
| 121 | + let packetMirrorName = sourceTypeResp["_packetMirrorName"] |
| 122 | + |
| 123 | + if (sourceType === "INSTANCE") { |
| 124 | + const instanceNameResp = await prompt([ |
| 125 | + { |
| 126 | + type: "autocomplete", |
| 127 | + name: "_name", |
| 128 | + message: "Enter the mirror source instance name to remove", |
| 129 | + choices: instanceChoices |
| 130 | + |
| 131 | + } |
| 132 | + ]) |
| 133 | + spinner.start("Verifying mirror source details") |
| 134 | + const instanceName = instanceNameResp['_name'].trim() |
| 135 | + |
| 136 | + const resources = mirroring.find((mirror) => mirror.name === packetMirrorName).mirroredResources |
| 137 | + resources.instances = resources.instances.filter((inst) => !inst.url.includes(instanceName)) |
| 138 | + resources.instances = resources.instances.length > 0 ? resources.instances : null |
| 139 | + |
| 140 | + let resp = await conn.remove_packet_mirroring_resources({ packetMirrorName, newMirroredResources: resources }) |
| 141 | + } else if (sourceType === "SUBNET") { |
| 142 | + const subnetNameResp = await prompt([ |
| 143 | + { |
| 144 | + type: "autocomplete", |
| 145 | + name: "_name", |
| 146 | + message: "Enter the mirror source subnet name to remove", |
| 147 | + choices: subnetChoices |
| 148 | + } |
| 149 | + ]) |
| 150 | + spinner.start("Verifying mirror source details") |
| 151 | + const subnetName = subnetNameResp['_name'].trim() |
| 152 | + |
| 153 | + const resources = mirroring.find((mirror) => mirror.name === packetMirrorName).mirroredResources |
| 154 | + resources.subnetworks = resources.subnetworks.filter((inst) => !inst.url.includes(subnetName)) |
| 155 | + resources.subnetworks = resources.subnetworks.length > 0 ? resources.subnetworks : null |
| 156 | + |
| 157 | + let resp = await conn.remove_packet_mirroring_resources({ packetMirrorName, newMirroredResources: resources }) |
| 158 | + } else if (sourceType === "TAG") { |
| 159 | + const tagNameResp = await prompt([ |
| 160 | + { |
| 161 | + type: "autocomplete", |
| 162 | + name: "_name", |
| 163 | + message: "Enter the mirror source tag name to remove", |
| 164 | + choices: tagChoices |
| 165 | + } |
| 166 | + ]) |
| 167 | + spinner.start("Verifying mirror source details") |
| 168 | + let tagName = tagNameResp["_name"].trim() |
| 169 | + |
| 170 | + const resources = mirroring.find((mirror) => mirror.name === packetMirrorName).mirroredResources |
| 171 | + resources.tags = resources.tags.filter((inst) => !inst.includes(tagName)) |
| 172 | + |
| 173 | + let resp = await conn.remove_packet_mirroring_resources({ packetMirrorName, newMirroredResources: resources }) |
| 174 | + } |
| 175 | + spinner.succeed("Deleted resource from packet mirroring Succesfully") |
| 176 | + spinner.stop() |
| 177 | + spinner.clear() |
| 178 | + return {} |
| 179 | +} |
| 180 | + |
| 181 | +export const gcpTrafficMirrorDelete = async () => { |
| 182 | + const id = uuidv4() |
| 183 | + const data = {} |
| 184 | + try { |
| 185 | + const { project, zone, network, key } = await verifyAccountDetails() |
| 186 | + const networkUrl = `https://www.googleapis.com/compute/v1/projects/${project}/global/networks/${network}` |
| 187 | + const conn = new GCP_CONN(key, zone, project); |
| 188 | + data["zone"] = zone |
| 189 | + data["project"] = project |
| 190 | + |
| 191 | + const [packetMirrors] = await conn.list_packet_mirroring() |
| 192 | + |
| 193 | + await deletePacketMirroringResources(conn, packetMirrors) |
| 194 | + } catch (e) { |
| 195 | + spinner.fail() |
| 196 | + console.log(chalk.bgRedBright("Metlo packet mirroring item removal failed. This might help in debugging it.")) |
| 197 | + console.log(e) |
| 198 | + console.log(data) |
| 199 | + } |
| 200 | +} |
0 commit comments