Skip to content

Commit

Permalink
add type hints to writing folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gruebel committed Sep 9, 2023
1 parent 1a3afb8 commit 2ce3538
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 32 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include policy_sentry/py.typed
recursive-include policy_sentry/shared *.txt *.html *.yml *.json
4 changes: 2 additions & 2 deletions policy_sentry/bin/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@click.group()
@click.version_option(version=__version__)
def policy_sentry():
def policy_sentry() -> None:
"""
Policy Sentry is a tool for generating least-privilege IAM Policies.
"""
Expand All @@ -21,7 +21,7 @@ def policy_sentry():
policy_sentry.add_command(command.query.query)


def main():
def main() -> None:
"""Policy Sentry is a tool for generating least-privilege IAM Policies."""
policy_sentry()

Expand Down
Empty file added policy_sentry/py.typed
Empty file.
27 changes: 15 additions & 12 deletions policy_sentry/writing/sid_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ class SidGroup:
def __init__(self) -> None:
# Dict instead of list
# sids instead of ARN
self.sids = {}
self.universal_conditions = {}
self.skip_resource_constraints = []
self.exclude_actions = []
self.wildcard_only_single_actions = []
self.sids: dict[str, dict[str, Any]] = {}
self.universal_conditions: dict[str, Any] = {}
self.skip_resource_constraints: list[str] = []
self.exclude_actions: list[str] = []
self.wildcard_only_single_actions: list[str] = []
# When a user requests all wildcard-only actions available under a service at a specific access level
self.wildcard_only_service_read = []
self.wildcard_only_service_write = []
self.wildcard_only_service_list = []
self.wildcard_only_service_tagging = []
self.wildcard_only_service_permissions_management = []
self.wildcard_only_service_read: list[str] = []
self.wildcard_only_service_write: list[str] = []
self.wildcard_only_service_list: list[str] = []
self.wildcard_only_service_tagging: list[str] = []
self.wildcard_only_service_permissions_management: list[str] = []

def get_sid_group(self) -> dict[str, dict[str, Any]]:
"""
Expand Down Expand Up @@ -172,7 +172,7 @@ def add_sts_actions(self, sts_actions: dict[str, list[str]]) -> None:
self.sids[sid_namespace] = temp_sid_dict

def add_requested_service_wide(
self, service_prefixes: str, access_level: str
self, service_prefixes: list[str], access_level: str
) -> None:
"""
When a user requests all wildcard-only actions available under a service at a specific access level
Expand Down Expand Up @@ -226,7 +226,7 @@ def get_rendered_policy(self, minimize: int | None = None) -> dict[str, Any]:
Returns:
Dictionary: The IAM Policy JSON
"""
statements = []
statements: list[dict[str, Any]] = []
# Only set the actions to lowercase if minimize is provided
all_actions = get_all_actions(lowercase=True)

Expand Down Expand Up @@ -330,6 +330,9 @@ def add_by_arn_and_access_level(
resource_type_name = get_resource_type_name_with_raw_arn(
raw_arn_format
)
if resource_type_name is None:
continue

sid_namespace = create_policy_sid_namespace(
service_prefix, access_level, resource_type_name
)
Expand Down
15 changes: 9 additions & 6 deletions policy_sentry/writing/template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Templates for the policy_sentry YML files.
These can be used for generating policies
"""
from __future__ import annotations

from typing import Any

ACTIONS_TEMPLATE = """mode: actions
name: ''
Expand Down Expand Up @@ -71,30 +74,30 @@
"skip-resource-constraints": [],
"exclude-actions": [],
"sts": {
"assume-role": [],
"assume-role-with-saml": [],
"assume-role": [],
"assume-role-with-saml": [],
"assume-role-with-web-identity": []
}
}

ACTIONS_TEMPLATE_DICT = {"mode": "actions", "name": "", "actions": []}


def create_crud_template():
def create_crud_template() -> str:
"""Generate the CRUD YML Template """
return CRUD_TEMPLATE


def create_actions_template():
def create_actions_template() -> str:
"""Generate the Actions YML template"""
return ACTIONS_TEMPLATE


def get_crud_template_dict():
def get_crud_template_dict() -> dict[str, Any]:
"""Generate the CRUD template in dict format"""
return CRUD_TEMPLATE_DICT


def get_actions_template_dict():
def get_actions_template_dict() -> dict[str, Any]:
"""Get the Actions template in dict format."""
return ACTIONS_TEMPLATE_DICT
12 changes: 8 additions & 4 deletions policy_sentry/writing/validate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""
Validation for the Policy Sentry YML Templates.
"""
from __future__ import annotations

import logging
from typing import Any

from schema import Optional, Schema, And, Use, Regex, SchemaError

logger = logging.getLogger(__name__)


def check(conf_schema, conf):
def check(conf_schema: Schema, conf: dict[str, Any]) -> bool:
"""
Validates a user-supplied JSON vs a defined schema.
Expand Down Expand Up @@ -60,7 +64,7 @@ def check(conf_schema, conf):
)


def check_actions_schema(cfg):
def check_actions_schema(cfg: dict[str, Any]) -> bool:
"""
Determines whether the user-provided config matches the required schema for Actions mode
"""
Expand All @@ -75,7 +79,7 @@ def check_actions_schema(cfg):
)


def check_crud_schema(cfg):
def check_crud_schema(cfg: dict[str, Any]) -> bool:
"""
Determines whether the user-provided config matches the required schema for CRUD mode
"""
Expand All @@ -90,7 +94,7 @@ def check_crud_schema(cfg):
)


def validate_condition_block(condition_block):
def validate_condition_block(condition_block: dict[str, Any]) -> bool:
"""
Validates the format of the condition block that should be supplied in the template.
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ pretty = true
exclude = [
'^policy_sentry/bin',
'^policy_sentry/command',
'^policy_sentry/writing',
]

[[tool.mypy.overrides]]
module = [
"schema"
]
ignore_missing_imports = true

[tool.pytest.ini_options]
testpaths = [
"test",
Expand Down
15 changes: 8 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ def get_description():
install_requires=REQUIRED_PACKAGES,
project_urls=PROJECT_URLS,
classifiers=[
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Typing :: Typed",
],
entry_points={"console_scripts": "policy_sentry=policy_sentry.bin.cli:main"},
zip_safe=True,
Expand Down

0 comments on commit 2ce3538

Please sign in to comment.