From fff985e9a685c6d5423324fa916c65f7a998f249 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 5 Jul 2024 13:02:19 +1000 Subject: [PATCH 1/2] added jsession via curl option to upload script --- xnat_ingest/cli/upload.py | 33 +++++++++++++++++++++++++++++++++ xnat_ingest/tests/test_cli.py | 1 + 2 files changed, 34 insertions(+) diff --git a/xnat_ingest/cli/upload.py b/xnat_ingest/cli/upload.py index 4f7afa2..f0ac20a 100644 --- a/xnat_ingest/cli/upload.py +++ b/xnat_ingest/cli/upload.py @@ -7,9 +7,11 @@ from collections import defaultdict import tempfile from operator import itemgetter +import subprocess as sp import click from tqdm import tqdm from natsort import natsorted +import xnat import boto3 import paramiko from fileformats.generic import File @@ -168,6 +170,17 @@ envvar="XNAT_INGEST_UPLOAD_VERIFY_SSL", help="Whether to verify the SSL certificate of the XNAT server", ) +@click.option( + "--use-curl-jsession/--dont-use-curl-jsession", + type=bool, + default=False, + envvar="XNAT_INGEST_UPLOAD_USE_CURL_JSESSION", + help=( + "Whether to use CURL to create a JSESSION token to authenticate with XNAT. This is " + "used to work around a strange authentication issue when running within a Kubernetes " + "cluster and targeting the XNAT Tomcat directly" + ), +) def upload( staged: str, server: str, @@ -185,6 +198,7 @@ def upload( use_manifest: bool, clean_up_older_than: int, verify_ssl: bool, + use_curl_jsession: bool, ): set_logger_handling( @@ -205,6 +219,22 @@ def upload( verify_ssl=verify_ssl, ) + if use_curl_jsession: + jsession = sp.check_output( + [ + "curl", + "-X", + "PUT", + "-d", + f"username={user}&password={password}", + f"{server}/data/services/auth", + ] + ).decode("utf-8") + xnat_repo.connection.depth = 1 + xnat_repo.connection.session = xnat.connect( + server, user=user, jsession=jsession + ) + with xnat_repo.connection: def xnat_session_exists(project_id, subject_id, visit_id): @@ -499,6 +529,9 @@ def iter_staged_sessions(): else: raise + if use_curl_jsession: + xnat_repo.exit() + if clean_up_older_than: logger.info( "Cleaning up files in %s older than %d days", diff --git a/xnat_ingest/tests/test_cli.py b/xnat_ingest/tests/test_cli.py index b593ff4..05b2d69 100644 --- a/xnat_ingest/tests/test_cli.py +++ b/xnat_ingest/tests/test_cli.py @@ -224,6 +224,7 @@ def test_stage_and_upload( "--always-include", "medimage/dicom-series", "--raise-errors", + "--use-curl-jsession", ], env={ "XNAT_INGEST_UPLOAD_HOST": xnat_server, From 7d49259776d8d4a3cc345bdc644414d9ff6e163f Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 5 Jul 2024 13:07:11 +1000 Subject: [PATCH 2/2] fixed up connection close for use-curl-jsession --- xnat_ingest/cli/upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xnat_ingest/cli/upload.py b/xnat_ingest/cli/upload.py index f0ac20a..b689191 100644 --- a/xnat_ingest/cli/upload.py +++ b/xnat_ingest/cli/upload.py @@ -530,7 +530,7 @@ def iter_staged_sessions(): raise if use_curl_jsession: - xnat_repo.exit() + xnat_repo.connection.exit() if clean_up_older_than: logger.info(