| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
|  | 2 |  | 
|  | 3 | # **sec_groups.sh** | 
|  | 4 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 5 | # Test security groups via the command line | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 6 |  | 
|  | 7 | echo "*********************************************************************" | 
|  | 8 | echo "Begin DevStack Exercise: $0" | 
|  | 9 | echo "*********************************************************************" | 
|  | 10 |  | 
|  | 11 | # This script exits on an error so that errors don't compound and you see | 
| Joe Gordon | 4640026 | 2013-06-30 04:32:27 -0700 | [diff] [blame] | 12 | # only the first error that occurred. | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 13 | set -o errexit | 
|  | 14 |  | 
|  | 15 | # Print the commands being run so that we can see the command that triggers | 
|  | 16 | # an error.  It is also useful for following allowing as the install occurs. | 
|  | 17 | set -o xtrace | 
|  | 18 |  | 
|  | 19 |  | 
|  | 20 | # Settings | 
|  | 21 | # ======== | 
|  | 22 |  | 
|  | 23 | # Keep track of the current directory | 
|  | 24 | EXERCISE_DIR=$(cd $(dirname "$0") && pwd) | 
|  | 25 | TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) | 
|  | 26 |  | 
|  | 27 | # Import common functions | 
|  | 28 | source $TOP_DIR/functions | 
|  | 29 |  | 
|  | 30 | # Import configuration | 
|  | 31 | source $TOP_DIR/openrc | 
|  | 32 |  | 
|  | 33 | # Import exercise configuration | 
|  | 34 | source $TOP_DIR/exerciserc | 
|  | 35 |  | 
| Kiall Mac Innes | a16c821 | 2014-01-12 19:35:43 +0000 | [diff] [blame] | 36 | # If nova api is not enabled we exit with exitcode 55 so that | 
|  | 37 | # the exercise is skipped | 
|  | 38 | is_service_enabled n-api || exit 55 | 
|  | 39 |  | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 40 |  | 
|  | 41 | # Testing Security Groups | 
| Dean Troyer | c5dfecd | 2012-09-08 14:20:43 -0500 | [diff] [blame] | 42 | # ======================= | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 43 |  | 
|  | 44 | # List security groups | 
|  | 45 | nova secgroup-list | 
|  | 46 |  | 
|  | 47 | # Create random name for new sec group and create secgroup of said name | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 48 | SEC_GROUP_NAME="ex-secgroup-$(openssl rand -hex 4)" | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 49 | nova secgroup-create $SEC_GROUP_NAME 'a test security group' | 
|  | 50 |  | 
|  | 51 | # Add some rules to the secgroup | 
|  | 52 | RULES_TO_ADD=( 22 3389 5900 ) | 
|  | 53 |  | 
|  | 54 | for RULE in "${RULES_TO_ADD[@]}"; do | 
| Dean Troyer | 029598e | 2013-01-17 11:17:16 -0600 | [diff] [blame] | 55 | nova secgroup-add-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0 | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 56 | done | 
|  | 57 |  | 
|  | 58 | # Check to make sure rules were added | 
|  | 59 | SEC_GROUP_RULES=( $(nova secgroup-list-rules $SEC_GROUP_NAME | grep -v \- | grep -v 'Source Group' | cut -d '|' -f3 | tr -d ' ') ) | 
| DennyZhang | 23178a9 | 2013-10-22 17:07:32 -0500 | [diff] [blame] | 60 | die_if_not_set $LINENO SEC_GROUP_RULES "Failure retrieving SEC_GROUP_RULES for $SEC_GROUP_NAME" | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 61 | for i in "${RULES_TO_ADD[@]}"; do | 
|  | 62 | skip= | 
|  | 63 | for j in "${SEC_GROUP_RULES[@]}"; do | 
|  | 64 | [[ $i == $j ]] && { skip=1; break; } | 
|  | 65 | done | 
|  | 66 | [[ -n $skip ]] || exit 1 | 
|  | 67 | done | 
|  | 68 |  | 
|  | 69 | # Delete rules and secgroup | 
|  | 70 | for RULE in "${RULES_TO_ADD[@]}"; do | 
| Dean Troyer | 029598e | 2013-01-17 11:17:16 -0600 | [diff] [blame] | 71 | nova secgroup-delete-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0 | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 72 | done | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 73 |  | 
| Dean Troyer | da85cda | 2013-02-15 11:07:14 -0600 | [diff] [blame] | 74 | # Delete secgroup | 
|  | 75 | nova secgroup-delete $SEC_GROUP_NAME || \ | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 76 | die $LINENO "Failure deleting security group $SEC_GROUP_NAME" | 
| Ben Andrews | dbdf6be | 2012-04-04 14:43:32 -0400 | [diff] [blame] | 77 |  | 
|  | 78 | set +o xtrace | 
|  | 79 | echo "*********************************************************************" | 
|  | 80 | echo "SUCCESS: End DevStack Exercise: $0" | 
|  | 81 | echo "*********************************************************************" |