From 24538e03d7dce6e5f8eeb9f4815426aeb440585e Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Tue, 13 Jun 2017 09:22:39 -0400 Subject: [PATCH] Add LastModified property to JSON object --- bin/compute_md_vital_stats.sh | 232 ++++++++++++++++++++++++---------- 1 file changed, 165 insertions(+), 67 deletions(-) diff --git a/bin/compute_md_vital_stats.sh b/bin/compute_md_vital_stats.sh index 44afa56..18b103b 100755 --- a/bin/compute_md_vital_stats.sh +++ b/bin/compute_md_vital_stats.sh @@ -99,26 +99,30 @@ display_help () { successFlag boolean success or failure? message string message string metadataLocation string HTTP location + creationInstant string ISO 8601 dateTime + LastModified string ISO 8601 dateTime currentTime string ISO 8601 dateTime - validUntil dateTime ISO 8601 dateTime - creationInstant dateTime ISO 8601 dateTime - validityInterval duration ISO 8601 duration - untilInvalid duration ISO 8601 duration - sinceCreation duration ISO 8601 duration + validUntil string ISO 8601 dateTime + validityInterval string ISO 8601 duration + sinceCreation string ISO 8601 duration + untilExpiration string ISO 8601 duration + deploymentLag string ISO 8601 duration For example: - - { - "successFlag": true, - "message": "Integrity of compressed metadata confirmed", - "metadataLocation": "http://md.incommon.org/InCommon/InCommon-metadata.xml", - "currentTime": "2017-06-06T23:57:54Z", - "validUntil": "2017-06-16T18:41:12Z", - "creationInstant": "2017-06-02T18:41:12Z", - "validityInterval": "P14DT0H0M0S", - "untilInvalid": "P9DT18H43M18S", - "sinceCreation": "P4DT5H16M42S" - } + + { + "successFlag": true, + "message": "Metadata successfully parsed", + "metadataLocation": "http://md.incommon.org/InCommon/InCommon-metadata.xml", + "creationInstant": "2017-06-12T18:47:48Z", + "LastModified": "2017-06-12T20:01:32Z", + "currentTime": "2017-06-13T12:20:23Z", + "validUntil": "2017-06-26T18:47:48Z", + "validityInterval": "P14DT0H0M0S", + "sinceCreation": "P0DT17H32M35S", + "untilExpiration": "P13DT6H27M25S", + "deploymentLag": "P0DT1H13M44S" + } EXAMPLES @@ -194,9 +198,9 @@ done xsl_filename="entities_timestamps_txt.xsl" # check XSLT script -xsl_path="$LIB_DIR/$xsl_filename" -if [ ! -f "$xsl_path" ]; then - echo "ERROR: $script_name: file does not exist: $xsl_path" >&2 +xsl_file="$LIB_DIR/$xsl_filename" +if [ ! -f "$xsl_file" ]; then + echo "ERROR: $script_name: file does not exist: $xsl_file" >&2 exit 2 fi @@ -278,8 +282,9 @@ if [ $status_code -ne 0 ]; then fi # specify temporary files -xml_path="${tmp_dir}/saml-metadata.xml" +xml_file="${tmp_dir}/saml-metadata.xml" out_file="${tmp_dir}/$out_filename" +header_file="${tmp_dir}/resource-header.txt" ####################################################################### # Functions @@ -295,12 +300,14 @@ escape_special_json_chars () { append_json_object () { local message=$( escape_special_json_chars "$message" ) local metadataLocation=$( escape_special_json_chars "$md_location" ) - local currentTime=$( escape_special_json_chars "$now" ) - local validUntil=$( escape_special_json_chars "$validUntil" ) local creationInstant=$( escape_special_json_chars "$creationInstant" ) + local last_modified=$( escape_special_json_chars "$last_modified" ) + local currentTime=$( escape_special_json_chars "$currentTime" ) + local validUntil=$( escape_special_json_chars "$validUntil" ) local validityInterval=$( escape_special_json_chars "$validityInterval" ) - local untilInvalid=$( escape_special_json_chars "$untilInvalid" ) local sinceCreation=$( escape_special_json_chars "$sinceCreation" ) + local untilExpiration=$( escape_special_json_chars "$untilExpiration" ) + local deploymentLag=$( escape_special_json_chars "$deploymentLag" ) local boolean_value="true" ! $success && boolean_value="false" @@ -310,35 +317,71 @@ append_json_object () { "successFlag": $boolean_value, "message": "$message", "metadataLocation": "$metadataLocation", + "creationInstant": "$creationInstant", + "LastModified": "$last_modified", "currentTime": "$currentTime", "validUntil": "$validUntil", - "creationInstant": "$creationInstant", "validityInterval": "$validityInterval", - "untilInvalid": "$untilInvalid", - "sinceCreation": "$sinceCreation" + "sinceCreation": "$sinceCreation", + "untilExpiration": "$untilExpiration", + "deploymentLag": "$deploymentLag" } JSON_OBJECT } -get_metadata () { +init_global_vars () { + + # success by default + success=true + message="Metadata successfully parsed" + + metadataLocation= + last_modified= + currentTime= + validUntil= + creationInstant= + validityInterval= + untilExpiration= + sinceCreation= + deploymentLag= +} + +get_cached_resource () { local status_code md_location="$1" - # get a cached metadata file - conditional_get $local_opts -C -d "$CACHE_DIR" -T "$tmp_dir" "$md_location" > "$xml_path" + # get a cached content file + conditional_get $local_opts -C -d "$CACHE_DIR" -T "$tmp_dir" "$md_location" > "$xml_file" + status_code=$? + if [ $status_code -eq 1 ]; then + # resource must be cached + success=false + message="Resource not found" + print_log_message -E "$script_name: metadata resource not cached: $md_location" + return 1 + fi + if [ $status_code -gt 1 ]; then + success=false + message="Lookup failed" + print_log_message -E "$script_name: conditional_get failed ($status_code) on location: $md_location" + return 3 + fi + + # get a cached header file + conditional_get $local_opts -CI -d "$CACHE_DIR" -T "$tmp_dir" "$md_location" > "$header_file" status_code=$? if [ $status_code -eq 1 ]; then - # metadata must be cached + # resource must be cached success=false - message="Metadata not found" - print_log_message -E "$script_name: metadata file not cached: $md_location" + message="Resource not found" + print_log_message -E "$script_name: metadata resource not cached: $md_location" return 1 fi if [ $status_code -gt 1 ]; then success=false - message="Request for metadata failed" + message="Lookup failed" print_log_message -E "$script_name: conditional_get failed ($status_code) on location: $md_location" return 3 fi @@ -346,23 +389,23 @@ get_metadata () { return 0 } -parse_metadata () { +parse_cached_content () { local status_code local tstamps local validityIntervalSecs - local secsUntilInvalid + local secsUntilExpiration local secsSinceCreation - print_log_message -I "$script_name parsing cached metadata file: $md_location" + print_log_message -I "$script_name parsing cached metadata file for resource: $md_location" # extract @ID, @creationInstant, @validUntil (in that order) - tstamps=$( /usr/bin/xsltproc $xsl_path $xml_path ) + tstamps=$( /usr/bin/xsltproc $xsl_file $xml_file ) status_code=$? if [ $status_code -ne 0 ]; then success=false message="Unable to parse metadata" - print_log_message -E "$script_name: xsltproc failed ($status_code) on script: $xsl_path" + print_log_message -E "$script_name: xsltproc failed ($status_code) on script: $xsl_file" return 0 fi @@ -404,7 +447,7 @@ parse_metadata () { fi print_log_message -D "$script_name found @creationInstant: $creationInstant" - # compute validityInterval + # compute length of the validityInterval (in secs) validityIntervalSecs=$( secsUntil -b $creationInstant $validUntil ) status_code=$? if [ $status_code -ne 0 ]; then @@ -414,18 +457,19 @@ parse_metadata () { return 0 fi - # if validityInterval > 14 days, then FAIL # TODO: Generalize - if (( validityIntervalSecs > 14*24*60*60 )); then + # convert secs to duration + validityInterval=$( secs2duration $validityIntervalSecs ) + status_code=$? + if [ $status_code -ne 0 ]; then success=false - message="Validity interval too large" - print_log_message -E "$script_name: validity interval exceeds maximum: $validityIntervalSecs" + message="Unable to convert validity interval" + print_log_message -E "$script_name: secs2duration failed ($status_code)" return 0 fi - validityInterval=$( secs2duration $validityIntervalSecs ) print_log_message -D "$script_name computed validity interval: $validityInterval" - # compute NOW - now=$( dateTime_now_canonical ) + # compute current dateTime + currentTime=$( dateTime_now_canonical ) status_code=$? if [ $status_code -ne 0 ]; then success=false @@ -433,9 +477,10 @@ parse_metadata () { print_log_message -E "$script_name: dateTime_now_canonical failed ($status_code)" return 0 fi + print_log_message -D "$script_name computed current time: $currentTime" - # compute secsUntilInvalid - secsUntilInvalid=$( echo $validUntil | secsUntil -b $now ) + # compute secsUntilExpiration + secsUntilExpiration=$( secsUntil -b $currentTime $validUntil ) status_code=$? if [ $status_code -ne 0 ]; then success=false @@ -444,18 +489,19 @@ parse_metadata () { return 0 fi - # if secsUntilInvalid <= 0, then FAIL - if (( secsUntilInvalid <= 0 )); then + # convert secs to duration + untilExpiration=$( secs2duration "$secsUntilExpiration" ) + status_code=$? + if [ $status_code -ne 0 ]; then success=false - message="Metadata is expired" - print_log_message -C "$script_name: seconds until invalid not positive: $secsUntilInvalid" + message="Unable to convert secs until expiration" + print_log_message -E "$script_name: secs2duration failed ($status_code)" return 0 fi - untilInvalid=$( secs2duration "$secsUntilInvalid" ) - print_log_message -D "$script_name computed time until invalid: $untilInvalid" + print_log_message -D "$script_name computed time until expiration: $untilExpiration" # compute secsSinceCreation - secsSinceCreation=$( echo $creationInstant | secsSince -e $now ) + secsSinceCreation=$( echo $creationInstant | secsSince -e $currentTime ) status_code=$? if [ $status_code -ne 0 ]; then success=false @@ -464,18 +510,70 @@ parse_metadata () { return 0 fi - # if secsSinceCreation <= 0, then FAIL - if (( secsSinceCreation <= 0 )); then + # convert secs to duration + sinceCreation=$( secs2duration "$secsSinceCreation" ) + status_code=$? + if [ $status_code -ne 0 ]; then success=false - message="Metadata is not valid" - print_log_message -C "$script_name: seconds since creation not positive: $secsSinceCreation" + message="Unable to convert secs since creation" + print_log_message -E "$script_name: secs2duration failed ($status_code)" return 0 fi - sinceCreation=$( secs2duration "$secsSinceCreation" ) print_log_message -D "$script_name computed time since creation: $sinceCreation" - # success - message="Metadata successfully parsed" + return 0 +} + +parse_cached_headers () { + + local header_name + local status_code + local last_modified_apache + local deploymentLagSecs + + print_log_message -I "$script_name parsing cached header file for resource: $md_location" + + # get the Last-Modified response header + header_name=Last-Modified + last_modified_apache=$( get_header_value $header_file $header_name ) + status_code=$? + if [ $status_code -ne 0 ]; then + print_log_message -E "$script_name: get_header_value failed ($status_code) to parse response header: $header_name" + fi + + # convert LastModified date to canonical format + last_modified=$( dateTime_apache2canonical "$last_modified_apache" ) + status_code=$? + if [ $status_code -ne 0 ]; then + success=false + message="Unable to convert LastModified date" + print_log_message -E "$script_name: dateTime_apache2canonical failed ($status_code)" + return 0 + fi + print_log_message -D "$script_name computed LastModified date: $last_modified" + + # compute the length of time between @creationInstant and LastModified (in secs) + deploymentLagSecs=$( secsUntil -b $creationInstant $last_modified ) + status_code=$? + if [ $status_code -ne 0 ]; then + success=false + message="Unable to compute deployment lag" + print_log_message -E "$script_name: secsUntil failed ($status_code)" + return 0 + fi + + # convert secs to duration + deploymentLag=$( secs2duration "$deploymentLagSecs" ) + status_code=$? + if [ $status_code -ne 0 ]; then + success=false + message="Unable to convert deployment lag tim" + print_log_message -E "$script_name: secs2duration failed ($status_code)" + return 0 + fi + print_log_message -D "$script_name computed deployment lag: $deploymentLag" + + return 0 } print_output_file () { @@ -487,12 +585,12 @@ print_output_file () { while true; do - success=true - - get_metadata "$1" + init_global_vars + get_cached_resource "$1" status_code=$? if [ $status_code -eq 0 ]; then - parse_metadata + parse_cached_content + parse_cached_headers fi append_json_object