-
Notifications
You must be signed in to change notification settings - Fork 0
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 #54 from AppThreat/feature/ruby
Ruby on rails
- Loading branch information
Showing
14 changed files
with
372 additions
and
14 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
on: [push, pull_request] | ||
on: [workflow_dispatch] | ||
|
||
permissions: | ||
contents: read | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
A cli, classes and functions for converting an atom slice to a different format | ||
""" | ||
__version__ = '0.6.0' | ||
__version__ = '0.7.0' |
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,10 @@ | ||
""" | ||
Common dataclasses | ||
""" | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class HttpRoute: | ||
url_pattern: str | ||
method: str |
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,62 @@ | ||
""" | ||
Ruby converter helper | ||
""" | ||
from atom_tools.lib.slices import AtomSlice | ||
from atom_tools.lib.ruby_semantics import code_to_routes | ||
|
||
|
||
def extract_params(url): | ||
params = [] | ||
if not url: | ||
return [] | ||
if ":" in url: | ||
for part in url.split("/"): | ||
if part.startswith(":"): | ||
param = { | ||
"name": part.replace(":", ""), | ||
"in": "path", | ||
"required": True | ||
} | ||
if part == ":id": | ||
param["schema"] = { | ||
"type": "integer", | ||
"format": "int64" | ||
} | ||
params.append(param) | ||
return params | ||
|
||
|
||
def convert(usages: AtomSlice): | ||
result = [] | ||
object_slices = usages.content.get("objectSlices", {}) | ||
for oslice in object_slices: | ||
# Nested lambdas lack prefixes | ||
if oslice.get('fullName').count("<lambda>") >= 3: | ||
continue | ||
file_name = oslice.get("fileName", "") | ||
line_nums = set() | ||
if oslice.get("lineNumber"): | ||
line_nums.add(oslice.get("lineNumber")) | ||
for usage in oslice.get("usages", []): | ||
routes = code_to_routes(usage.get("targetObj", {}).get("name", {})) | ||
if routes: | ||
if usage.get("lineNumber"): | ||
line_nums.add(usage.get("lineNumber")) | ||
for route in routes: | ||
params = extract_params(route.url_pattern) | ||
amethod = { | ||
"operationId": f"{oslice.get('fullName')}" if oslice.get("fullName") else oslice.get( | ||
"fileName"), | ||
"x-atom-usages": { | ||
"call": {file_name: list(line_nums)} | ||
} | ||
} | ||
if params: | ||
amethod["parameters"] = params | ||
aresult = { | ||
route.url_pattern: { | ||
route.method.lower(): amethod | ||
} | ||
} | ||
result.append(aresult) | ||
return result |
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,113 @@ | ||
""" | ||
Ruby semantic utils | ||
""" | ||
import re | ||
from typing import List | ||
|
||
from atom_tools.lib import HttpRoute | ||
|
||
|
||
def _get_dangling_routes(i, kind, code, code_parts, url_prefix="/"): | ||
""" | ||
Internal method | ||
Args: | ||
i: | ||
kind: | ||
code: | ||
code_parts: | ||
url_prefix: | ||
Returns: | ||
""" | ||
routes = [] | ||
url_pattern = _clean_url(f"{url_prefix}{re.sub('^:', '', code_parts[i + 1])}") | ||
if kind == "resources": | ||
routes.append(HttpRoute(url_pattern=url_pattern, method="GET")) | ||
if ("match " in code and "via: :all" in code) or ("only: [" not in code and "shallow:" not in code): | ||
routes.append(HttpRoute(url_pattern=f"{url_pattern}/new", method="GET")) | ||
routes.append(HttpRoute(url_pattern=url_pattern, method="POST")) | ||
routes.append(HttpRoute(url_pattern=f"{url_pattern}/:id", method="GET")) | ||
routes.append(HttpRoute(url_pattern=f"{url_pattern}/:id/edit", method="GET")) | ||
routes.append(HttpRoute(url_pattern=f"{url_pattern}/:id", method="PUT")) | ||
routes.append(HttpRoute(url_pattern=f"{url_pattern}/:id", method="DELETE")) | ||
return routes | ||
|
||
|
||
def _clean_url(url_pattern): | ||
return re.sub('[,/]$', '', url_pattern) | ||
|
||
|
||
def code_to_routes(code: str) -> List[HttpRoute]: | ||
""" | ||
Convert code string to routes | ||
Args: | ||
code: Code snippet | ||
Returns: | ||
List of http routes | ||
""" | ||
routes = [] | ||
if not code: | ||
return [] | ||
keyword_found = False | ||
for keyword in ( | ||
"namespace", "scope", "concern", "resource", "resources", "get", | ||
"post", "patch", "delete", "put", "head", "match", | ||
"options"): | ||
if f"{keyword} " in code: | ||
keyword_found = True | ||
break | ||
if not keyword_found: | ||
return [] | ||
code_parts = code.strip().replace("...", "").split() | ||
# Dangling resources - leads to many kinds of automatic routes | ||
has_resources = "resources " in code or "resource " in code | ||
url_prefix = "" | ||
has_scope = False | ||
for i, part in enumerate(code_parts): | ||
if not part or len(part) < 2: | ||
continue | ||
if part in ("scope",) or part.startswith("scope("): | ||
has_scope = True | ||
if len(code_parts) >= i + 1 and code_parts[i + 1].startswith('":'): | ||
url_prefix = f"""/{re.sub('[:",]', '', code_parts[i + 1])}""" | ||
continue | ||
if (part in ("resource", "resources", "namespace", "member") | ||
and len(code_parts) >= i + 1 | ||
and code_parts[i + 1].startswith(":")): | ||
url_pattern = _clean_url(f"/{re.sub('^:', '', code_parts[i + 1])}") | ||
# Is there an alias for this patten | ||
if len(code_parts) > i + 3 and code_parts[i + 2] in ("path:", "path", "path("): | ||
url_pattern = _clean_url(code_parts[i + 3].replace('"', "")) | ||
routes += _get_dangling_routes(i, part, code, code_parts, | ||
f"{url_prefix}/{url_pattern}/") | ||
continue | ||
if len(code_parts) > i + 2 and code_parts[i + 2] in ("resources", "resource"): | ||
routes += _get_dangling_routes(i, code_parts[i + 2], code, code_parts, f"{url_prefix}/") | ||
elif i == len(code_parts) - 2 and part in ("resource", "resources"): | ||
routes += _get_dangling_routes(i, part, code, code_parts, f"{url_prefix}/") | ||
else: | ||
url_prefix = f"{url_prefix}{url_pattern}" | ||
continue | ||
if part in ("collection", "member", "concern", "do", "as:", "constraints:") or part.startswith( | ||
":") or part.startswith('"'): | ||
continue | ||
if part == "end" and url_prefix: | ||
url_prefix = "/".join(url_prefix.split("/")[:-1]) | ||
for m in ("get", "post", "delete", "patch", "put", "head", "options"): | ||
if part == m and len(code_parts) > i + 1 and code_parts[i + 1].startswith('"'): | ||
routes.append( | ||
HttpRoute(url_pattern=f"""{url_prefix}/{code_parts[i + 1].replace('"', "")}""", | ||
method=m.upper() if m != "patch" else "PUT")) | ||
break | ||
if has_resources: | ||
if not routes: | ||
for i, part in enumerate(code_parts): | ||
for m in ("resource", "resources"): | ||
if part == m and code_parts[i + 1].startswith(':') and ( | ||
i == len(code_parts) - 2 or (len(code_parts) > i + 2 and code_parts[i + 1] != "do")): | ||
routes += _get_dangling_routes(i, m, code, code_parts, f"{url_prefix}/" if has_scope else "/") | ||
|
||
return routes |
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,6 +1,6 @@ | ||
[project] | ||
name = "atom-tools" | ||
version = "0.6.0" | ||
version = "0.7.0" | ||
description = "Collection of tools for use with AppThreat/atom." | ||
authors = [ | ||
{ name = "Caroline Russell", email = "[email protected]" }, | ||
|
@@ -13,6 +13,7 @@ classifiers = [ | |
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
"Programming Language :: Python :: 3.13", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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.