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

Upgrade to pydantic v2 #286

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.15.7]
### Updates
- Bumped pycfmodel to use pydantic v2

## [1.15.6]
## Fixes
- Fix logo displayed in pypi
### Updates
- Bumped python used in readthedocs to 3.9

## [1.15.5]
### Changes
- Add dependabot config
- Migrate to `pyproject.toml`

## [1.15.4]
## Fixes
- Fix `KMSKeyWildcardPrincipalRule` to work without a KMS policy
Expand Down
14 changes: 5 additions & 9 deletions cfripper/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from typing import DefaultDict, Dict, List

from pydantic import BaseModel
from pydantic import RootModel

from cfripper.config.constants import (
AWS_CLOUDTRAIL_ACCOUNT_IDS,
Expand Down Expand Up @@ -201,7 +201,7 @@ def load_rules_config_file(self, rules_config_file: TextIOWrapper):
spec.loader.exec_module(module)
rules_config = vars(module).get("RULES_CONFIG")
# Validate rules_config format
RulesConfigMapping(__root__=rules_config)
RulesConfigMapping.model_validate(rules_config)
self.rules_config = rules_config
except Exception:
logger.exception(f"Failed to read config file: {filename}")
Expand Down Expand Up @@ -236,7 +236,7 @@ def get_filters_from_filename_path(cls, filename: Path) -> List[Filter]:
spec.loader.exec_module(module)
filters = vars(module).get("FILTERS") or []
# Validate filters format
RulesFiltersMapping(__root__=filters)
RulesFiltersMapping.model_validate(filters)
return filters

def add_filters(self, filters: List[Filter]):
Expand All @@ -245,9 +245,5 @@ def add_filters(self, filters: List[Filter]):
self.rules_filters[rule].append(rule_filter)


class RulesConfigMapping(BaseModel):
__root__: Dict[str, RuleConfig]


class RulesFiltersMapping(BaseModel):
__root__: List[Filter]
RulesConfigMapping = RootModel[Dict[str, RuleConfig]]
RulesFiltersMapping = RootModel[List[Filter]]
7 changes: 4 additions & 3 deletions cfripper/config/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from typing import Any, Callable, Dict, List, Optional, Set, Union

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator
from pydash.objects import get

from cfripper.model.enums import RuleMode, RuleRisk
Expand Down Expand Up @@ -83,9 +83,10 @@ class Filter(BaseModel):
risk_value: Optional[RuleRisk] = None
rules: Set[str] = None

@validator("eval", pre=True)
@field_validator("eval", mode="before")
@classmethod
def set_eval(cls, eval, values):
return build_evaluator(eval, values["debug"])
return build_evaluator(eval, values.data["debug"])

def __call__(self, **kwargs):
if self.debug:
Expand Down
9 changes: 3 additions & 6 deletions cfripper/model/result.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Collection, List, Optional

from pydantic import BaseModel, Extra
from pydantic import BaseModel, ConfigDict

from cfripper.model.enums import RuleGranularity, RuleMode, RuleRisk

Expand All @@ -15,8 +15,7 @@ class Failure(BaseModel):
resource_ids: Optional[set] = set()
resource_types: Optional[set] = set()

class Config(BaseModel.Config):
extra = Extra.forbid
model_config = ConfigDict(extra="forbid")

def serializable(self):
return {
Expand All @@ -32,9 +31,7 @@ def serializable(self):


class Result(BaseModel):
class Config(BaseModel.Config):
extra = Extra.forbid
arbitrary_types_allowed = True
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)

exceptions: List[Exception] = []
failures: List[Failure] = []
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies = [
"cfn_flip>=1.2.0",
"click>=8.0.0",
"pluggy~=0.13.1",
"pycfmodel>=0.22.0",
"pycfmodel>=1.0.0",
"pydash>=4.7.6",
"PyYAML>=4.2b1"
]
Expand Down
90 changes: 44 additions & 46 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,80 +1,78 @@
# This file was autogenerated by uv v0.1.2 via the following command:
# uv pip compile --no-emit-index-url --no-annotate -v pyproject.toml --extra dev --output-file requirements-dev.txt
# uv pip compile --no-emit-index-url --no-annotate -v pyproject.toml --upgrade --extra dev --output-file requirements-dev.txt
annotated-types==0.7.0
antlr4-python3-runtime==4.13.1
attrs==23.2.0
aws-sam-translator==1.85.0
aws-xray-sdk==2.12.1
boto3==1.34.44
botocore==1.34.44
certifi==2024.2.2
aws-sam-translator==1.89.0
aws-xray-sdk==2.14.0
boto3==1.34.123
botocore==1.34.123
certifi==2024.6.2
cffi==1.16.0
cfn-flip==1.3.0
cfn-lint==0.85.2
cfn-lint==0.87.5
charset-normalizer==3.3.2
click==8.1.7
coverage==7.4.1
cryptography==42.0.4
docker==7.0.0
ecdsa==0.18.0
exceptiongroup==1.2.0
coverage==7.5.3
cryptography==42.0.8
docker==7.1.0
exceptiongroup==1.2.1
graphql-core==3.2.3
idna==3.7
importlib-resources==6.1.1
iniconfig==2.0.0
jinja2==3.1.3
jinja2==3.1.4
jmespath==1.0.1
joserfc==0.11.1
jschema-to-python==1.2.3
jsondiff==2.0.0
jsonpatch==1.33
jsonpickle==3.0.2
jsonpointer==2.4
jsonschema==4.21.1
jsonpath-ng==1.6.1
jsonpickle==3.2.1
jsonpointer==3.0.0
jsonschema==4.22.0
jsonschema-path==0.3.2
jsonschema-specifications==2023.12.1
junit-xml==1.9
lazy-object-proxy==1.10.0
markupsafe==2.1.5
moto==5.0.1
moto==5.0.9
mpmath==1.3.0
multipart==0.2.4
networkx==3.1
networkx==3.2.1
openapi-schema-validator==0.6.2
openapi-spec-validator==0.7.1
packaging==23.2
packaging==24.1
pathable==0.4.3
pbr==6.0.0
pkgutil-resolve-name==1.3.10
pluggy==0.13.1
py-partiql-parser==0.5.1
pyasn1==0.5.1
pycfmodel==0.22.0
pycparser==2.21
pydantic==1.10.14
pydash==7.0.7
pyparsing==3.1.1
ply==3.11
py-partiql-parser==0.5.5
pycfmodel==1.0.0
pycparser==2.22
pydantic==2.7.3
pydantic-core==2.18.4
pydash==8.0.1
pyparsing==3.1.2
pytest==7.4.4
pytest-cov==4.1.0
python-dateutil==2.8.2
python-jose==3.3.0
pyyaml==6.0.1
pytest-cov==5.0.0
python-dateutil==2.9.0.post0
pyyaml==6.0.2rc1
referencing==0.31.1
regex==2023.12.25
requests==2.31.0
responses==0.25.0
regex==2024.5.15
requests==2.32.3
responses==0.25.2
rfc3339-validator==0.1.4
rpds-py==0.18.0
rsa==4.9
ruff==0.2.1
s3transfer==0.10.0
rpds-py==0.18.1
ruff==0.4.8
s3transfer==0.10.1
sarif-om==1.0.4
setuptools==69.1.0
setuptools==70.0.0
six==1.16.0
sshpubkeys==3.3.1
sympy==1.12
sympy==1.12.1
tomli==2.0.1
typing-extensions==4.9.0
typing-extensions==4.12.2
urllib3==1.26.18
uv==0.1.2
werkzeug==3.0.1
uv==0.2.10
werkzeug==3.0.3
wrapt==1.16.0
xmltodict==0.13.0
zipp==3.17.0
101 changes: 50 additions & 51 deletions requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,97 +1,96 @@
# This file was autogenerated by uv v0.1.2 via the following command:
# uv pip compile --no-emit-index-url --no-annotate -v pyproject.toml --extra dev --extra docs --output-file requirements-docs.txt
# uv pip compile --no-emit-index-url --no-annotate -v pyproject.toml --upgrade --extra docs --extra dev --output-file requirements-docs.txt
annotated-types==0.7.0
antlr4-python3-runtime==4.13.1
attrs==23.2.0
aws-sam-translator==1.85.0
aws-xray-sdk==2.12.1
boto3==1.34.44
botocore==1.34.44
certifi==2024.2.2
aws-sam-translator==1.89.0
aws-xray-sdk==2.14.0
boto3==1.34.123
botocore==1.34.123
certifi==2024.6.2
cffi==1.16.0
cfn-flip==1.3.0
cfn-lint==0.85.2
cfn-lint==0.87.5
charset-normalizer==3.3.2
click==8.1.7
coverage==7.4.1
cryptography==42.0.4
coverage==7.5.3
cryptography==42.0.8
csscompressor==0.9.5
docker==7.0.0
ecdsa==0.18.0
exceptiongroup==1.2.0
docker==7.1.0
exceptiongroup==1.2.1
ghp-import==2.1.0
graphql-core==3.2.3
htmlmin==0.1.12
idna==3.7
importlib-metadata==7.0.1
importlib-resources==6.1.1
importlib-metadata==7.1.0
iniconfig==2.0.0
jinja2==3.1.3
jinja2==3.1.4
jmespath==1.0.1
joserfc==0.11.1
jschema-to-python==1.2.3
jsmin==3.0.1
jsondiff==2.0.0
jsonpatch==1.33
jsonpickle==3.0.2
jsonpointer==2.4
jsonschema==4.21.1
jsonpath-ng==1.6.1
jsonpickle==3.2.1
jsonpointer==3.0.0
jsonschema==4.22.0
jsonschema-path==0.3.2
jsonschema-specifications==2023.12.1
junit-xml==1.9
lazy-object-proxy==1.10.0
markdown==3.5.2
markdown==3.6
markupsafe==2.1.5
mergedeep==1.3.4
mkdocs==1.3.0
mkdocs-macros-plugin==0.7.0
mkdocs-material==8.2.8
mkdocs-material-extensions==1.0.3
mkdocs-minify-plugin==0.5.0
moto==5.0.1
moto==5.0.9
mpmath==1.3.0
multipart==0.2.4
networkx==3.1
networkx==3.2.1
openapi-schema-validator==0.6.2
openapi-spec-validator==0.7.1
packaging==23.2
packaging==24.1
pathable==0.4.3
pbr==6.0.0
pkgutil-resolve-name==1.3.10
pluggy==0.13.1
py-partiql-parser==0.5.1
pyasn1==0.5.1
pycfmodel==0.22.0
pycparser==2.21
pydantic==1.10.14
pydash==7.0.7
pygments==2.17.2
pymdown-extensions==10.7
pyparsing==3.1.1
ply==3.11
py-partiql-parser==0.5.5
pycfmodel==1.0.0
pycparser==2.22
pydantic==2.7.3
pydantic-core==2.18.4
pydash==8.0.1
pygments==2.18.0
pymdown-extensions==10.8.1
pyparsing==3.1.2
pytest==7.4.4
pytest-cov==4.1.0
python-dateutil==2.8.2
python-jose==3.3.0
pyyaml==6.0.1
pytest-cov==5.0.0
python-dateutil==2.9.0.post0
pyyaml==6.0.2rc1
pyyaml-env-tag==0.1
referencing==0.31.1
regex==2023.12.25
requests==2.31.0
responses==0.25.0
regex==2024.5.15
requests==2.32.3
responses==0.25.2
rfc3339-validator==0.1.4
rpds-py==0.18.0
rsa==4.9
ruff==0.2.1
s3transfer==0.10.0
rpds-py==0.18.1
ruff==0.4.8
s3transfer==0.10.1
sarif-om==1.0.4
setuptools==69.1.0
setuptools==70.0.0
six==1.16.0
sshpubkeys==3.3.1
sympy==1.12
sympy==1.12.1
termcolor==2.4.0
tomli==2.0.1
typing-extensions==4.9.0
typing-extensions==4.12.2
urllib3==1.26.18
uv==0.1.2
watchdog==4.0.0
werkzeug==3.0.1
uv==0.2.10
watchdog==4.0.1
werkzeug==3.0.3
wrapt==1.16.0
xmltodict==0.13.0
zipp==3.17.0
zipp==3.19.2
Loading
Loading