Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions lib/get-resources.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,16 @@ function kccSaId(ref, project) {
return '?';
}

function kccPolicyMemberId(item, project, runServiceRefs) {
export function kccBucketId(ref, bucketRefs) {
return ref.external || bucketRefs.get(ref.name) || ref.name || '?';
}

export function canonicalPubSubId(id, project, collection) {
if (!id || id.startsWith('projects/')) return id;
return `projects/${project}/${collection}/${id}`;
}

function kccPolicyMemberId(item, project, runServiceRefs, bucketRefs) {
const ref = (item.spec && item.spec.resourceRef) || {};
switch (ref.kind) {
case 'Project':
Expand All @@ -344,7 +353,7 @@ function kccPolicyMemberId(item, project, runServiceRefs) {
case 'ArtifactRegistryRepository':
return 'ar/' + kccArId(ref);
case 'StorageBucket':
return 'bucket/' + (ref.external || ref.name || '?');
return 'bucket/' + kccBucketId(ref, bucketRefs);
case 'RunService': {
const external = ref.external || '';
const m = external.match(/projects\/[^/]+\/locations\/([^/]+)\/services\/([^/]+)$/);
Expand Down Expand Up @@ -524,10 +533,8 @@ export async function collectNamespace(namespace, { includeSystem = false } = {}
let liveBucket = byType('storage.googleapis.com/Bucket').map((a) => a.displayName);
if (!includeSystem) liveBucket = liveBucket.filter((b) => !isSystemBucket(b, project));
push('StorageBucket', liveBucket, 'gcp');
push('StorageBucket', await kccField(
'StorageBucket', namespace,
(i) => i.spec && i.spec.resourceID,
), 'kcc');
const storageBucketItems = await listCustomObjects('StorageBucket', namespace);
push('StorageBucket', storageBucketItems.map((i) => resourceId(i)).filter(Boolean), 'kcc');

// --- ComputeAddress (регіональні й глобальні) -------------------------------
// Asset Inventory інколи повертає вже видалені адреси (перевірено
Expand Down Expand Up @@ -631,9 +638,15 @@ export async function collectNamespace(namespace, { includeSystem = false } = {}
push('EventarcTrigger', await kccField('EventarcTrigger', namespace, (i) => kccScopedId(i)), 'kcc');

push('PubSubTopic', byType('pubsub.googleapis.com/Topic').map((a) => a.displayName), 'gcp');
push('PubSubTopic', await kccField('PubSubTopic', namespace, resourceId), 'kcc');
push('PubSubTopic', await kccField(
'PubSubTopic', namespace,
(i) => canonicalPubSubId(resourceId(i), project, 'topics'),
), 'kcc');
push('PubSubSubscription', byType('pubsub.googleapis.com/Subscription').map((a) => a.displayName), 'gcp');
push('PubSubSubscription', await kccField('PubSubSubscription', namespace, resourceId), 'kcc');
push('PubSubSubscription', await kccField(
'PubSubSubscription', namespace,
(i) => canonicalPubSubId(resourceId(i), project, 'subscriptions'),
), 'kcc');

push('SecretManagerSecret', byType('secretmanager.googleapis.com/Secret').map((a) => a.displayName), 'gcp');
push('SecretManagerSecret', await kccField('SecretManagerSecret', namespace, resourceId), 'kcc');
Expand Down Expand Up @@ -719,10 +732,11 @@ export async function collectNamespace(namespace, { includeSystem = false } = {}
push('IAMPolicyMember', liveIamPolicyResources(iamPolicies, project, includeSystem), 'gcp');

const runServiceRefs = new Map(runServiceItems.map((i) => [i.metadata && i.metadata.name, kccScopedId(i)]));
const bucketRefs = new Map(storageBucketItems.map((i) => [i.metadata && i.metadata.name, resourceId(i)]));
push('IAMPolicyMember', await kccField(
'IAMPolicyMember', namespace,
(item) => {
const rid = kccPolicyMemberId(item, project, runServiceRefs);
const rid = kccPolicyMemberId(item, project, runServiceRefs, bucketRefs);
const spec = item.spec || {};
return {
id: `${rid}/${spec.role}/${spec.member}`,
Expand Down
22 changes: 22 additions & 0 deletions test/iam-policy-inventory.test.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
canonicalPubSubId,
kccBucketId,
liveIamPolicyIds,
liveIamPolicyResources,
replaceBucketIamPolicies,
} from '../lib/get-resources.mjs';

test('canonicalizes short KCC Pub/Sub IDs to Cloud Asset full paths', () => {
assert.equal(
canonicalPubSubId('download-record', 'abie-ua', 'topics'),
'projects/abie-ua/topics/download-record',
);
assert.equal(
canonicalPubSubId('projects/abie-ua/topics/download-record', 'abie-ua', 'topics'),
'projects/abie-ua/topics/download-record',
);
});

test('resolves KCC bucket references through the bucket resourceID', () => {
const refs = new Map([['dev-nessie-warehouse', 'dev-nessie-warehouse-abie-ua']]);

assert.equal(
kccBucketId({ name: 'dev-nessie-warehouse' }, refs),
'dev-nessie-warehouse-abie-ua',
);
});

const searchEntries = [
{
assetType: 'cloudresourcemanager.googleapis.com/Project',
Expand Down
Loading