@@ -3,6 +3,9 @@ import EFS, {
33 FileSystemDescription ,
44 DescribeFileSystemsRequest ,
55 DescribeFileSystemsResponse ,
6+ DescribeFileSystemPolicyRequest ,
7+ FileSystemPolicyDescription ,
8+ Policy ,
69} from 'aws-sdk/clients/efs'
710import { AWSError } from 'aws-sdk/lib/error'
811import CloudGraph from '@cloudgraph/sdk'
@@ -20,6 +23,42 @@ const serviceName = 'EFS'
2023const errorLog = new AwsErrorLog ( serviceName )
2124const endpoint = initTestEndpoint ( serviceName )
2225
26+ const getFileSystemPolicy = async ( {
27+ efs,
28+ FileSystemId,
29+ } : {
30+ efs : EFS
31+ FileSystemId : string
32+ } ) : Promise < Policy > =>
33+ new Promise < Policy > ( resolve => {
34+ const args : DescribeFileSystemPolicyRequest = { FileSystemId }
35+ try {
36+ efs . describeFileSystemPolicy (
37+ args ,
38+ ( err : AWSError , data : FileSystemPolicyDescription ) => {
39+ if ( err ) {
40+ errorLog . generateAwsErrorLog ( {
41+ functionName : 'efs:describeFileSystemPolicy' ,
42+ err,
43+ } )
44+ }
45+
46+ /**
47+ * No policy for this file system
48+ */
49+ if ( isEmpty ( data ) ) {
50+ return resolve ( '' )
51+ }
52+
53+ const { Policy : policy } = data
54+ resolve ( policy )
55+ }
56+ )
57+ } catch ( error ) {
58+ resolve ( '' )
59+ }
60+ } )
61+
2362const listFileSystems = async ( {
2463 efs,
2564 region,
@@ -29,7 +68,7 @@ const listFileSystems = async ({
2968 efs : EFS
3069 region : string
3170 token ?: string
32- resolveRegion : Function
71+ resolveRegion : ( ) => void
3372} ) : Promise < FileSystemDescription [ ] > =>
3473 new Promise < FileSystemDescription [ ] > ( resolve => {
3574 const efsList : FileSystemDescription [ ] = [ ]
@@ -57,10 +96,7 @@ const listFileSystems = async ({
5796 return resolveRegion ( )
5897 }
5998
60- const {
61- FileSystems : fileSystems = [ ] ,
62- NextMarker : token ,
63- } : { FileSystems ?: any ; NextMarker ?: any } = data
99+ const { FileSystems : fileSystems = [ ] , NextMarker : token } = data
64100
65101 efsList . push ( ...fileSystems )
66102
@@ -100,6 +136,7 @@ const listFileSystems = async ({
100136
101137export interface RawAwsEfs extends Omit < FileSystemDescription , 'Tags' > {
102138 region : string
139+ policy : Policy
103140 Tags ?: TagMap
104141}
105142
@@ -115,21 +152,23 @@ export default async ({
115152 new Promise ( async resolve => {
116153 const efsFileSystems : RawAwsEfs [ ] = [ ]
117154 const regionPromises = [ ]
155+ const policyPromises = [ ]
118156
119157 /**
120158 * Get all the EFS File Systems
121159 */
122160
123- regions . split ( ',' ) . map ( region => {
161+ regions . split ( ',' ) . forEach ( region => {
124162 const efs = new EFS ( { ...config , region, endpoint } )
125163 const regionPromise = new Promise < void > ( async resolveRegion => {
126164 const efsList = await listFileSystems ( { efs, region, resolveRegion } )
127165 if ( ! isEmpty ( efsList ) ) {
128166 efsFileSystems . push (
129- ...efsList . map ( efs => ( {
130- ...efs ,
167+ ...efsList . map ( efsItem => ( {
168+ ...efsItem ,
131169 region,
132- Tags : convertAwsTagsToTagMap ( efs . Tags ) ,
170+ policy : '' ,
171+ Tags : convertAwsTagsToTagMap ( efsItem . Tags ) ,
133172 } ) )
134173 )
135174 }
@@ -139,6 +178,21 @@ export default async ({
139178 } )
140179
141180 await Promise . all ( regionPromises )
181+
182+ // get policy for each rest api
183+ efsFileSystems . forEach ( ( { FileSystemId : id , region } , idx ) => {
184+ const efs = new EFS ( { ...config , region, endpoint } )
185+ const modelPromise = new Promise < void > ( async resolvePolicy => {
186+ efsFileSystems [ idx ] . policy = await getFileSystemPolicy ( {
187+ efs,
188+ FileSystemId : id ,
189+ } )
190+ resolvePolicy ( )
191+ } )
192+ policyPromises . push ( modelPromise )
193+ } )
194+ await Promise . all ( policyPromises )
195+
142196 errorLog . reset ( )
143197
144198 resolve ( groupBy ( efsFileSystems , 'region' ) )
0 commit comments