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

Authentication helpers #921

Closed
wants to merge 1 commit into from
Closed
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
117 changes: 117 additions & 0 deletions tools/create_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
## A user in the GMT is used to restrict access to certain routes, data-retentions, optimizations, machines etc.
## In order to deliver this functionality a capability dictionary is created by this script that is then filled
## into the `users` table
## By default a "DEFAULT" user is created which has unlimited access to all machines and routes.
##
## However since you will be adding machines and optimizations etc. to the GMT you need a dynamic mechanism to update
## the capabilities dictionary and also add new users with restricted capabilities.
##
## This is the default dictionary created:
# {
# "api": {
# "quotas": { # An empty dictionary here means that no quotas apply
# },
# "routes": [ # This will be dynamically loaded from the current main.py
# "/v1/carbondb/add",
# "/v1/ci/measurement/add",
# "/v1/software/add",
# "/v1/hog/add"
# ]
# },
# "measurement": {
# "flow-process-duration": 3600,
# "total-duration": 3600
# },
# "data": {
# "runs": {
# "retention": 3600,
# },
# "measurements": {
# "retention": 3600,
# },
# "ci_measurements": {
# "retention": 3600,
# },
# "hog_measurements": {
# "retention": 3600,
# },
# "hog_coalitions": {
# "retention": 3600,
# },
# "hog_tasks": {
# "retention": 3600,
# },
# },
# "machines": [ # This will be dynamically loaded from the current database
# 1,
# ],
# "optimizations": [ # This will be dynamically loaded from the current filesystem
# "container_memory_utilization",
# "container_cpu_utilization",
# "message_optimization",
# "container_build_time",
# "container_boot_time",
# "container_image_size",
# ],
# }

import ast

def parse_fastapi_routes(file_path):
with open(file_path, 'r') as file:
content = file.read()

# Parse the content into an AST
tree = ast.parse(content)

routes = []

# Function to extract the path from a decorator
def extract_path(decorator):
if isinstance(decorator, ast.Call) and isinstance(decorator.func, ast.Attribute):
if decorator.func.attr in ['post']:
# Check if there's at least one argument
if decorator.args:
# The first argument should be the path
path_arg = decorator.args[0]
if isinstance(path_arg, ast.Constant):
return decorator.func.attr.upper(), path_arg.value
return None

# Traverse the AST
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) or isinstance(node, ast.AsyncFunctionDef):
for decorator in node.decorator_list:
result = extract_path(decorator)
if result:
method, path = result
routes.append((method, path))

return routes



if __name__ == '__main__':

# parser = argparse.ArgumentParser()
# parser.add_argument('filename', type=str)

# parser.add_argument('run_id', type=str)
# parser.add_argument('db_host', type=str)
# parser.add_argument('db_pw', type=str)


# Example usage
file_path = '/Users/arne/Sites/green-coding/green-metrics-tool/api/main.py'
routes = parse_fastapi_routes(file_path)

# Print the routes
for method, path in routes:
print(f"{method}: {path}")

## TODO
# - Set quotas for routes
# - Set restrictions to machines
# - Update a user maybe when a new machine gets added?
# - Insert a new machine script? Then also add this machine to every user?

Loading