generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to extract functions and reuse utils + clean up
- Loading branch information
1 parent
75b6555
commit bf580ce
Showing
7 changed files
with
298 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
#!/bin/bash | ||
|
||
source ./utils.sh | ||
|
||
client= | ||
audience=https://epicproxy-np.et0502.epichosted.com/FhirProxy/oauth2/token | ||
secret=/path/to/ucsd-epic-private-key.pem | ||
root=$CDCTI_HOME/examples/CA/ | ||
fpath="$1" | ||
shift | ||
|
||
jwt_token=$(generate_jwt "$client" "$audience" "$secret") || fail "Failed to generate JWT token" | ||
|
||
hurl \ | ||
--variable fpath=$fpath \ | ||
--file-root $root \ | ||
--variable jwt=$(jwt encode --exp='+5min' --jti $(uuidgen) --alg RS256 -k $client -i $client -s $client -a $audience --no-iat -S @$secret) \ | ||
--variable "fpath=$fpath" \ | ||
--file-root "$root" \ | ||
--variable "jwt=$jwt_token" \ | ||
epic/results.hurl \ | ||
$@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,102 @@ | ||
#!/bin/bash | ||
|
||
# Check if $CDCTI_HOME is set | ||
if [ -z "$CDCTI_HOME" ]; then | ||
echo "Error: CDCTI_HOME is not set. Please set this environment variable before running the script." | ||
exit 1 | ||
fi | ||
source ./utils.sh | ||
|
||
LOCAL_KEY_PATH="$CDCTI_HOME/mock_credentials/organization-trusted-intermediary-private-key-local.pem" | ||
|
||
# default values | ||
env=local | ||
root=$CDCTI_HOME/examples/ | ||
content_type=application/hl7-v2 | ||
client_id=flexion | ||
client_sender=simulated-sender | ||
verbose="" | ||
submission_id="" | ||
|
||
show_help() { | ||
echo "Usage: $(basename $0) <ENDPOINT_NAME> [OPTIONS]" | ||
echo | ||
echo "Options:" | ||
echo " -f <REL_PATH> The path to the hl7/fhir file to submit, relative the root path (Required for waters API)" | ||
echo " -r <ROOT_PATH> The root path to the hl7/fhir files (Default: $root)" | ||
echo " -t <CONTENT_TYPE> The content type for the message (e.g. 'application/hl7-v2' or 'application/fhir+ndjson') (Default: $content_type)" | ||
echo " -e [local | staging | production ] The environment to run the test in (Default: $env)" | ||
echo " -c <CLIENT_ID> The client id to use (Default: $client_id)" | ||
echo " -s <CLIENT_SENDER> The client sender to use (Default: $client_sender)" | ||
echo " -x <KEY_PATH> The path to the client private key for the environment" | ||
echo " -i <SUBMISSION_ID> The submissionId to call the history API with (Required for history API)" | ||
echo " -v Verbose mode" | ||
echo " -h Display this help and exit" | ||
|
||
show_usage() { | ||
cat <<EOF | ||
Usage: $(basename "$0") <ENDPOINT_NAME> [OPTIONS] | ||
ENDPOINT_NAME: | ||
The name of the endpoint to call (required) | ||
Options: | ||
-f <REL_PATH> Path to the hl7/fhir file to submit (Required for waters API) | ||
-r <ROOT_PATH> Root path to the hl7/fhir files (Default: $root) | ||
-t <CONTENT_TYPE> Content type for the message (Default: $content_type) | ||
-e <ENVIRONMENT> Environment: local|staging|production (Default: $env) | ||
-c <CLIENT_ID> Client ID (Default: $client_id) | ||
-s <CLIENT_SENDER> Client sender (Default: $client_sender) | ||
-x <KEY_PATH> Path to the client private key | ||
-i <SUBMISSION_ID> Submission ID for history API | ||
-v Verbose mode | ||
-h Display this help and exit | ||
Environment Variables: | ||
CDCTI_HOME Base directory for CDC TI repository (Required) | ||
EOF | ||
} | ||
|
||
# Check if required ENDPOINT_NAME is provided | ||
if [ $# -eq 0 ]; then | ||
echo "Error: Missing required argument <ENDPOINT_NAME>" | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
# Check if first argument is -h | ||
if [ "$1" = "-h" ]; then | ||
show_help | ||
exit 0 | ||
fi | ||
|
||
endpoint_name=rs/"$1".hurl # Assign the first argument to endpoint_name | ||
shift # Remove the first argument from the list of arguments | ||
|
||
while getopts ':f:r:t:e:c:s:x:i:vh' opt; do | ||
case "$opt" in | ||
f) | ||
fpath="$OPTARG" | ||
;; | ||
r) | ||
root="$OPTARG" | ||
;; | ||
t) | ||
content_type="$OPTARG" | ||
;; | ||
e) | ||
env="$OPTARG" | ||
;; | ||
c) | ||
client_id="$OPTARG" | ||
;; | ||
s) | ||
client_sender="$OPTARG" | ||
;; | ||
x) | ||
secret="$OPTARG" | ||
;; | ||
i) | ||
submission_id="--variable submissionid=$OPTARG" | ||
;; | ||
v) | ||
verbose="--verbose" | ||
;; | ||
h) | ||
show_help | ||
parse_arguments() { | ||
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then | ||
show_usage | ||
exit 0 | ||
;; | ||
:) | ||
echo -e "Option requires an argument" | ||
show_help | ||
exit 1 | ||
;; | ||
?) | ||
echo -e "Invalid command option" | ||
show_help | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
shift "$(($OPTIND - 1))" | ||
|
||
if [ "$env" = "local" ]; then | ||
host=localhost | ||
url=http://$host:7071 | ||
if [ -z "$secret" ] && [ "$client_id" = "flexion" ]; then | ||
secret="$CDCTI_HOME/mock_credentials/organization-trusted-intermediary-private-key-local.pem" | ||
fi | ||
elif [ "$env" = "staging" ]; then | ||
host=staging.prime.cdc.gov | ||
url=https://$host:443 | ||
elif [ "$env" = "production" ]; then | ||
host=prime.cdc.gov | ||
url=https://$host:443 | ||
else | ||
echo "Error: Invalid environment $env" | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
if [ -z "$secret" ]; then | ||
echo "Error: Please provide the private key for $client_id" | ||
exit 1 | ||
fi | ||
|
||
hurl \ | ||
--variable fpath=$fpath \ | ||
--file-root $root \ | ||
--variable url=$url \ | ||
--variable content-type=$content_type \ | ||
--variable client-id=$client_id \ | ||
--variable client-sender=$client_sender \ | ||
--variable jwt=$(jwt encode --exp='+5min' --jti $(uuidgen) --alg RS256 -k $client_id.$client_sender -i $client_id.$client_sender -s $client_id.$client_sender -a $host --no-iat -S @$secret) \ | ||
$submission_id \ | ||
$verbose \ | ||
$endpoint_name \ | ||
$@ | ||
|
||
[ $# -eq 0 ] && fail "Missing required argument <ENDPOINT_NAME>" | ||
endpoint_name="rs/$1.hurl" | ||
shift # Remove endpoint name from args | ||
|
||
while getopts ':f:r:t:e:c:s:x:i:v' opt; do | ||
case "$opt" in | ||
f) fpath="$OPTARG" ;; | ||
r) root="$OPTARG" ;; | ||
t) content_type="$OPTARG" ;; | ||
e) env="$OPTARG" ;; | ||
c) client_id="$OPTARG" ;; | ||
s) client_sender="$OPTARG" ;; | ||
x) secret="$OPTARG" ;; | ||
i) submission_id="--variable submissionid=$OPTARG" ;; | ||
v) verbose="--verbose" ;; | ||
?) fail "Invalid option -$OPTARG" ;; | ||
esac | ||
done | ||
|
||
shift "$(($OPTIND - 1))" | ||
remaining_args="$*" | ||
} | ||
|
||
setup_credentials() { | ||
if [ -z "$secret" ] && [ "$client_id" = "flexion" ] && [ "$env" = "local" ]; then | ||
if [ -f "$LOCAL_KEY_PATH" ]; then | ||
secret="$LOCAL_KEY_PATH" | ||
else | ||
fail "Local environment key not found at: $LOCAL_KEY_PATH" | ||
fi | ||
fi | ||
|
||
[ -n "$secret" ] || fail "Please provide the private key for $client_id" | ||
[ -f "$secret" ] || fail "Private key file not found: $secret" | ||
} | ||
|
||
run_hurl_command() { | ||
url=$(get_api_url "$env" "rs") | ||
host=$(extract_host_from_url "$url") | ||
jwt_token=$(generate_jwt "$client_id.$client_sender" "$host" "$secret") || fail "Failed to generate JWT token" | ||
|
||
hurl \ | ||
--variable "fpath=$fpath" \ | ||
--file-root "$root" \ | ||
--variable "url=$url" \ | ||
--variable "content-type=$content_type" \ | ||
--variable "client-id=$client_id" \ | ||
--variable "client-sender=$client_sender" \ | ||
--variable "jwt=$jwt_token" \ | ||
${submission_id:-} \ | ||
${verbose:-} \ | ||
"$endpoint_name" \ | ||
${remaining_args:+$remaining_args} | ||
} | ||
|
||
check_env_vars CDCTI_HOME | ||
parse_arguments "$@" | ||
setup_credentials | ||
run_hurl_command |
Oops, something went wrong.