forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Directory API processor, Cloud Asset Inventory processor and (G…
…oogleCloudPlatform#692) Cloud Ops Monitoring processor with corresponding examples.
- Loading branch information
Showing
14 changed files
with
558 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
retryPeriod: 3 day ago | ||
|
||
# Example for using the Cloud Asset Inventory processor, which looks up | ||
# all organizations service accounts and then queries their metrics to | ||
# produce a report. | ||
# | ||
# The processor requires Cloud Asset Viewer permission on the correct | ||
# level. | ||
|
||
processors: | ||
- cai | ||
- monitoring | ||
|
||
cai: | ||
parent: organizations/1234567890 | ||
# parent: projects/123456789 | ||
assetTypes: | ||
- iam.googleapis.com/ServiceAccount | ||
# - iam.googleapis.com/ServiceAccountKey | ||
|
||
# See: https://cloud.google.com/asset-inventory/docs/reference/rest/v1/feeds#ContentType | ||
contentType: "RESOURCE" | ||
|
||
# readTime: "2021-09-13T14:14:23.045123456Z" | ||
|
||
monitoring: | ||
timeSeries: "{{ assets['iam.googleapis.com/ServiceAccount']|json_encode }}" | ||
# Specify your monitoring metric scope project below, this only works if every project | ||
# is in its own metric scope. | ||
#project: | | ||
# projects/{{ item.resource.data.projectId }} | ||
key: | | ||
{{ item.resource.data.email }} | ||
query: | | ||
fetch iam_service_account | ||
| metric iam.googleapis.com/service_account/authn_events_count | ||
| filter (resource.unique_id == '{{ item.resource.data.uniqueId }}') | ||
| group_by 14d, [.sum] | ||
| every 14d | ||
outputs: | ||
- type: logger | ||
level: debug | ||
message: | | ||
Service Account Summary (use in last 14 days) | ||
{% for sa, ts in time_series.items() %} | ||
- {{ sa }}: {% if ts.int64s|first|length > 0 %}{{ ts.int64s|first|first }} times{% else %}not used{% endif %} | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
retryPeriod: 3 day ago | ||
|
||
# This is an example for Directory API processor, which allows fetching of | ||
# groups, users, group memberships and group settings from the appropriate | ||
# APIs. | ||
|
||
processors: | ||
- directory | ||
|
||
directory: | ||
### Fetching list of groups ### | ||
api: groups | ||
# Either use customer ID (directory ID) or domain: | ||
# customerId: C00000000 | ||
# domain: example.com | ||
# For query syntax, see: | ||
# https://developers.google.com/admin-sdk/directory/v1/guides/search-groups | ||
query: "" | ||
|
||
### Fetching group settings ### | ||
# api: groupsettings | ||
# groupUniqueIds: ["group@example.com"] | ||
|
||
### Fetching list of group members ### | ||
# api: members | ||
# groupKey: 04i7ojhp37zlnuw | ||
|
||
### Fetching list of users ### | ||
# api: users | ||
# (domain or customerId, query, etc) | ||
|
||
# Specify the service account for getting the token (needs to have appropriate | ||
# permissions in admin.google.com) | ||
serviceAccountEmail: service-account@your-project.iam.gserviceaccount.com | ||
|
||
outputs: | ||
- type: logger | ||
level: debug | ||
message: | | ||
{{ results|json_encode() }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
retryPeriod: 3 day ago | ||
|
||
# This example shows Directory API called twice, first to retrieve | ||
# all groups and then to retrieve group settings for those groups. | ||
# Then finally, all groups that allow external members are printed | ||
# out. | ||
# | ||
# You could also use Pub/Sub fanout to parallelize the processing | ||
# (see groups-example.yaml) | ||
# | ||
processors: | ||
- { processor: directory, config: get_groups } | ||
- { processor: directory, config: get_group_settings } | ||
|
||
get_groups: | ||
### Fetching list of groups ### | ||
api: groups | ||
domain: example.com | ||
query: "" | ||
serviceAccountEmail: service-account@your-project.iam.gserviceaccount.com | ||
|
||
get_group_settings: | ||
### Fetching list of groups ### | ||
api: groupsettings | ||
domain: example.com | ||
groupUniqueIds: | | ||
{{ results|map(attribute='email')|list|json_encode }} | ||
query: "" | ||
serviceAccountEmail: service-account@your-project.iam.gserviceaccount.com | ||
|
||
outputs: | ||
- type: logger | ||
level: debug | ||
message: | | ||
The following groups allow external members: | ||
{% for group in results|selectattr('allowExternalMembers', 'equalto', 'true') %} | ||
- {{ group['email'] }} ({{ group['name'] }}) | ||
{% endfor %} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from .base import Processor, NotConfiguredException | ||
from googleapiclient import discovery | ||
|
||
|
||
class CaiProcessor(Processor): | ||
|
||
def process(self, config_key=None): | ||
if config_key is None: | ||
config_key = 'cai' | ||
if config_key not in self.config: | ||
raise NotConfiguredException('No settings configured!') | ||
|
||
cai_config = self.config[config_key] | ||
if 'parent' not in cai_config: | ||
raise NotConfiguredException('No parent configured!') | ||
|
||
cai_service = discovery.build('cloudasset', 'v1') | ||
|
||
request_parameters = {} | ||
request_parameters['parent'] = self._jinja_expand_string( | ||
cai_config['parent']) | ||
|
||
for k in ['readTime', 'pageSize', 'contentType']: | ||
if k in cai_config: | ||
request_parameters[k] = self._jinja_expand_string(cai_config[k]) | ||
if 'assetTypes' in cai_config: | ||
request_parameters['assetTypes'] = self._jinja_var_to_list( | ||
cai_config['assetTypes']) | ||
|
||
assets = {} | ||
page_token = None | ||
while True: | ||
if page_token is not None: | ||
request_parameters['pageToken'] = page_token | ||
request = cai_service.assets().list(**request_parameters) | ||
response = request.execute() | ||
if 'assets' in response: | ||
for asset in response['assets']: | ||
if asset['assetType'] not in assets: | ||
assets[asset['assetType']] = [] | ||
assets[asset['assetType']].append(asset) | ||
|
||
if 'nextPageToken' in response: | ||
page_token = response['nextPageToken'] | ||
else: | ||
break | ||
|
||
return {'assets': assets} |
Oops, something went wrong.