2012-04-04 14:43:32 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# **sec_groups.sh**
|
|
|
|
|
2013-02-15 11:07:14 -06:00
|
|
|
# Test security groups via the command line
|
2012-04-04 14:43:32 -04:00
|
|
|
|
|
|
|
echo "*********************************************************************"
|
|
|
|
echo "Begin DevStack Exercise: $0"
|
|
|
|
echo "*********************************************************************"
|
|
|
|
|
|
|
|
# This script exits on an error so that errors don't compound and you see
|
2013-06-30 04:32:27 -07:00
|
|
|
# only the first error that occurred.
|
2012-04-04 14:43:32 -04:00
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
# Print the commands being run so that we can see the command that triggers
|
|
|
|
# an error. It is also useful for following allowing as the install occurs.
|
|
|
|
set -o xtrace
|
|
|
|
|
|
|
|
|
|
|
|
# Settings
|
|
|
|
# ========
|
|
|
|
|
|
|
|
# Keep track of the current directory
|
|
|
|
EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
|
|
|
|
TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
|
|
|
|
|
|
|
|
# Import common functions
|
|
|
|
source $TOP_DIR/functions
|
|
|
|
|
|
|
|
# Import configuration
|
|
|
|
source $TOP_DIR/openrc
|
|
|
|
|
|
|
|
# Import exercise configuration
|
|
|
|
source $TOP_DIR/exerciserc
|
|
|
|
|
2014-01-12 19:35:43 +00:00
|
|
|
# If nova api is not enabled we exit with exitcode 55 so that
|
|
|
|
# the exercise is skipped
|
|
|
|
is_service_enabled n-api || exit 55
|
|
|
|
|
2012-04-04 14:43:32 -04:00
|
|
|
|
|
|
|
# Testing Security Groups
|
2012-09-08 14:20:43 -05:00
|
|
|
# =======================
|
2012-04-04 14:43:32 -04:00
|
|
|
|
|
|
|
# List security groups
|
|
|
|
nova secgroup-list
|
|
|
|
|
|
|
|
# Create random name for new sec group and create secgroup of said name
|
2013-02-15 11:07:14 -06:00
|
|
|
SEC_GROUP_NAME="ex-secgroup-$(openssl rand -hex 4)"
|
2012-04-04 14:43:32 -04:00
|
|
|
nova secgroup-create $SEC_GROUP_NAME 'a test security group'
|
|
|
|
|
|
|
|
# Add some rules to the secgroup
|
|
|
|
RULES_TO_ADD=( 22 3389 5900 )
|
|
|
|
|
|
|
|
for RULE in "${RULES_TO_ADD[@]}"; do
|
2013-01-17 11:17:16 -06:00
|
|
|
nova secgroup-add-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0
|
2012-04-04 14:43:32 -04:00
|
|
|
done
|
|
|
|
|
|
|
|
# Check to make sure rules were added
|
|
|
|
SEC_GROUP_RULES=( $(nova secgroup-list-rules $SEC_GROUP_NAME | grep -v \- | grep -v 'Source Group' | cut -d '|' -f3 | tr -d ' ') )
|
2013-10-22 17:07:32 -05:00
|
|
|
die_if_not_set $LINENO SEC_GROUP_RULES "Failure retrieving SEC_GROUP_RULES for $SEC_GROUP_NAME"
|
2012-04-04 14:43:32 -04:00
|
|
|
for i in "${RULES_TO_ADD[@]}"; do
|
|
|
|
skip=
|
|
|
|
for j in "${SEC_GROUP_RULES[@]}"; do
|
|
|
|
[[ $i == $j ]] && { skip=1; break; }
|
|
|
|
done
|
|
|
|
[[ -n $skip ]] || exit 1
|
|
|
|
done
|
|
|
|
|
|
|
|
# Delete rules and secgroup
|
|
|
|
for RULE in "${RULES_TO_ADD[@]}"; do
|
2013-01-17 11:17:16 -06:00
|
|
|
nova secgroup-delete-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0
|
2012-04-04 14:43:32 -04:00
|
|
|
done
|
|
|
|
|
2013-02-15 11:07:14 -06:00
|
|
|
# Delete secgroup
|
|
|
|
nova secgroup-delete $SEC_GROUP_NAME || \
|
2013-02-26 12:38:18 -08:00
|
|
|
die $LINENO "Failure deleting security group $SEC_GROUP_NAME"
|
2012-04-04 14:43:32 -04:00
|
|
|
|
|
|
|
set +o xtrace
|
|
|
|
echo "*********************************************************************"
|
|
|
|
echo "SUCCESS: End DevStack Exercise: $0"
|
|
|
|
echo "*********************************************************************"
|