Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 20 additions & 2 deletions functions/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ 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);
}

Expand All @@ -84,6 +89,19 @@ exports.helloAuditLog = cloudevent => {
console.log('Caller IP:', metadata.callerIp);
console.log('User agent:', metadata.callerSuppliedUserAgent);
}

const resource = cloudevent.data && cloudevent.data.resource;
if (resource) {
console.log('Resource type:', resource.type);
}

const labels = resource && resource.labels;
if (labels) {
console.log('Labels');
Object.keys(labels).map(label => {
console.log(` ${label}: ${labels[label]}`);
});
}
};
// [END functions_log_cloudevent]

Expand Down
10 changes: 9 additions & 1 deletion functions/v2/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ 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: {
methodName: 'storage.objects.write',
requestMetadata: {
callerIp: '8.8.8.8',
callerSuppliedUserAgent: 'example-user-agent',
Expand All @@ -160,6 +160,12 @@ describe('functions_log_cloudevent', () => {
},
resourceName: 'some-resource',
},
resource: {
type: 'some-type',
labels: {
bar: 'baz',
},
},
},
};
const response = await invocation(PORT, event);
Expand All @@ -181,6 +187,8 @@ describe('functions_log_cloudevent', () => {
);
assert.match(output, /Caller IP: 8\.8\.8\.8/);
assert.match(output, /User agent: example-user-agent/);
assert.match(output, /Resource type: some-type/);
assert.match(output, /bar: baz/);
});
});

Expand Down