Skip to content

Commit c99417b

Browse files
author
Marco Franceschi
committed
feat: Fetched extra grant information for kms service
1 parent b6df1f2 commit c99417b

4 files changed

Lines changed: 92 additions & 2 deletions

File tree

src/services/kms/data.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import KMS, {
1111
KeyMetadata,
1212
ListKeysRequest,
1313
ListKeysResponse,
14+
GrantListEntry
1415
} from 'aws-sdk/clients/kms'
1516

1617
import { TagMap } from '../../types'
@@ -35,6 +36,7 @@ export type AwsKms = KeyListEntry &
3536
Tags: TagMap
3637
keyRotationEnabled: boolean
3738
Aliases?: AliasListEntry[]
39+
Grants?: GrantListEntry[]
3840
}
3941

4042
export default async ({
@@ -53,6 +55,7 @@ export default async ({
5355
const policyPromises = []
5456
const tagPromises = []
5557
const aliasesPromises = []
58+
const grantsPromises = []
5659

5760
/**
5861
* Step 1) for all regions, list the kms keys
@@ -373,11 +376,43 @@ export default async ({
373376
resolveAliases()
374377
})
375378
)
376-
377379
aliasesPromises.push(aliasesPromise)
380+
381+
const grantsPromise = new Promise<void>(resolveGrants =>
382+
kms.listGrants({ KeyId }, (err, data) => {
383+
if (err) {
384+
errorLog.generateAwsErrorLog({
385+
functionName: 'kms:listGrants',
386+
err,
387+
})
388+
resolveGrants()
389+
}
390+
391+
/**
392+
* No grants data
393+
*/
394+
395+
if (isEmpty(data)) {
396+
return resolveGrants()
397+
}
398+
399+
/**
400+
* Add the grants to the key
401+
*/
402+
403+
const { Grants: grants } = data || {}
404+
405+
kmsData[idx].Grants = grants
406+
407+
resolveGrants()
408+
})
409+
)
410+
411+
grantsPromises.push(grantsPromise)
378412
})
379413

380414
await Promise.all(aliasesPromises)
415+
await Promise.all(grantsPromises)
381416
errorLog.reset()
382417

383418
resolve(groupBy(kmsData, 'region'))

src/services/kms/format.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { generateUniqueId } from '@cloudgraph/sdk'
2-
import { AliasListEntry } from 'aws-sdk/clients/kms'
2+
import { AliasListEntry, GrantListEntry } from 'aws-sdk/clients/kms'
33

44
import { AwsKms } from './data'
55
import {
66
AwsKms as AwsKmsType,
77
AwsKmsAliasListEntry,
8+
AwsKmsGrantListEntry,
89
} from '../../types/generated'
910
import { formatTagsFromMap, formatIamJsonPolicy } from '../../utils/format'
1011

@@ -38,6 +39,7 @@ export default ({
3839
DeletionDate: deletionDate,
3940
ValidTo: validTo,
4041
Aliases: aliases = [],
42+
Grants: grants = []
4143
} = key
4244

4345
const formatAliases = (
@@ -58,6 +60,27 @@ export default ({
5860
)
5961
}
6062

63+
const formatGrants = (
64+
grants?: GrantListEntry[]
65+
): AwsKmsGrantListEntry[] => {
66+
return (
67+
grants?.map(a => ({
68+
id: generateUniqueId({
69+
arn,
70+
...a,
71+
}),
72+
grantId: a.GrantId,
73+
name: a.Name,
74+
creationDate: a.CreationDate?.toISOString(),
75+
granteePrincipal: a.GranteePrincipal,
76+
retiringPrincipal: a.RetiringPrincipal,
77+
issuingAccount: a.IssuingAccount,
78+
keyId: a.KeyId,
79+
operations: a.Operations
80+
})) || []
81+
)
82+
}
83+
6184
return {
6285
accountId: account,
6386
arn,
@@ -78,5 +101,6 @@ export default ({
78101
deletionDate: deletionDate?.toISOString(),
79102
validTo: validTo?.toISOString(),
80103
aliases: formatAliases(aliases),
104+
grants: formatGrants(grants),
81105
}
82106
}

src/services/kms/schema.graphql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
type awsKmsGrantListEntry
2+
@generate(
3+
query: { get: false, query: true, aggregate: false }
4+
mutation: { add: false, delete: false }
5+
subscription: false
6+
) {
7+
id: String! @id
8+
grantId: String @search(by: [hash, regexp])
9+
keyId: String @search(by: [hash, regexp])
10+
name: String @search(by: [hash, regexp])
11+
creationDate: DateTime @search(by: [day])
12+
granteePrincipal: String @search(by: [hash, regexp])
13+
retiringPrincipal: String @search(by: [hash, regexp])
14+
issuingAccount: String @search(by: [hash, regexp])
15+
operations: [String] @search(by: [hash, regexp])
16+
}
17+
118
type awsKmsAliasListEntry
219
@generate(
320
query: { get: false, query: true, aggregate: false }
@@ -28,6 +45,7 @@ type awsKms implements awsBaseService @key(fields: "id") {
2845
deletionDate: DateTime @search(by: [day])
2946
validTo: DateTime @search(by: [day])
3047
aliases: [awsKmsAliasListEntry]
48+
grants: [awsKmsGrantListEntry]
3149
lambda: [awsLambda] @hasInverse(field: kms) #change to plural
3250
cloudtrail: [awsCloudtrail] @hasInverse(field: kms) #change to plural
3351
redshiftCluster: [awsRedshiftCluster] @hasInverse(field: kms) #change to plural

src/types/generated.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,7 @@ export type AwsKms = AwsBaseService & {
33263326
elasticSearchDomains?: Maybe<Array<Maybe<AwsElasticSearchDomain>>>;
33273327
emrCluster?: Maybe<Array<Maybe<AwsEmrCluster>>>;
33283328
enabled?: Maybe<Scalars['Boolean']>;
3329+
grants?: Maybe<Array<Maybe<AwsKmsGrantListEntry>>>;
33293330
keyManager?: Maybe<Scalars['String']>;
33303331
keyRotationEnabled?: Maybe<Scalars['Boolean']>;
33313332
keyState?: Maybe<Scalars['String']>;
@@ -3356,6 +3357,18 @@ export type AwsKmsAliasListEntry = {
33563357
targetKeyId?: Maybe<Scalars['String']>;
33573358
};
33583359

3360+
export type AwsKmsGrantListEntry = {
3361+
creationDate?: Maybe<Scalars['DateTime']>;
3362+
grantId?: Maybe<Scalars['String']>;
3363+
granteePrincipal?: Maybe<Scalars['String']>;
3364+
id: Scalars['String'];
3365+
issuingAccount?: Maybe<Scalars['String']>;
3366+
keyId?: Maybe<Scalars['String']>;
3367+
name?: Maybe<Scalars['String']>;
3368+
operations?: Maybe<Array<Maybe<Scalars['String']>>>;
3369+
retiringPrincipal?: Maybe<Scalars['String']>;
3370+
};
3371+
33593372
export type AwsLambda = AwsBaseService & {
33603373
appSync?: Maybe<Array<Maybe<AwsAppSync>>>;
33613374
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;

0 commit comments

Comments
 (0)