-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from sudiptob2/feat/255/cloud-datastore-crawler
Feat/255/cloud datastore crawler
- Loading branch information
Showing
11 changed files
with
176 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,5 +89,8 @@ | |
}, | ||
"firestore_collections": { | ||
"fetch": true | ||
}, | ||
"datastore_kinds": { | ||
"fetch": true | ||
} | ||
} |
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,38 @@ | ||
# Copyright 2023 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 | ||
# | ||
# https://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 googleapiclient import discovery | ||
from httplib2 import Credentials | ||
|
||
from .interface_client import IClient | ||
|
||
|
||
class DatastoreClient(IClient): | ||
"""DatastoreClient class.""" | ||
|
||
def get_service(self, credentials: Credentials) -> discovery.Resource: | ||
"""Get discovery service for datastore resource. | ||
Args: | ||
credentials: An google.oauth2.credentials.Credentials object. | ||
Returns: | ||
An object of discovery.Resource | ||
""" | ||
return discovery.build( | ||
"datastore", | ||
"v1", | ||
credentials=credentials, | ||
cache_discovery=False, | ||
) |
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,56 @@ | ||
# Copyright 2023 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 | ||
# | ||
# https://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. | ||
|
||
import logging | ||
import sys | ||
from typing import List, Dict, Any, Union | ||
|
||
from googleapiclient import discovery | ||
|
||
from gcp_scanner.crawler.interface_crawler import ICrawler | ||
|
||
|
||
class DatastoreCrawler(ICrawler): | ||
"""Handle crawling of cloud datastore data.""" | ||
|
||
def crawl(self, project_id: str, service: discovery.Resource, | ||
config: Dict[str, Union[bool, str]] = None) -> Dict[str, List[Dict[str, Any]]]: | ||
"""Retrieve a list of datastore kinds available in the project. | ||
Args: | ||
project_id: A name of a project to query info about. | ||
service: A resource object for interacting with the Firestore API. | ||
config: Configuration options for the crawler (Optional). | ||
Returns: | ||
A list of objects representing the crawled data. | ||
""" | ||
|
||
logging.info("Retrieving Datastore entities.") | ||
datastore_kinds = dict() | ||
query = { | ||
"query": { | ||
"kind": [{"name": "__kind__"}] | ||
} | ||
} | ||
try: | ||
request = service.projects().runQuery(projectId=project_id, body=query) | ||
if request is not None: | ||
response = request.execute() | ||
entity_results = response.get("batch", {}).get("entityResults", []) | ||
datastore_kinds['kinds'] = [entity["entity"]["key"]["path"][0]["name"] for entity in entity_results] | ||
except Exception: | ||
logging.info("Failed to retrieve datastore entities for project %s", project_id) | ||
logging.info(sys.exc_info()) | ||
return datastore_kinds |
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,3 @@ | ||
#!/bin/bash | ||
|
||
# Todo |
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,16 @@ | ||
{ | ||
"kinds": [ | ||
"Person", | ||
"Pet", | ||
"Toy", | ||
"__Stat_Kind_IsRootEntity__", | ||
"__Stat_Kind__", | ||
"__Stat_PropertyName_Kind__", | ||
"__Stat_PropertyType_Kind__", | ||
"__Stat_PropertyType_PropertyName_Kind__", | ||
"__Stat_PropertyType__", | ||
"__Stat_Total__", | ||
"gsoc-2023", | ||
"test-collection-2" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"projects/test-gcp-scanner-2/databases/(default)": [ | ||
"Person", | ||
"gsoc-2023", | ||
"test-collection-2" | ||
] | ||
|