Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 updated api client to have log & output messages, updated api end… #25

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
91 changes: 87 additions & 4 deletions reptor/api/APIClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,89 @@ def _get_headers(self, json_content=False) -> typing.Dict:
self.reptor.logger.debug(f"HTTP Headers: {headers}")
return headers

@property
def log(self):
"""Access the logging directly from plugin

Returns:
ReptorAdapter: Logging Module
"""
return self.reptor.get_logger()

@property
def console(self):
"""Access the rich console that allows markdown etc.

Returns:
Console: rich console
"""
return reptor_console

def success(self, msg, *args, **kwargs):
"""Use this to print Green text by default. You can change colors etc.

See the logger.py for examples.

Args:
msg (str): Any message you want to print
"""
self.log.success(msg, *args, **kwargs)

def display(self, msg, *args, **kwargs):
"""Use this to print blue text by default. You can change colors etc.

See the logger.py for examples.

Args:
msg (str): Any message you want to print
"""
self.log.display(msg, *args, **kwargs)

def highlight(self, msg, *args, **kwargs):
"""Prints a yellow message. Good for highlighting certain
output.

Args:
msg (str): Any message you want to print
"""
self.log.highlight(msg, *args, **kwargs)

def debug(self, msg, *args, **kwargs):
"""Default DEBUG method of the logger. Use this instead of accessing
log or reptor.get_logger()

Args:
msg (str): Message to show in DEBUG log
"""
self.log.debug(msg, *args, **kwargs)

def info(self, msg, *args, **kwargs):
"""Default INFO method of the logger. Use this instead of accessing
log or reptor.get_logger()

Args:
msg (str): Message to show in INFO log
"""
self.log.info(msg, *args, **kwargs)

def warning(self, msg, *args, **kwargs):
"""Default WARNING method of the logger. Use this instead of accessing
log or reptor.get_logger()

Args:
msg (str): Message to show in WARNING log
"""
self.log.warning(msg, *args, **kwargs)

def error(self, msg, *args, **kwargs):
"""Default ERROR method of the logger. Use this instead of accessing
log or reptor.get_logger()

Args:
msg (str): Message to show in ERROR log
"""
self.log.error(msg, *args, **kwargs)

def get(self, url: str) -> requests.models.Response:
"""Sends a get request

Expand All @@ -52,7 +135,7 @@ def get(self, url: str) -> requests.models.Response:
verify=self.verify,
)
response.raise_for_status()
self.reptor.get_logger().debug(f"Received response: {response.content}")
self.debug(f"Received response: {response.content}")
return response

def post(
Expand All @@ -77,7 +160,7 @@ def post(
verify=self.verify,
)
response.raise_for_status()
self.reptor.get_logger().debug(f"Received response: {response.content}")
self.debug(f"Received response: {response.content}")
return response

def put(self, url: str, data: object) -> requests.models.Response:
Expand All @@ -97,7 +180,7 @@ def put(self, url: str, data: object) -> requests.models.Response:
verify=self.verify,
)
response.raise_for_status()
self.reptor.get_logger().debug(f"Received response: {response.content}")
self.debug(f"Received response: {response.content}")
return response

def patch(self, url: str, data: object) -> requests.models.Response:
Expand All @@ -117,5 +200,5 @@ def patch(self, url: str, data: object) -> requests.models.Response:
verify=self.verify,
)
response.raise_for_status()
self.reptor.get_logger().debug(f"Received response: {response.content}")
self.debug(f"Received response: {response.content}")
return response
16 changes: 7 additions & 9 deletions reptor/api/NotesAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, **kwargs) -> None:
f"api/v1/pentestprojects/{self.project_id}/notes/",
)
else:
self.reptor.logger.fail_with_exit(
raise ValueError(
"Either specify a project ID (-p|--project_id) or use --private-note"
)

Expand Down Expand Up @@ -85,14 +85,14 @@ def write_note(
If no notename defined, content gets appended to 'Uploads' note
"""
if not content:
self.reptor.logger.info("Reading from stdin...")
self.info("Reading from stdin...")
content = sys.stdin.read()

note = self.get_note_by_title(
notename, parent_notename=parent_notename, icon=icon
)

self.reptor.logger.debug(f"Working with note: {note.id}")
self.debug(f"Working with note: {note.id}")

with self._auto_lock_note(
note.id
Expand All @@ -107,15 +107,13 @@ def write_note(
note_text += ": "

note_text += content
self.reptor.logger.debug(
f"We are sending data with a lenght of: {len(note_text)}"
)
self.debug(f"We are sending data with a lenght of: {len(note_text)}")
url = urljoin(self.base_endpoint, note.id, "")
r = self.put(url, {"text": note_text})

try:
r.raise_for_status()
self.reptor.logger.info(f'Note written to "{notename}".')
self.info(f'Note written to "{notename}".')
except HTTPError as e:
raise HTTPError(
f'{str(e)} Are you uploading binary content to note? (Try "file" subcommand)'
Expand Down Expand Up @@ -153,7 +151,7 @@ def upload_file(

for file in files:
if file.name == "<stdin>":
self.reptor.logger.info("Reading from stdin...")
self.info("Reading from stdin...")
else:
filename = basename(file.name)
content = file.buffer.read()
Expand All @@ -162,7 +160,7 @@ def upload_file(
filename = f"data.{filetype}"

if not content:
self.reptor.logger.warning(f"{file.name} is empty. Will not upload.")
self.warning(f"{file.name} is empty. Will not upload.")
continue

# Lock during upload to prevent unnecessary uploads and for endpoint setup
Expand Down
10 changes: 4 additions & 6 deletions reptor/api/TemplatesAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def upload_new_template(
self.base_endpoint, data={"data": {"title": template.data.title}}
)
raw_data = res.json()
self.reptor.logger.debug(raw_data)
self.debug(raw_data)
if raw_data:
updated_template = FindingTemplate(raw_data)
updated_template.data = template.data
Expand All @@ -58,17 +58,15 @@ def upload_new_template(
"status": "in-progress",
"data": updated_template.data._to_api_json(),
}
self.reptor.logger.debug(updated_data)
self.debug(updated_data)
res2 = self.put(
f"{self.base_endpoint}{updated_template.id}",
updated_data,
)
return_template = updated_template

except Exception as e:
self.reptor.logger.fail(
f"Could not upload finding with title: {template.data.title}"
)
self.reptor.logger.fail(f"Error Message Infos: {e}")
self.fail(f"Could not upload finding with title: {template.data.title}")
self.fail(f"Error Message Infos: {e}")

return return_template