@infrascan/cli 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +45 -17
  2. package/package.json +3 -2
package/dist/index.mjs CHANGED
@@ -69,27 +69,39 @@ var GraphCmd = class extends CommandLineAction {
69
69
  };
70
70
 
71
71
  // src/commands/scan.ts
72
- import { readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
73
- import { resolve as resolve2, join as join2 } from "path";
72
+ import { writeFileSync as writeFileSync2 } from "fs";
73
+ import { resolve as resolve3, join as join2 } from "path";
74
74
  import {
75
75
  CommandLineAction as CommandLineAction2
76
76
  } from "@rushstack/ts-command-line";
77
77
  import buildFsConnector2 from "@infrascan/fs-connector";
78
78
  import {
79
79
  fromIni,
80
+ fromNodeProviderChain,
80
81
  fromTemporaryCredentials
81
82
  } from "@aws-sdk/credential-providers";
82
83
  import {
83
- loadSharedConfigFiles,
84
- DEFAULT_PROFILE
84
+ loadSharedConfigFiles
85
85
  } from "@smithy/shared-ini-file-loader";
86
+
87
+ // src/config.ts
88
+ import { readFileSync as readFileSync2 } from "fs";
89
+ import { resolve as resolve2 } from "path";
90
+ import { env } from "process";
91
+ import { DEFAULT_PROFILE } from "@smithy/shared-ini-file-loader";
86
92
  function getConfig(path) {
87
93
  const resolvedConfigPath = resolve2(path);
88
94
  return JSON.parse(readFileSync2(resolvedConfigPath, "utf8"));
89
95
  }
96
+ function getDefaultRegion(parsedAwsConfig, currentProfile) {
97
+ const profile = currentProfile ?? env.AWS_PROFILE ?? DEFAULT_PROFILE;
98
+ return env.AWS_REGION ?? parsedAwsConfig?.[profile]?.region;
99
+ }
100
+
101
+ // src/commands/scan.ts
90
102
  function writeScanMetadata(outputDirectory, metadata) {
91
103
  writeFileSync2(
92
- join2(resolve2(outputDirectory), "metadata.json"),
104
+ join2(resolve3(outputDirectory), "metadata.json"),
93
105
  JSON.stringify(metadata),
94
106
  "utf8"
95
107
  );
@@ -110,9 +122,10 @@ function resolveCredentialProvider(profile, roleToAssume) {
110
122
  clientConfig: { region }
111
123
  });
112
124
  }
113
- throw new Error(
114
- "An error occurred while resolving credentials for Infrascan \u2014 no profile or role given."
125
+ console.log(
126
+ "No profile or role given, falling back to default provider chain."
115
127
  );
128
+ return (region) => fromNodeProviderChain({ clientConfig: { region } });
116
129
  }
117
130
  var ScanCmd = class extends CommandLineAction2 {
118
131
  constructor(infrascanClient) {
@@ -128,8 +141,7 @@ var ScanCmd = class extends CommandLineAction2 {
128
141
  parameterLongName: "--config",
129
142
  parameterShortName: "-c",
130
143
  argumentName: "PATH_TO_CONFIG",
131
- description: "Config to use for the scan.",
132
- defaultValue: "./config.json"
144
+ description: "Config to use for the scan."
133
145
  });
134
146
  this._outputDirectory = this.defineStringParameter({
135
147
  parameterLongName: "--output",
@@ -138,27 +150,43 @@ var ScanCmd = class extends CommandLineAction2 {
138
150
  description: "Location to save scan output to.",
139
151
  defaultValue: "./state"
140
152
  });
153
+ this._regions = this.defineStringListParameter({
154
+ parameterLongName: "--region",
155
+ argumentName: "REGION_TO_SCAN",
156
+ description: "The region to scan. This flag can be provided many times e.g. --region us-east-1 --region eu-west-1"
157
+ });
141
158
  }
142
159
  async onExecute() {
143
- const scanConfig = getConfig(this._config.value);
144
- const connector = buildFsConnector2(this._outputDirectory.value, {
145
- createTargetDirectory: true
146
- });
147
- const metadata = [];
148
160
  const awsConfig = await loadSharedConfigFiles().catch((err) => {
149
161
  console.warn(
150
162
  `Failed to load AWS config file, falling back to @infrascan/sdk defaults. (${err.message})`
151
163
  );
152
164
  return void 0;
153
165
  });
166
+ const defaultRegionFromEnv = getDefaultRegion(awsConfig?.configFile);
167
+ const runtimeRegionsToScan = this._regions.values.length > 0 ? this._regions.values : void 0;
168
+ const scanConfig = this._config.value ? getConfig(this._config.value) : [
169
+ {
170
+ defaultRegion: defaultRegionFromEnv
171
+ }
172
+ ];
173
+ const connector = buildFsConnector2(this._outputDirectory.value, {
174
+ createTargetDirectory: true
175
+ });
176
+ const metadata = [];
154
177
  for (const accountConfig of scanConfig) {
155
- const { profile, roleToAssume, regions } = accountConfig;
178
+ const {
179
+ profile,
180
+ roleToAssume,
181
+ regions,
182
+ defaultRegion: configDefaultRegion
183
+ } = accountConfig;
156
184
  const credentials = resolveCredentialProvider(profile, roleToAssume);
157
- const defaultRegion = awsConfig?.configFile?.[profile ?? DEFAULT_PROFILE]?.region;
185
+ const defaultRegion = configDefaultRegion ?? getDefaultRegion(awsConfig?.configFile, profile);
158
186
  const accountMetadata = await this.infrascanClient.performScan(
159
187
  credentials,
160
188
  connector,
161
- { regions, defaultRegion }
189
+ { regions: runtimeRegionsToScan ?? regions, defaultRegion }
162
190
  );
163
191
  metadata.push(accountMetadata);
164
192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infrascan/cli",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "Thin command line wrapper around the infrascan sdk",
5
5
  "main": "dist/index.mjs",
6
6
  "scripts": {
@@ -39,7 +39,7 @@
39
39
  "@aws-sdk/credential-providers": "3.428.0",
40
40
  "@infrascan/aws": "^0.3.0",
41
41
  "@infrascan/fs-connector": "^0.2.2",
42
- "@infrascan/sdk": "^0.3.0",
42
+ "@infrascan/sdk": "^0.3.1",
43
43
  "@rushstack/ts-command-line": "^4.15.2",
44
44
  "@smithy/shared-ini-file-loader": "^2.2.2",
45
45
  "minimatch": "^6.1.6"
@@ -47,6 +47,7 @@
47
47
  "devDependencies": {
48
48
  "@aws-sdk/types": "3.428.0",
49
49
  "@infrascan/shared-types": "^0.2.2",
50
+ "@smithy/types": "^2.4.0",
50
51
  "ts-node": "^10.9.1",
51
52
  "typescript": "^5.1.3"
52
53
  }