blob: 7a70cc41c8e0998a12039a346f4ba4fef6eea40a [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)
Attila Fazekas91b8d132013-01-06 22:40:09 +010010# ``STACK_USER``
Dean Troyerd81a0272012-08-31 18:04:55 -050011
Dean Troyerd81a0272012-08-31 18:04:55 -050012# ``stack.sh`` calls the entry points in this order:
13#
14# install_keystone
15# configure_keystone
16# init_keystone
17# start_keystone
Dean Troyerd835de82012-11-29 17:11:35 -060018# create_keystone_accounts
Dean Troyerd81a0272012-08-31 18:04:55 -050019# stop_keystone
20# cleanup_keystone
21
Dean Troyer7903b792012-09-13 17:16:12 -050022# Save trace setting
23XTRACE=$(set +o | grep xtrace)
24set +o xtrace
Dean Troyerd81a0272012-08-31 18:04:55 -050025
26
27# Defaults
28# --------
29
Dean Troyerd81a0272012-08-31 18:04:55 -050030# Set up default directories
31KEYSTONE_DIR=$DEST/keystone
32KEYSTONE_CONF_DIR=${KEYSTONE_CONF_DIR:-/etc/keystone}
33KEYSTONE_CONF=$KEYSTONE_CONF_DIR/keystone.conf
Dean Troyerbc071bc2012-10-01 14:06:44 -050034KEYSTONE_AUTH_CACHE_DIR=${KEYSTONE_AUTH_CACHE_DIR:-/var/cache/keystone}
Dean Troyerd81a0272012-08-31 18:04:55 -050035
36KEYSTONECLIENT_DIR=$DEST/python-keystoneclient
37
Dean Troyerbc071bc2012-10-01 14:06:44 -050038# Select the backend for Keystone's service catalog
Dean Troyerb80379c2012-09-10 18:30:37 -050039KEYSTONE_CATALOG_BACKEND=${KEYSTONE_CATALOG_BACKEND:-sql}
Dean Troyerd81a0272012-08-31 18:04:55 -050040KEYSTONE_CATALOG=$KEYSTONE_CONF_DIR/default_catalog.templates
41
Dean Troyerbc071bc2012-10-01 14:06:44 -050042# Select Keystone's token format
43# Choose from 'UUID' and 'PKI'
44KEYSTONE_TOKEN_FORMAT=${KEYSTONE_TOKEN_FORMAT:-PKI}
45
Dean Troyerd81a0272012-08-31 18:04:55 -050046# Set Keystone interface configuration
Dean Troyerd81a0272012-08-31 18:04:55 -050047KEYSTONE_AUTH_HOST=${KEYSTONE_AUTH_HOST:-$SERVICE_HOST}
48KEYSTONE_AUTH_PORT=${KEYSTONE_AUTH_PORT:-35357}
Dean Troyerc83a7e12012-11-29 11:47:58 -060049KEYSTONE_AUTH_PORT_INT=${KEYSTONE_AUTH_PORT_INT:-35358}
50KEYSTONE_AUTH_PROTOCOL=${KEYSTONE_AUTH_PROTOCOL:-$SERVICE_PROTOCOL}
51
52# Public facing bits
Dean Troyerd81a0272012-08-31 18:04:55 -050053KEYSTONE_SERVICE_HOST=${KEYSTONE_SERVICE_HOST:-$SERVICE_HOST}
54KEYSTONE_SERVICE_PORT=${KEYSTONE_SERVICE_PORT:-5000}
Dean Troyerc83a7e12012-11-29 11:47:58 -060055KEYSTONE_SERVICE_PORT_INT=${KEYSTONE_SERVICE_PORT_INT:-5001}
56KEYSTONE_SERVICE_PROTOCOL=${KEYSTONE_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
Dean Troyerd81a0272012-08-31 18:04:55 -050057
58
59# Entry Points
60# ------------
61
62# cleanup_keystone() - Remove residual data files, anything left over from previous
63# runs that a clean run would need to clean up
64function cleanup_keystone() {
65 # kill instances (nova)
66 # delete image files (glance)
67 # This function intentionally left blank
68 :
69}
70
71# configure_keystoneclient() - Set config files, create data dirs, etc
72function configure_keystoneclient() {
73 setup_develop $KEYSTONECLIENT_DIR
74}
75
76# configure_keystone() - Set config files, create data dirs, etc
77function configure_keystone() {
78 setup_develop $KEYSTONE_DIR
79
80 if [[ ! -d $KEYSTONE_CONF_DIR ]]; then
81 sudo mkdir -p $KEYSTONE_CONF_DIR
Dean Troyerd81a0272012-08-31 18:04:55 -050082 fi
Attila Fazekas91b8d132013-01-06 22:40:09 +010083 sudo chown $STACK_USER $KEYSTONE_CONF_DIR
Dean Troyerd81a0272012-08-31 18:04:55 -050084
85 if [[ "$KEYSTONE_CONF_DIR" != "$KEYSTONE_DIR/etc" ]]; then
86 cp -p $KEYSTONE_DIR/etc/keystone.conf.sample $KEYSTONE_CONF
87 cp -p $KEYSTONE_DIR/etc/policy.json $KEYSTONE_CONF_DIR
88 fi
89
90 # Rewrite stock ``keystone.conf``
Terry Wilson428af5a2012-11-01 16:12:39 -040091 local dburl
92 database_connection_url dburl keystone
Dean Troyerc83a7e12012-11-29 11:47:58 -060093
94 if is_service_enabled tls-proxy; then
95 # Set the service ports for a proxy to take the originals
96 iniset $KEYSTONE_CONF DEFAULT public_port $KEYSTONE_SERVICE_PORT_INT
97 iniset $KEYSTONE_CONF DEFAULT admin_port $KEYSTONE_AUTH_PORT_INT
98 fi
99
Dean Troyerd81a0272012-08-31 18:04:55 -0500100 iniset $KEYSTONE_CONF DEFAULT admin_token "$SERVICE_TOKEN"
Doug Hellmann4de55e92012-10-26 12:24:28 -0400101 iniset $KEYSTONE_CONF signing token_format "$KEYSTONE_TOKEN_FORMAT"
Terry Wilson428af5a2012-11-01 16:12:39 -0400102 iniset $KEYSTONE_CONF sql connection $dburl
Dean Troyerd81a0272012-08-31 18:04:55 -0500103 iniset $KEYSTONE_CONF ec2 driver "keystone.contrib.ec2.backends.sql.Ec2"
104 sed -e "
105 /^pipeline.*ec2_extension crud_/s|ec2_extension crud_extension|ec2_extension s3_extension crud_extension|;
106 " -i $KEYSTONE_CONF
107
108 # Append the S3 bits
109 iniset $KEYSTONE_CONF filter:s3_extension paste.filter_factory "keystone.contrib.s3:S3Extension.factory"
110
111 if [[ "$KEYSTONE_CATALOG_BACKEND" = "sql" ]]; then
112 # Configure ``keystone.conf`` to use sql
113 iniset $KEYSTONE_CONF catalog driver keystone.catalog.backends.sql.Catalog
114 inicomment $KEYSTONE_CONF catalog template_file
115 else
116 cp -p $FILES/default_catalog.templates $KEYSTONE_CATALOG
117
118 # Add swift endpoints to service catalog if swift is enabled
119 if is_service_enabled swift; then
120 echo "catalog.RegionOne.object_store.publicURL = http://%SERVICE_HOST%:8080/v1/AUTH_\$(tenant_id)s" >> $KEYSTONE_CATALOG
121 echo "catalog.RegionOne.object_store.adminURL = http://%SERVICE_HOST%:8080/" >> $KEYSTONE_CATALOG
122 echo "catalog.RegionOne.object_store.internalURL = http://%SERVICE_HOST%:8080/v1/AUTH_\$(tenant_id)s" >> $KEYSTONE_CATALOG
123 echo "catalog.RegionOne.object_store.name = Swift Service" >> $KEYSTONE_CATALOG
124 fi
125
126 # Add quantum endpoints to service catalog if quantum is enabled
127 if is_service_enabled quantum; then
128 echo "catalog.RegionOne.network.publicURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
129 echo "catalog.RegionOne.network.adminURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
130 echo "catalog.RegionOne.network.internalURL = http://%SERVICE_HOST%:$Q_PORT/" >> $KEYSTONE_CATALOG
131 echo "catalog.RegionOne.network.name = Quantum Service" >> $KEYSTONE_CATALOG
132 fi
133
Vincent Untz7e86dbe2012-12-13 08:50:37 +0100134 sed -e "
Dean Troyerd81a0272012-08-31 18:04:55 -0500135 s,%SERVICE_HOST%,$SERVICE_HOST,g;
136 s,%S3_SERVICE_PORT%,$S3_SERVICE_PORT,g;
137 " -i $KEYSTONE_CATALOG
138
139 # Configure ``keystone.conf`` to use templates
140 iniset $KEYSTONE_CONF catalog driver "keystone.catalog.backends.templated.TemplatedCatalog"
141 iniset $KEYSTONE_CONF catalog template_file "$KEYSTONE_CATALOG"
142 fi
143
144 # Set up logging
145 LOGGING_ROOT="devel"
146 if [ "$SYSLOG" != "False" ]; then
147 LOGGING_ROOT="$LOGGING_ROOT,production"
148 fi
149 KEYSTONE_LOG_CONFIG="--log-config $KEYSTONE_CONF_DIR/logging.conf"
150 cp $KEYSTONE_DIR/etc/logging.conf.sample $KEYSTONE_CONF_DIR/logging.conf
151 iniset $KEYSTONE_CONF_DIR/logging.conf logger_root level "DEBUG"
152 iniset $KEYSTONE_CONF_DIR/logging.conf logger_root handlers "devel,production"
153
154}
155
Dean Troyerd835de82012-11-29 17:11:35 -0600156# create_keystone_accounts() - Sets up common required keystone accounts
157
158# Tenant User Roles
159# ------------------------------------------------------------------
160# service -- --
161# -- -- Member
162# admin admin admin
163# demo admin admin
164# demo demo Member, anotherrole
165# invisible_to_admin demo Member
166
167# Migrated from keystone_data.sh
168create_keystone_accounts() {
169
170 # admin
171 ADMIN_TENANT=$(keystone tenant-create \
172 --name admin \
173 | grep " id " | get_field 2)
174 ADMIN_USER=$(keystone user-create \
175 --name admin \
176 --pass "$ADMIN_PASSWORD" \
177 --email admin@example.com \
178 | grep " id " | get_field 2)
179 ADMIN_ROLE=$(keystone role-create \
180 --name admin \
181 | grep " id " | get_field 2)
182 keystone user-role-add \
183 --user_id $ADMIN_USER \
184 --role_id $ADMIN_ROLE \
185 --tenant_id $ADMIN_TENANT
186
187 # service
188 SERVICE_TENANT=$(keystone tenant-create \
189 --name $SERVICE_TENANT_NAME \
190 | grep " id " | get_field 2)
191
192 # The Member role is used by Horizon and Swift so we need to keep it:
193 MEMBER_ROLE=$(keystone role-create --name=Member | grep " id " | get_field 2)
194 # ANOTHER_ROLE demonstrates that an arbitrary role may be created and used
195 # TODO(sleepsonthefloor): show how this can be used for rbac in the future!
196 ANOTHER_ROLE=$(keystone role-create --name=anotherrole | grep " id " | get_field 2)
197
198 # invisible tenant - admin can't see this one
199 INVIS_TENANT=$(keystone tenant-create --name=invisible_to_admin | grep " id " | get_field 2)
200
201 # demo
202 DEMO_TENANT=$(keystone tenant-create \
203 --name=demo \
204 | grep " id " | get_field 2)
205 DEMO_USER=$(keystone user-create \
206 --name demo \
207 --pass "$ADMIN_PASSWORD" \
208 --email demo@example.com \
209 | grep " id " | get_field 2)
210 keystone user-role-add --user_id $DEMO_USER --role_id $MEMBER_ROLE --tenant_id $DEMO_TENANT
211 keystone user-role-add --user_id $ADMIN_USER --role_id $ADMIN_ROLE --tenant_id $DEMO_TENANT
212 keystone user-role-add --user_id $DEMO_USER --role_id $ANOTHER_ROLE --tenant_id $DEMO_TENANT
213 keystone user-role-add --user_id $DEMO_USER --role_id $MEMBER_ROLE --tenant_id $INVIS_TENANT
214
215 # Keystone
216 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
217 KEYSTONE_SERVICE=$(keystone service-create \
218 --name keystone \
219 --type identity \
220 --description "Keystone Identity Service" \
221 | grep " id " | get_field 2)
222 keystone endpoint-create \
223 --region RegionOne \
224 --service_id $KEYSTONE_SERVICE \
Dean Troyerc83a7e12012-11-29 11:47:58 -0600225 --publicurl "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/v2.0" \
226 --adminurl "$KEYSTONE_AUTH_PROTOCOL://$KEYSTONE_AUTH_HOST:$KEYSTONE_AUTH_PORT/v2.0" \
227 --internalurl "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/v2.0"
Dean Troyerd835de82012-11-29 17:11:35 -0600228 fi
229
230 # TODO(dtroyer): This is part of a series of changes...remove these when
231 # complete if they are really unused
232# KEYSTONEADMIN_ROLE=$(keystone role-create \
233# --name KeystoneAdmin \
234# | grep " id " | get_field 2)
235# KEYSTONESERVICE_ROLE=$(keystone role-create \
236# --name KeystoneServiceAdmin \
237# | grep " id " | get_field 2)
238
239 # TODO(termie): these two might be dubious
240# keystone user-role-add \
241# --user_id $ADMIN_USER \
242# --role_id $KEYSTONEADMIN_ROLE \
243# --tenant_id $ADMIN_TENANT
244# keystone user-role-add \
245# --user_id $ADMIN_USER \
246# --role_id $KEYSTONESERVICE_ROLE \
247# --tenant_id $ADMIN_TENANT
248}
249
Dean Troyerd81a0272012-08-31 18:04:55 -0500250# init_keystone() - Initialize databases, etc.
251function init_keystone() {
252 # (Re)create keystone database
Terry Wilson428af5a2012-11-01 16:12:39 -0400253 recreate_database keystone utf8
Dean Troyerd81a0272012-08-31 18:04:55 -0500254
255 # Initialize keystone database
256 $KEYSTONE_DIR/bin/keystone-manage db_sync
257
Dean Troyerbc071bc2012-10-01 14:06:44 -0500258 if [[ "$KEYSTONE_TOKEN_FORMAT" == "PKI" ]]; then
259 # Set up certificates
Vishvananda Ishaya23431f32012-12-12 15:57:33 -0800260 rm -rf $KEYSTONE_CONF_DIR/ssl
Dean Troyerbc071bc2012-10-01 14:06:44 -0500261 $KEYSTONE_DIR/bin/keystone-manage pki_setup
262
263 # Create cache dir
264 sudo mkdir -p $KEYSTONE_AUTH_CACHE_DIR
Attila Fazekas91b8d132013-01-06 22:40:09 +0100265 sudo chown $STACK_USER $KEYSTONE_AUTH_CACHE_DIR
Vishvananda Ishaya23431f32012-12-12 15:57:33 -0800266 rm -f $KEYSTONE_AUTH_CACHE_DIR/*
Dean Troyerbc071bc2012-10-01 14:06:44 -0500267 fi
Dean Troyerd81a0272012-08-31 18:04:55 -0500268}
269
270# install_keystoneclient() - Collect source and prepare
271function install_keystoneclient() {
272 git_clone $KEYSTONECLIENT_REPO $KEYSTONECLIENT_DIR $KEYSTONECLIENT_BRANCH
273}
274
275# install_keystone() - Collect source and prepare
276function install_keystone() {
277 git_clone $KEYSTONE_REPO $KEYSTONE_DIR $KEYSTONE_BRANCH
278}
279
280# start_keystone() - Start running processes, including screen
281function start_keystone() {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600282 # Get right service port for testing
283 local service_port=$KEYSTONE_SERVICE_PORT
284 if is_service_enabled tls-proxy; then
285 service_port=$KEYSTONE_SERVICE_PORT_INT
286 fi
287
Dean Troyerd81a0272012-08-31 18:04:55 -0500288 # Start Keystone in a screen window
289 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 -0600290 echo "Waiting for keystone to start..."
Dean Troyerc83a7e12012-11-29 11:47:58 -0600291 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 -0600292 echo "keystone did not start"
293 exit 1
294 fi
Dean Troyerc83a7e12012-11-29 11:47:58 -0600295
296 # Start proxies if enabled
297 if is_service_enabled tls-proxy; then
298 start_tls_proxy '*' $KEYSTONE_SERVICE_PORT $KEYSTONE_SERVICE_HOST $KEYSTONE_SERVICE_PORT_INT &
299 start_tls_proxy '*' $KEYSTONE_AUTH_PORT $KEYSTONE_AUTH_HOST $KEYSTONE_AUTH_PORT_INT &
300 fi
Dean Troyerd81a0272012-08-31 18:04:55 -0500301}
302
303# stop_keystone() - Stop running processes
304function stop_keystone() {
305 # Kill the Keystone screen window
306 screen -S $SCREEN_NAME -p key -X kill
307}
Dean Troyer7903b792012-09-13 17:16:12 -0500308
309# Restore xtrace
310$XTRACE