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

[DNM] test pipeline failures #1211

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions fence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ def _load_keys(app, root_dir):
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

app.keypairs = keys.load_keypairs(os.path.join(root_dir, "keys"))

app.jwt_public_keys = {
pub_keys = {
config["BASE_URL"]: OrderedDict(
[(str(keypair.kid), str(keypair.public_key)) for keypair in app.keypairs]
)
}
app.jwt_public_keys = pub_keys


def _setup_oidc_clients(app):
Expand Down
28 changes: 17 additions & 11 deletions fence/blueprints/data/indexd.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@ def get_signed_url_for_file(
)

prepare_presigned_url_audit_log(requested_protocol, indexed_file)
signed_url, authorized_user_from_passport = indexed_file.get_signed_url(
requested_protocol,
action,
expires_in,
force_signed_url=force_signed_url,
r_pays_project=r_pays_project,
file_name=file_name,
users_from_passports=users_from_passports,
bucket=bucket,
)
try:
signed_url, authorized_user_from_passport = indexed_file.get_signed_url(
requested_protocol,
action,
expires_in,
force_signed_url=force_signed_url,
r_pays_project=r_pays_project,
file_name=file_name,
users_from_passports=users_from_passports,
bucket=bucket,
)
except Exception as e:
logger.error("signed url error: ")
logger.error(str(e))

# a single user from the list was authorized so update the audit log to reflect that
# users info
Expand Down Expand Up @@ -574,7 +578,9 @@ def get_signed_url(
)
# don't check the authorization if the file is public
# (downloading public files with no auth is fine)
if not self.public_acl and not self.check_legacy_authorization(action):
not_a_public_acl = not self.public_acl
legacy_auth_failed = not self.check_legacy_authorization(action)
if not_a_public_acl and legacy_auth_failed:
raise Unauthorized(
f"You don't have access permission on this file: {self.file_id}"
)
Expand Down
7 changes: 5 additions & 2 deletions fence/jwt/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
return default private key for the app
"""

import datetime
import logging
import os

import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
import dateutil.parser
Expand Down Expand Up @@ -78,6 +77,10 @@ def timestamp_key(name):
keypairs = [
Keypair.from_directory(d) for d in keypair_directories if os.path.isdir(d)
]
print("AAAAA")
print(keypairs)
logging.info("AAAA")
logging.info(keypairs)

if not keypairs:
raise EnvironmentError(
Expand Down
2 changes: 1 addition & 1 deletion fence/jwt/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def generate_signed_session_token(kid, private_key, expires_in, context=None):
claims = {
"pur": "session",
"aud": ["fence", issuer],
"sub": context.get("user_id", ""),
"sub": str(context.get("user_id", "")),
"iss": issuer,
"iat": iat,
"exp": exp,
Expand Down
4 changes: 4 additions & 0 deletions fence/jwt/validate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import authutils.errors
import authutils.token.keys
import authutils.token.validate
Expand Down Expand Up @@ -109,6 +111,8 @@ def validate_jwt(
).get("iss")
except jwt.InvalidTokenError as e:
raise JWTError(e)
logging.error("token info: ")
logging.error(jwt.get_unverified_header(encoded_token))
attempt_refresh = attempt_refresh and (token_iss != iss)
public_key = public_key or authutils.token.keys.get_public_key_for_token(
encoded_token, attempt_refresh=attempt_refresh, pkey_cache=pkey_cache
Expand Down
3 changes: 2 additions & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

target_metadata = Base.metadata

test_config_path = os.environ.get("TEST_CONFIG_PATH")
fence_config.load(
config_path=os.environ.get("TEST_CONFIG_PATH"), # for tests
config_path=test_config_path, # for tests
search_folders=CONFIG_SEARCH_FOLDERS, # for deployments
)
config.set_main_option("sqlalchemy.url", str(fence_config["DB"]))
Expand Down
701 changes: 324 additions & 377 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ boto = "*"
# for testing with updated libaries as git repos:
# foobar = {git = "https://github.com/uc-cdis/some-repo", rev = "feat/test"}

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
addict = "^2.2.1"
cdisutilstest = {git = "https://github.com/uc-cdis/cdisutils-test", tag = "2.0.0"}
codacy-coverage = "^1.3.11"
Expand Down
3 changes: 2 additions & 1 deletion tests/ci_commands_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

mkdir -p /var/tmp/uwsgi_flask_metrics/ || true
export PROMETHEUS_MULTIPROC_DIR="/var/tmp/uwsgi_flask_metrics/"
poetry run pytest -vv --cov=fence --cov-report xml tests
echo "running tests"
poetry run pytest -vv --cov=fence --cov-report xml .
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import copy
import time
from pathlib import Path
import flask
from datetime import datetime
import mock
Expand Down Expand Up @@ -437,7 +438,13 @@ def app(kid, rsa_private_key, rsa_public_key):
mocker = Mocker()
mocker.mock_functions()

root_dir = os.path.dirname(os.path.realpath(__file__))
# root_dir = os.path.dirname(os.path.realpath(__file__))
current_path = Path(__file__).resolve()
root_dir = current_path.parent.parent
os.chdir(root_dir)
x = str(root_dir)
test_dir = os.path.join(x, "tests")
test_config = os.path.join(test_dir, "test-fence-config.yaml")

# delete the record operation from the data blueprint, because right now it calls a
# whole bunch of stuff on the arborist client to do some setup for the uploader role
Expand All @@ -449,12 +456,12 @@ def app(kid, rsa_private_key, rsa_public_key):
app_init(
fence.app,
test_settings,
root_dir=root_dir,
config_path=os.path.join(root_dir, "test-fence-config.yaml"),
root_dir=test_dir,
config_path=test_config,
)

# migrate the database to the latest version
os.environ["TEST_CONFIG_PATH"] = os.path.join(root_dir, "test-fence-config.yaml")
os.environ["TEST_CONFIG_PATH"] = os.path.join(test_dir, "test-fence-config.yaml")
alembic_main(["--raiseerr", "upgrade", "head"])

# We want to set up the keys so that the test application can load keys
Expand Down
2 changes: 1 addition & 1 deletion tests/link/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_google_link_session(app, client, encoded_creds_jwt):
)

assert flask.session.get("google_link") is True
assert flask.session.get("user_id") == user_id
assert flask.session.get("user_id") == str(user_id)
assert flask.session.get("google_proxy_group_id") == proxy_group_id
assert flask.session.get("redirect") == redirect

Expand Down
3 changes: 2 additions & 1 deletion tests/oidc/core/user_info/test_userinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json

import pytest
from gen3authz.client.arborist.errors import ArboristError

from fence.models import UserGoogleAccount

Expand Down Expand Up @@ -77,6 +76,8 @@ def test_userinfo_arborist_authz(
"/user",
headers={"Authorization": "Bearer " + encoded_creds_jwt["jwt"]},
).json
print(f"client: {client}")
print("testtesttesttest")

actual_authz = resp.get("authz", {})
actual_resources = resp.get("resources", [])
Expand Down
16 changes: 13 additions & 3 deletions tests/test_drs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import flask
import httpx
import hashlib
Expand Down Expand Up @@ -67,15 +69,17 @@ def test_get_presigned_url_with_access_id(
primary_google_service_account,
cloud_manager,
google_signed_url,
app,
):
access_id = indexd_client["indexed_file_location"]
test_guid = "1"
context_claims = utils.authorized_download_context_claims(
user_client.username, user_client.user_id
)
user = {
"Authorization": "Bearer "
+ jwt.encode(
utils.authorized_download_context_claims(
user_client.username, user_client.user_id
),
context_claims,
key=rsa_private_key,
headers={"kid": kid},
algorithm="RS256",
Expand All @@ -86,6 +90,12 @@ def test_get_presigned_url_with_access_id(
"/ga4gh/drs/v1/objects/" + test_guid + "/access/" + access_id,
headers=user,
)
if res.status_code != 200:
logging.warning("Failed to get presigned url with access id")
log_info = res.status_code | {"kid": kid, "cc": context_claims}
logging.error(log_info)
logging.error("keys: ")
logging.error(str(list(list(app.jwt_public_keys.items())[0][1].items())))
assert res.status_code == 200


Expand Down
10 changes: 5 additions & 5 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def unauthorized_context_claims(user_name, user_id):
iat, exp = iat_and_exp()
return {
"aud": [iss],
"sub": user_id,
"sub": str(user_id),
"pur": "access",
"iss": iss,
"iat": iat,
Expand Down Expand Up @@ -320,7 +320,7 @@ def authorized_download_context_claims(user_name, user_id):
iat, exp = iat_and_exp()
return {
"aud": [iss],
"sub": user_id,
"sub": str(user_id),
"iss": iss,
"iat": iat,
"exp": exp,
Expand Down Expand Up @@ -353,7 +353,7 @@ def authorized_service_account_management_claims(user_name, user_id, client_id):
iat, exp = iat_and_exp()
return {
"aud": [iss],
"sub": user_id,
"sub": str(user_id),
"iss": iss,
"iat": iat,
"exp": exp,
Expand Down Expand Up @@ -403,7 +403,7 @@ def authorized_download_credentials_context_claims(
iat, exp = iat_and_exp()
return {
"aud": [iss],
"sub": user_id,
"sub": str(user_id),
"iss": iss,
"iat": iat,
"exp": exp,
Expand Down Expand Up @@ -436,7 +436,7 @@ def authorized_upload_context_claims(user_name, user_id):
iat, exp = iat_and_exp()
return {
"aud": [iss],
"sub": user_id,
"sub": str(user_id),
"iss": iss,
"pur": "access",
"iat": iat,
Expand Down
Loading