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
21 changes: 8 additions & 13 deletions functions/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,20 @@ exports.helloGCS = cloudevent => {
*/
exports.helloAuditLog = cloudevent => {
// Print out details from the CloudEvent itself
console.log('API method:', cloudevent.methodname);
console.log('Event type:', cloudevent.type);

// Print out the CloudEvent's `subject` property
// See https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#subject
console.log('Subject:', cloudevent.subject);

// Print out details from the Cloud Audit Logging entry
// Print out details from the `protoPayload`
// This field encapsulates a Cloud Audit Logging entry
// See https://cloud.google.com/logging/docs/audit#audit_log_entry_structure
const payload = cloudevent.data && cloudevent.data.protoPayload;
if (payload) {
console.log('API method:', payload.methodName);
console.log('Resource name:', payload.resourceName);
}

const request = payload.request;
if (request) {
console.log('Request type:', request['@type']);
}

const metadata = payload && payload.requestMetadata;
if (metadata) {
console.log('Caller IP:', metadata.callerIp);
console.log('User agent:', metadata.callerSuppliedUserAgent);
console.log('Principal:', payload.authenticationInfo.principalEmail);
}
};
// [END functions_log_cloudevent]
Expand Down
17 changes: 4 additions & 13 deletions functions/v2/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,14 @@ describe('functions_log_cloudevent', () => {

it('should process a CloudEvent', async () => {
const event = {
methodname: 'storage.objects.write',
type: 'google.cloud.audit.log.v1.written',
subject:
'storage.googleapis.com/projects/_/buckets/my-bucket/objects/test.txt',
data: {
protoPayload: {
requestMetadata: {
callerIp: '8.8.8.8',
callerSuppliedUserAgent: 'example-user-agent',
},
request: {
'@type': 'type.googleapis.com/storage.objects.write',
methodName: 'storage.objects.write',
authenticationInfo: {
principalEmail: '[email protected]',
},
resourceName: 'some-resource',
},
Expand All @@ -175,12 +171,7 @@ describe('functions_log_cloudevent', () => {
/Subject: storage.googleapis.com\/projects\/_\/buckets\/my-bucket\/objects\/test\.txt/
);
assert.match(output, /Resource name: some-resource/);
assert.match(
output,
/Request type: type\.googleapis\.com\/storage\.objects.write/
);
assert.match(output, /Caller IP: 8\.8\.8\.8/);
assert.match(output, /User agent: example-user-agent/);
assert.match(output, /Principal: example@example\.com/);
});
});

Expand Down