blob: 3f10abbf7ce5707b301513ec9ef9ca29147a1b2c [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>
Attila Fazekas22ef5732012-12-16 14:03:06 +010045--os-auth-url <auth_url>
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100046--os-cacert <cert file>
Attila Fazekas22ef5732012-12-16 14:03:06 +010047--target-dir <target_directory>
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000048--skip-project <project-name>
Attila Fazekas22ef5732012-12-16 14:03:06 +010049--debug
50
51Example:
52$0 -AP
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000053$0 -P -C myproject -u myuser -p mypass
Attila Fazekas22ef5732012-12-16 14:03:06 +010054EOF
55}
56
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000057if ! 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-auth-url:,target-dir:,heat-url:,skip-project:,os-cacert:,help,debug -- "$@"); then
Attila Fazekas22ef5732012-12-16 14:03:06 +010058 display_help
59 exit 1
60fi
61eval set -- $options
62ADDPASS=""
Steve Bakere389aed2014-09-23 17:10:39 +120063HEAT_URL=""
Attila Fazekas22ef5732012-12-16 14:03:06 +010064
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000065# The services users usually in the service project.
Attila Fazekas22ef5732012-12-16 14:03:06 +010066# rc files for service users, is out of scope.
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000067# Supporting different project for services is out of scope.
68SKIP_PROJECT="service"
Attila Fazekas22ef5732012-12-16 14:03:06 +010069MODE=""
70ROLE=Member
71USER_NAME=""
72USER_PASS=""
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010073while [ $# -gt 0 ]; do
Attila Fazekas22ef5732012-12-16 14:03:06 +010074 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 ;;
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000078 --os-tenant-name) export OS_PROJECT_NAME=$2; shift ;;
79 --os-tenant-id) export OS_PROJECT_ID=$2; shift ;;
80 --os-project-name) export OS_PROJECT_NAME=$2; shift ;;
81 --os-project-id) export OS_PROJECT_ID=$2; shift ;;
82 --skip-tenant) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
83 --skip-project) SKIP_PROJECT="$SKIP_PROJECT$2,"; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010084 --os-auth-url) export OS_AUTH_URL=$2; shift ;;
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100085 --os-cacert) export OS_CACERT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010086 --target-dir) ACCOUNT_DIR=$2; shift ;;
Steve Bakere389aed2014-09-23 17:10:39 +120087 --heat-url) HEAT_URL=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010088 --debug) set -o xtrace ;;
89 -u) MODE=${MODE:-one}; USER_NAME=$2; shift ;;
90 -p) USER_PASS=$2; shift ;;
91 -A) MODE=all; ;;
92 -P) ADDPASS="yes" ;;
Jamie Lennox9d6d8f82015-06-22 03:37:59 +000093 -C) MODE=create; PROJECT=$2; shift ;;
Attila Fazekas22ef5732012-12-16 14:03:06 +010094 -r) ROLE=$2; shift ;;
95 (--) shift; break ;;
96 (-*) echo "$0: error - unrecognized option $1" >&2; display_help; exit 1 ;;
97 (*) echo "$0: error - unexpected argument $1" >&2; display_help; exit 1 ;;
98 esac
99 shift
100done
101
102if [ -z "$OS_PASSWORD" ]; then
103 if [ -z "$ADMIN_PASSWORD" ];then
104 echo "The admin password is required option!" >&2
105 exit 2
106 else
107 OS_PASSWORD=$ADMIN_PASSWORD
108 fi
109fi
110
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000111if [ -z "$OS_PROJECT_ID" -a "$OS_TENANT_ID" ]; then
112 export OS_PROJECT_ID=$OS_TENANT_ID
113fi
114
115if [ -z "$OS_PROJECT_NAME" -a "$OS_TENANT_NAME" ]; then
116 export OS_PROJECT_NAME=$OS_TENANT_NAME
117fi
118
119if [ -z "$OS_PROJECT_NAME" -a -z "$OS_PROJECT_ID" ]; then
120 export OS_PROJECT_NAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100121fi
122
123if [ -z "$OS_USERNAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400124 export OS_USERNAME=admin
Attila Fazekas22ef5732012-12-16 14:03:06 +0100125fi
126
127if [ -z "$OS_AUTH_URL" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400128 export OS_AUTH_URL=http://localhost:5000/v2.0/
Attila Fazekas22ef5732012-12-16 14:03:06 +0100129fi
130
131USER_PASS=${USER_PASS:-$OS_PASSWORD}
132USER_NAME=${USER_NAME:-$OS_USERNAME}
133
134if [ -z "$MODE" ]; then
135 echo "You must specify at least -A or -u parameter!" >&2
136 echo
137 display_help
138 exit 3
139fi
140
141export -n SERVICE_TOKEN SERVICE_ENDPOINT OS_SERVICE_TOKEN OS_SERVICE_ENDPOINT
142
Ian Wienandbcdce9e2014-06-24 15:06:02 +1000143EC2_URL=$(openstack endpoint show -f value -c publicurl ec2 || true)
Steve Martinellibce88992014-03-14 00:40:06 -0500144if [[ -z $EC2_URL ]]; then
Andrey Pavlov0ea8b722015-02-06 22:37:53 +0300145 EC2_URL=http://localhost:8773/
Steve Martinellibce88992014-03-14 00:40:06 -0500146fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100147
Ian Wienandbcdce9e2014-06-24 15:06:02 +1000148S3_URL=$(openstack endpoint show -f value -c publicurl s3 || true)
Steve Martinellibce88992014-03-14 00:40:06 -0500149if [[ -z $S3_URL ]]; then
150 S3_URL=http://localhost:3333
151fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100152
153mkdir -p "$ACCOUNT_DIR"
154ACCOUNT_DIR=`readlink -f "$ACCOUNT_DIR"`
155EUCALYPTUS_CERT=$ACCOUNT_DIR/cacert.pem
Adam Spiersc85ade72013-10-01 00:35:16 +0100156if [ -e "$EUCALYPTUS_CERT" ]; then
157 mv "$EUCALYPTUS_CERT" "$EUCALYPTUS_CERT.old"
158fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100159if ! nova x509-get-root-cert "$EUCALYPTUS_CERT"; then
160 echo "Failed to update the root certificate: $EUCALYPTUS_CERT" >&2
Adam Spiersc85ade72013-10-01 00:35:16 +0100161 if [ -e "$EUCALYPTUS_CERT.old" ]; then
162 mv "$EUCALYPTUS_CERT.old" "$EUCALYPTUS_CERT"
163 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100164fi
165
166
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function add_entry {
Attila Fazekas22ef5732012-12-16 14:03:06 +0100168 local user_id=$1
169 local user_name=$2
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000170 local project_id=$3
171 local project_name=$4
Attila Fazekas22ef5732012-12-16 14:03:06 +0100172 local user_passwd=$5
173
174 # The admin user can see all user's secret AWS keys, it does not looks good
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000175 local line=`openstack ec2 credentials list --user $user_id | grep " $project_id "`
Attila Fazekas22ef5732012-12-16 14:03:06 +0100176 if [ -z "$line" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000177 openstack ec2 credentials create --user $user_id --project $project_id 1>&2
178 line=`openstack ec2 credentials list --user $user_id | grep " $project_id "`
Attila Fazekas22ef5732012-12-16 14:03:06 +0100179 fi
180 local ec2_access_key ec2_secret_key
Steve Martinellibce88992014-03-14 00:40:06 -0500181 read ec2_access_key ec2_secret_key <<< `echo $line | awk '{print $2 " " $4 }'`
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000182 mkdir -p "$ACCOUNT_DIR/$project_name"
183 local rcfile="$ACCOUNT_DIR/$project_name/$user_name"
184 # 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 +0100185 # Generally the subject DN parts should be in reverse order like the Issuer
186 # The Serial does not seams correctly marked either
187 local ec2_cert="$rcfile-cert.pem"
188 local ec2_private_key="$rcfile-pk.pem"
189 # Try to preserve the original file on fail (best effort)
Adam Spiersc85ade72013-10-01 00:35:16 +0100190 if [ -e "$ec2_private_key" ]; then
191 mv -f "$ec2_private_key" "$ec2_private_key.old"
192 fi
193 if [ -e "$ec2_cert" ]; then
194 mv -f "$ec2_cert" "$ec2_cert.old"
195 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100196 # It will not create certs when the password is incorrect
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000197 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 +0100198 if [ -e "$ec2_private_key.old" ]; then
199 mv -f "$ec2_private_key.old" "$ec2_private_key"
200 fi
201 if [ -e "$ec2_cert.old" ]; then
202 mv -f "$ec2_cert.old" "$ec2_cert"
203 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100204 fi
205 cat >"$rcfile" <<EOF
206# you can source this file
Attila Fazekas5b813bc2013-01-08 16:51:05 +0100207export EC2_ACCESS_KEY="$ec2_access_key"
208export EC2_SECRET_KEY="$ec2_secret_key"
209export EC2_URL="$EC2_URL"
210export S3_URL="$S3_URL"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100211# OpenStack USER ID = $user_id
212export OS_USERNAME="$user_name"
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000213# OpenStack project ID = $project_id
214export OS_PROJECT_NAME="$project_name"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100215export OS_AUTH_URL="$OS_AUTH_URL"
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000216export OS_CACERT="$OS_CACERT"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100217export EC2_CERT="$ec2_cert"
218export EC2_PRIVATE_KEY="$ec2_private_key"
219export EC2_USER_ID=42 #not checked by nova (can be a 12-digit id)
220export EUCALYPTUS_CERT="$ACCOUNT_DIR/cacert.pem"
221export NOVA_CERT="$ACCOUNT_DIR/cacert.pem"
222EOF
223 if [ -n "$ADDPASS" ]; then
224 echo "export OS_PASSWORD=\"$user_passwd\"" >>"$rcfile"
225 fi
Steve Bakere389aed2014-09-23 17:10:39 +1200226 if [ -n "$HEAT_URL" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000227 echo "export HEAT_URL=\"$HEAT_URL/$project_id\"" >>"$rcfile"
Steve Bakere389aed2014-09-23 17:10:39 +1200228 echo "export OS_NO_CLIENT_AUTH=True" >>"$rcfile"
229 fi
Attila Fazekas22ef5732012-12-16 14:03:06 +0100230}
231
232#admin users expected
Steve Martinellibce88992014-03-14 00:40:06 -0500233function create_or_get_project {
234 local name=$1
235 local id
236 eval $(openstack project show -f shell -c id $name)
237 if [[ -z $id ]]; then
238 eval $(openstack project create -f shell -c id $name)
Attila Fazekas22ef5732012-12-16 14:03:06 +0100239 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500240 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100241}
242
Ian Wienandaee18c72014-02-21 15:35:08 +1100243function create_or_get_role {
Steve Martinellibce88992014-03-14 00:40:06 -0500244 local name=$1
245 local id
246 eval $(openstack role show -f shell -c id $name)
247 if [[ -z $id ]]; then
248 eval $(openstack role create -f shell -c id $name)
Attila Fazekas22ef5732012-12-16 14:03:06 +0100249 fi
Steve Martinellibce88992014-03-14 00:40:06 -0500250 echo $id
Attila Fazekas22ef5732012-12-16 14:03:06 +0100251}
252
253# Provides empty string when the user does not exists
Ian Wienandaee18c72014-02-21 15:35:08 +1100254function get_user_id {
Steve Martinellibce88992014-03-14 00:40:06 -0500255 openstack user list | grep " $1 " | cut -d " " -f2
Attila Fazekas22ef5732012-12-16 14:03:06 +0100256}
257
258if [ $MODE != "create" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000259 # looks like I can't ask for all project related to a specified user
260 openstack project list --long --quote none -f csv | grep ',True' | grep -v "${SKIP_PROJECT}" | while IFS=, read project_id project_name desc enabled; do
261 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 +0100262 if [ $MODE = one -a "$user_name" != "$USER_NAME" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -0400263 continue;
Attila Fazekas22ef5732012-12-16 14:03:06 +0100264 fi
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100265
266 # Checks for a specific password defined for an user.
Dean Troyer3324f192014-09-18 09:26:39 -0500267 # Example for an username johndoe: JOHNDOE_PASSWORD=1234
JordanP7c6d0052014-10-06 23:08:50 +0200268 # This mechanism is used by lib/swift
269 eval SPECIFIC_UPASSWORD="\$${user_name}_password"
Sahid Orentino Ferdjaoui1814e672014-02-11 17:56:07 +0100270 if [ -n "$SPECIFIC_UPASSWORD" ]; then
271 USER_PASS=$SPECIFIC_UPASSWORD
272 fi
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000273 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100274 done
275 done
276else
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000277 project_name=$PROJECT
278 project_id=$(create_or_get_project "$PROJECT")
Attila Fazekas22ef5732012-12-16 14:03:06 +0100279 user_name=$USER_NAME
280 user_id=`get_user_id $user_name`
281 if [ -z "$user_id" ]; then
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000282 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 -0500283 user_id=$id
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000284 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100285 else
Steve Martinellibce88992014-03-14 00:40:06 -0500286 role_id=$(create_or_get_role "$ROLE")
Jamie Lennox9d6d8f82015-06-22 03:37:59 +0000287 openstack role add "$role_id" --user "$user_id" --project "$project_id"
288 add_entry "$user_id" "$user_name" "$project_id" "$project_name" "$USER_PASS"
Attila Fazekas22ef5732012-12-16 14:03:06 +0100289 fi
290fi