-
Notifications
You must be signed in to change notification settings - Fork 18
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 #147 from isimluk/arc
Azure Arc Autodiscovery
- Loading branch information
Showing
15 changed files
with
229 additions
and
17 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
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
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,18 @@ | ||
class ApiError(Exception): | ||
pass | ||
|
||
|
||
class NoStreamsError(ApiError): | ||
def __init__(self, app_id): | ||
super().__init__( | ||
'Falcon Streaming API not discovered. This may be caused by second instance of this application ' | ||
'already running in your environment with the same application_id={}, or by missing streaming API ' | ||
'capability.'.format(app_id)) | ||
|
||
|
||
class RTRError(ApiError): | ||
pass | ||
|
||
|
||
class RTRConnectionError(ApiError): | ||
pass |
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,85 @@ | ||
from .errors import ApiError, RTRError, RTRConnectionError | ||
|
||
|
||
class RTRSession: | ||
def __init__(self, falcon_api, device_id): | ||
self.falcon = falcon_api | ||
self.device_id = device_id | ||
self.session = self._connect() | ||
|
||
def close(self): | ||
# return self.falcon.client.command('RTR_DeleteSession', session_id=self.id) | ||
# Below is a workaround for the above not working properly (/cc @jshcodes) | ||
|
||
from falconpy import OAuth2, RealTimeResponse # pylint: disable=C0415 | ||
from ..config import config # pylint: disable=C0415 | ||
from ..log import log # pylint: disable=C0415 | ||
falcon_auth = OAuth2( | ||
client_id=config.get('falcon', 'client_id'), | ||
client_secret=config.get('falcon', 'client_secret') | ||
) | ||
falcon_rtr = RealTimeResponse(falcon_auth) | ||
response = falcon_rtr.delete_session(session_id=self.id) | ||
if response['status_code'] != 204: | ||
log.debug('Unable to close the RTR session: reponse was: %s', response) | ||
|
||
def execute_and_wait(self, action, base_command, command_string): | ||
command = self._execute(action, base_command, command_string) | ||
response = self._rtr_wait(command[0]) | ||
|
||
if response['stderr']: | ||
raise RTRError(f'RTR Execute device: {self.device_id}, stderr: {response["stderr"]}') | ||
return response | ||
|
||
def get_file(self, filepath): | ||
command = self.execute_and_wait('RTR_ExecuteActiveResponderCommand', 'get', 'get ' + filepath) | ||
for f in self._list_files(): | ||
if f['cloud_request_id'] == command['task_id']: | ||
return self._fetch_file(f['sha256'], filepath) | ||
|
||
raise RTRError(f'RTR File Not Found: device: {self.device_id}, file {filepath}') | ||
|
||
@property | ||
def id(self): | ||
return self.session['session_id'] | ||
|
||
def _list_files(self): | ||
return self.falcon._resources( # pylint: disable=W0212 | ||
'RTR_ListFiles', | ||
parameters={ | ||
'session_id': self.id | ||
} | ||
) | ||
|
||
def _fetch_file(self, sha256, filepath): | ||
response = self.falcon.client.command( | ||
'RTR_GetExtractedFileContents', | ||
parameters={ | ||
'session_id': self.id, | ||
'sha256': sha256, | ||
'filepath': filepath, | ||
} | ||
) | ||
if not isinstance(response, (bytes, bytearray)): | ||
raise RTRError(f"Could not fetch RTR file from Falcon: {response['body']}") | ||
return response | ||
|
||
def _connect(self): | ||
response = None | ||
try: | ||
response = self.falcon.init_rtr_session(self.device_id) | ||
except ApiError as e: | ||
raise RTRConnectionError(f"{e}") from e | ||
|
||
if len(response) != 1: | ||
raise RTRError(f'Unexpected response from RTR Init: {response}') | ||
return response[0] | ||
|
||
def _rtr_wait(self, command): | ||
check = self.falcon.check_rtr_command_status(command['cloud_request_id'], 0)[0] | ||
while not check['complete']: | ||
check = self.falcon.check_rtr_command_status(command['cloud_request_id'], 0)[0] | ||
return check | ||
|
||
def _execute(self, action, base_command, command_string): | ||
return self.falcon.execute_rtr_command(action, self.id, base_command, command_string) |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ google-cloud-resource-manager >= 1.0.2 | |
tls-syslog | ||
google-auth | ||
google-api-python-client | ||
py7zr |
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
Oops, something went wrong.