Skip to content

Commit 8cfb843

Browse files
feat(samples): add samples for analyzeIamPolicy and analyzeIamPolicyLongrunning (#433)
1 parent c4e39f9 commit 8cfb843

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Analyze Iam Policy
19+
// description: Analyzes accessible IAM policies that match a request.
20+
// usage: node analyzeIamPolicy
21+
22+
async function main() {
23+
// [START asset_quickstart_analyze_iam_policy]
24+
const util = require('util');
25+
const {AssetServiceClient} = require('@google-cloud/asset');
26+
27+
const client = new AssetServiceClient();
28+
const projectId = await client.getProjectId();
29+
30+
async function analyzeIamPolicy() {
31+
const request = {
32+
analysisQuery: {
33+
scope: `projects/${projectId}`,
34+
resourceSelector: {
35+
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`,
36+
},
37+
options: {
38+
expandGroups: true,
39+
outputGroupEdges: true,
40+
},
41+
},
42+
};
43+
44+
// Handle the operation using the promise pattern.
45+
const result = await client.analyzeIamPolicy(request);
46+
// Do things with with the response.
47+
console.log(util.inspect(result, {depth: null}));
48+
}
49+
// [END asset_quickstart_analyze_iam_policy]
50+
analyzeIamPolicy();
51+
}
52+
53+
process.on('unhandledRejection', err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Analyze Iam Policy Longrunning and write results to Bigquery
19+
// description: Analyzes accessible IAM policies that match a request.
20+
// usage: node analyzeIamPolicyLongrunningBigquery <dataset_id> <table_prefix>
21+
22+
async function main(datasetId, tablePrefix) {
23+
// [START asset_quickstart_analyze_iam_policy_longrunning_bigquery]
24+
const util = require('util');
25+
const {AssetServiceClient} = require('@google-cloud/asset');
26+
27+
const client = new AssetServiceClient();
28+
const projectId = await client.getProjectId();
29+
30+
async function analyzeIamPolicyLongrunningBigquery() {
31+
// TODO(developer): choose the dataset and table prefix
32+
// const datasetId = ''
33+
// const tablePrefix = ''
34+
35+
const request = {
36+
analysisQuery: {
37+
scope: `projects/${projectId}`,
38+
resourceSelector: {
39+
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`,
40+
},
41+
options: {
42+
expandGroups: true,
43+
outputGroupEdges: true,
44+
},
45+
},
46+
outputConfig: {
47+
bigqueryDestination: {
48+
dataset: `projects/${projectId}/datasets/${datasetId}`,
49+
tablePrefix: tablePrefix,
50+
},
51+
},
52+
};
53+
54+
// Handle the operation using the promise pattern.
55+
const [operation] = await client.analyzeIamPolicyLongrunning(request);
56+
57+
// Operation#promise starts polling for the completion of the operation.
58+
const [result] = await operation.promise();
59+
60+
// Do things with with the response.
61+
console.log(util.inspect(result, {depth: null}));
62+
}
63+
// [END asset_quickstart_analyze_iam_policy_longrunning_bigquery]
64+
analyzeIamPolicyLongrunningBigquery();
65+
}
66+
67+
process.on('unhandledRejection', err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
71+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Analyze Iam Policy Longrunning and write results to GCS
19+
// description: Analyzes accessible IAM policies that match a request.
20+
// usage: node analyzeIamPolicyLongrunningGcs
21+
// <gs://my-bucket/my-analysis.json>
22+
23+
async function main(gcsUri) {
24+
// [START asset_quickstart_analyze_iam_policy_longrunning_gcs]
25+
const util = require('util');
26+
const {AssetServiceClient} = require('@google-cloud/asset');
27+
28+
const client = new AssetServiceClient();
29+
const projectId = await client.getProjectId();
30+
31+
async function analyzeIamPolicyLongrunningGcs() {
32+
// TODO(developer): choose the gcs path uri
33+
// const gcsUri = 'Gcs path uri, e.g.: gs://<my_bucket>/<my_analysis_file>'
34+
35+
const request = {
36+
analysisQuery: {
37+
scope: `projects/${projectId}`,
38+
resourceSelector: {
39+
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`,
40+
},
41+
options: {
42+
expandGroups: true,
43+
outputGroupEdges: true,
44+
},
45+
},
46+
outputConfig: {
47+
gcsDestination: {
48+
uri: gcsUri,
49+
},
50+
},
51+
};
52+
53+
// Handle the operation using the promise pattern.
54+
const [operation] = await client.analyzeIamPolicyLongrunning(request);
55+
56+
// Operation#promise starts polling for the completion of the operation.
57+
const [result] = await operation.promise();
58+
59+
// Do things with with the response.
60+
console.log(util.inspect(result, {depth: null}));
61+
}
62+
// [END asset_quickstart_analyze_iam_policy_longrunning_gcs]
63+
analyzeIamPolicyLongrunningGcs();
64+
}
65+
66+
process.on('unhandledRejection', err => {
67+
console.error(err.message);
68+
process.exitCode = 1;
69+
});
70+
main(...process.argv.slice(2));

packages/google-cloud-asset/samples/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"@google-cloud/asset": "^3.11.0",
19+
"@google-cloud/bigquery": "^5.5.0",
1920
"@google-cloud/compute": "^2.0.0",
2021
"@google-cloud/storage": "^5.0.0",
2122
"uuid": "^8.0.0",

packages/google-cloud-asset/samples/test/sample.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ const storage = new Storage();
2626
const bucketName = `asset-nodejs-${uuid.v4()}`;
2727
const bucket = storage.bucket(bucketName);
2828

29+
const {BigQuery} = require('@google-cloud/bigquery');
30+
const bigquery = new BigQuery();
31+
const options = {
32+
location: 'US',
33+
};
34+
const datasetId = `asset_nodejs_${uuid.v4()}`.replace(/-/gi, '_');
35+
2936
const Compute = require('@google-cloud/compute');
3037
const zone = new Compute().zone('us-central1-c');
3138
const vmName = `asset-nodejs-${uuid.v4()}`;
@@ -49,11 +56,14 @@ const delay = async test => {
4956
describe('quickstart sample tests', () => {
5057
before(async () => {
5158
await bucket.create();
59+
await bigquery.createDataset(datasetId, options);
60+
await bigquery.dataset(datasetId).exists();
5261
[vm] = await zone.createVM(vmName, {os: 'ubuntu'});
5362
});
5463

5564
after(async () => {
5665
await bucket.delete();
66+
await bigquery.dataset(datasetId).delete({force: true}).catch(console.warn);
5767
await vm.delete();
5868
});
5969

@@ -99,4 +109,41 @@ describe('quickstart sample tests', () => {
99109
const stdout = execSync(`node listAssets ${assetType}`);
100110
assert.include(stdout, assetType);
101111
});
112+
113+
it('should analyze iam policy successfully', async () => {
114+
const stdout = execSync('node analyzeIamPolicy');
115+
assert.include(stdout, '//cloudresourcemanager.googleapis.com/projects');
116+
});
117+
118+
it('should analyze iam policy and write analysis results to gcs successfully', async function () {
119+
this.retries(2);
120+
await delay(this.test);
121+
const uri = `gs://${bucketName}/my-analysis.json`;
122+
execSync(`node analyzeIamPolicyLongrunningGcs ${uri}`);
123+
const file = await bucket.file('my-analysis.json');
124+
const exists = await file.exists();
125+
assert.ok(exists);
126+
await file.delete();
127+
});
128+
129+
it('should analyze iam policy and write analysis results to bigquery successfully', async function () {
130+
this.retries(2);
131+
await delay(this.test);
132+
const tablePrefix = 'analysis_nodejs';
133+
execSync(
134+
`node analyzeIamPolicyLongrunningBigquery ${datasetId} ${tablePrefix}`
135+
);
136+
const metadataTable = await bigquery
137+
.dataset(datasetId)
138+
.table('analysis_nodejs_analysis');
139+
const metadataTable_exists = await metadataTable.exists();
140+
assert.ok(metadataTable_exists);
141+
const resultsTable = await bigquery
142+
.dataset(datasetId)
143+
.table('analysis_nodejs_analysis_result');
144+
const resultsTable_exists = await resultsTable.exists();
145+
assert.ok(resultsTable_exists);
146+
await metadataTable.delete();
147+
await resultsTable.delete();
148+
});
102149
});

0 commit comments

Comments
 (0)