Skip to content

Commit

Permalink
Refactored to extract functions and reuse utils + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Oct 23, 2024
1 parent 75b6555 commit bf580ce
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 321 deletions.
68 changes: 42 additions & 26 deletions scripts/hurl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
#### Usage

```
Usage: ./rs.sh <ENDPOINT_NAME> [OPTIONS]
Usage: rs.sh <ENDPOINT_NAME> [OPTIONS]
ENDPOINT_NAME:
The name of the endpoint to call (required)
Options:
-f <REL_PATH> The path to the hl7/fhir file to submit, relative the root path (Required for waters API)
-r <ROOT_PATH> The root path to the hl7/fhir files (Default: $CDCTI_HOME/examples/)
-t <CONTENT_TYPE> The content type for the message (e.g. 'application/hl7-v2' or 'application/fhir+ndjson') (Default: application/hl7-v2)
-e [local | staging | production ] The environment to run the test in (Default: local)
-c <CLIENT_ID> The client id to use (Default: flexion)
-s <CLIENT_SENDER> The client sender to use (Default: simulated-sender)
-x <KEY_PATH> The path to the client private key for the environment
-i <SUBMISSION_ID> The submissionId to call the history API with (Required for history API)
-v Verbose mode
-h Display this help and exit
-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: /Users/bbogado/Code/Flexion/CDC-TI/trusted-intermediary/examples/)
-t <CONTENT_TYPE> Content type for the message (Default: application/hl7-v2)
-e <ENVIRONMENT> Environment: local|staging|production (Default: local)
-c <CLIENT_ID> Client ID (Default: flexion)
-s <CLIENT_SENDER> Client sender (Default: simulated-sender)
-x <KEY_PATH> Path to the client private key
-i <SUBMISSION_ID> SubmissionId for history API
-v Verbose mode
-h Display this help and exit
Environment Variables:
CDCTI_HOME Base directory for CDC TI repository (Required)
```

#### Examples
Expand Down Expand Up @@ -67,45 +73,50 @@ Checking the history in staging for a submission id
#### Usage

```
Usage: ti.rs <ENDPOINT_NAME> [OPTIONS]
Usage: ti.sh <ENDPOINT_NAME> [OPTIONS]
ENDPOINT_NAME:
The name of the endpoint to call (required)
Options:
-f <REL_PATH> The path to the hl7/fhir file to submit, relative the root path (Required for orders and results APIs)
-r <ROOT_PATH> The root path to the hl7/fhir files (Default: $CDCTI_HOME/examples/)
-e [local | staging] The environment to run the test in (Default: local)
-c <CLIENT> The client id to use (Default: report-stream)
-j <JWT> The JWT to use for authentication
-i <SUBMISSION_ID> The submissionId to call the metadata API with (Required for metadata API)
-f <REL_PATH> Path to the hl7/fhir file to submit (Required for orders and results APIs)
-i <SUBMISSION_ID> Submission ID for metadata API (Required for orders, results and metadata API)
-r <ROOT_PATH> Root path to the hl7/fhir files (Default: /Users/bbogado/Code/Flexion/CDC-TI/trusted-intermediary/examples/)
-e <ENVIRONMENT> Environment: local|staging (Default: local)
-j <JWT> JWT token for authentication
-v Verbose mode
-h Display this help and exit
Environment Variables:
CDCTI_HOME Base directory for CDC TI repository (Required)
```

#### Examples

Submit an order to local environment:
```
./ti.rs orders -f Test/Orders/003_AL_ORM_O01_NBS_Fully_Populated_1_hl7_translation.fhir -i 100
./ti.sh orders -f Test/Orders/003_AL_ORM_O01_NBS_Fully_Populated_1_hl7_translation.fhir -i 100
```

Submit an order to staging:
```
./ti.rs orders -f Test/Orders/003_AL_ORM_O01_NBS_Fully_Populated_0_initial_message.hl7 -e staging -j eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
./ti.sh orders -f Test/Orders/003_AL_ORM_O01_NBS_Fully_Populated_0_initial_message.hl7 -e staging -j eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
```

Submit a result to local environment:
```
./ti.rs results -f Test/Results/002_AL_ORU_R01_NBS_Fully_Populated_1_hl7_translation.fhir -i 100
./ti.sh results -f Test/Results/002_AL_ORU_R01_NBS_Fully_Populated_1_hl7_translation.fhir -i 100
```

Get metadata from local environment:
```
./ti.rs metadata -i 100
./ti.sh metadata -i 100
```

Authenticate to local environment:
```
./ti.rs auth
./ti.sh auth
```

Get OpenAPI docs from local environment:
Expand All @@ -115,7 +126,7 @@ Get OpenAPI docs from local environment:

Get Health info from local environment:
```
./ti.rs health
./ti.sh health
```

### Epic/UCSD
Expand All @@ -127,13 +138,18 @@ Get Health info from local environment:

#### Usage

`./epic.rs results`
`./epic.sh results`

## Local Submission Scripts

- `submit_message.sh`: sends a HL7 message to a locally running RS instance. It also grabs the snapshots of the file in azurite after converting to FHIR, after applying transformations in TI, and after converting back to HL7. It copies these files to the same folder where the submitted file is
```
./submit_message.sh /path/to/message.hl7
Usage: submit_message.sh -f <message_file.hl7> [-e <environment>]
Options:
-f <FILE> Message file path (required)
-e <ENVIRONMENT> Environment: local|staging|production (Default: )
-h Display this help and exit
```
- `update_examples.sh`: sends all the HL7 files with `_0_initial_message.hl7` suffix in the `/examples` folder to a locally running RS instance. As the previous script, it copies the snapshots at each stage
```
Expand Down
12 changes: 9 additions & 3 deletions scripts/hurl/epic.sh
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 \
$@
207 changes: 89 additions & 118 deletions scripts/hurl/rs.sh
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
Loading

0 comments on commit bf580ce

Please sign in to comment.