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