Skip to content

Commit 33ae7da

Browse files
committed
feat: add publicIp missing services
1 parent 34b2a2c commit 33ae7da

File tree

11 files changed

+162
-7
lines changed

11 files changed

+162
-7
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ Scan cloud infrastructure via the [Azure SDK](https://github.com/Azure/azure-sdk
44

55
<!-- toc -->
66

7-
- [Install](#install)
8-
- [Authentication](#authentication)
9-
- [Supported Services](#supported-services)
10-
<!-- tocstop -->
7+
- [CloudGraph Azure Provider](#cloudgraph-azure-provider)
8+
- [Docs](#docs)
9+
- [Install](#install)
10+
- [Authentication](#authentication)
11+
- [Supported Services](#supported-services)
12+
- [Development](#development)
13+
- [Testing](#testing)
1114

1215
## Docs
1316

@@ -95,6 +98,7 @@ CloudGraph needs read permissions in order to ingest your data. To keep things e
9598
| postgreSqlServers | resourceGroup, databasePostgreSql |
9699
| privateDns | resourceGroup |
97100
| publicIp | networkInterface, resourceGroup |
101+
| publicIpPrefix | |
98102
| recoveryInstances | recoveryVaults, resourceGroup |
99103
| recoveryPolicies | recoveryVaults, resourceGroup |
100104
| recoveryVaults | recoveryInstances, recoveryPolicies, resourceGroup |

src/enums/serviceAliases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default {
5656
[services.postgreSqlServers]: 'postgreSqlServers',
5757
[services.privateDns]: 'privateDnsZones',
5858
[services.publicIp]: 'publicIps',
59+
[services.publicIpPrefix]: 'publicIpPrefixes',
5960
[services.recoveryVaults]: 'recoveryVaults',
6061
[services.recoveryInstances]: 'recoveryInstances',
6162
[services.recoveryPolicies]: 'recoveryPolicies',

src/enums/serviceMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import AzurePolicyAssignment from '../services/policyAssignment'
5151
import AzurePostgreSqlServer from '../services/postgreSqlServers'
5252
import AzurePrivateDns from '../services/privateDns'
5353
import AzurePublicIp from '../services/publicIp'
54+
import AzurePublicIpPrefix from '../services/publicIpPrefix'
5455
import AzureRecoveryVault from '../services/recoveryVaults'
5556
import AzureRecoveryInstance from '../services/recoveryInstances'
5657
import AzureRecoveryPolicy from '../services/recoveryPolicies'
@@ -160,6 +161,7 @@ export default {
160161
[services.postgreSqlServers]: AzurePostgreSqlServer,
161162
[services.privateDns]: AzurePrivateDns,
162163
[services.publicIp]: AzurePublicIp,
164+
[services.publicIpPrefix]: AzurePublicIpPrefix,
163165
[services.recoveryVaults]: AzureRecoveryVault,
164166
[services.recoveryInstances]: AzureRecoveryInstance,
165167
[services.recoveryPolicies]: AzureRecoveryPolicy,

src/enums/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default {
6262
postgreSqlServers: 'postgreSqlServers',
6363
privateDns: 'privateDns',
6464
publicIp: 'publicIp',
65+
publicIpPrefix: 'publicIpPrefix',
6566
recoveryVaults: 'recoveryVaults',
6667
recoveryInstances: 'recoveryInstances',
6768
recoveryPolicies: 'recoveryPolicies',

src/properties/logger.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default {
2020
foundAKSManagedClusters: (num: number): string =>
2121
`Found ${num} AKS managed clusters`,
2222
/* App Gateway */
23-
foundApplicationGateway: (num: number): string => `Found ${num} Application Gateways`,
23+
foundApplicationGateway: (num: number): string =>
24+
`Found ${num} Application Gateways`,
2425
/* App Service */
2526
foundAppServiceEnvironments: (num: number): string =>
2627
`Found ${num} app service environments`,
@@ -152,11 +153,14 @@ export default {
152153
`Found ${num} private dns zones`,
153154
// Public Ips
154155
foundPublicIps: (num: number): string => `Found ${num} public ips`,
156+
foundPublicIpPrefixes: (num: number): string =>
157+
`Found ${num} public ip prefixes`,
155158
// Recovery Vaults
156159
foundRecoveryVaults: (num: number): string => `Found ${num} recovery vaults`,
157160
foundRecoveryInstances: (num: number): string =>
158-
`Found ${num} recovery instances`,
159-
foundRecoveryPolicies: (num: number): string => `Found ${num} recovery policies`,
161+
`Found ${num} recovery instances`,
162+
foundRecoveryPolicies: (num: number): string =>
163+
`Found ${num} recovery policies`,
160164
// RedisCache
161165
foundRedisCaches: (num: number): string => `Found ${num} Redis caches`,
162166
// Replication Appliances
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { NetworkManagementClient, PublicIPPrefix } from '@azure/arm-network'
2+
import { PagedAsyncIterableIterator } from '@azure/core-paging'
3+
import CloudGraph from '@cloudgraph/sdk'
4+
5+
import azureLoggerText from '../../properties/logger'
6+
import { AzureServiceInput, TagMap } from '../../types'
7+
import { tryCatchWrapper } from '../../utils'
8+
import { lowerCaseLocation } from '../../utils/format'
9+
import { getResourceGroupFromEntity } from '../../utils/idParserUtils'
10+
11+
const { logger } = CloudGraph
12+
const lt = { ...azureLoggerText }
13+
const serviceName = 'PublicIp'
14+
15+
export interface RawAzurePublicIpPrefix
16+
extends Omit<PublicIPPrefix, 'tags' | 'location'> {
17+
region: string
18+
resourceGroupId: string
19+
Tags: TagMap
20+
}
21+
22+
export default async ({
23+
regions,
24+
config,
25+
}: AzureServiceInput): Promise<{
26+
[property: string]: RawAzurePublicIpPrefix[]
27+
}> => {
28+
try {
29+
const { tokenCredentials, subscriptionId } = config
30+
const client = new NetworkManagementClient(tokenCredentials, subscriptionId)
31+
32+
const publicIpPrefixData: PublicIPPrefix[] = []
33+
await tryCatchWrapper(
34+
async () => {
35+
const publicIpPrefixIterable: PagedAsyncIterableIterator<PublicIPPrefix> =
36+
client.publicIPPrefixes.listAll()
37+
for await (const publicIpPrefix of publicIpPrefixIterable) {
38+
publicIpPrefix && publicIpPrefixData.push(publicIpPrefix)
39+
}
40+
},
41+
{
42+
service: serviceName,
43+
client,
44+
scope: 'publicIPPrefixes',
45+
operation: 'listAll',
46+
}
47+
)
48+
49+
const result: {
50+
[property: string]: RawAzurePublicIpPrefix[]
51+
} = {}
52+
let numOfGroups = 0
53+
publicIpPrefixData.forEach(({ tags, location, ...rest }) => {
54+
const region = lowerCaseLocation(location)
55+
if (regions.includes(region)) {
56+
if (!result[region]) {
57+
result[region] = []
58+
}
59+
const resourceGroupId = getResourceGroupFromEntity(rest)
60+
result[region].push({
61+
...rest,
62+
region,
63+
resourceGroupId,
64+
Tags: tags || {},
65+
})
66+
numOfGroups += 1
67+
}
68+
})
69+
logger.debug(lt.foundPublicIpPrefixes(numOfGroups))
70+
71+
return result
72+
} catch (e) {
73+
logger.error(e)
74+
return {}
75+
}
76+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AzurePublicIpPrefix } from '../../types/generated'
2+
import { formatTagsFromMap } from '../../utils/format'
3+
import { RawAzurePublicIpPrefix } from './data'
4+
5+
export default ({
6+
service,
7+
account: subscriptionId,
8+
region,
9+
}: {
10+
service: RawAzurePublicIpPrefix
11+
account: string
12+
region: string
13+
}): AzurePublicIpPrefix => {
14+
const { id, name, ipTags, resourceGroupId, Tags } = service
15+
return {
16+
id,
17+
name,
18+
region,
19+
resourceGroupId,
20+
subscriptionId,
21+
ipTags,
22+
tags: formatTagsFromMap(Tags),
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Service } from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import mutation from './mutation'
5+
import getData from './data'
6+
7+
export default class AzurePublicIpPrefix
8+
extends BaseService
9+
implements Service
10+
{
11+
format = format.bind(this)
12+
13+
getData = getData.bind(this)
14+
15+
mutation = mutation
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `mutation($input: [AddazurePublicIpPrefixInput!]!) {
2+
addazurePublicIpPrefix(input: $input, upsert: true) {
3+
numUids
4+
}
5+
}`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type azurePublicIpTags
2+
@generate(
3+
query: { get: false, query: true, aggregate: false }
4+
mutation: { add: false, delete: false }
5+
subscription: false
6+
) {
7+
ipTagType: String @search(by: [hash, regexp])
8+
tag: String @search(by: [hash, regexp])
9+
}
10+
11+
type azurePublicIpPrefix implements azureResource
12+
@generate(
13+
query: { get: true, query: true, aggregate: true }
14+
mutation: { add: true, delete: false }
15+
)
16+
@key(fields: "id") {
17+
ipTags: [azurePublicIpTags]
18+
}

0 commit comments

Comments
 (0)