Skip to content

Commit

Permalink
Merge pull request #13 from Australian-Imaging-Service/develop
Browse files Browse the repository at this point in the history
added jsession via curl option to upload script
  • Loading branch information
tclose authored Jul 5, 2024
2 parents c433f01 + 7d49259 commit a53df7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions xnat_ingest/cli/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -185,6 +198,7 @@ def upload(
use_manifest: bool,
clean_up_older_than: int,
verify_ssl: bool,
use_curl_jsession: bool,
):

set_logger_handling(
Expand All @@ -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):
Expand Down Expand Up @@ -499,6 +529,9 @@ def iter_staged_sessions():
else:
raise

if use_curl_jsession:
xnat_repo.connection.exit()

if clean_up_older_than:
logger.info(
"Cleaning up files in %s older than %d days",
Expand Down
1 change: 1 addition & 0 deletions xnat_ingest/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a53df7b

Please sign in to comment.