blob: 25f713ca935c8acec5ccd1845bbb15f5ce61a455 [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
Adam Spiersc85ade72013-10-01 00:35:16 +01009set -o errexit
10set -o xtrace
11
Attila Fazekas22ef5732012-12-16 14:03:06 +010012ACCOUNT_DIR=./accrc
13
Ian Wienandaee18c72014-02-21 15:35:08 +110014function display_help {
Attila Fazekas22ef5732012-12-16 14:03:06 +010015cat <<EOF
16
17usage: $0 <options..>
18
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000019This script creates certificates and sourcable rc files per project/user.
Attila Fazekas22ef5732012-12-16 14:03:06 +010020
21Target account directory hierarchy:
22target_dir-|
23 |-cacert.pem
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000024 |-project1-name|
25 | |- user1
26 | |- user1-cert.pem
27 | |- user1-pk.pem
28 | |- user2
29 | ..
30 |-project2-name..
Attila Fazekas22ef5732012-12-16 14:03:06 +010031 ..
32
33Optional 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
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000037-C <project_name> create user and project, the specifid project will be the user's project
38-r <name> when combined with -C and the (-u) user exists it will be the user's project role in the (-C)project (default: Member)
Attila Fazekas22ef5732012-12-16 14:03:06 +010039-p <userpass> password for the user
Steve Bakere389aed2014-09-23 17:10:39 +120040--heat-url <heat_url>
Attila Fazekas22ef5732012-12-16 14:03:06 +010041--os-username <username>
42--os-password <admin password>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000043--os-project-name <project_name>
44--os-project-id <project_id>
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000045--os-user-domain-id <user_domain_id>
46--os-user-domain-name <user_domain_name>
47--os-project-domain-id <project_domain_id>
48--os-project-domain-name <project_domain_name>
Attila Fazekas22ef5732012-12-16 14:03:06 +010049--os-auth-url <auth_url>
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100050--os-cacert <cert file>
Attila Fazekas22ef5732012-12-16 14:03:06 +010051--target-dir <target_directory>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000052--skip-project <project-name>
Attila Fazekas22ef5732012-12-16 14:03:06 +010053--debug
54
55Example:
56$0 -AP
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000057$0 -P -C myproject -u myuser -p mypass
Attila Fazekas22ef5732012-12-16 14:03:06 +010058EOF
59}
60
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000061if ! options=$(getopt -o hPAp:u:r:C: -l os-username:,os-password:,os-tenant-id:,os-tenant-name:,os-project-name:,os-project-id:,os-project-domain-id:,os-project-domain-name:,os-user-domain-id:,os-user-domain-name:,os-auth-url:,target-dir:,heat-url:,skip-project:,os-cacert:,help,debug -- "$@"); then
Attila Fazekas22ef5732012-12-16 14:03:06 +010062 display_help
63 exit 1
64fi
65eval set -- $options
66ADDPASS=""
Steve Bakere389aed2014-09-23 17:10:39 +120067HEAT_URL=""
Attila Fazekas22ef5732012-12-16 14:03:06 +010068
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000069# The services users usually in the service project.
Attila Fazekas22ef5732012-12-16 14:03:06 +010070# rc files for service users, is out of scope.
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000071# Supporting different project for services is out of scope.
72SKIP_PROJECT="service"
Attila Fazekas22ef5732012-12-16 14:03:06 +010073MODE=""
74ROLE=Member
75USER_NAME=""
76USER_PASS=""
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010077while [ $# -gt 0 ]; do
Attila Fazekas22ef5732012-12-16 14:03:06 +010078 case "$1" in
79 -h|--help) display_help; exit 0 ;;
80 --os-username) export OS_USERNAME=$2; shift ;;
81 --os-password) export OS_PASSWORD=$2; shift ;;
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000082 --os-tenant-name) export OS_PROJECT_NAME=$2; shift ;;
83 --os-tenant-id) export OS_PROJECT_ID=$2; shift ;;
84 --os-project-name) export OS_PROJECT_NAME=$2; shift ;;
85 --os-project-id) export OS_PROJECT_ID=$2; shift ;;
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +000086 --os-user-domain-id) export OS_USER_DOMAIN_ID=$2; shift ;;
87 --os-user-domain-name) export OS_USER_DOMAIN_NAME=$2; shift ;;
88 --os-project-domain-id) export OS_PROJECT_DOMAIN_ID=$2; shift ;;
89 --os-project-domain-name) export OS_PROJECT_DOMAIN_NAME=$2; shift ;;
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000090 --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
91 --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010092 --os-auth-url) export OS_AUTH_URL=$2; shift ;;
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100093 --os-cacert) export OS_CACERT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010094 --target-dir) ACCOUNT_DIR=$2; shift ;;
Steve Bakere389aed2014-09-23 17:10:39 +120095 --heat-url) HEAT_URL=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010096 --debug) set -o xtrace ;;
97 -u) MODE=${MODE:-one}; USER_NAME=$2; shift ;;
98 -p) USER_PASS=$2; shift ;;
99 -A) MODE=all; ;;
100 -P) ADDPASS="yes" ;;
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000101 -C) MODE=create; PROJECT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100102 -r) ROLE=$2; shift ;;
103 (--) shift; break ;;
104 (-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;;
105 (*) echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;;
106 esac
107 shift
108done
109
110if [ -z "$OS_PASSWORD" ]; then
111 if [ -z "$ADMIN_PASSWORD" ];then
112 echo "The admin password is required option!" >&2
113 exit 2
114 else
115 OS_PASSWORD=$ADMIN_PASSWORD
116 fi
117fi
118
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000119if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then
120 export OS_PROJECT_ID=$OS_TENANT_ID
121fi
122
123if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then
124 export OS_PROJECT_NAME=$OS_TENANT_NAME
125fi
126
127if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then
128 export OS_PROJECT_NAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100129fi
130
131if [ -z "$OS_USERNAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400132 export OS_USERNAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100133fi
134
135if [ -z "$OS_AUTH_URL" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400136 export OS_AUTH_URL=http://localhost:5000/v2.0/
Attila Fazekas22ef5732012-12-16 14:03:06 +0100137fi
138
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000139if [ -z "$OS_USER_DOMAIN_ID" -a -z "$OS_USER_DOMAIN_NAME" ]; then
140 # purposefully not exported as it would force v3 auth within this file.
141 OS_USER_DOMAIN_ID=default
142fi
143
144if [ -z "$OS_PROJECT_DOMAIN_ID" -a -z "$OS_PROJECT_DOMAIN_NAME" ]; then
145 # purposefully not exported as it would force v3 auth within this file.
146 OS_PROJECT_DOMAIN_ID=default
147fi
148
Attila Fazekas22ef5732012-12-16 14:03:06 +0100149USER_PASS=${USER_PASS:-$OS_PASSWORD}
150USER_NAME=${USER_NAME:-$OS_USERNAME}
151
152if [ -z "$MODE" ]; then
153 echo "You must specify at least -A or -u parameter!" >&2
154 echo
155 display_help
156 exit 3
157fi
158
159export -n SERVICE_TOKEN SERVICE_ENDPOINT OS_SERVICE_TOKEN OS_SERVICE_ENDPOINT
160
Andrey Pavlov485c9622015-08-24 22:55:19 +0300161EC2_URL=$(openstack endpoint list --service ec2 --interface public --os-identity-api-version=3 -c URL -f value || true)
Steve Martinellibce88992014-03-14 00:40:06 -0500162if [[ -z $EC2_URL ]]; then
Andrey Pavlov0ea8b722015-02-06 22:37:53 +0300163 EC2_URL=http://localhost:8773/
Steve Martinellibce88992014-03-14 00:40:06 -0500164fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100165
Andrey Pavlov485c9622015-08-24 22:55:19 +0300166S3_URL=$(openstack endpoint list --service s3 --interface public --os-identity-api-version=3 -c URL -f value || true)
Steve Martinellibce88992014-03-14 00:40:06 -0500167if [[ -z $S3_URL ]]; then
168 S3_URL=http://localhost:3333
169fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100170
171mkdir -p "$ACCOUNT_DIR"
172ACCOUNT_DIR=`readlink -f "$ACCOUNT_DIR"`
173EUCALYPTUS_CERT=$ACCOUNT_DIR/cacert.pem
Adam Spiersc85ade72013-10-01 00:35:16 +0100174if [ -e "$EUCALYPTUS_CERT" ]; then
175 mv "$EUCALYPTUS_CERT" "$EUCALYPTUS_CERT.old"
176fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100177if ! nova x509-get-root-cert "$EUCALYPTUS_CERT"; then
178 echo "Failed to update the root certificate: $EUCALYPTUS_CERT" >&2
Adam Spiersc85ade72013-10-01 00:35:16 +0100179 if [ -e "$EUCALYPTUS_CERT.old" ]; then
180 mv "$EUCALYPTUS_CERT.old" "$EUCALYPTUS_CERT"
181 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100182fi
183
184
Ian Wienandaee18c72014-02-21 15:35:08 +1100185function add_entry {
Attila Fazekas22ef5732012-12-16 14:03:06 +0100186 local user_id=$1
187 local user_name=$2
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000188 local project_id=$3
189 local project_name=$4
Attila Fazekas22ef5732012-12-16 14:03:06 +0100190 local user_passwd=$5
191
192 # The admin user can see all user's secret AWS keys, it does not looks good
Ian Wienandada886d2015-10-07 14:06:26 +1100193 local line
194 line=$(openstack ec2 credentials list --user $user_id | grep " $project_id " || true)
Attila Fazekas22ef5732012-12-16 14:03:06 +0100195 if [ -z "$line" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000196 openstack ec2 credentials create --user $user_id --project $project_id 1>&2
197 line=`openstack ec2 credentials list --user $user_id | grep " $project_id "`
Attila Fazekas22ef5732012-12-16 14:03:06 +0100198 fi
199 local ec2_access_key ec2_secret_key
Steve Martinellibce88992014-03-14 00:40:06 -0500200 read ec2_access_key ec2_secret_key <<< `echo $line | awk '{print $2 " " $4 }'`
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000201 mkdir -p "$ACCOUNT_DIR/$project_name"
202 local rcfile="$ACCOUNT_DIR/$project_name/$user_name"
203 # The certs subject part are the project ID "dash" user ID, but the CN should be the first part of the DN
Attila Fazekas22ef5732012-12-16 14:03:06 +0100204 # Generally the subject DN parts should be in reverse order like the Issuer
205 # The Serial does not seams correctly marked either
206 local ec2_cert="$rcfile-cert.pem"
207 local ec2_private_key="$rcfile-pk.pem"
208 # Try to preserve the original file on fail (best effort)
Adam Spiersc85ade72013-10-01 00:35:16 +0100209 if [ -e "$ec2_private_key" ]; then
210 mv -f "$ec2_private_key" "$ec2_private_key.old"
211 fi
212 if [ -e "$ec2_cert" ]; then
213 mv -f "$ec2_cert" "$ec2_cert.old"
214 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100215 # It will not create certs when the password is incorrect
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000216 if ! nova --os-password "$user_passwd" --os-username "$user_name" --os-project-name "$project_name" x509-create-cert "$ec2_private_key" "$ec2_cert"; then
Adam Spiersc85ade72013-10-01 00:35:16 +0100217 if [ -e "$ec2_private_key.old" ]; then
218 mv -f "$ec2_private_key.old" "$ec2_private_key"
219 fi
220 if [ -e "$ec2_cert.old" ]; then
221 mv -f "$ec2_cert.old" "$ec2_cert"
222 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100223 fi
224 cat >"$rcfile" <<EOF
225# you can source this file
Attila Fazekas5b813bc2013-01-08 16:51:05 +0100226export EC2_ACCESS_KEY="$ec2_access_key"
227export EC2_SECRET_KEY="$ec2_secret_key"
228export EC2_URL="$EC2_URL"
229export S3_URL="$S3_URL"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100230# OpenStack USER ID = $user_id
231export OS_USERNAME="$user_name"
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000232# OpenStack project ID = $project_id
233export OS_PROJECT_NAME="$project_name"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100234export OS_AUTH_URL="$OS_AUTH_URL"
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000235export OS_CACERT="$OS_CACERT"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100236export EC2_CERT="$ec2_cert"
237export EC2_PRIVATE_KEY="$ec2_private_key"
238export EC2_USER_ID=42 #not checked by nova (can be a 12-digit id)
239export EUCALYPTUS_CERT="$ACCOUNT_DIR/cacert.pem"
240export NOVA_CERT="$ACCOUNT_DIR/cacert.pem"
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000241export OS_AUTH_TYPE=v2password
Attila Fazekas22ef5732012-12-16 14:03:06 +0100242EOF
243 if [ -n "$ADDPASS" ]; then
244 echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile"
245 fi
Steve Bakere389aed2014-09-23 17:10:39 +1200246 if [ -n "$HEAT_URL" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000247 echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile"
Steve Bakere389aed2014-09-23 17:10:39 +1200248 echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile"
249 fi
Jamie Lennoxc54d4ab2015-06-22 04:07:18 +0000250 for v in OS_USER_DOMAIN_ID OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_ID OS_PROJECT_DOMAIN_NAME; do
251 if [ ${!v} ]; then
252 echo "export $v=${!v}" >>"$rcfile"
253 else
254 echo "unset $v" >>"$rcfile"
255 fi
256 done
Attila Fazekas22ef5732012-12-16 14:03:06 +0100257}
258
259#admin users expected
Steve Martinellibce88992014-03-14 00:40:06 -0500260function create_or_get_project {
261 local name=$1
262 local id
263 eval $(openstack project show -f shell -c id $name)
264 if [[ -z $id ]]; then
265 eval $(openstack project create -f shell -c id $name)
Attila Fazekas22ef5732012-12-16 14:03:06 +0100266 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500267 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100268}
269
Ian Wienandaee18c72014-02-21 15:35:08 +1100270function create_or_get_role {
Steve Martinellibce88992014-03-14 00:40:06 -0500271 local name=$1
272 local id
273 eval $(openstack role show -f shell -c id $name)
274 if [[ -z $id ]]; then
275 eval $(openstack role create -f shell -c id $name)
Attila Fazekas22ef5732012-12-16 14:03:06 +0100276 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500277 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100278}
279
280# Provides empty string when the user does not exists
Ian Wienandaee18c72014-02-21 15:35:08 +1100281function get_user_id {
Steve Martinellibce88992014-03-14 00:40:06 -0500282 openstack user list | grep " $1 " | cut -d " " -f2
Attila Fazekas22ef5732012-12-16 14:03:06 +0100283}
284
285if [ $MODE != "create" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000286 # looks like I can't ask for all project related to a specified user
287 openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_PROJECT}" | while IFS=, read project_id project_name desc enabled; do
288 openstack user list --project $project_id --long --quote none -f csv | grep ',True' | while IFS=, read user_id user_name project email enabled; do
Attila Fazekas22ef5732012-12-16 14:03:06 +0100289 if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400290 continue;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100291 fi
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100292
293 # Checks for a specific password defined for an user.
Dean Troyer3324f192014-09-18 09:26:39 -0500294 # Example for an username johndoe: JOHNDOE_PASSWORD=1234
JordanP7c6d0052014-10-06 23:08:50 +0200295 # This mechanism is used by lib/swift
296 eval SPECIFIC_UPASSWORD="\$${user_name}_password"
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100297 if [ -n "$SPECIFIC_UPASSWORD" ]; then
298 USER_PASS=$SPECIFIC_UPASSWORD
299 fi
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000300 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100301 done
302 done
303else
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000304 project_name=$PROJECT
305 project_id=$(create_or_get_project "$PROJECT")
Attila Fazekas22ef5732012-12-16 14:03:06 +0100306 user_name=$USER_NAME
307 user_id=`get_user_id $user_name`
308 if [ -z "$user_id" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000309 eval $(openstack user create "$user_name" --project "$project_id" --password "$USER_PASS" --email "$user_name@example.com" -f shell -c id)
Steve Martinellibce88992014-03-14 00:40:06 -0500310 user_id=$id
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000311 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100312 else
Steve Martinellibce88992014-03-14 00:40:06 -0500313 role_id=$(create_or_get_role "$ROLE")
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000314 openstack role add "$role_id" --user "$user_id" --project "$project_id"
315 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100316 fi
317fi