Skip to content

Commit 67139df

Browse files
committed
improve IAM Db loading
1 parent a125451 commit 67139df

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

policy_sentry/command/initialize.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
from policy_sentry import set_stream_logger
1515
from policy_sentry.querying.all import get_all_service_prefixes
16-
from policy_sentry.shared.awsdocs import (
17-
create_database,
18-
update_html_docs_directory,
19-
)
2016
from policy_sentry.shared.constants import (
2117
BUNDLED_DATA_DIRECTORY,
2218
BUNDLED_DATASTORE_FILE_PATH,
@@ -90,6 +86,13 @@ def initialize(
9086
Initialize the local data file to store AWS IAM information, which can be used to generate IAM policies, and for
9187
querying the database.
9288
"""
89+
90+
# importing 'awsdocs' is quite pricey, when it is actually only used for initialize the IAM DB
91+
from policy_sentry.shared.awsdocs import (
92+
create_database,
93+
update_html_docs_directory,
94+
)
95+
9396
if not access_level_overrides_file:
9497
overrides_file = LOCAL_ACCESS_OVERRIDES_FILE
9598
else:

policy_sentry/shared/iam_data.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from __future__ import annotations
44

55
import functools
6-
import json
6+
import gc
77
import logging
88
from pathlib import Path
99
from typing import Any, cast
1010

11+
import orjson
12+
1113
from policy_sentry.shared.constants import (
1214
DATASTORE_FILE_PATH,
1315
POLICY_SENTRY_SCHEMA_VERSION_NAME,
@@ -18,7 +20,23 @@
1820
# On initialization, load the IAM data
1921
iam_definition_path = DATASTORE_FILE_PATH
2022
logger.debug(f"Leveraging the IAM definition at {iam_definition_path}")
21-
iam_definition = json.loads(Path(iam_definition_path).read_bytes())
23+
24+
25+
def load_iam_definition() -> dict[str, Any]:
26+
gc_enabled = gc.isenabled()
27+
if gc_enabled:
28+
# https://github.com/msgpack/msgpack-python?tab=readme-ov-file#performance-tips
29+
gc.disable()
30+
31+
data: dict[str, Any] = orjson.loads(Path(iam_definition_path).read_bytes())
32+
33+
if gc_enabled:
34+
gc.enable()
35+
36+
return data
37+
38+
39+
iam_definition = load_iam_definition()
2240

2341

2442
@functools.lru_cache(maxsize=1)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ requests==2.32.3
66
# Config files and schema validation
77
PyYAML==6.0.1
88
schema==0.7.7
9+
# IAM DB
10+
orjson==3.10.6

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"requests",
1414
"schema",
1515
"PyYAML",
16+
"orjson",
1617
]
1718
PROJECT_URLS = {
1819
"Documentation": "https://policy-sentry.readthedocs.io/",

0 commit comments

Comments
 (0)