Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #419 from JupiterOne/fix/cloud-asset-cloud-run-key…
Browse files Browse the repository at this point in the history
…-bug

INT-2448 - Change cloud run service key and its typeAndKeyFromResourceIdentifier
  • Loading branch information
eXtremeX authored Feb 3, 2022
2 parents b503889 + 9736ff8 commit 58e47c1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/steps/cloud-run/__snapshots__/converters.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_rawData": Array [
Object {
"name": "default",
Expand Down
14 changes: 7 additions & 7 deletions src/steps/cloud-run/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -231,8 +231,8 @@ Object {
"collectedRelationships": Array [
Object {
"_class": "MANAGES",
"_fromEntityKey": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd|manages|cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_fromEntityKey": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service|manages|cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_toEntityKey": "cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_type": "google_cloud_run_service_manages_configuration",
"displayName": "MANAGES",
Expand All @@ -255,7 +255,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -457,8 +457,8 @@ Object {
"collectedRelationships": Array [
Object {
"_class": "MANAGES",
"_fromEntityKey": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd|manages|cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_fromEntityKey": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service|manages|cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_toEntityKey": "cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_type": "google_cloud_run_service_manages_route",
"displayName": "MANAGES",
Expand All @@ -481,7 +481,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "projects/j1-gc-integration-dev-v2/locations/us-central1/services/example-cloud-run-service",
"_rawData": Array [
Object {
"name": "default",
Expand Down
2 changes: 2 additions & 0 deletions src/steps/cloud-run/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('#createCloudRunServiceEntity', () => {
createCloudRunServiceEntity(
getMockCloudRunService(),
DEFAULT_INTEGRATION_CONFIG_PROJECT_ID,
// This would otherwise be returned by cacheCloudRunServiceKeyAndUid()
`projects/${DEFAULT_INTEGRATION_CONFIG_PROJECT_ID}/locations/us-central1/services/example-cloud-run-service`,
),
).toMatchSnapshot();
});
Expand Down
11 changes: 8 additions & 3 deletions src/steps/cloud-run/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ export interface MetadataComputedPropertyData {
duplicateProperties: string[];
}

export function getCloudRunServiceKey(uid: string) {
return `cloudrun_service:${uid}`;
export function getCloudRunServiceKey(
projectId: string,
location: string,
name: string,
) {
return `projects/${projectId}/locations/${location}/services/${name}`;
}

export function getCloudRunRouteKey(uid: string) {
Expand All @@ -35,6 +39,7 @@ export function getCloudRunConfigurationKey(uid: string) {
export function createCloudRunServiceEntity(
data: run_v1.Schema$Service,
projectId: string,
key: string,
) {
// Build webLink
let webLink = '';
Expand All @@ -54,7 +59,7 @@ export function createCloudRunServiceEntity(
assign: {
_class: ENTITY_CLASS_CLOUD_RUN_SERVICE,
_type: ENTITY_TYPE_CLOUD_RUN_SERVICE,
_key: getCloudRunServiceKey(data.metadata?.uid as string),
_key: key,
name: data.metadata?.name,
function: ['workload-management'],
displayName: data.metadata?.name as string,
Expand Down
24 changes: 19 additions & 5 deletions src/steps/cloud-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
RelationshipClass,
} from '@jupiterone/integration-sdk-core';
import { IntegrationConfig, IntegrationStepContext } from '../../types';
import {
cacheCloudRunServiceKeyAndUid,
getCloudRunServiceKeyFromUid,
} from '../../utils/jobState';
import { CloudRunClient } from './client';
import {
STEP_CLOUD_RUN_SERVICES,
Expand All @@ -22,7 +26,6 @@ import {
createCloudRunConfigurationEntity,
createCloudRunRouteEntity,
createCloudRunServiceEntity,
getCloudRunServiceKey,
MetadataComputedPropertyData,
} from './converters';

Expand All @@ -37,10 +40,17 @@ export async function fetchCloudRunServices(
const client = new CloudRunClient({ config });

await client.iterateCloudRunServices(async (service) => {
const key = await cacheCloudRunServiceKeyAndUid(
jobState,
service,
client.projectId,
);
const serviceEntity = createCloudRunServiceEntity(
service,
client.projectId,
key,
);

await jobState.addEntity(serviceEntity);
});
}
Expand All @@ -63,9 +73,11 @@ export async function fetchCloudRunRoutes(
(owner) => owner.kind === 'Service',
);
if (ownerService) {
const serviceEntity = await jobState.findEntity(
getCloudRunServiceKey(ownerService.uid as string),
const serviceKey = await getCloudRunServiceKeyFromUid(
jobState,
ownerService.uid as string,
);
const serviceEntity = await jobState.findEntity(serviceKey);
if (serviceEntity) {
await jobState.addRelationship(
createDirectRelationship({
Expand Down Expand Up @@ -115,9 +127,11 @@ export async function fetchCloudRunConfigurations(
(owner) => owner.kind === 'Service',
);
if (ownerService) {
const serviceEntity = await jobState.findEntity(
getCloudRunServiceKey(ownerService.uid as string),
const serviceKey = await getCloudRunServiceKeyFromUid(
jobState,
ownerService.uid as string,
);
const serviceEntity = await jobState.findEntity(serviceKey);
if (serviceEntity) {
await jobState.addRelationship(
createDirectRelationship({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Object {
exports[`getTypeAndKeyFromResourceIdentifier should find the correct keys for all available resources 8`] = `
Object {
"identifier": "//run.googleapis.com/projects/12345/locations/us-east1/services/abcdef12345",
"key": "cloudrun_service:abcdef12345",
"key": "projects/12345/locations/us-east1/services/abcdef12345",
"type": "google_cloud_run_service",
}
`;
Expand Down
5 changes: 1 addition & 4 deletions src/utils/iamBindings/typeToKeyGeneratorMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
BIG_QUERY_TABLE_ENTITY_TYPE,
} from '../../steps/big-query';
import { ENTITY_TYPE_CLOUD_RUN_SERVICE } from '../../steps/cloud-run/constants';
import { getCloudRunServiceKey } from '../../steps/cloud-run/converters';
import {
ENTITY_TYPE_COMPUTE_BACKEND_BUCKET,
ENTITY_TYPE_COMPUTE_BACKEND_SERVICE,
Expand Down Expand Up @@ -115,9 +114,7 @@ export const J1_TYPE_TO_KEY_GENERATOR_MAP: {
context!.jobState,
finalIdentifierKeyMap(id),
)) ?? finalIdentifierKeyMap(id),
[ENTITY_TYPE_CLOUD_RUN_SERVICE]: customPrefixAndIdKeyMap(
getCloudRunServiceKey,
),
[ENTITY_TYPE_CLOUD_RUN_SERVICE]: fullPathKeyMap,
[ENTITY_TYPE_COMPUTE_BACKEND_BUCKET]: selfLinkKeyMap,
[ENTITY_TYPE_COMPUTE_BACKEND_SERVICE]: selfLinkKeyMap,
[ENTITY_TYPE_COMPUTE_DISK]: customPrefixAndIdKeyMap(getComputeDiskKey),
Expand Down
28 changes: 27 additions & 1 deletion src/utils/jobState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JobState } from '@jupiterone/integration-sdk-core';
import { cloudresourcemanager_v3, compute_v1 } from 'googleapis';
import { cloudresourcemanager_v3, compute_v1, run_v1 } from 'googleapis';
import { getCloudRunServiceKey } from '../steps/cloud-run/converters';

export const PEERED_NETWORKS = 'network:all_peerings';

Expand All @@ -25,6 +26,31 @@ export async function getProjectNameFromId(
return jobState.getData<string>(`projectId:${projectId}`);
}

export async function cacheCloudRunServiceKeyAndUid(
jobState,
cloudRunService: run_v1.Schema$Service,
projectId: string,
): Promise<string> {
const location = cloudRunService.metadata?.labels
? cloudRunService.metadata?.labels['cloud.googleapis.com/location']
: '';
const name = cloudRunService.metadata?.name;

const uid = cloudRunService.metadata?.uid;
const key = getCloudRunServiceKey(projectId, location, name as string);

await jobState.setData(`cloudRunServiceUid:${uid}`, key);

return key;
}

export async function getCloudRunServiceKeyFromUid(
jobState: JobState,
uid: string,
) {
return jobState.getData<string>(`cloudRunServiceUid:${uid}`);
}

export async function getPeeredNetworks(jobState: JobState) {
return jobState.getData<string[]>(PEERED_NETWORKS);
}
Expand Down

0 comments on commit 58e47c1

Please sign in to comment.