blob: d71a1e07553689cb62d0c2823bf5a0b08b94a786 [file] [log] [blame]
Ben Andrewsdbdf6be2012-04-04 14:43:32 -04001#!/usr/bin/env bash
2
3# **sec_groups.sh**
4
Dean Troyerda85cda2013-02-15 11:07:14 -06005# Test security groups via the command line
Ben Andrewsdbdf6be2012-04-04 14:43:32 -04006
7echo "*********************************************************************"
8echo "Begin DevStack Exercise: $0"
9echo "*********************************************************************"
10
11# This script exits on an error so that errors don't compound and you see
Joe Gordon46400262013-06-30 04:32:27 -070012# only the first error that occurred.
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040013set -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.
17set -o xtrace
18
19
20# Settings
21# ========
22
23# Keep track of the current directory
24EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
25TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
26
27# Import common functions
28source $TOP_DIR/functions
29
30# Import configuration
31source $TOP_DIR/openrc
32
33# Import exercise configuration
34source $TOP_DIR/exerciserc
35
Kiall Mac Innesa16c8212014-01-12 19:35:43 +000036# If nova api is not enabled we exit with exitcode 55 so that
37# the exercise is skipped
38is_service_enabled n-api || exit 55
39
Dean Troyer2aa2a892013-08-04 19:53:19 -050040# Skip if the hypervisor is Docker
41[[ "$VIRT_DRIVER" == "docker" ]] && exit 55
42
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040043
44# Testing Security Groups
Dean Troyerc5dfecd2012-09-08 14:20:43 -050045# =======================
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040046
47# List security groups
48nova secgroup-list
49
50# Create random name for new sec group and create secgroup of said name
Dean Troyerda85cda2013-02-15 11:07:14 -060051SEC_GROUP_NAME="ex-secgroup-$(openssl rand -hex 4)"
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040052nova secgroup-create $SEC_GROUP_NAME 'a test security group'
53
54# Add some rules to the secgroup
55RULES_TO_ADD=( 22 3389 5900 )
56
57for RULE in "${RULES_TO_ADD[@]}"; do
Dean Troyer029598e2013-01-17 11:17:16 -060058 nova secgroup-add-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040059done
60
61# Check to make sure rules were added
62SEC_GROUP_RULES=( $(nova secgroup-list-rules $SEC_GROUP_NAME | grep -v \- | grep -v 'Source Group' | cut -d '|' -f3 | tr -d ' ') )
DennyZhang23178a92013-10-22 17:07:32 -050063die_if_not_set $LINENO SEC_GROUP_RULES "Failure retrieving SEC_GROUP_RULES for $SEC_GROUP_NAME"
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040064for i in "${RULES_TO_ADD[@]}"; do
65 skip=
66 for j in "${SEC_GROUP_RULES[@]}"; do
67 [[ $i == $j ]] && { skip=1; break; }
68 done
69 [[ -n $skip ]] || exit 1
70done
71
72# Delete rules and secgroup
73for RULE in "${RULES_TO_ADD[@]}"; do
Dean Troyer029598e2013-01-17 11:17:16 -060074 nova secgroup-delete-rule $SEC_GROUP_NAME tcp $RULE $RULE 0.0.0.0/0
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040075done
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040076
Dean Troyerda85cda2013-02-15 11:07:14 -060077# Delete secgroup
78nova secgroup-delete $SEC_GROUP_NAME || \
Nachi Ueno07115eb2013-02-26 12:38:18 -080079 die $LINENO "Failure deleting security group $SEC_GROUP_NAME"
Ben Andrewsdbdf6be2012-04-04 14:43:32 -040080
81set +o xtrace
82echo "*********************************************************************"
83echo "SUCCESS: End DevStack Exercise: $0"
84echo "*********************************************************************"