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