Beispiel #1
0
        internal async Task ListEventsAsync(
            ListLogEntriesRequest request,
            Action <EventBase> callback,
            ExponentialBackOff backOff,
            CancellationToken cancellationToken)
        {
            using (ApplicationTraceSources.Default.TraceMethod().WithParameters(request.Filter))
            {
                try
                {
                    string nextPageToken = null;
                    do
                    {
                        request.PageToken = nextPageToken;

                        using (var stream = await this.service.Entries
                                            .List(request)
                                            .ExecuteAsStreamWithRetryAsync(backOff, cancellationToken)
                                            .ConfigureAwait(false))
                            using (var reader = new JsonTextReader(new StreamReader(stream)))
                            {
                                nextPageToken = ListLogEntriesParser.Read(reader, callback);
                            }
                    }while (nextPageToken != null);
                }
                catch (GoogleApiException e) when(e.Error != null && e.Error.Code == 403)
                {
                    throw new ResourceAccessDeniedException(
                              "You do not have sufficient permissions to view logs. " +
                              "You need the 'Logs Viewer' role (or an equivalent custom role) " +
                              "to perform this action.",
                              e);
                }
            }
        }
        public void WhenPageHasDataAndNextPageToken_ThenReadPageReturnsToken()
        {
            var json = @"
                {
                  'entries': [
                    {
                      'protoPayload': {
                        '@type': 'type.googleapis.com/google.cloud.audit.AuditLog',
                        'authenticationInfo': {
                        },
                        'requestMetadata': {
                        },
                        'serviceName': 'compute.googleapis.com',
                        'methodName': 'beta.compute.instances.listReferrers',
                        'authorizationInfo': [
                        ],
                        'resourceName': 'projects/project-1/zones/us-central1-b/instances',
                        'request': {
                          '@type': 'type.googleapis.com/compute.instances.listReferrers'
                        },
                        'resourceLocation': {
                          'currentLocations': [
                            'us-central1-b'
                          ]
                        }
                      },
                      'insertId': 'gdq8xba8',
                      'resource': {
                        'type': 'gce_instance',
                        'labels': {
                          'zone': 'us-central1-b',
                          'instance_id': '',
                          'project_id': 'project-1'
                        }
                      },
                      'timestamp': '2020-05-15T10:56:20.474Z',
                      'severity': 'INFO',
                      'logName': 'projects/project-1/logs/cloudaudit.googleapis.com%2Fdata_access',
                      'receiveTimestamp': '2020-05-15T10:56:26.900097443Z'
                    }
                  ],
                  'nextPageToken': 'EAE4oeeNuZes8rwyStsEIhoiCgoIZ'
                }";

            var events = new List <EventBase>();

            var token = ListLogEntriesParser.Read(
                new JsonTextReader(new StringReader(json)),
                events.Add);

            Assert.AreEqual("EAE4oeeNuZes8rwyStsEIhoiCgoIZ", token);
            Assert.AreEqual(1, events.Count);
        }
        public void WhenPageIsEmpty_ThenReadPageReturnsEmptySequence()
        {
            var json = @"{}";

            var events = new List <EventBase>();

            var token = ListLogEntriesParser.Read(
                new JsonTextReader(new StringReader(json)),
                events.Add);

            Assert.IsNull(token);
            Assert.AreEqual(0, events.Count);
        }
        public void WhenStreamContainsObjectInsteadOfArry_ThenNoEventsAreReported()
        {
            var json = @"
            {
                'this': 'is invalid'
            }";

            var events = new List <EventBase>();

            var token = ListLogEntriesParser.Read(
                new JsonTextReader(new StringReader(json)),
                events.Add);

            Assert.AreEqual(0, events.Count());
        }
        public void WhenPageOnlyHasNextPageToken_ThenReadPageReturnsToken()
        {
            var json = @"
                {
                  'nextPageToken': 'EAE4oeeNuZes8rwyStsEIhoiCgoIZ'
                }";


            var events = new List <EventBase>();

            var token = ListLogEntriesParser.Read(
                new JsonTextReader(new StringReader(json)),
                events.Add);

            Assert.AreEqual("EAE4oeeNuZes8rwyStsEIhoiCgoIZ", token);
            Assert.AreEqual(0, events.Count);
        }
        public void WhenStreamContainsAnUnknownRecord_ThenReadReportsUnknownEvent()
        {
            var json = @"{
                'entries': [ {
                   'protoPayload': {
                     '@type': 'type.googleapis.com/google.cloud.audit.AuditLog',
                     'authenticationInfo': {
                     },
                     'requestMetadata': {
                     },
                     'serviceName': 'compute.googleapis.com',
                     'methodName': 'v1.compute.instances.delete',
                     'authorizationInfo': [
                     ],
                     'resourceName': 'projects/123/zones/us-central1-a/instances/instance-1',
                     'request': {
                       'requestId': 'f802d080-d71e-4cae-a105-41fed099e362',
                       '@type': 'type.googleapis.com/compute.instances.delete'
                     },
                     'response': {
                     },
                     'resourceLocation': {
                       'currentLocations': [
                         'us-central1-a'
                       ]
                     }
                   },
                   'insertId': '7rriyre2bn74',
                   'resource': {
                     'type': 'gce_instance',
                     'labels': {
                       'instance_id': '3771111960822',
                       'project_id': 'project-1',
                       'zone': 'us-central1-a'
                     }
                   },
                   'timestamp': '2020-05-04T02:07:40.933Z',
                   'severity': 'NOTICE',
                   'logName': 'projects/project-1/logs/cloudaudit.googleapis.com%2Factivity',
                   'operation': {
                     'id': 'operation-1588558060966-5a4c8feedd25b-f4637780-33f35e50',
                     'producer': 'compute.googleapis.com',
                     'first': true
                   },
                   'receiveTimestamp': '2020-05-04T02:07:41.604695630Z'
                 },
                 {
                  'protoPayload': {
                     '@type': 'type.googleapis.com/google.cloud.audit.AuditLog',
                     'authenticationInfo': {
                     },
                     'requestMetadata': {
                     },
                     'serviceName': 'compute.googleapis.com',
                     'methodName': 'unknown.method',
                     'authorizationInfo': [
                     ],
                     'resourceName': 'projects/project-1/zones/us-central1-a/instances/instance-2',
                     'request': {
                       '@type': 'type.googleapis.com/compute.instances.start'
                     },
                     'response': {
                     },
                     'resourceLocation': {
                       'currentLocations': [
                         'us-central1-a'
                       ]
                     }
                   },
                   'insertId': 'vcq6epd7n72',
                   'resource': {
                     'type': 'gce_instance',
                     'labels': {
                       'project_id': 'project-1',
                       'instance_id': '489405111114222',
                       'zone': 'us-central1-a'
                     }
                   },
                   'timestamp': '2020-05-04T13:56:26.405Z',
                   'severity': 'NOTICE',
                   'logName': 'projects/project-1/logs/cloudaudit.googleapis.com%2Factivity',
                   'operation': {
                     'id': 'operation-1588600586345-5a4d2e5a39c56-47d0ce05-a9d7073c',
                     'producer': 'compute.googleapis.com',
                     'first': true
                   },
                   'receiveTimestamp': '2020-05-04T13:56:27.582777461Z'
                 }]
                }";

            var events = new List <EventBase>();

            var token = ListLogEntriesParser.Read(
                new JsonTextReader(new StringReader(json)),
                events.Add);

            Assert.AreEqual(2, events.Count());

            Assert.IsInstanceOf(typeof(DeleteInstanceEvent), events.First());
            var deleteEvent = (DeleteInstanceEvent)events.First();

            Assert.AreEqual("instance-1", deleteEvent.InstanceReference.Name);

            Assert.IsInstanceOf(typeof(UnknownEvent), events.Last());
        }