Skip to content

Instantly share code, notes, and snippets.

@trscavo
Created July 7, 2015 16:54
Show Gist options
  • Save trscavo/00f1f2506ba4e44c6733 to your computer and use it in GitHub Desktop.
Save trscavo/00f1f2506ba4e44c6733 to your computer and use it in GitHub Desktop.
Produce a list of all entityIDs in the given SAML metadata file
#!/bin/bash
###########################################################
# Produce a list of all entityIDs in the given SAML metadata file.
#
# Usage: eids.sh [FILE]
#
# Optionally takes the path to the metadata file as a command-line
# parameter. If none is given, takes its input from stdin.
#
###########################################################
script_name=${0##*/} # equivalent to basename $0
verbose_mode=false # a potential command-line option
#####################################################################
# Initialize directories and files
#####################################################################
# create a temporary directory
tmp_dir=$( mktemp -d 2>/dev/null || mktemp -d -t "${script_name%%.*}" )
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: Unable to create temporary dir\n" >&2
exit 2
fi
$verbose_mode && printf "$script_name using temp dir: %s\n" "$tmp_dir"
# read the input into a temporary file
md_file=${tmp_dir}/tmp_metadata.xml
if [ $# -eq 1 ]; then
if [ ! -f "$1" ] ; then
printf "ERROR: The metadata file does not exist: %s\n" "$1" >&2
exit 2
fi
file_name="$1"
# copy input file into the temp file
/bin/cat "$1" > "$md_file"
else
file_name='(stdin)'
# read input from stdin into the temp file
/bin/cat - > "$md_file"
fi
$verbose_mode && printf "$script_name processing input file: %s\n" "$file_name"
# Does the file contain an aggregate of SAML metadata?
entities_descriptors=$( cat "$md_file" | grep -E '<(md:)?EntitiesDescriptor ' )
if [ -z "$entities_descriptors" ]; then
printf "ERROR: The file is NOT a SAML metadata aggregate: %s\n" "$md_file" >&2
exit 2
fi
num_descriptors=$( echo "$entities_descriptors" | wc -l )
if [ "$num_descriptors" -gt 1 ]; then
printf "ERROR: Multiple EntitiesDescriptor elements found: %d\n" "$num_descriptors" >&2
exit 2
fi
#####################################################################
# Main processing
#####################################################################
# produce a list of all entityIDs in the metadata file
/bin/cat $md_file \
| grep -F ' entityID=' \
| sed -e 's/^.* entityID="\([^"]*\)".*$/\1/'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment