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