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

feat: add support for all aws regions and their az #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as pino from "pino";
import axios from "axios";
import { normalizeServiceName } from "../lib/utils";
const L = pino();
const SOURCES = ["managed_policies", "service_names"];
const SOURCES = ["managed_policies", "service_names", "regions"];

function classFromJson({ jsonObj, key }: { jsonObj: any; key: string }) {
let out =
Expand Down Expand Up @@ -39,6 +39,12 @@ async function fetchConstants({ target }: { target: string }) {
json.contents[10].contents[3].contents[6].contents;
fs.writeJsonSync("data/all_services_title.json", service_titles);
break;
case "regions":
const { data: regions_json } = await axios.get(
"https://raw.githubusercontent.com/jsonmaur/aws-regions/master/regions.json"
);
fs.writeJsonSync("data/all_regions.json", regions_json);
break;
default:
throw `invalid target: ${target}`;
}
Expand Down Expand Up @@ -90,6 +96,21 @@ async function updateConstants({ target }: { target: string }) {
fs.writeFileSync("./lib/services.ts", payload);
break;
}
case "regions":
let data = fs.readJSONSync("./data/all_regions.json");
let out: any = {};
_.each(data, region => {
out[normalizeServiceName(region.code, { useBlacklist: false })] =
region.code;
_.each(
region.zones,
zone =>
(out[normalizeServiceName(zone, { useBlacklist: false })] = zone)
);
});
let payload = classFromJson({ jsonObj: out, key: "REGIONS" });
fs.writeFileSync("./lib/regions.ts", payload);
break;
default:
throw `invalid target: ${target}`;
}
Expand Down
1 change: 1 addition & 0 deletions data/all_regions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"N. Virginia","full_name":"US East (N. Virginia)","code":"us-east-1","public":true,"zones":["us-east-1a","us-east-1b","us-east-1c","us-east-1d","us-east-1e","us-east-1f"]},{"name":"Ohio","full_name":"US East (Ohio)","code":"us-east-2","public":true,"zones":["us-east-2a","us-east-2b","us-east-2c"]},{"name":"N. California","full_name":"US West (N. California)","code":"us-west-1","public":true,"zone_limit":2,"zones":["us-west-1a","us-west-1b","us-west-1c"]},{"name":"Oregon","full_name":"US West (Oregon)","code":"us-west-2","public":true,"zones":["us-west-2a","us-west-2b","us-west-2c","us-west-2d"]},{"name":"GovCloud West","full_name":"AWS GovCloud (US)","code":"us-gov-west-1","public":false,"zones":["us-gov-west-1a","us-gov-west-1b","us-gov-west-1c"]},{"name":"GovCloud East","full_name":"AWS GovCloud (US-East)","code":"us-gov-east-1","public":false,"zones":["us-gov-east-1a","us-gov-east-1b","us-gov-east-1c"]},{"name":"Canada","full_name":"Canada (Central)","code":"ca-central-1","public":true,"zones":["ca-central-1a","ca-central-1b","ca-central-1c"]},{"name":"Stockholm","full_name":"EU (Stockholm)","code":"eu-north-1","public":true,"zones":["eu-north-1a","eu-north-1b","eu-north-1c"]},{"name":"Ireland","full_name":"EU (Ireland)","code":"eu-west-1","public":true,"zones":["eu-west-1a","eu-west-1b","eu-west-1c"]},{"name":"London","full_name":"EU (London)","code":"eu-west-2","public":true,"zones":["eu-west-2a","eu-west-2b","eu-west-2c"]},{"name":"Paris","full_name":"EU (Paris)","code":"eu-west-3","public":true,"zones":["eu-west-3a","eu-west-3b","eu-west-3c"]},{"name":"Frankfurt","full_name":"EU (Frankfurt)","code":"eu-central-1","public":true,"zones":["eu-central-1a","eu-central-1b","eu-central-1c"]},{"name":"Milan","full_name":"EU (Milan)","code":"eu-south-1","public":true,"zones":["eu-south-1a","eu-south-1b","eu-south-1c"]},{"name":"Cape Town","full_name":"Africa (Cape Town)","code":"af-south-1","public":true,"zones":["af-south-1a","af-south-1b","af-south-1c"]},{"name":"Tokyo","full_name":"Asia Pacific (Tokyo)","code":"ap-northeast-1","public":true,"zone_limit":3,"zones":["ap-northeast-1a","ap-northeast-1b","ap-northeast-1c","ap-northeast-1d"]},{"name":"Seoul","full_name":"Asia Pacific (Seoul)","code":"ap-northeast-2","public":true,"zones":["ap-northeast-2a","ap-northeast-2b","ap-northeast-2c"]},{"name":"Osaka","full_name":"Asia Pacific (Osaka-Local)","code":"ap-northeast-3","public":false,"zones":["ap-northeast-3a"]},{"name":"Singapore","full_name":"Asia Pacific (Singapore)","code":"ap-southeast-1","public":true,"zones":["ap-southeast-1a","ap-southeast-1b","ap-southeast-1c"]},{"name":"Sydney","full_name":"Asia Pacific (Sydney)","code":"ap-southeast-2","public":true,"zones":["ap-southeast-2a","ap-southeast-2b","ap-southeast-2c"]},{"name":"Hong Kong","full_name":"Asia Pacific (Hong Kong)","code":"ap-east-1","public":true,"zones":["ap-east-1a","ap-east-1b","ap-east-1c"]},{"name":"Mumbai","full_name":"Asia Pacific (Mumbai)","code":"ap-south-1","public":true,"zones":["ap-south-1a","ap-south-1b","ap-south-1c"]},{"name":"São Paulo","full_name":"South America (São Paulo)","code":"sa-east-1","public":true,"zone_limit":2,"zones":["sa-east-1a","sa-east-1b","sa-east-1c"]},{"name":"Bahrain","full_name":"Middle East (Bahrain)","code":"me-south-1","public":true,"zones":["me-south-1a","me-south-1b","me-south-1c"]},{"name":"Beijing","full_name":"China (Beijing)","code":"cn-north-1","public":false,"zones":["cn-north-1a","cn-north-1b"]},{"name":"Ningxia","full_name":"China (Ningxia)","code":"cn-northwest-1","public":false,"zones":["cn-northwest-1a","cn-northwest-1b","cn-northwest-1c"]}]
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./principals";
export * from "./policies";
export * from "./services";
export * from "./regions";
106 changes: 106 additions & 0 deletions lib/regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// NOTE: THIS IS MACHINE GENERATED. CHANGES WILL BE OVERWRITTEN!

export class Regions {
public static readonly US_EAST_1 = "us-east-1"
public static readonly US_EAST_1_A = "us-east-1a"
public static readonly US_EAST_1_B = "us-east-1b"
public static readonly US_EAST_1_C = "us-east-1c"
public static readonly US_EAST_1_D = "us-east-1d"
public static readonly US_EAST_1_E = "us-east-1e"
public static readonly US_EAST_1_F = "us-east-1f"
public static readonly US_EAST_2 = "us-east-2"
public static readonly US_EAST_2_A = "us-east-2a"
public static readonly US_EAST_2_B = "us-east-2b"
public static readonly US_EAST_2_C = "us-east-2c"
public static readonly US_WEST_1 = "us-west-1"
public static readonly US_WEST_1_A = "us-west-1a"
public static readonly US_WEST_1_B = "us-west-1b"
public static readonly US_WEST_1_C = "us-west-1c"
public static readonly US_WEST_2 = "us-west-2"
public static readonly US_WEST_2_A = "us-west-2a"
public static readonly US_WEST_2_B = "us-west-2b"
public static readonly US_WEST_2_C = "us-west-2c"
public static readonly US_WEST_2_D = "us-west-2d"
public static readonly US_GOV_WEST_1 = "us-gov-west-1"
public static readonly US_GOV_WEST_1_A = "us-gov-west-1a"
public static readonly US_GOV_WEST_1_B = "us-gov-west-1b"
public static readonly US_GOV_WEST_1_C = "us-gov-west-1c"
public static readonly US_GOV_EAST_1 = "us-gov-east-1"
public static readonly US_GOV_EAST_1_A = "us-gov-east-1a"
public static readonly US_GOV_EAST_1_B = "us-gov-east-1b"
public static readonly US_GOV_EAST_1_C = "us-gov-east-1c"
public static readonly CA_CENTRAL_1 = "ca-central-1"
public static readonly CA_CENTRAL_1_A = "ca-central-1a"
public static readonly CA_CENTRAL_1_B = "ca-central-1b"
public static readonly CA_CENTRAL_1_C = "ca-central-1c"
public static readonly EU_NORTH_1 = "eu-north-1"
public static readonly EU_NORTH_1_A = "eu-north-1a"
public static readonly EU_NORTH_1_B = "eu-north-1b"
public static readonly EU_NORTH_1_C = "eu-north-1c"
public static readonly EU_WEST_1 = "eu-west-1"
public static readonly EU_WEST_1_A = "eu-west-1a"
public static readonly EU_WEST_1_B = "eu-west-1b"
public static readonly EU_WEST_1_C = "eu-west-1c"
public static readonly EU_WEST_2 = "eu-west-2"
public static readonly EU_WEST_2_A = "eu-west-2a"
public static readonly EU_WEST_2_B = "eu-west-2b"
public static readonly EU_WEST_2_C = "eu-west-2c"
public static readonly EU_WEST_3 = "eu-west-3"
public static readonly EU_WEST_3_A = "eu-west-3a"
public static readonly EU_WEST_3_B = "eu-west-3b"
public static readonly EU_WEST_3_C = "eu-west-3c"
public static readonly EU_CENTRAL_1 = "eu-central-1"
public static readonly EU_CENTRAL_1_A = "eu-central-1a"
public static readonly EU_CENTRAL_1_B = "eu-central-1b"
public static readonly EU_CENTRAL_1_C = "eu-central-1c"
public static readonly EU_SOUTH_1 = "eu-south-1"
public static readonly EU_SOUTH_1_A = "eu-south-1a"
public static readonly EU_SOUTH_1_B = "eu-south-1b"
public static readonly EU_SOUTH_1_C = "eu-south-1c"
public static readonly AF_SOUTH_1 = "af-south-1"
public static readonly AF_SOUTH_1_A = "af-south-1a"
public static readonly AF_SOUTH_1_B = "af-south-1b"
public static readonly AF_SOUTH_1_C = "af-south-1c"
public static readonly AP_NORTHEAST_1 = "ap-northeast-1"
public static readonly AP_NORTHEAST_1_A = "ap-northeast-1a"
public static readonly AP_NORTHEAST_1_B = "ap-northeast-1b"
public static readonly AP_NORTHEAST_1_C = "ap-northeast-1c"
public static readonly AP_NORTHEAST_1_D = "ap-northeast-1d"
public static readonly AP_NORTHEAST_2 = "ap-northeast-2"
public static readonly AP_NORTHEAST_2_A = "ap-northeast-2a"
public static readonly AP_NORTHEAST_2_B = "ap-northeast-2b"
public static readonly AP_NORTHEAST_2_C = "ap-northeast-2c"
public static readonly AP_NORTHEAST_3 = "ap-northeast-3"
public static readonly AP_NORTHEAST_3_A = "ap-northeast-3a"
public static readonly AP_SOUTHEAST_1 = "ap-southeast-1"
public static readonly AP_SOUTHEAST_1_A = "ap-southeast-1a"
public static readonly AP_SOUTHEAST_1_B = "ap-southeast-1b"
public static readonly AP_SOUTHEAST_1_C = "ap-southeast-1c"
public static readonly AP_SOUTHEAST_2 = "ap-southeast-2"
public static readonly AP_SOUTHEAST_2_A = "ap-southeast-2a"
public static readonly AP_SOUTHEAST_2_B = "ap-southeast-2b"
public static readonly AP_SOUTHEAST_2_C = "ap-southeast-2c"
public static readonly AP_EAST_1 = "ap-east-1"
public static readonly AP_EAST_1_A = "ap-east-1a"
public static readonly AP_EAST_1_B = "ap-east-1b"
public static readonly AP_EAST_1_C = "ap-east-1c"
public static readonly AP_SOUTH_1 = "ap-south-1"
public static readonly AP_SOUTH_1_A = "ap-south-1a"
public static readonly AP_SOUTH_1_B = "ap-south-1b"
public static readonly AP_SOUTH_1_C = "ap-south-1c"
public static readonly SA_EAST_1 = "sa-east-1"
public static readonly SA_EAST_1_A = "sa-east-1a"
public static readonly SA_EAST_1_B = "sa-east-1b"
public static readonly SA_EAST_1_C = "sa-east-1c"
public static readonly ME_SOUTH_1 = "me-south-1"
public static readonly ME_SOUTH_1_A = "me-south-1a"
public static readonly ME_SOUTH_1_B = "me-south-1b"
public static readonly ME_SOUTH_1_C = "me-south-1c"
public static readonly CN_NORTH_1 = "cn-north-1"
public static readonly CN_NORTH_1_A = "cn-north-1a"
public static readonly CN_NORTH_1_B = "cn-north-1b"
public static readonly CN_NORTHWEST_1 = "cn-northwest-1"
public static readonly CN_NORTHWEST_1_A = "cn-northwest-1a"
public static readonly CN_NORTHWEST_1_B = "cn-northwest-1b"
public static readonly CN_NORTHWEST_1_C = "cn-northwest-1c"
}
17 changes: 9 additions & 8 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import * as _ from "lodash";
*/
export function normalizeServiceName(
name: string,
opts?: { snakeCase?: boolean; stripPrefix?: boolean }
opts?: { snakeCase?: boolean; stripPrefix?: boolean; useBlacklist?: boolean }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing blacklist for F_S was conflicting with the region AF_SOUTH_1 so seemed like a good addition to have an optional setting here for future use

): string {
const blacklist = ["S_3", "EC_2", "IO_T", "V_2", "F_S", "AP_I"];
opts = _.defaults(opts, {
snakeCase: true,
stripPrefix: false
stripPrefix: false,
useBlacklist: true
});
let normName = name;

Expand All @@ -21,13 +22,13 @@ export function normalizeServiceName(

if (opts.snakeCase) {
let modKey = _.snakeCase(name).toUpperCase();
blacklist.forEach(ent => {
if (modKey.indexOf(ent) >= 0) {
modKey = modKey.replace(ent, ent.replace("_", ""));
}
});
opts.useBlacklist &&
blacklist.forEach(ent => {
if (modKey.indexOf(ent) >= 0) {
modKey = modKey.replace(ent, ent.replace("_", ""));
}
});
normName = modKey;
}

return normName;
}
16 changes: 15 additions & 1 deletion test/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
FederatedPrincipals,
ServicePrincipals,
ManagedPolicies
ManagedPolicies, Regions
} from "../lib";

describe("principles", () => {
Expand All @@ -20,3 +20,17 @@ describe("policies", () => {
expect(Object.keys(ManagedPolicies).length).toEqual(619);
});
});

describe("regions", () => {
test("public", () => {
expect(Regions.AP_SOUTH_1).toEqual("ap-south-1");
});

test("govcloud", () => {
expect(Regions.US_GOV_EAST_1).toEqual("us-gov-east-1");
})

test("china", () => {
expect(Regions.CN_NORTHWEST_1).toEqual("cn-northwest-1");
});
})
4 changes: 4 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ describe("normalizeServiceName", () => {
})
).toEqual("s3");
});

test("useBlacklist", () => {
expect(normalizeServiceName("af-south-1", {useBlacklist: false})).toEqual("AF_SOUTH_1");
});
});