@@ -8,7 +8,7 @@ import * as fs from 'fs'
8
8
import * as path from 'path'
9
9
import { getLogger , Logger } from '../../../shared/logger'
10
10
import { localize } from '../../../shared/utilities/vsCodeUtils'
11
- import { VueWebview } from '../../../webviews/main'
11
+ import { VueWebview , VueWebviewPanel } from '../../../webviews/main'
12
12
import { ExtContext } from '../../../shared/extensions'
13
13
import { telemetry } from '../../../shared/telemetry/telemetry'
14
14
import { AccessAnalyzer , SharedIniFileCredentials } from 'aws-sdk'
@@ -31,8 +31,8 @@ import { ExpiredTokenException } from '@aws-sdk/client-sso-oidc'
31
31
32
32
const defaultTerraformConfigPath = 'resources/policychecks-tf-default.yaml'
33
33
// Diagnostics for Custom checks are shared
34
- const customPolicyCheckDiagnosticCollection = vscode . languages . createDiagnosticCollection ( 'customPolicyCheck' )
35
- const validatePolicyDiagnosticCollection = vscode . languages . createDiagnosticCollection ( 'validatePolicy' )
34
+ export const customPolicyCheckDiagnosticCollection = vscode . languages . createDiagnosticCollection ( 'customPolicyCheck' )
35
+ export const validatePolicyDiagnosticCollection = vscode . languages . createDiagnosticCollection ( 'validatePolicy' )
36
36
37
37
export interface IamPolicyChecksInitialData {
38
38
checkNoNewAccessFilePath : string
@@ -56,7 +56,7 @@ export class IamPolicyChecksWebview extends VueWebview {
56
56
public static readonly sourcePath : string = 'src/awsService/accessanalyzer/vue/index.js'
57
57
public readonly id = 'iamPolicyChecks'
58
58
private static editedDocumentUri : vscode . Uri
59
- private static editedDocumentFileName : string
59
+ public static editedDocumentFileName : string
60
60
private static editedDocument : string
61
61
62
62
public constructor (
@@ -119,14 +119,18 @@ export class IamPolicyChecksWebview extends VueWebview {
119
119
// Send the current active text editor to Webview to show what is being targeted by the user
120
120
vscode . window . onDidChangeActiveTextEditor ( ( message : any ) => {
121
121
const editedFile = vscode . window . activeTextEditor ?. document
122
- IamPolicyChecksWebview . editedDocumentFileName = editedFile ! . uri . path
123
- IamPolicyChecksWebview . editedDocument = editedFile ! . getText ( )
124
- IamPolicyChecksWebview . editedDocumentUri = editedFile ! . uri
125
- this . onChangeInputPath . fire ( editedFile ! . uri . path )
122
+ if ( editedFile !== undefined ) {
123
+ IamPolicyChecksWebview . editedDocumentFileName = editedFile . uri . path
124
+ IamPolicyChecksWebview . editedDocument = editedFile . getText ( )
125
+ IamPolicyChecksWebview . editedDocumentUri = editedFile . uri
126
+ this . onChangeInputPath . fire ( editedFile . uri . path )
127
+ }
126
128
} )
127
129
vscode . workspace . onDidChangeTextDocument ( ( message : any ) => {
128
130
const editedFile = vscode . window . activeTextEditor ?. document
129
- IamPolicyChecksWebview . editedDocument = editedFile ! . getText ( )
131
+ if ( editedFile !== undefined ) {
132
+ IamPolicyChecksWebview . editedDocument = editedFile . getText ( )
133
+ }
130
134
} )
131
135
}
132
136
@@ -715,10 +719,10 @@ export class IamPolicyChecksWebview extends VueWebview {
715
719
}
716
720
717
721
public pushCustomCheckDiagnostic ( diagnostics : vscode . Diagnostic [ ] , finding : any , isBlocking : boolean ) {
718
- const message = ` ${ finding . findingType } : ${ finding . message } - Resource name: ${ finding . resourceName } , Policy name: ${ finding . policyName } `
719
- if ( message . includes ( 'existingPolicyDocument' ) ) {
720
- message . replace ( 'existingPolicyDocument' , 'reference document' )
721
- }
722
+ const findingMessage : string = finding . message . includes ( 'existingPolicyDocument' )
723
+ ? finding . message . replace ( 'existingPolicyDocument' , 'reference document' )
724
+ : finding . message
725
+ const message = ` ${ finding . findingType } : ${ findingMessage } - Resource name: ${ finding . resourceName } , Policy name: ${ finding . policyName } `
722
726
if ( finding . details . reasons ) {
723
727
finding . details . reasons . forEach ( ( reason : any ) => {
724
728
diagnostics . push (
@@ -744,7 +748,7 @@ export class IamPolicyChecksWebview extends VueWebview {
744
748
745
749
const Panel = VueWebview . compilePanel ( IamPolicyChecksWebview )
746
750
747
- export async function renderIamPolicyChecks ( context : ExtContext ) : Promise < void > {
751
+ export async function renderIamPolicyChecks ( context : ExtContext ) : Promise < VueWebviewPanel | undefined > {
748
752
const logger : Logger = getLogger ( )
749
753
try {
750
754
const client = new AccessAnalyzer ( { region : context . regionProvider . defaultRegionId } )
@@ -795,13 +799,14 @@ export async function renderIamPolicyChecks(context: ExtContext): Promise<void>
795
799
viewColumn : vscode . ViewColumn . Beside ,
796
800
title : localize ( 'AWS.iamPolicyChecks.title' , 'IAM Policy Checks' ) ,
797
801
} )
802
+ return wv
798
803
} catch ( err ) {
799
804
logger . error ( err as Error )
800
805
}
801
806
}
802
807
803
808
// Helper function to get document contents from a path
804
- async function _readCustomChecksFile ( input : string ) : Promise < string > {
809
+ export async function _readCustomChecksFile ( input : string ) : Promise < string > {
805
810
if ( fs . existsSync ( input ) ) {
806
811
return fs . readFileSync ( input ) . toString ( )
807
812
} else {
@@ -823,7 +828,7 @@ async function _readCustomChecksFile(input: string): Promise<string> {
823
828
}
824
829
825
830
//Check if Cfn and Tf tools are installed
826
- function arePythonToolsInstalled ( ) : boolean {
831
+ export function arePythonToolsInstalled ( ) : boolean {
827
832
const logger : Logger = getLogger ( )
828
833
let cfnToolInstalled = true
829
834
let tfToolInstalled = true
@@ -846,12 +851,12 @@ function arePythonToolsInstalled(): boolean {
846
851
return cfnToolInstalled && tfToolInstalled
847
852
}
848
853
849
- function isProcessNotFoundErr ( errMsg : string ) {
854
+ export function isProcessNotFoundErr ( errMsg : string ) {
850
855
return errMsg . includes ( 'command not found' ) || errMsg . includes ( 'ENOENT' )
851
856
}
852
857
853
858
// Since TypeScript can only get the CLI tool's error output as a string, we have to parse and sanitize it ourselves
854
- function parseCliErrorMessage ( message : string , documentType : PolicyChecksDocumentType ) : string {
859
+ export function parseCliErrorMessage ( message : string , documentType : PolicyChecksDocumentType ) : string {
855
860
const cfnMatch = message . match ( / E R R O R : .* / )
856
861
const botoMatch = message . match ( / (?< = b o t o c o r e \. e x c e p t i o n s \. ) .* / ) // Boto errors have a special match
857
862
const terraformMatch = message . match ( / A t t r i b u t e E r r o r : .* / ) // Terraform CLI responds with a different error schema... this catches invalid .json plans
@@ -876,7 +881,7 @@ function parseCliErrorMessage(message: string, documentType: PolicyChecksDocumen
876
881
return message
877
882
}
878
883
879
- function getCheckNoNewAccessErrorMessage ( finding : any ) {
884
+ export function getCheckNoNewAccessErrorMessage ( finding : any ) {
880
885
if ( finding . findingType === 'ERROR' ) {
881
886
if (
882
887
finding . message . includes (
@@ -888,7 +893,7 @@ function getCheckNoNewAccessErrorMessage(finding: any) {
888
893
}
889
894
}
890
895
891
- function getResultCssColor ( resultType : PolicyChecksResult ) : string {
896
+ export function getResultCssColor ( resultType : PolicyChecksResult ) : string {
892
897
switch ( resultType ) {
893
898
case 'Success' :
894
899
return 'var(--vscode-terminal-ansiGreen)'
@@ -899,17 +904,17 @@ function getResultCssColor(resultType: PolicyChecksResult): string {
899
904
}
900
905
}
901
906
902
- function isCloudFormationTemplate ( document : string ) : boolean {
907
+ export function isCloudFormationTemplate ( document : string ) : boolean {
903
908
const cfnFileTypes = [ '.yaml' , '.yml' , '.json' ]
904
909
return cfnFileTypes . some ( ( t ) => document . endsWith ( t ) )
905
910
}
906
911
907
- function isTerraformPlan ( document : string ) {
912
+ export function isTerraformPlan ( document : string ) {
908
913
const terraformPlanFileTypes = [ '.json' ]
909
914
return terraformPlanFileTypes . some ( ( t ) => document . endsWith ( t ) )
910
915
}
911
916
912
- function isJsonPolicyLanguage ( document : string ) {
917
+ export function isJsonPolicyLanguage ( document : string ) {
913
918
const policyLanguageFileTypes = [ '.json' ]
914
919
return policyLanguageFileTypes . some ( ( t ) => document . endsWith ( t ) )
915
920
}
0 commit comments