blob: 34f3372392015b72c58126501ae88f52be29074a [file] [log] [blame]
Dean Troyerd81a0272012-08-31 18:04:55 -05001# lib/keystone
2# Functions to control the configuration and operation of **Keystone**
3
4# Dependencies:
5# ``functions`` file
6# ``BASE_SQL_CONN``
Dean Troyerc83a7e12012-11-29 11:47:58 -06007# ``SERVICE_HOST``, ``SERVICE_PROTOCOL``
Dean Troyerd81a0272012-08-31 18:04:55 -05008# ``SERVICE_TOKEN``
9# ``S3_SERVICE_PORT`` (template backend only)
10
Dean Troyerd81a0272012-08-31 18:04:55 -050011# ``stack.sh`` calls the entry points in this order:
12#
13# install_keystone
14# configure_keystone
15# init_keystone
16# start_keystone
Dean Troyerd835de82012-11-29 17:11:35 -060017# create_keystone_accounts
Dean Troyerd81a0272012-08-31 18:04:55 -050018# stop_keystone
19# cleanup_keystone
20
Dean Troyer7903b792012-09-13 17:16:12 -050021# Save trace setting
22XTRACE=$(set +o | grep xtrace)
23set +o xtrace
Dean Troyerd81a0272012-08-31 18:04:55 -050024
25
26# Defaults
27# --------
28
Dean Troyerd81a0272012-08-31 18:04:55 -050029# Set up default directories
30KEYSTONE_DIR=$DEST/keystone
31KEYSTONE_CONF_DIR=${KEYSTONE_CONF_DIR:-/etc/keystone}
32KEYSTONE_CONF=$KEYSTONE_CONF_DIR/keystone.conf
Dean Troyerbc071bc2012-10-01 14:06:44 -050033KEYSTONE_AUTH_CACHE_DIR=${KEYSTONE_AUTH_CACHE_DIR:-/var/cache/keystone}
Dean Troyerd81a0272012-08-31 18:04:55 -050034
35KEYSTONECLIENT_DIR=$DEST/python-keystoneclient
36
Dean Troyerbc071bc2012-10-01 14:06:44 -050037# Select the backend for Keystone's service catalog
Dean Troyerb80379c2012-09-10 18:30:37 -050038KEYSTONE_CATALOG_BACKEND=${KEYSTONE_CATALOG_BACKEND:-sql}
Dean Troyerd81a0272012-08-31 18:04:55 -050039KEYSTONE_CATALOG=$KEYSTONE_CONF_DIR/default_catalog.templates
40
Dean Troyerbc071bc2012-10-01 14:06:44 -050041# Select Keystone's token format
42# Choose from 'UUID' and 'PKI'
43KEYSTONE_TOKEN_FORMAT=${KEYSTONE_TOKEN_FORMAT:-PKI}
44
Dean Troyerd81a0272012-08-31 18:04:55 -050045# Set Keystone interface configuration
Dean Troyerd81a0272012-08-31 18:04:55 -050046KEYSTONE_AUTH_HOST=${KEYSTONE_AUTH_HOST:-$SERVICE_HOST}
47KEYSTONE_AUTH_PORT=${KEYSTONE_AUTH_PORT:-35357}
Dean Troyerc83a7e12012-11-29 11:47:58 -060048KEYSTONE_AUTH_PORT_INT=${KEYSTONE_AUTH_PORT_INT:-35358}
49KEYSTONE_AUTH_PROTOCOL=${KEYSTONE_AUTH_PROTOCOL:-$SERVICE_PROTOCOL}
50
51# Public facing bits
Dean Troyerd81a0272012-08-31 18:04:55 -050052KEYSTONE_SERVICE_HOST=${KEYSTONE_SERVICE_HOST:-$SERVICE_HOST}
53KEYSTONE_SERVICE_PORT=${KEYSTONE_SERVICE_PORT:-5000}
Dean Troyerc83a7e12012-11-29 11:47:58 -060054KEYSTONE_SERVICE_PORT_INT=${KEYSTONE_SERVICE_PORT_INT:-5001}
55KEYSTONE_SERVICE_PROTOCOL=${KEYSTONE_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
Dean Troyerd81a0272012-08-31 18:04:55 -050056
57
58# Entry Points
59# ------------
60
61# cleanup_keystone() - Remove residual data files, anything left over from previous
62# runs that a clean run would need to clean up
63function cleanup_keystone() {
64 # kill instances (nova)
65 # delete image files (glance)
66 # This function intentionally left blank
67 :
68}
69
70# configure_keystoneclient() - Set config files, create data dirs, etc
71function configure_keystoneclient() {
72 setup_develop $KEYSTONECLIENT_DIR
73}
74
75# configure_keystone() - Set config files, create data dirs, etc
76function configure_keystone() {
77 setup_develop $KEYSTONE_DIR
78
79 if [[ ! -d $KEYSTONE_CONF_DIR ]]; then
80 sudo mkdir -p $KEYSTONE_CONF_DIR
Dean Troyerd81a0272012-08-31 18:04:55 -050081 fi
Vincent Untz90e10882012-12-13 08:47:06 +010082 sudo chown `whoami` $KEYSTONE_CONF_DIR
Dean Troyerd81a0272012-08-31 18:04:55 -050083
84 if [[ "$KEYSTONE_CONF_DIR" != "$KEYSTONE_DIR/etc" ]]; then
85 cp -p $KEYSTONE_DIR/etc/keystone.conf.sample $KEYSTONE_CONF
86 cp -p $KEYSTONE_DIR/etc/policy.json $KEYSTONE_CONF_DIR
87 fi
88
89 # Rewrite stock ``keystone.conf``
Terry Wilson428af5a2012-11-01 16:12:39 -040090 local dburl
91 database_connection_url dburl keystone
Dean Troyerc83a7e12012-11-29 11:47:58 -060092
93 if is_service_enabled tls-proxy; then
94 # Set the service ports for a proxy to take the originals
95 iniset $KEYSTONE_CONF DEFAULT public_port $KEYSTONE_SERVICE_PORT_INT
96 iniset $KEYSTONE_CONF DEFAULT admin_port $KEYSTONE_AUTH_PORT_INT
97 fi
98
Dean Troyerd81a0272012-08-31 18:04:55 -050099 iniset $KEYSTONE_CONF DEFAULT admin_token "$SERVICE_TOKEN"
Doug Hellmann4de55e92012-10-26 12:24:28 -0400100 iniset $KEYSTONE_CONF signing token_format "$KEYSTONE_TOKEN_FORMAT"
Terry Wilson428af5a2012-11-01 16:12:39 -0400101 iniset $KEYSTONE_CONF sql connection $dburl
Dean Troyerd81a0272012-08-31 18:04:55 -0500102 iniset $KEYSTONE_CONF ec2 driver "keystone.contrib.ec2.backends.sql.Ec2"
103 sed -e "
104 /^pipeline.*ec2_extension crud_/s|ec2_extension crud_extension|ec2_extension s3_extension crud_extension|;
105 " -i $KEYSTONE_CONF
106
107 # Append the S3 bits
108 iniset $KEYSTONE_CONF filter:s3_extension paste.filter_factory "keystone.contrib.s3:S3Extension.factory"
109
110 if [[ "$KEYSTONE_CATALOG_BACKEND" = "sql" ]]; then
111 # Configure ``keystone.conf`` to use sql
112 iniset $KEYSTONE_CONF catalog driver keystone.catalog.backends.sql.Catalog
113 inicomment $KEYSTONE_CONF catalog template_file
114 else
115 cp -p $FILES/default_catalog.templates $KEYSTONE_CATALOG
116
117 # Add swift endpoints to service catalog if swift is enabled
118 if is_service_enabled swift; then
119 echo "catalog.RegionOne.object_store.publicURL = http://%SERVICE_HOST%:8080/v1/AUTH_\$(tenant_id)s" >> $KEYSTONE_CATALOG
120 echo "catalog.RegionOne.object_store.adminURL = http://%SERVICE_HOST%:8080/" >> $KEYSTONE_CATALOG
121 echo "catalog.RegionOne.object_store.internalURL = http://%SERVICE_HOST%:8080/v1/AUTH_\$(tenant_id)s" >> $KEYSTONE_CATALOG
122 echo "catalog.RegionOne.object_store.name = Swift Service" >> $KEYSTONE_CATALOG
123 fi
124
125 # Add quantum endpoints to service catalog if quantum is enabled
126 if is_service_enabled quantum; then
127 echo "catalog.RegionOne.network.publicURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
128 echo "catalog.RegionOne.network.adminURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
129 echo "catalog.RegionOne.network.internalURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
130 echo "catalog.RegionOne.network.name = Quantum Service" >> $KEYSTONE_CATALOG
131 fi
132
Vincent Untz7e86dbe2012-12-13 08:50:37 +0100133 sed -e "
Dean Troyerd81a0272012-08-31 18:04:55 -0500134 s,%SERVICE_HOST%,$SERVICE_HOST,g;
135 s,%S3_SERVICE_PORT%,$S3_SERVICE_PORT,g;
136 " -i $KEYSTONE_CATALOG
137
138 # Configure ``keystone.conf`` to use templates
139 iniset $KEYSTONE_CONF catalog driver "keystone.catalog.backends.templated.TemplatedCatalog"
140 iniset $KEYSTONE_CONF catalog template_file "$KEYSTONE_CATALOG"
141 fi
142
143 # Set up logging
144 LOGGING_ROOT="devel"
145 if [ "$SYSLOG" != "False" ]; then
146 LOGGING_ROOT="$LOGGING_ROOT,production"
147 fi
148 KEYSTONE_LOG_CONFIG="--log-config $KEYSTONE_CONF_DIR/logging.conf"
149 cp $KEYSTONE_DIR/etc/logging.conf.sample $KEYSTONE_CONF_DIR/logging.conf
150 iniset $KEYSTONE_CONF_DIR/logging.conf logger_root level "DEBUG"
151 iniset $KEYSTONE_CONF_DIR/logging.conf logger_root handlers "devel,production"
152
153}
154
Dean Troyerd835de82012-11-29 17:11:35 -0600155# create_keystone_accounts() - Sets up common required keystone accounts
156
157# Tenant User Roles
158# ------------------------------------------------------------------
159# service -- --
160# -- -- Member
161# admin admin admin
162# demo admin admin
163# demo demo Member, anotherrole
164# invisible_to_admin demo Member
165
166# Migrated from keystone_data.sh
167create_keystone_accounts() {
168
169 # admin
170 ADMIN_TENANT=$(keystone tenant-create \
171 --name admin \
172 | grep " id " | get_field 2)
173 ADMIN_USER=$(keystone user-create \
174 --name admin \
175 --pass "$ADMIN_PASSWORD" \
176 --email admin@example.com \
177 | grep " id " | get_field 2)
178 ADMIN_ROLE=$(keystone role-create \
179 --name admin \
180 | grep " id " | get_field 2)
181 keystone user-role-add \
182 --user_id $ADMIN_USER \
183 --role_id $ADMIN_ROLE \
184 --tenant_id $ADMIN_TENANT
185
186 # service
187 SERVICE_TENANT=$(keystone tenant-create \
188 --name $SERVICE_TENANT_NAME \
189 | grep " id " | get_field 2)
190
191 # The Member role is used by Horizon and Swift so we need to keep it:
192 MEMBER_ROLE=$(keystone role-create --name=Member | grep " id " | get_field 2)
193 # ANOTHER_ROLE demonstrates that an arbitrary role may be created and used
194 # TODO(sleepsonthefloor): show how this can be used for rbac in the future!
195 ANOTHER_ROLE=$(keystone role-create --name=anotherrole | grep " id " | get_field 2)
196
197 # invisible tenant - admin can't see this one
198 INVIS_TENANT=$(keystone tenant-create --name=invisible_to_admin | grep " id " | get_field 2)
199
200 # demo
201 DEMO_TENANT=$(keystone tenant-create \
202 --name=demo \
203 | grep " id " | get_field 2)
204 DEMO_USER=$(keystone user-create \
205 --name demo \
206 --pass "$ADMIN_PASSWORD" \
207 --email demo@example.com \
208 | grep " id " | get_field 2)
209 keystone user-role-add --user_id $DEMO_USER --role_id $MEMBER_ROLE --tenant_id $DEMO_TENANT
210 keystone user-role-add --user_id $ADMIN_USER --role_id $ADMIN_ROLE --tenant_id $DEMO_TENANT
211 keystone user-role-add --user_id $DEMO_USER --role_id $ANOTHER_ROLE --tenant_id $DEMO_TENANT
212 keystone user-role-add --user_id $DEMO_USER --role_id $MEMBER_ROLE --tenant_id $INVIS_TENANT
213
214 # Keystone
215 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
216 KEYSTONE_SERVICE=$(keystone service-create \
217 --name keystone \
218 --type identity \
219 --description "Keystone Identity Service" \
220 | grep " id " | get_field 2)
221 keystone endpoint-create \
222 --region RegionOne \
223 --service_id $KEYSTONE_SERVICE \
Dean Troyerc83a7e12012-11-29 11:47:58 -0600224 --publicurl "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/v2.0" \
225 --adminurl "$KEYSTONE_AUTH_PROTOCOL://$KEYSTONE_AUTH_HOST:$KEYSTONE_AUTH_PORT/v2.0" \
226 --internalurl "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/v2.0"
Dean Troyerd835de82012-11-29 17:11:35 -0600227 fi
228
229 # TODO(dtroyer): This is part of a series of changes...remove these when
230 # complete if they are really unused
231# KEYSTONEADMIN_ROLE=$(keystone role-create \
232# --name KeystoneAdmin \
233# | grep " id " | get_field 2)
234# KEYSTONESERVICE_ROLE=$(keystone role-create \
235# --name KeystoneServiceAdmin \
236# | grep " id " | get_field 2)
237
238 # TODO(termie): these two might be dubious
239# keystone user-role-add \
240# --user_id $ADMIN_USER \
241# --role_id $KEYSTONEADMIN_ROLE \
242# --tenant_id $ADMIN_TENANT
243# keystone user-role-add \
244# --user_id $ADMIN_USER \
245# --role_id $KEYSTONESERVICE_ROLE \
246# --tenant_id $ADMIN_TENANT
247}
248
Dean Troyerd81a0272012-08-31 18:04:55 -0500249# init_keystone() - Initialize databases, etc.
250function init_keystone() {
251 # (Re)create keystone database
Terry Wilson428af5a2012-11-01 16:12:39 -0400252 recreate_database keystone utf8
Dean Troyerd81a0272012-08-31 18:04:55 -0500253
254 # Initialize keystone database
255 $KEYSTONE_DIR/bin/keystone-manage db_sync
256
Dean Troyerbc071bc2012-10-01 14:06:44 -0500257 if [[ "$KEYSTONE_TOKEN_FORMAT" == "PKI" ]]; then
258 # Set up certificates
Vishvananda Ishaya23431f32012-12-12 15:57:33 -0800259 rm -rf $KEYSTONE_CONF_DIR/ssl
Dean Troyerbc071bc2012-10-01 14:06:44 -0500260 $KEYSTONE_DIR/bin/keystone-manage pki_setup
261
262 # Create cache dir
263 sudo mkdir -p $KEYSTONE_AUTH_CACHE_DIR
264 sudo chown `whoami` $KEYSTONE_AUTH_CACHE_DIR
Vishvananda Ishaya23431f32012-12-12 15:57:33 -0800265 rm -f $KEYSTONE_AUTH_CACHE_DIR/*
Dean Troyerbc071bc2012-10-01 14:06:44 -0500266 fi
Dean Troyerd81a0272012-08-31 18:04:55 -0500267}
268
269# install_keystoneclient() - Collect source and prepare
270function install_keystoneclient() {
271 git_clone $KEYSTONECLIENT_REPO $KEYSTONECLIENT_DIR $KEYSTONECLIENT_BRANCH
272}
273
274# install_keystone() - Collect source and prepare
275function install_keystone() {
276 git_clone $KEYSTONE_REPO $KEYSTONE_DIR $KEYSTONE_BRANCH
277}
278
279# start_keystone() - Start running processes, including screen
280function start_keystone() {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600281 # Get right service port for testing
282 local service_port=$KEYSTONE_SERVICE_PORT
283 if is_service_enabled tls-proxy; then
284 service_port=$KEYSTONE_SERVICE_PORT_INT
285 fi
286
Dean Troyerd81a0272012-08-31 18:04:55 -0500287 # Start Keystone in a screen window
288 screen_it key "cd $KEYSTONE_DIR && $KEYSTONE_DIR/bin/keystone-all --config-file $KEYSTONE_CONF $KEYSTONE_LOG_CONFIG -d --debug"
Dean Troyerd835de82012-11-29 17:11:35 -0600289 echo "Waiting for keystone to start..."
Dean Troyerc83a7e12012-11-29 11:47:58 -0600290 if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= curl -s http://$SERVICE_HOST:$service_port/v2.0/ >/dev/null; do sleep 1; done"; then
Dean Troyerd835de82012-11-29 17:11:35 -0600291 echo "keystone did not start"
292 exit 1
293 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600294
295 # Start proxies if enabled
296 if is_service_enabled tls-proxy; then
297 start_tls_proxy '*' $KEYSTONE_SERVICE_PORT $KEYSTONE_SERVICE_HOST $KEYSTONE_SERVICE_PORT_INT &
298 start_tls_proxy '*' $KEYSTONE_AUTH_PORT $KEYSTONE_AUTH_HOST $KEYSTONE_AUTH_PORT_INT &
299 fi
Dean Troyerd81a0272012-08-31 18:04:55 -0500300}
301
302# stop_keystone() - Stop running processes
303function stop_keystone() {
304 # Kill the Keystone screen window
305 screen -S $SCREEN_NAME -p key -X kill
306}
Dean Troyer7903b792012-09-13 17:16:12 -0500307
308# Restore xtrace
309$XTRACE