cdk-insights 0.7.4 → 0.8.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.
package/README.md CHANGED
@@ -16,44 +16,38 @@ npx cdk-insights scan
16
16
 
17
17
  # Or install in your project
18
18
  npm install --save-dev cdk-insights
19
- npx cdk-insights scan
20
- ```
21
19
 
22
- ### Development Setup
20
+ # Initialize npm scripts automatically
21
+ npx cdk-insights init
23
22
 
24
- For contributors, git hooks are automatically installed via Husky when you run:
25
-
26
- ```bash
27
- npm install
23
+ # Then use familiar npm commands
24
+ npm run cdk-insights
25
+ npm run cdk-insights:all
26
+ npm run cdk-insights:ci
28
27
  ```
29
28
 
30
- The hooks will:
29
+ ### What `cdk-insights init` adds
31
30
 
32
- - **Pre-commit**: Prompt for version bump and update changelog
33
- - **Pre-push**: Run tests before pushing
34
- - **Commit-msg**: Validate commit message format
35
-
36
- You can also add convenience scripts to your `package.json`:
31
+ The `init` command adds these npm scripts to your `package.json`:
37
32
 
38
33
  ```json
39
34
  {
40
35
  "scripts": {
41
- "scan": "cdk-insights scan",
42
- "scan:all": "cdk-insights scan --all",
43
- "scan:json": "cdk-insights scan --format json",
44
- "scan:markdown": "cdk-insights scan --format markdown",
45
- "scan:summary": "cdk-insights scan --format summary",
46
- "scan:with-issue": "cdk-insights scan --withIssue"
36
+ "cdk-insights": "cdk-insights scan",
37
+ "cdk-insights:all": "cdk-insights scan --all",
38
+ "cdk-insights:json": "cdk-insights scan --output json",
39
+ "cdk-insights:markdown": "cdk-insights scan --output markdown",
40
+ "cdk-insights:ci": "cdk-insights scan --all --output json --fail-on-critical"
47
41
  }
48
42
  }
49
43
  ```
50
44
 
51
- Then run: `npm run scan`
45
+ Use `npx cdk-insights init --all` to include additional scripts for GitHub issues and summary output.
52
46
 
53
47
  ### Quick Compatibility Check
54
48
 
55
49
  ```bash
56
- node --version # Should be 18+
50
+ node --version # Should be 20+
57
51
  ls cdk.json # Should exist in CDK project
58
52
  ```
59
53
 
@@ -73,12 +67,35 @@ ls cdk.json # Should exist in CDK project
73
67
 
74
68
  ## 💡 Usage Examples for AWS CDK Projects
75
69
 
76
- | Scenario | Command Example |
77
- | ---------------------- | -------------------------------------------------------------------- |
78
- | Full project scan | `npx cdk-insights scan --all --format summary` |
79
- | Security-only focus | `npx cdk-insights scan --services IAM,S3,KMS --rule-filter Security` |
80
- | Markdown report output | `npx cdk-insights scan --format markdown > report.md` |
81
- | CI/CD pipeline check | `npx cdk-insights scan --format json --fail-on-critical` |
70
+ | Scenario | Command Example |
71
+ | ---------------------- | --------------------------------------------------------------------- |
72
+ | Full project scan | `npx cdk-insights scan --all --output summary` |
73
+ | Security-only focus | `npx cdk-insights scan --services IAM,S3,KMS --rule-filter Security` |
74
+ | Markdown report output | `npx cdk-insights scan --output markdown > report.md` |
75
+ | CI/CD pipeline check | `npx cdk-insights scan --all --output json --fail-on-critical` |
76
+ | Create GitHub issue | `npx cdk-insights scan --output markdown --with-issue` |
77
+
78
+ ---
79
+
80
+ ## 🔄 CI/CD Integration
81
+
82
+ CDK Insights automatically detects CI environments (GitHub Actions, GitLab CI, Jenkins, etc.) and adjusts behavior accordingly:
83
+
84
+ ```yaml
85
+ # GitHub Actions example
86
+ - name: Run CDK Insights
87
+ run: npx cdk-insights scan --fail-on-critical
88
+ env:
89
+ CDK_INSIGHTS_LICENSE_KEY: ${{ secrets.CDK_INSIGHTS_LICENSE_KEY }}
90
+ ```
91
+
92
+ In CI mode, CDK Insights will:
93
+ - Automatically analyze all stacks
94
+ - Output JSON format for easy parsing
95
+ - Skip interactive prompts
96
+ - Exit with code 1 on critical issues (with `--fail-on-critical`)
97
+
98
+ 👉 [Full CI/CD Setup Guide →](https://github.com/TheLeePriest/cdk-insights/blob/main/docs/ci-setup.md)
82
99
 
83
100
  ---
84
101
 
@@ -1,9 +1,202 @@
1
1
  import { type IAspect } from 'aws-cdk-lib';
2
2
  import { type INagLogger, type NagLoggerComplianceData, type NagLoggerErrorData, type NagLoggerNonComplianceData, type NagLoggerNotApplicableData, type NagLoggerSuppressedData, type NagLoggerSuppressedErrorData, NagPack, type NagPackProps } from 'cdk-nag';
3
3
  import type { IConstruct } from 'constructs';
4
- export declare const createExtremelyHelpfulConsoleLogger: () => INagLogger;
5
- export declare const createCdkInsightsAspect: () => IAspect;
4
+ /** Metadata schema version for tracking compatibility */
5
+ declare const CDK_INSIGHTS_METADATA_VERSION = "2.2.0";
6
+ /** Prefix used to identify cdk-insights annotations in CloudFormation metadata */
7
+ declare const CDK_INSIGHTS_ANNOTATION_PREFIX = "cdk-insights::";
8
+ /** Confidence level for source location detection */
9
+ export type SourceLocationConfidence = 'high' | 'medium' | 'low';
10
+ /** Source location information for a construct */
11
+ export interface SourceLocation {
12
+ filePath: string;
13
+ line: number;
14
+ column: number;
15
+ /** End line for multi-line constructs */
16
+ endLine?: number;
17
+ /** End column */
18
+ endColumn?: number;
19
+ /** How confident we are in this location */
20
+ confidence?: SourceLocationConfidence;
21
+ /** The method used to determine this location */
22
+ method?: 'creation_stack' | 'metadata_trace' | 'source_analysis' | 'source_map' | 'ast_analysis' | 'fallback';
23
+ /** The function or class containing this construct */
24
+ enclosingScope?: string;
25
+ /** Code snippet around the construct definition */
26
+ codeSnippet?: string;
27
+ /** Original TypeScript file if source maps were used */
28
+ originalFile?: string;
29
+ }
30
+ /** Enhanced metadata captured for each construct */
31
+ export interface CdkInsightsMetadata {
32
+ source: 'cdk_insights';
33
+ version: string;
34
+ stackName: string;
35
+ stackId: string;
36
+ constructPath: string;
37
+ constructType: string;
38
+ friendlyName: string;
39
+ id?: string;
40
+ /** Logical ID in CloudFormation template */
41
+ logicalId?: string;
42
+ sourceLocation?: SourceLocation;
43
+ /** Parent construct path for hierarchy visualization */
44
+ parentPath?: string;
45
+ /** Number of child constructs */
46
+ childCount?: number;
47
+ /** Resource tags if available */
48
+ tags?: Record<string, string>;
49
+ timestamp: string;
50
+ /** The L2 construct type (e.g., "aws_s3.Bucket") if this is an L1 resource under an L2 */
51
+ l2ConstructType?: string;
52
+ /** The L2 construct ID */
53
+ l2ConstructId?: string;
54
+ /** Full construct hierarchy from root to this node */
55
+ constructHierarchy?: ConstructHierarchyEntry[];
56
+ /** CloudFormation resource type (e.g., "AWS::S3::Bucket") */
57
+ cfnResourceType?: string;
58
+ /** CloudFormation logical IDs this resource depends on */
59
+ dependencies?: string[];
60
+ /** CloudFormation logical IDs that depend on this resource */
61
+ dependents?: string[];
62
+ /** Properties that may have security/compliance implications */
63
+ sensitiveProperties?: SensitiveProperty[];
64
+ /** AWS service category (e.g., "Storage", "Compute", "Security") */
65
+ serviceCategory?: string;
66
+ /** Whether this resource uses default configuration (potential concern) */
67
+ usesDefaults?: boolean;
68
+ /** CDK construct library version if detectable */
69
+ cdkVersion?: string;
70
+ /**
71
+ * Human-readable description of what created this resource.
72
+ * Helpful for implicitly created resources like subnets from a VPC.
73
+ */
74
+ createdBy?: string;
75
+ /**
76
+ * Source location of the nearest ancestor that has a known source location.
77
+ * Used when this resource doesn't have a direct source location.
78
+ */
79
+ rootSourceLocation?: SourceLocation;
80
+ /**
81
+ * Search hint to help users find this resource in their codebase.
82
+ * Contains keywords to search for.
83
+ */
84
+ searchHint?: string;
85
+ }
86
+ /** Entry in the construct hierarchy */
87
+ export interface ConstructHierarchyEntry {
88
+ id: string;
89
+ type: string;
90
+ /** Fully qualified name if available (e.g., "aws-cdk-lib.aws_s3.Bucket") */
91
+ fqn?: string;
92
+ }
93
+ /** Property that may have security/compliance implications */
94
+ export interface SensitiveProperty {
95
+ /** Property path (e.g., "encryption.enabled") */
96
+ path: string;
97
+ /** Current value (redacted if sensitive) */
98
+ value: string | boolean | number | null;
99
+ /** Whether this is using a default value */
100
+ isDefault?: boolean;
101
+ /** Recommendation for this property */
102
+ recommendation?: string;
103
+ }
104
+ /** Configuration options for the logger */
105
+ export interface CdkInsightsLoggerOptions {
106
+ /** Log compliant resources (default: false) */
107
+ logCompliance?: boolean;
108
+ /** Log not-applicable rules (default: false) */
109
+ logNotApplicable?: boolean;
110
+ /** Enable verbose/debug logging (default: false) */
111
+ verbose?: boolean;
112
+ }
113
+ /** Configuration options for the aspect */
114
+ export interface CdkInsightsAspectOptions {
115
+ /** Run CDK-Nag compliance checks (default: true) */
116
+ runNagChecks?: boolean;
117
+ /** Capture source location from stack traces (default: true) */
118
+ captureSourceLocation?: boolean;
119
+ /** Capture resource tags (default: true) */
120
+ captureTags?: boolean;
121
+ /** Capture parent/child relationships (default: true) */
122
+ captureRelationships?: boolean;
123
+ /** Capture full construct hierarchy (default: true) */
124
+ captureHierarchy?: boolean;
125
+ /** Capture resource dependencies (default: true) */
126
+ captureDependencies?: boolean;
127
+ /** Capture sensitive properties that may have security implications (default: true) */
128
+ captureSensitiveProperties?: boolean;
129
+ /** Logger options */
130
+ logger?: CdkInsightsLoggerOptions;
131
+ /**
132
+ * Enable source file analysis for location detection (default: true)
133
+ * When enabled, the aspect will search source files to find construct definitions.
134
+ * This is useful when CDK debug mode is not enabled.
135
+ */
136
+ analyzeSourceFiles?: boolean;
137
+ /**
138
+ * Enable source map support for mapping compiled JS back to TS (default: true)
139
+ * When enabled and source maps are available, the aspect will use them to
140
+ * provide accurate TypeScript file locations even when stack traces point to
141
+ * compiled JavaScript files.
142
+ */
143
+ useSourceMaps?: boolean;
144
+ /**
145
+ * Include code snippets around construct definitions (default: true)
146
+ * When enabled, the source location will include a few lines of code
147
+ * around the construct definition for quick reference.
148
+ */
149
+ includeCodeSnippets?: boolean;
150
+ }
151
+ /**
152
+ * Helper function to check if CDK stack traces are enabled.
153
+ * Users should set CDK_DEBUG=true or add context @aws-cdk/core:stackTrace: true
154
+ * for the most reliable source location capture.
155
+ */
156
+ export declare const isCdkDebugEnabled: () => boolean;
157
+ /**
158
+ * Creates a configurable CDK-Nag logger with verbosity control.
159
+ */
160
+ export declare const createCdkInsightsLogger: (options?: CdkInsightsLoggerOptions) => INagLogger;
161
+ /**
162
+ * Creates a playful logger with more expressive messages.
163
+ * Useful for development and debugging.
164
+ */
165
+ export declare const createExtremelyHelpfulConsoleLogger: (options?: CdkInsightsLoggerOptions) => INagLogger;
166
+ /**
167
+ * Creates a CDK Insights aspect using functional composition.
168
+ * This is the recommended approach for new projects.
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * import { Aspects } from 'aws-cdk-lib';
173
+ * import { createCdkInsightsAspect } from 'cdk-insights';
174
+ *
175
+ * // Basic usage
176
+ * Aspects.of(app).add(createCdkInsightsAspect());
177
+ *
178
+ * // With options
179
+ * Aspects.of(app).add(createCdkInsightsAspect({
180
+ * runNagChecks: true,
181
+ * captureSourceLocation: true,
182
+ * captureTags: true,
183
+ * logger: { verbose: true }
184
+ * }));
185
+ *
186
+ * // Metadata-only mode (no CDK-Nag)
187
+ * Aspects.of(app).add(createCdkInsightsAspect({
188
+ * runNagChecks: false
189
+ * }));
190
+ * ```
191
+ */
192
+ export declare const createCdkInsightsAspect: (options?: CdkInsightsAspectOptions) => IAspect;
193
+ /**
194
+ * Legacy class-based logger for backward compatibility.
195
+ * @deprecated Use createCdkInsightsLogger() instead
196
+ */
6
197
  export declare class ExtremelyHelpfulConsoleLogger implements INagLogger {
198
+ private readonly options;
199
+ constructor(options?: CdkInsightsLoggerOptions);
7
200
  onCompliance(data: NagLoggerComplianceData): void;
8
201
  onNonCompliance(data: NagLoggerNonComplianceData): void;
9
202
  onSuppressed(data: NagLoggerSuppressedData): void;
@@ -11,8 +204,38 @@ export declare class ExtremelyHelpfulConsoleLogger implements INagLogger {
11
204
  onSuppressedError(data: NagLoggerSuppressedErrorData): void;
12
205
  onNotApplicable(data: NagLoggerNotApplicableData): void;
13
206
  }
207
+ /**
208
+ * Class-based CDK Insights aspect for backward compatibility.
209
+ * Extends NagPack for integration with CDK-Nag ecosystem.
210
+ *
211
+ * @deprecated Use createCdkInsightsAspect() for new projects
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * import { Aspects } from 'aws-cdk-lib';
216
+ * import { CdkInsightsAspect } from 'cdk-insights';
217
+ *
218
+ * Aspects.of(app).add(new CdkInsightsAspect());
219
+ * ```
220
+ */
14
221
  export declare class CdkInsightsAspect extends NagPack implements IAspect {
15
222
  private readonly delegate;
16
- constructor(props?: NagPackProps);
223
+ private readonly options;
224
+ constructor(props?: NagPackProps & CdkInsightsAspectOptions);
17
225
  visit(node: IConstruct): void;
18
226
  }
227
+ /** Re-export constants for external use */
228
+ export { CDK_INSIGHTS_METADATA_VERSION, CDK_INSIGHTS_ANNOTATION_PREFIX };
229
+ /**
230
+ * Clears all internal caches. Useful for testing or when processing
231
+ * multiple independent CDK apps in the same process.
232
+ */
233
+ export declare const clearCaches: () => void;
234
+ /**
235
+ * Returns cache statistics for debugging/monitoring.
236
+ */
237
+ export declare const getCacheStats: () => {
238
+ sourceFiles: number;
239
+ constructLocations: number;
240
+ sourceMaps: number;
241
+ };