Skip to content

Commit

Permalink
[PROJQUAY-1021] task: Update "Black" to version 20.8b1
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtismullins committed Nov 30, 2020
1 parent 28a5200 commit bd7252c
Show file tree
Hide file tree
Showing 234 changed files with 5,310 additions and 2,241 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# https://issues.redhat.com/browse/PROJQUAY-92
# https://github.com/psf/black/issues/1207#issuecomment-566249522
pip install black==19.10b0 --no-binary=regex
pip install black==20.8b1
pip install flake8
- name: Check Formatting (Black)
run: |
# TODO(kleesc): Re-enable after buildman rewrite
black --line-length=100 --target-version=py38 --check --diff . --exclude "buildman"
black --line-length=100 --target-version=py38 --check --diff .
- name: Check Formatting (Flake8)
run: |
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,4 @@ generate-proto-py:


black:
black --line-length 100 --target-version py36 --exclude "/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist|buildman)/" . # TODO(kleesc): Re-enable after buildman rewrite

black --line-length=100 --target-version=py38 --exclude "/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/" .
6 changes: 5 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def _request_start():

host, port = os.getenv("PYDEV_DEBUG").split(":")
pydevd_pycharm.settrace(
host, port=int(port), stdoutToServer=True, stderrToServer=True, suspend=False,
host,
port=int(port),
stdoutToServer=True,
stderrToServer=True,
suspend=False,
)

logger.debug(
Expand Down
25 changes: 20 additions & 5 deletions auth/auth_context_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,42 @@ def to_signed_dict(self):
# TODO: Remove this all once the new code is fully deployed.
if self.token:
dict_data.update(
{"kind": "token", "token": self.token.get_code(),}
{
"kind": "token",
"token": self.token.get_code(),
}
)

if self.oauthtoken:
dict_data.update(
{"kind": "oauth", "oauth": self.oauthtoken.uuid, "user": self.authed_user.username,}
{
"kind": "oauth",
"oauth": self.oauthtoken.uuid,
"user": self.authed_user.username,
}
)

if self.user or self.robot:
dict_data.update(
{"kind": "user", "user": self.authed_user.username,}
{
"kind": "user",
"user": self.authed_user.username,
}
)

if self.appspecifictoken:
dict_data.update(
{"kind": "user", "user": self.authed_user.username,}
{
"kind": "user",
"user": self.authed_user.username,
}
)

if self.is_anonymous:
dict_data.update(
{"kind": "anonymous",}
{
"kind": "anonymous",
}
)

# End of legacy information.
Expand Down
13 changes: 10 additions & 3 deletions auth/context_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def description(self, entity_reference):
return "user %s" % entity_reference.username

def analytics_id_and_public_metadata(self, entity_reference):
return entity_reference.username, {"username": entity_reference.username,}
return entity_reference.username, {
"username": entity_reference.username,
}


class RobotEntityHandler(ContextEntityHandler):
Expand All @@ -121,7 +123,10 @@ def description(self, entity_reference):
return "robot %s" % entity_reference.username

def analytics_id_and_public_metadata(self, entity_reference):
return entity_reference.username, {"username": entity_reference.username, "is_robot": True,}
return entity_reference.username, {
"username": entity_reference.username,
"is_robot": True,
}


class TokenEntityHandler(ContextEntityHandler):
Expand All @@ -138,7 +143,9 @@ def description(self, entity_reference):
return "token %s" % entity_reference.friendly_name

def analytics_id_and_public_metadata(self, entity_reference):
return "token:%s" % entity_reference.id, {"token": entity_reference.friendly_name,}
return "token:%s" % entity_reference.id, {
"token": entity_reference.friendly_name,
}


class OAuthTokenEntityHandler(ContextEntityHandler):
Expand Down
12 changes: 10 additions & 2 deletions auth/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@

SCOPE_MAX_TEAM_ROLES = defaultdict(lambda: None)
SCOPE_MAX_TEAM_ROLES.update(
{scopes.CREATE_REPO: "creator", scopes.DIRECT_LOGIN: "admin", scopes.ORG_ADMIN: "admin",}
{
scopes.CREATE_REPO: "creator",
scopes.DIRECT_LOGIN: "admin",
scopes.ORG_ADMIN: "admin",
}
)

SCOPE_MAX_USER_ROLES = defaultdict(lambda: None)
SCOPE_MAX_USER_ROLES.update(
{scopes.READ_USER: "read", scopes.DIRECT_LOGIN: "admin", scopes.ADMIN_USER: "admin",}
{
scopes.READ_USER: "read",
scopes.DIRECT_LOGIN: "admin",
scopes.ADMIN_USER: "admin",
}
)


Expand Down
19 changes: 16 additions & 3 deletions auth/registry_jwt_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
"description": "List of access granted to the subject",
"items": {
"type": "object",
"required": ["type", "name", "actions",],
"required": [
"type",
"name",
"actions",
],
"properties": {
"type": {
"type": "string",
"description": "We only allow repository permissions",
"enum": ["repository",],
"enum": [
"repository",
],
},
"name": {
"type": "string",
Expand All @@ -45,7 +51,14 @@
"actions": {
"type": "array",
"description": "List of specific verbs which can be performed against repository",
"items": {"type": "string", "enum": ["push", "pull", "*",],},
"items": {
"type": "string",
"enum": [
"push",
"pull",
"*",
],
},
},
},
},
Expand Down
8 changes: 7 additions & 1 deletion auth/test/test_auth_context_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ def get_oauth_token(_):
(ContextEntityKind.user, "devtable", model.user.get_user),
],
)
@pytest.mark.parametrize("v1_dict_format", [(True), (False),])
@pytest.mark.parametrize(
"v1_dict_format",
[
(True),
(False),
],
)
def test_signed_auth_context(kind, entity_reference, loader, v1_dict_format, initialized_db):
if kind == ContextEntityKind.anonymous:
validated = ValidatedAuthContext()
Expand Down
3 changes: 2 additions & 1 deletion auth/test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ def test_invalid_unicode_3(app):
header = "basic " + b64encode(auth).decode("ascii")
result = validate_basic_auth(header)
assert result == ValidateResult(
AuthKind.basic, error_message="Could not find robot with specified username",
AuthKind.basic,
error_message="Could not find robot with specified username",
)
16 changes: 14 additions & 2 deletions auth/test/test_registry_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@

def _access(typ="repository", name="somens/somerepo", actions=None):
actions = [] if actions is None else actions
return [{"type": typ, "name": name, "actions": actions,}]
return [
{
"type": typ,
"name": name,
"actions": actions,
}
]


def _delete_field(token_data, field_name):
Expand Down Expand Up @@ -228,7 +234,13 @@ def test_mixing_keys_e2e(initialized_db):
_parse_token(deleted_key_token)


@pytest.mark.parametrize("token", ["someunicodetoken✡", "\xc9\xad\xbd",])
@pytest.mark.parametrize(
"token",
[
"someunicodetoken✡",
"\xc9\xad\xbd",
],
)
def test_unicode_token(token):
with pytest.raises(InvalidJWTException):
_parse_token(token)
8 changes: 7 additions & 1 deletion auth/test/test_signedgrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def test_token(header, expected_result):
def test_valid_grant():
header = "token " + generate_signed_token({"a": "b"}, {"c": "d"})
expected = ValidateResult(
AuthKind.signed_grant, signed_data={"grants": {"a": "b",}, "user_context": {"c": "d"},}
AuthKind.signed_grant,
signed_data={
"grants": {
"a": "b",
},
"user_context": {"c": "d"},
},
)
assert validate_signed_grant(header) == expected
4 changes: 2 additions & 2 deletions avatars/avatars.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ def get_data_for_external_user(self, external_user):
return self.get_data(external_user.username, external_user.email, "user")

def get_data(self, name, email_or_id, kind="user"):
""" Computes and returns the full data block for the avatar:
"""Computes and returns the full data block for the avatar:
{
'name': name,
'hash': The gravatar hash, if any.
'color': The color for the avatar
}
"""
"""
colors = self.colors

# Note: email_or_id may be None if gotten from external auth when email is disabled,
Expand Down
31 changes: 19 additions & 12 deletions buildman/build_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

from app import instance_keys
from util.security import jwtutil
from util.security.registry_jwt import \
generate_bearer_token, \
InvalidBearerTokenException, \
ALGORITHM, \
JWT_CLOCK_SKEW_SECONDS
from util.security.registry_jwt import (
generate_bearer_token,
InvalidBearerTokenException,
ALGORITHM,
JWT_CLOCK_SKEW_SECONDS,
)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -40,7 +41,7 @@
"type": "number",
"description": "The number of seconds until the job expires",
},
}
},
}


Expand All @@ -54,7 +55,7 @@ def build_token(aud, token_type, build_id, job_id, expiration, instance_keys):
"token_type": token_type,
"build_id": build_id,
"job_id": job_id,
"expiration": expiration
"expiration": expiration,
}

token = generate_bearer_token(aud, ANONYMOUS_SUB, token_data, {}, expiration, instance_keys)
Expand Down Expand Up @@ -87,7 +88,7 @@ def verify_build_token(token, aud, token_type, instance_keys):
algorithms=[ALGORITHM],
audience=aud,
issuer=instance_keys.service_name,
leeway=JWT_CLOCK_SKEW_SECONDS
leeway=JWT_CLOCK_SKEW_SECONDS,
)
except jwtutil.InvalidTokenError as ite:
logger.error("Invalid token reason: %s", ite)
Expand All @@ -99,19 +100,25 @@ def verify_build_token(token, aud, token_type, instance_keys):
if payload["sub"] != ANONYMOUS_SUB:
raise InvalidBuildTokenException("Wrong sub field in JWT")

if ("context" not in payload
if (
"context" not in payload
or not payload["context"]["token_type"]
or not payload["context"]["build_id"]
or not payload["context"]["job_id"]
or not payload["context"]["expiration"]):
or not payload["context"]["expiration"]
):
raise InvalidBuildTokenException("Missing context field in JWT")

try:
jsonschema.validate(payload["context"], BUILD_TOKEN_CONTEXT_SCHEMA)
except jsonschema.ValidationError:
raise InvalidBuildTokenException("Unable to validate build token context schema: malformed context")
raise InvalidBuildTokenException(
"Unable to validate build token context schema: malformed context"
)

if payload["context"]["token_type"] != token_type:
raise InvalidBuildTokenException("Build token type in JWT does not match expected type: %s" % token_type)
raise InvalidBuildTokenException(
"Build token type in JWT does not match expected type: %s" % token_type
)

return payload
2 changes: 1 addition & 1 deletion buildman/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
build_logs,
dockerfile_build_queue,
instance_keys,
OVERRIDE_CONFIG_DIRECTORY
OVERRIDE_CONFIG_DIRECTORY,
)
from util.log import logfile_path

Expand Down
Loading

0 comments on commit bd7252c

Please sign in to comment.