diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000..2a8129f43 --- /dev/null +++ b/.flake8 @@ -0,0 +1,5 @@ +[flake8] +ignore = E203, E266, E501, W503, F403, F401 +max-line-length = 88 +max-complexity = 18 +select = B,C,E,F,W,T4,B9 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..d362be355 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,39 @@ +default_stages: [commit] +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: requirements-txt-fixer + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + + - repo: https://github.com/psf/black + rev: 22.8.0 + hooks: + - id: black + name: black formatting + entry: bash -c 'black --check $(git diff --name-only --cached --diff-filter=ACMR | grep .py)' + + - repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + name: isort (python) + entry: bash -c 'isort --check-only $(git diff --name-only --cached --diff-filter=ACMR | grep .py)' + + - repo: https://github.com/pycqa/flake8 + rev: 5.0.4 + hooks: + - id: flake8 + name: flake8 format check + entry: bash -c 'flake8 --config=.flake8 $(git diff --name-only --cached --diff-filter=ACMR | grep .py)' + + - repo: local + hooks: + - id: jira-ticket + name: Check for Jira ticket + language: pygrep + entry: '\A(?!ANPL+-[0-9]+)' + args: [--multiline] + stages: [commit-msg] diff --git a/controlpanel/settings/test.py b/controlpanel/settings/test.py index a315a9ecb..94063ad68 100644 --- a/controlpanel/settings/test.py +++ b/controlpanel/settings/test.py @@ -1,29 +1,29 @@ +# First-party/Local from controlpanel.settings.common import * - -ENV = 'test' +ENV = "test" AWS_COMPUTE_ACCOUNT_ID = "test_compute_account_id" AWS_DATA_ACCOUNT_ID = "123456789012" # XXX DO NOT CHANGE - it will break moto tests K8S_WORKER_ROLE_NAME = "nodes.example.com" SAML_PROVIDER = "test-saml" -LOGGING["loggers"]["django_structlog"]["level"] = "WARNING" -LOGGING["loggers"]["controlpanel"]["level"] = "WARNING" +LOGGING["loggers"]["django_structlog"]["level"] = "WARNING" # noqa: F405 +LOGGING["loggers"]["controlpanel"]["level"] = "WARNING" # noqa: F405 AUTHENTICATION_BACKENDS = [ - 'rules.permissions.ObjectPermissionBackend', - 'django.contrib.auth.backends.ModelBackend', + "rules.permissions.ObjectPermissionBackend", + "django.contrib.auth.backends.ModelBackend", ] -MIDDLEWARE.remove('mozilla_django_oidc.middleware.SessionRefresh') -REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].remove( - 'mozilla_django_oidc.contrib.drf.OIDCAuthentication', +MIDDLEWARE.remove("mozilla_django_oidc.middleware.SessionRefresh") # noqa: F405 +REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"].remove( # noqa: F405 + "mozilla_django_oidc.contrib.drf.OIDCAuthentication", ) OIDC_OP_JWKS_ENDPOINT = "https://example.com/.well-known/jwks.json" OIDC_ALLOW_UNSECURED_JWT = True OIDC_DOMAIN = "oidc.idp.example.com" -TOOLS_DOMAIN = 'example.com' +TOOLS_DOMAIN = "example.com" CSRF_COOKIE_SECURE = False SESSION_COOKIE_SECURE = False diff --git a/controlpanel/wsgi.py b/controlpanel/wsgi.py index 0db025379..db861aba0 100644 --- a/controlpanel/wsgi.py +++ b/controlpanel/wsgi.py @@ -7,9 +7,13 @@ https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/ """ +# Standard library import os +# Third-party from django.core.wsgi import get_wsgi_application + +# First-party/Local from controlpanel.utils import load_app_conf_from_file os.environ.setdefault("DJANGO_SETTINGS_MODULE", "controlpanel.settings") diff --git a/doc/running.md b/doc/running.md index da8e89aeb..2d837e225 100644 --- a/doc/running.md +++ b/doc/running.md @@ -37,6 +37,8 @@ python3 -m venv venv source venv/bin/activate pip3 install -r requirements.txt pip3 install -r requirements.dev.txt +pre-commit install --hook-type commit-msg +pre-commit install ``` In addition, you must have: @@ -82,7 +84,7 @@ and have [cluster admin access to Kubernetes](https://silver-dollop-30c6a355.pag ### AWS Configuration In order to run the app you'll need various permissions set up for you in the -wider infrastructure of the project, mainly for AWS platform. +wider infrastructure of the project, mainly for AWS platform. As the docs for AWS (linked above) mention, you'll need to add yourself an AWS user account linked to your MoJ email address via the @@ -257,14 +259,14 @@ and then ask a colleague for help. ### Local AWS profile setup (on first run only) This app needs to interact with multiple AWS accounts in order to support the users' needs. -The AWS resources like IAM, s3 buckets are under our data account and will be managed by +The AWS resources like IAM, s3 buckets are under our data account and will be managed by app through [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html). In order to make sure the boto3 can obtain the right profile for local env. The following steps will show how to create it. Assume that the name of profile for our aws data account is ```admin-data``` #### Add the AWS credential into .aws/credentials -it should look like below +it should look like below ``` [admin-data] aws_access_key_id = @@ -317,17 +319,17 @@ If you want to run the control panel app to manage AWS resources under single ro following environment variable to define the profile you want to use - ```AWS_PROFILE```: The profile which will be used for ```boto3``` auth export AWS_PROFILE = "admin-data" -- Make sure there is NO other AWS boto3 environment variables defined. +- Make sure there is NO other AWS boto3 environment variables defined. #### AWS credential setting for multiple AWS roles -If you want to run the app to manage the AWS resources cross different AWS accounts by assuming +If you want to run the app to manage the AWS resources cross different AWS accounts by assuming different roles, then - Check whether following 2 more environment variables have been setup in the env file or not - `AWS_DATA_ACCOUNT_ROLE`: The role_arn of admin-data account - `AWS_DEV_ACCOUNT_ROLE` : The role_arn of admin-dev account - + if you are not sure what the value of role_arn of those two accounts is, you can find them out by - checking the aws config file. + checking the aws config file. More detail about the settings for mult-account is [here](architecture.md) (last section) - Make sure other AWS boto3 settings e.g. ```AWS_PROFILE``` are NOT defined in your env, otherwise the app will @@ -370,7 +372,7 @@ Go to http://localhost:8000/, sign in via Auth0 and marvel at your locally running control panel. NOTES: if you use aws-vault to manage your AWS credentials, during the running process of the app, -you may encounter a popup window for asking you to provide key-chain password from time to time, +you may encounter a popup window for asking you to provide key-chain password from time to time, which is normal. ### Loading tools @@ -389,3 +391,35 @@ Check that you have `_AUTH_CLIENT_DOMAIN`, `_AUTH_CLIENT_ID` and `