| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
|  | 2 |  | 
| Dean Troyer | 0f2d954 | 2013-02-20 17:51:19 -0600 | [diff] [blame] | 3 | # **create_userrc.sh** | 
|  | 4 |  | 
|  | 5 | # Pre-create rc files and credentials for the default users. | 
|  | 6 |  | 
|  | 7 | # Warning: This script just for development purposes | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 8 |  | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 9 | set -o errexit | 
|  | 10 | set -o xtrace | 
|  | 11 |  | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 12 | ACCOUNT_DIR=./accrc | 
|  | 13 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 14 | function display_help { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 15 | cat <<EOF | 
|  | 16 |  | 
|  | 17 | usage: $0 <options..> | 
|  | 18 |  | 
|  | 19 | This script creates certificates and sourcable rc files per tenant/user. | 
|  | 20 |  | 
|  | 21 | Target account directory hierarchy: | 
|  | 22 | target_dir-| | 
|  | 23 | |-cacert.pem | 
|  | 24 | |-tenant1-name| | 
|  | 25 | |             |- user1 | 
|  | 26 | |             |- user1-cert.pem | 
|  | 27 | |             |- user1-pk.pem | 
|  | 28 | |             |- user2 | 
|  | 29 | |             .. | 
|  | 30 | |-tenant2-name.. | 
|  | 31 | .. | 
|  | 32 |  | 
|  | 33 | Optional Arguments | 
|  | 34 | -P include password to the rc files; with -A it assume all users password is the same | 
|  | 35 | -A try with all user | 
|  | 36 | -u <username> create files just for the specified user | 
|  | 37 | -C <tanent_name> create user and tenant, the specifid tenant will be the user's tenant | 
|  | 38 | -r <name> when combined with -C and the (-u) user exists it will be the user's tenant role in the (-C)tenant (default: Member) | 
|  | 39 | -p <userpass> password for the user | 
|  | 40 | --os-username <username> | 
|  | 41 | --os-password <admin password> | 
|  | 42 | --os-tenant-name <tenant_name> | 
|  | 43 | --os-tenant-id <tenant_id> | 
|  | 44 | --os-auth-url <auth_url> | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 45 | --os-cacert <cert file> | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 46 | --target-dir <target_directory> | 
|  | 47 | --skip-tenant <tenant-name> | 
|  | 48 | --debug | 
|  | 49 |  | 
|  | 50 | Example: | 
|  | 51 | $0 -AP | 
|  | 52 | $0 -P -C mytenant -u myuser -p mypass | 
|  | 53 | EOF | 
|  | 54 | } | 
|  | 55 |  | 
| Dean Troyer | d8864fe | 2014-02-17 11:00:42 -0600 | [diff] [blame] | 56 | if ! options=$(getopt -o hPAp:u:r:C: -l os-username:,os-password:,os-tenant-name:,os-tenant-id:,os-auth-url:,target-dir:,skip-tenant:,os-cacert:,help,debug -- "$@"); then | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 57 | display_help | 
|  | 58 | exit 1 | 
|  | 59 | fi | 
|  | 60 | eval set -- $options | 
|  | 61 | ADDPASS="" | 
|  | 62 |  | 
|  | 63 | # The services users usually in the service tenant. | 
|  | 64 | # rc files for service users, is out of scope. | 
|  | 65 | # Supporting different tanent for services is out of scope. | 
|  | 66 | SKIP_TENANT=",service," # tenant names are between commas(,) | 
|  | 67 | MODE="" | 
|  | 68 | ROLE=Member | 
|  | 69 | USER_NAME="" | 
|  | 70 | USER_PASS="" | 
| Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 71 | while [ $# -gt 0 ]; do | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 72 | case "$1" in | 
|  | 73 | -h|--help) display_help; exit 0 ;; | 
|  | 74 | --os-username) export OS_USERNAME=$2; shift ;; | 
|  | 75 | --os-password) export OS_PASSWORD=$2; shift ;; | 
|  | 76 | --os-tenant-name) export OS_TENANT_NAME=$2; shift ;; | 
|  | 77 | --os-tenant-id) export OS_TENANT_ID=$2; shift ;; | 
|  | 78 | --skip-tenant) SKIP_TENANT="$SKIP_TENANT$2,"; shift ;; | 
|  | 79 | --os-auth-url) export OS_AUTH_URL=$2; shift ;; | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 80 | --os-cacert) export OS_CACERT=$2; shift ;; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 81 | --target-dir) ACCOUNT_DIR=$2; shift ;; | 
|  | 82 | --debug) set -o xtrace ;; | 
|  | 83 | -u) MODE=${MODE:-one};  USER_NAME=$2; shift ;; | 
|  | 84 | -p) USER_PASS=$2; shift ;; | 
|  | 85 | -A) MODE=all; ;; | 
|  | 86 | -P) ADDPASS="yes" ;; | 
|  | 87 | -C) MODE=create; TENANT=$2; shift ;; | 
|  | 88 | -r) ROLE=$2; shift ;; | 
|  | 89 | (--) shift; break ;; | 
|  | 90 | (-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;; | 
|  | 91 | (*)  echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;; | 
|  | 92 | esac | 
|  | 93 | shift | 
|  | 94 | done | 
|  | 95 |  | 
|  | 96 | if [ -z "$OS_PASSWORD" ]; then | 
|  | 97 | if [ -z "$ADMIN_PASSWORD" ];then | 
|  | 98 | echo "The admin password is required option!"  >&2 | 
|  | 99 | exit 2 | 
|  | 100 | else | 
|  | 101 | OS_PASSWORD=$ADMIN_PASSWORD | 
|  | 102 | fi | 
|  | 103 | fi | 
|  | 104 |  | 
|  | 105 | if [ -z "$OS_TENANT_NAME" -a -z "$OS_TENANT_ID" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 106 | export OS_TENANT_NAME=admin | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 107 | fi | 
|  | 108 |  | 
|  | 109 | if [ -z "$OS_USERNAME" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 110 | export OS_USERNAME=admin | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 111 | fi | 
|  | 112 |  | 
|  | 113 | if [ -z "$OS_AUTH_URL" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 114 | export OS_AUTH_URL=http://localhost:5000/v2.0/ | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 115 | fi | 
|  | 116 |  | 
|  | 117 | USER_PASS=${USER_PASS:-$OS_PASSWORD} | 
|  | 118 | USER_NAME=${USER_NAME:-$OS_USERNAME} | 
|  | 119 |  | 
|  | 120 | if [ -z "$MODE" ]; then | 
|  | 121 | echo "You must specify at least -A or -u parameter!"  >&2 | 
|  | 122 | echo | 
|  | 123 | display_help | 
|  | 124 | exit 3 | 
|  | 125 | fi | 
|  | 126 |  | 
|  | 127 | export -n SERVICE_TOKEN SERVICE_ENDPOINT OS_SERVICE_TOKEN OS_SERVICE_ENDPOINT | 
|  | 128 |  | 
|  | 129 | EC2_URL=http://localhost:8773/service/Cloud | 
|  | 130 | S3_URL=http://localhost:3333 | 
|  | 131 |  | 
|  | 132 | ec2=`keystone endpoint-get --service ec2 | awk '/\|[[:space:]]*ec2.publicURL/ {print $4}'` | 
|  | 133 | [ -n "$ec2" ] && EC2_URL=$ec2 | 
|  | 134 |  | 
|  | 135 | s3=`keystone endpoint-get --service s3 | awk '/\|[[:space:]]*s3.publicURL/ {print $4}'` | 
|  | 136 | [ -n "$s3" ] && S3_URL=$s3 | 
|  | 137 |  | 
|  | 138 |  | 
|  | 139 | mkdir -p "$ACCOUNT_DIR" | 
|  | 140 | ACCOUNT_DIR=`readlink -f "$ACCOUNT_DIR"` | 
|  | 141 | EUCALYPTUS_CERT=$ACCOUNT_DIR/cacert.pem | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 142 | if [ -e "$EUCALYPTUS_CERT" ]; then | 
|  | 143 | mv "$EUCALYPTUS_CERT" "$EUCALYPTUS_CERT.old" | 
|  | 144 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 145 | if ! nova x509-get-root-cert "$EUCALYPTUS_CERT"; then | 
|  | 146 | echo "Failed to update the root certificate: $EUCALYPTUS_CERT" >&2 | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 147 | if [ -e "$EUCALYPTUS_CERT.old" ]; then | 
|  | 148 | mv "$EUCALYPTUS_CERT.old" "$EUCALYPTUS_CERT" | 
|  | 149 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 150 | fi | 
|  | 151 |  | 
|  | 152 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 153 | function add_entry { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 154 | local user_id=$1 | 
|  | 155 | local user_name=$2 | 
|  | 156 | local tenant_id=$3 | 
|  | 157 | local tenant_name=$4 | 
|  | 158 | local user_passwd=$5 | 
|  | 159 |  | 
|  | 160 | # The admin user can see all user's secret AWS keys, it does not looks good | 
|  | 161 | local line=`keystone ec2-credentials-list --user_id $user_id | grep -E "^\\|[[:space:]]*($tenant_name|$tenant_id)[[:space:]]*\\|" | head -n 1` | 
|  | 162 | if [ -z "$line" ]; then | 
|  | 163 | keystone ec2-credentials-create --user-id $user_id --tenant-id $tenant_id 1>&2 | 
|  | 164 | line=`keystone ec2-credentials-list --user_id $user_id | grep -E "^\\|[[:space:]]*($tenant_name|$tenant_id)[[:space:]]*\\|" | head -n 1` | 
|  | 165 | fi | 
|  | 166 | local ec2_access_key ec2_secret_key | 
|  | 167 | read ec2_access_key ec2_secret_key <<<  `echo $line | awk '{print $4 " " $6 }'` | 
|  | 168 | mkdir -p "$ACCOUNT_DIR/$tenant_name" | 
|  | 169 | local rcfile="$ACCOUNT_DIR/$tenant_name/$user_name" | 
|  | 170 | # The certs subject part are the tenant ID "dash" user ID, but the CN should be the first part of the DN | 
|  | 171 | # Generally the subject DN parts should be in reverse order like the Issuer | 
|  | 172 | # The Serial does not seams correctly marked either | 
|  | 173 | local ec2_cert="$rcfile-cert.pem" | 
|  | 174 | local ec2_private_key="$rcfile-pk.pem" | 
|  | 175 | # Try to preserve the original file on fail (best effort) | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 176 | if [ -e "$ec2_private_key" ]; then | 
|  | 177 | mv -f "$ec2_private_key" "$ec2_private_key.old" | 
|  | 178 | fi | 
|  | 179 | if [ -e "$ec2_cert" ]; then | 
|  | 180 | mv -f "$ec2_cert" "$ec2_cert.old" | 
|  | 181 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 182 | # It will not create certs when the password is incorrect | 
|  | 183 | if ! nova --os-password "$user_passwd" --os-username "$user_name" --os-tenant-name "$tenant_name" x509-create-cert "$ec2_private_key" "$ec2_cert"; then | 
| Adam Spiers | c85ade7 | 2013-10-01 00:35:16 +0100 | [diff] [blame] | 184 | if [ -e "$ec2_private_key.old" ]; then | 
|  | 185 | mv -f "$ec2_private_key.old" "$ec2_private_key" | 
|  | 186 | fi | 
|  | 187 | if [ -e "$ec2_cert.old" ]; then | 
|  | 188 | mv -f "$ec2_cert.old" "$ec2_cert" | 
|  | 189 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 190 | fi | 
|  | 191 | cat >"$rcfile" <<EOF | 
|  | 192 | # you can source this file | 
| Attila Fazekas | 5b813bc | 2013-01-08 16:51:05 +0100 | [diff] [blame] | 193 | export EC2_ACCESS_KEY="$ec2_access_key" | 
|  | 194 | export EC2_SECRET_KEY="$ec2_secret_key" | 
|  | 195 | export EC2_URL="$EC2_URL" | 
|  | 196 | export S3_URL="$S3_URL" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 197 | # OpenStack USER ID = $user_id | 
|  | 198 | export OS_USERNAME="$user_name" | 
| tanlin | 2b69f23 | 2014-02-12 16:11:32 +0800 | [diff] [blame] | 199 | # OpenStack Tenant ID = $tenant_id | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 200 | export OS_TENANT_NAME="$tenant_name" | 
|  | 201 | export OS_AUTH_URL="$OS_AUTH_URL" | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 202 | export OS_CACERT="$OS_CACERT" | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 203 | export EC2_CERT="$ec2_cert" | 
|  | 204 | export EC2_PRIVATE_KEY="$ec2_private_key" | 
|  | 205 | export EC2_USER_ID=42 #not checked by nova (can be a 12-digit id) | 
|  | 206 | export EUCALYPTUS_CERT="$ACCOUNT_DIR/cacert.pem" | 
|  | 207 | export NOVA_CERT="$ACCOUNT_DIR/cacert.pem" | 
|  | 208 | EOF | 
|  | 209 | if [ -n "$ADDPASS" ]; then | 
|  | 210 | echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile" | 
|  | 211 | fi | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | #admin users expected | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 215 | function create_or_get_tenant { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 216 | local tenant_name=$1 | 
|  | 217 | local tenant_id=`keystone tenant-list | awk '/\|[[:space:]]*'"$tenant_name"'[[:space:]]*\|.*\|/ {print $2}'` | 
|  | 218 | if [ -n "$tenant_id" ]; then | 
|  | 219 | echo $tenant_id | 
|  | 220 | else | 
|  | 221 | keystone tenant-create --name "$tenant_name" | awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}' | 
|  | 222 | fi | 
|  | 223 | } | 
|  | 224 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 225 | function create_or_get_role { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 226 | local role_name=$1 | 
|  | 227 | local role_id=`keystone role-list| awk '/\|[[:space:]]*'"$role_name"'[[:space:]]*\|/ {print $2}'` | 
|  | 228 | if [ -n "$role_id" ]; then | 
|  | 229 | echo $role_id | 
|  | 230 | else | 
| Attila Fazekas | 5b813bc | 2013-01-08 16:51:05 +0100 | [diff] [blame] | 231 | keystone role-create --name "$role_name" |awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}' | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 232 | fi | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | # Provides empty string when the user does not exists | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 236 | function get_user_id { | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 237 | local user_name=$1 | 
|  | 238 | keystone user-list | awk '/^\|[^|]*\|[[:space:]]*'"$user_name"'[[:space:]]*\|.*\|/ {print $2}' | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | if [ $MODE != "create" ]; then | 
|  | 242 | # looks like I can't ask for all tenant related to a specified  user | 
|  | 243 | for tenant_id_at_name in `keystone tenant-list | awk 'BEGIN {IGNORECASE = 1} /true[[:space:]]*\|$/ {print  $2 "@" $4}'`; do | 
|  | 244 | read tenant_id tenant_name <<< `echo "$tenant_id_at_name" | sed 's/@/ /'` | 
|  | 245 | if echo $SKIP_TENANT| grep -q ",$tenant_name,"; then | 
|  | 246 | continue; | 
|  | 247 | fi | 
|  | 248 | for user_id_at_name in `keystone user-list --tenant-id $tenant_id | awk 'BEGIN {IGNORECASE = 1} /true[[:space:]]*\|[^|]*\|$/ {print  $2 "@" $4}'`; do | 
|  | 249 | read user_id user_name <<< `echo "$user_id_at_name" | sed 's/@/ /'` | 
|  | 250 | if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 251 | continue; | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 252 | fi | 
| Sahid Orentino Ferdjaoui | 1814e67 | 2014-02-11 17:56:07 +0100 | [diff] [blame] | 253 |  | 
|  | 254 | # Checks for a specific password defined for an user. | 
|  | 255 | # Example for an username johndoe: | 
|  | 256 | #                     JOHNDOE_PASSWORD=1234 | 
|  | 257 | eval SPECIFIC_UPASSWORD="\$${USER_NAME^^}_PASSWORD" | 
|  | 258 | if [ -n "$SPECIFIC_UPASSWORD" ]; then | 
|  | 259 | USER_PASS=$SPECIFIC_UPASSWORD | 
|  | 260 | fi | 
| Attila Fazekas | 22ef573 | 2012-12-16 14:03:06 +0100 | [diff] [blame] | 261 | add_entry "$user_id" "$user_name" "$tenant_id" "$tenant_name" "$USER_PASS" | 
|  | 262 | done | 
|  | 263 | done | 
|  | 264 | else | 
|  | 265 | tenant_name=$TENANT | 
|  | 266 | tenant_id=`create_or_get_tenant "$TENANT"` | 
|  | 267 | user_name=$USER_NAME | 
|  | 268 | user_id=`get_user_id $user_name` | 
|  | 269 | if [ -z "$user_id" ]; then | 
|  | 270 | #new user | 
|  | 271 | user_id=`keystone user-create --name "$user_name" --tenant-id "$tenant_id" --pass "$USER_PASS" --email "$user_name@example.com" | awk '/\|[[:space:]]*id[[:space:]]*\|.*\|/ {print $4}'` | 
|  | 272 | #The password is in the cmd line. It is not a good thing | 
|  | 273 | add_entry "$user_id" "$user_name" "$tenant_id" "$tenant_name" "$USER_PASS" | 
|  | 274 | else | 
|  | 275 | #new role | 
|  | 276 | role_id=`create_or_get_role "$ROLE"` | 
|  | 277 | keystone user-role-add --user-id "$user_id" --tenant-id "$tenant_id" --role-id "$role_id" | 
|  | 278 | add_entry "$user_id" "$user_name" "$tenant_id" "$tenant_name" "$USER_PASS" | 
|  | 279 | fi | 
|  | 280 | fi |