Skip to content

Commit c1370e3

Browse files
author
Ace Nassri
authored
chore(functions): move helloAuditLogging to separate directory (GoogleCloudPlatform#2611)
* chore(functions): move helloAuditLogging to separate directory * html -> yaml * Remove e2e-test * Fix lint
1 parent f5cfa8a commit c1370e3

4 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: functions-v2-helloauditlog
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'functions/v2/helloAuditLog/**'
8+
pull_request:
9+
paths:
10+
- 'functions/v2/helloAuditLog/**'
11+
pull_request_target:
12+
types: [labeled]
13+
schedule:
14+
- cron: '0 2 * * *'
15+
jobs:
16+
test:
17+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: 'write'
21+
pull-requests: 'write'
22+
id-token: 'write'
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
ref: ${{github.event.pull_request.head.ref}}
27+
repository: ${{github.event.pull_request.head.repo.full_name}}
28+
- uses: google-github-actions/auth@v0.7.0
29+
with:
30+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
31+
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
32+
create_credentials_file: 'true'
33+
access_token_lifetime: 600s
34+
- uses: actions/setup-node@v3
35+
with:
36+
node-version: 14
37+
- run: npm install
38+
working-directory: functions/v2/helloAuditLog
39+
- run: npm test
40+
working-directory: functions/v2/helloAuditLog
41+
env:
42+
MOCHA_REPORTER_SUITENAME: functions_v2_helloauditlog
43+
MOCHA_REPORTER_OUTPUT: functions_v2_helloauditlog_sponge_log.xml
44+
MOCHA_REPORTER: xunit
45+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
46+
uses: actions/github-script@v6
47+
with:
48+
github-token: ${{ secrets.GITHUB_TOKEN }}
49+
script: |
50+
try {
51+
await github.rest.issues.removeLabel({
52+
name: 'actions:force-run',
53+
owner: 'GoogleCloudPlatform',
54+
repo: 'nodejs-docs-samples',
55+
issue_number: context.payload.pull_request.number
56+
});
57+
} catch (e) {
58+
if (!e.message.includes('Label does not exist')) {
59+
throw e;
60+
}
61+
}
62+
- if: ${{ github.event_name == 'schedule'}}
63+
run: |
64+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
65+
chmod +x ./flakybot
66+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// [START functions_log_cloudevent]
18+
const functions = require('@google-cloud/functions-framework');
19+
20+
// Register a CloudEvent callback with the Functions Framework that will
21+
// be triggered by an Eventarc Cloud Audit Logging trigger.
22+
//
23+
// Note: this is NOT designed for second-party (Cloud Audit Logs -> Pub/Sub) triggers!
24+
functions.cloudEvent('helloAuditLog', cloudEvent => {
25+
// Print out details from the CloudEvent itself
26+
console.log('Event type:', cloudEvent.type);
27+
28+
// Print out the CloudEvent's `subject` property
29+
// See https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#subject
30+
console.log('Subject:', cloudEvent.subject);
31+
32+
// Print out details from the `protoPayload`
33+
// This field encapsulates a Cloud Audit Logging entry
34+
// See https://cloud.google.com/logging/docs/audit#audit_log_entry_structure
35+
const payload = cloudEvent.data && cloudEvent.data.protoPayload;
36+
if (payload) {
37+
console.log('API method:', payload.methodName);
38+
console.log('Resource name:', payload.resourceName);
39+
console.log('Principal:', payload.authenticationInfo.principalEmail);
40+
}
41+
});
42+
// [END functions_log_cloudevent]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "nodejs-docs-samples-functions-v2-hello-audit-log",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=12.0.0"
13+
},
14+
"scripts": {
15+
"test": "mocha test/index.test.js"
16+
},
17+
"dependencies": {
18+
"@google-cloud/functions-framework": "^3.0.0"
19+
},
20+
"devDependencies": {
21+
"mocha": "^9.1.3",
22+
"p-retry": "^4.6.1",
23+
"sinon": "^13.0.0",
24+
"supertest": "^6.0.0",
25+
"uuid": "^8.3.2"
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
const assert = require('assert');
16+
const sinon = require('sinon');
17+
const supertest = require('supertest');
18+
19+
const functionsFramework = require('@google-cloud/functions-framework/testing');
20+
21+
beforeEach(() => {
22+
// require the module that includes the functions we are testing
23+
require('../index');
24+
25+
// stub the console so we can use it for side effect assertions
26+
sinon.stub(console, 'log');
27+
sinon.stub(console, 'error');
28+
});
29+
30+
afterEach(() => {
31+
// restore the console stub
32+
console.log.restore();
33+
console.error.restore();
34+
});
35+
36+
describe('functions_log_cloudevent', () => {
37+
it('should process a CloudEvent', async () => {
38+
const event = {
39+
type: 'google.cloud.audit.log.v1.written',
40+
subject:
41+
'storage.googleapis.com/projects/_/buckets/my-bucket/objects/test.txt',
42+
data: {
43+
protoPayload: {
44+
methodName: 'storage.objects.write',
45+
authenticationInfo: {
46+
principalEmail: 'example@example.com',
47+
},
48+
resourceName: 'some-resource',
49+
},
50+
},
51+
};
52+
const server = functionsFramework.getTestServer('helloAuditLog');
53+
await supertest(server)
54+
.post('/')
55+
.send(event)
56+
.set('Content-Type', 'application/json')
57+
.expect(204);
58+
59+
assert(console.log.calledWith('API method:', 'storage.objects.write'));
60+
assert(
61+
console.log.calledWith('Event type:', 'google.cloud.audit.log.v1.written')
62+
);
63+
assert(
64+
console.log.calledWith(
65+
'Subject:',
66+
'storage.googleapis.com/projects/_/buckets/my-bucket/objects/test.txt'
67+
)
68+
);
69+
assert(console.log.calledWith('Resource name:', 'some-resource'));
70+
assert(console.log.calledWith('Principal:', 'example@example.com'));
71+
});
72+
});

0 commit comments

Comments
 (0)