Skip to content

POC fastapi - #167

Merged
whotwagner merged 40 commits into
ait-testbed:developmentfrom
thorinaboenke:poc_fastapi
Aug 14, 2025
Merged

POC fastapi#167
whotwagner merged 40 commits into
ait-testbed:developmentfrom
thorinaboenke:poc_fastapi

Conversation

@thorinaboenke

Copy link
Copy Markdown
Contributor

No description provided.

@thorinaboenke
thorinaboenke marked this pull request as draft May 5, 2025 08:57
Comment thread create_hashes.py Outdated
@@ -0,0 +1,196 @@
import httpx

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should extract that library to a seperate python package "attackmate_api". We can also put the attackmate_client to that package as executable. By seperating that part, other python programms can add only that api-package instead of the full attackmate-package(with lots of dependencies)

DEFAULT_TIMEOUT = 60.0 # what should the timeout be for requests? what about background?
# make timeout configurable?

logger = logging.getLogger('playbook')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we extract that file to a seperate package as an api, we don't necessary need a logger, and can simply throw exceptions. the logging part can be done by the python-script that uses that package

Comment on lines +94 to +104
token = self._get_session_token()
if not token:
# Attempt login if credentials are set on the client instance
if self.username and self.password:
logger.info(
f"No active token for {self.server_url}, try login with provided credentials."
)
token = self._login(self.username, self.password)
if not token:
logger.error(f"Auth required for {self.server_url} but no token available and login failed")
return None # Or raise an AuthException?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this to a seperate method: "authenticate()"

Comment on lines +106 to +108
headers = {'X-Auth-Token': token}
if content_data:
headers['Content-Type'] = 'application/yaml'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seperate this to a method "set_headers(token)"

if content_data:
headers['Content-Type'] = 'application/yaml'

url = f"{self.server_url}/{endpoint.lstrip('/')}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if urllib might be suitable

"""Runs a command on a given AttackMate instance."""
try:
logger.info(f"Executing command type '{command_data.type}' on instance") # type: ignore
# TODO does this work? need to pass command class object here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it?

Comment thread remote_rest/main.py
@@ -0,0 +1,168 @@
from contextlib import asynccontextmanager

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might talk about extracting that attackmate-remote-server to a seperate repo. People who use attackmate in their python-scripts, might not want to download all the dependencies for the attackmate-remote-server

Comment thread remote_rest/auth_utils.py
# In-Memory token Store
# token looks like this token : {"username": str, "expires": datetime}
# state is lost on server restart.
# Not inherently thread-safe for multi-worker setups without locks ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we will switch at some point to jwt. I guess that would be thread-safe, since we don't have any state anymore

Comment thread remote_rest/auth_utils.py

def get_user_hash(username: str) -> Optional[str]:
"""Fetches the hashed password from environment variables."""
env_var_name = f"USER_{username.upper()}_HASH"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like something for pydantic-settings

Comment thread remote_rest/client.py
import os
import sys
from typing import Any, Dict, List, Optional

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use our remote_client object. and we will put that executable to the attackmate-api-repo

@thorinaboenke
thorinaboenke marked this pull request as ready for review August 14, 2025 09:33

@whotwagner whotwagner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will merge that now. and separate the projects later

@whotwagner
whotwagner merged commit aeb0114 into ait-testbed:development Aug 14, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants