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