2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.openstack.utils;
24 import java.io.Serializable;
25 import java.util.Calendar;
26 import java.util.HashMap;
29 import java.util.Optional;
30 import org.openecomp.mso.cloud.CloudIdentity;
31 import org.openecomp.mso.cloud.CloudSite;
32 import org.openecomp.mso.logger.MsoAlarmLogger;
33 import org.openecomp.mso.logger.MsoLogger;
34 import org.openecomp.mso.logger.MessageEnum;
35 import org.openecomp.mso.openstack.beans.MsoTenant;
36 import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
37 import org.openecomp.mso.openstack.exceptions.MsoCloudSiteNotFound;
38 import org.openecomp.mso.openstack.exceptions.MsoException;
39 import org.openecomp.mso.openstack.exceptions.MsoOpenstackException;
40 import org.openecomp.mso.openstack.exceptions.MsoTenantAlreadyExists;
41 import com.woorea.openstack.base.client.OpenStackBaseException;
42 import com.woorea.openstack.base.client.OpenStackConnectException;
43 import com.woorea.openstack.base.client.OpenStackRequest;
44 import com.woorea.openstack.base.client.OpenStackResponseException;
45 import com.woorea.openstack.keystone.Keystone;
46 import com.woorea.openstack.keystone.model.Access;
47 import com.woorea.openstack.keystone.model.Metadata;
48 import com.woorea.openstack.keystone.model.Role;
49 import com.woorea.openstack.keystone.model.Roles;
50 import com.woorea.openstack.keystone.model.Tenant;
51 import com.woorea.openstack.keystone.model.User;
52 import com.woorea.openstack.keystone.utils.KeystoneUtils;
53 import com.woorea.openstack.keystone.model.Authentication;
55 public class MsoKeystoneUtils extends MsoTenantUtils {
57 // Cache the Keystone Clients statically. Since there is just one MSO user, there is no
58 // benefit to re-authentication on every request (or across different flows). The
59 // token will be used until it expires.
61 // The cache key is "cloudId"
62 private static Map <String, KeystoneCacheEntry> adminClientCache = new HashMap<>();
64 private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
67 public MsoKeystoneUtils (String msoPropID) {
69 this.msoPropID = msoPropID;
70 LOGGER.debug("MsoKeyStoneUtils:" + msoPropID);
74 * Create a tenant with the specified name in the given cloud. If the tenant already exists,
75 * an Exception will be thrown. The MSO User will also be added to the "member" list of
76 * the new tenant to perform subsequent Nova/Heat commands in the tenant. If the MSO User
77 * association fails, the entire transaction will be rolled back.
79 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin
80 * requests go to the centralized identity service in DCP. However, if some artifact
81 * must exist in each local LCP instance as well, then it will be needed to access the
85 * @param tenantName The tenant name to create
86 * @param cloudSiteId The cloud identifier (may be a region) in which to create the tenant.
87 * @return the tenant ID of the newly created tenant
88 * @throws MsoTenantAlreadyExists Thrown if the requested tenant already exists
89 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
91 public String createTenant (String tenantName,
93 Map <String, String> metadata,
94 boolean backout) throws MsoException {
95 // Obtain the cloud site information where we will create the tenant
96 Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
97 if (!cloudSiteOpt.isPresent()) {
98 LOGGER.error(MessageEnum.RA_CREATE_TENANT_ERR, "MSOCloudSite not found", "", "", MsoLogger.ErrorCode.DataError, "MSOCloudSite not found");
99 throw new MsoCloudSiteNotFound (cloudSiteId);
101 Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSiteOpt.get());
102 Tenant tenant = null;
104 // Check if the tenant already exists
105 tenant = findTenantByName (keystoneAdminClient, tenantName);
107 if (tenant != null) {
108 // Tenant already exists. Throw an exception
109 LOGGER.error(MessageEnum.RA_TENANT_ALREADY_EXIST, tenantName, cloudSiteId, "", "", MsoLogger.ErrorCode.DataError, "Tenant already exists");
110 throw new MsoTenantAlreadyExists (tenantName, cloudSiteId);
113 // Does not exist, create a new one
114 tenant = new Tenant ();
115 tenant.setName (tenantName);
116 tenant.setDescription ("SDN Tenant (via MSO)");
117 tenant.setEnabled (true);
119 OpenStackRequest <Tenant> request = keystoneAdminClient.tenants ().create (tenant);
120 tenant = executeAndRecordOpenstackRequest (request, msoProps);
121 } catch (OpenStackBaseException e) {
122 // Convert Keystone OpenStackResponseException to MsoOpenstackException
123 throw keystoneErrorToMsoException (e, "CreateTenant");
124 } catch (RuntimeException e) {
126 throw runtimeExceptionToMsoException (e, "CreateTenant");
129 // Add MSO User to the tenant as a member and
130 // apply tenant metadata if supported by the cloud site
132 CloudIdentity cloudIdentity = cloudSiteOpt.get().getIdentityService ();
134 User msoUser = findUserByNameOrId (keystoneAdminClient, cloudIdentity.getMsoId ());
135 Role memberRole = findRoleByNameOrId (keystoneAdminClient, cloudIdentity.getMemberRole ());
137 OpenStackRequest <Void> request = keystoneAdminClient.tenants ().addUser (tenant.getId (),
139 memberRole.getId ());
140 executeAndRecordOpenstackRequest (request, msoProps);
142 if (cloudIdentity.hasTenantMetadata () && metadata != null && !metadata.isEmpty ()) {
143 Metadata tenantMetadata = new Metadata ();
144 tenantMetadata.setMetadata (metadata);
146 OpenStackRequest <Metadata> metaRequest = keystoneAdminClient.tenants ()
147 .createOrUpdateMetadata (tenant.getId (),
149 executeAndRecordOpenstackRequest (metaRequest, msoProps);
151 } catch (Exception e) {
152 // Failed to attach MSO User to the new tenant. Can't operate without access,
153 // so roll back the tenant.
156 LOGGER.warn(MessageEnum.RA_CREATE_TENANT_ERR, "Create Tenant errored, Tenant deletion suppressed", "Openstack", "", MsoLogger.ErrorCode.DataError, "Create Tenant error, Tenant deletion suppressed");
161 OpenStackRequest <Void> request = keystoneAdminClient.tenants ().delete (tenant.getId ());
162 executeAndRecordOpenstackRequest (request, msoProps);
163 } catch (Exception e2) {
164 // Just log this one. We will report the original exception.
165 LOGGER.error (MessageEnum.RA_CREATE_TENANT_ERR, "Nested exception rolling back tenant", "Openstack", "", MsoLogger.ErrorCode.DataError, "Create Tenant error, Nested exception rolling back tenant", e2);
170 // Propagate the original exception on user/role/tenant mapping
171 if (e instanceof OpenStackBaseException) {
172 // Convert Keystone Exception to MsoOpenstackException
173 throw keystoneErrorToMsoException ((OpenStackBaseException) e, "CreateTenantUser");
175 MsoAdapterException me = new MsoAdapterException (e.getMessage (), e);
176 me.addContext ("CreateTenantUser");
180 return tenant.getId ();
184 * Query for a tenant by ID in the given cloud. If the tenant exists,
185 * return an MsoTenant object. If not, return null.
187 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin
188 * requests go to the centralized identity service in DCP. However, if some artifact
189 * must exist in each local LCP instance as well, then it will be needed to access the
193 * @param tenantId The Openstack ID of the tenant to query
194 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
195 * @return the tenant properties of the queried tenant, or null if not found
196 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
198 public MsoTenant queryTenant (String tenantId, String cloudSiteId) throws MsoException {
199 // Obtain the cloud site information where we will query the tenant
200 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
201 () -> new MsoCloudSiteNotFound(cloudSiteId));
203 Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
205 // Check if the tenant exists and return its Tenant Id
207 Tenant tenant = findTenantById (keystoneAdminClient, tenantId);
208 if (tenant == null) {
212 Map <String, String> metadata = new HashMap<>();
213 if (cloudSite.getIdentityService ().hasTenantMetadata ()) {
214 OpenStackRequest <Metadata> request = keystoneAdminClient.tenants ().showMetadata (tenant.getId ());
215 Metadata tenantMetadata = executeAndRecordOpenstackRequest (request, msoProps);
216 if (tenantMetadata != null) {
217 metadata = tenantMetadata.getMetadata ();
220 return new MsoTenant (tenant.getId (), tenant.getName (), metadata);
221 } catch (OpenStackBaseException e) {
222 // Convert Keystone OpenStackResponseException to MsoOpenstackException
223 throw keystoneErrorToMsoException (e, "QueryTenant");
224 } catch (RuntimeException e) {
226 throw runtimeExceptionToMsoException (e, "QueryTenant");
231 * Query for a tenant with the specified name in the given cloud. If the tenant exists,
232 * return an MsoTenant object. If not, return null. This query is useful if the client
233 * knows it has the tenant name, skipping an initial lookup by ID that would always fail.
235 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin
236 * requests go to the centralized identity service in DCP. However, if some artifact
237 * must exist in each local LCP instance as well, then it will be needed to access the
241 * @param tenantName The name of the tenant to query
242 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
243 * @return the tenant properties of the queried tenant, or null if not found
244 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
246 public MsoTenant queryTenantByName (String tenantName, String cloudSiteId) throws MsoException {
247 // Obtain the cloud site information where we will query the tenant
248 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
249 () -> new MsoCloudSiteNotFound(cloudSiteId));
250 Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
253 Tenant tenant = findTenantByName (keystoneAdminClient, tenantName);
254 if (tenant == null) {
258 Map <String, String> metadata = new HashMap<>();
259 if (cloudSite.getIdentityService ().hasTenantMetadata ()) {
260 OpenStackRequest <Metadata> request = keystoneAdminClient.tenants ().showMetadata (tenant.getId ());
261 Metadata tenantMetadata = executeAndRecordOpenstackRequest (request, msoProps);
262 if (tenantMetadata != null) {
263 metadata = tenantMetadata.getMetadata ();
266 return new MsoTenant (tenant.getId (), tenant.getName (), metadata);
267 } catch (OpenStackBaseException e) {
268 // Convert Keystone OpenStackResponseException to MsoOpenstackException
269 throw keystoneErrorToMsoException (e, "QueryTenantName");
270 } catch (RuntimeException e) {
272 throw runtimeExceptionToMsoException (e, "QueryTenantName");
277 * Delete the specified Tenant (by ID) in the given cloud. This method returns true or
278 * false, depending on whether the tenant existed and was successfully deleted, or if
279 * the tenant already did not exist. Both cases are treated as success (no Exceptions).
281 * Note for the AIC Cloud (DCP/LCP): all admin requests go to the centralized identity
282 * service in DCP. So deleting a tenant from one cloudSiteId will remove it from all
283 * sites managed by that identity service.
286 * @param tenantId The Openstack ID of the tenant to delete
287 * @param cloudSiteId The cloud identifier from which to delete the tenant.
288 * @return true if the tenant was deleted, false if the tenant did not exist.
289 * @throws MsoOpenstackException If the Openstack API call returns an exception.
291 public boolean deleteTenant (String tenantId, String cloudSiteId) throws MsoException {
292 // Obtain the cloud site information where we will query the tenant
293 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
294 () -> new MsoCloudSiteNotFound(cloudSiteId));
295 Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
298 // Check that the tenant exists. Also, need the ID to delete
299 Tenant tenant = findTenantById (keystoneAdminClient, tenantId);
300 if (tenant == null) {
301 LOGGER.error(MessageEnum.RA_TENANT_NOT_FOUND, tenantId, cloudSiteId, "", "", MsoLogger.ErrorCode.DataError, "Tenant not found");
305 OpenStackRequest <Void> request = keystoneAdminClient.tenants ().delete (tenant.getId ());
306 executeAndRecordOpenstackRequest (request, msoProps);
307 LOGGER.debug ("Deleted Tenant " + tenant.getId () + " (" + tenant.getName () + ")");
309 // Clear any cached clients. Not really needed, ID will not be reused.
310 MsoHeatUtils.expireHeatClient (tenant.getId (), cloudSiteId);
311 MsoNeutronUtils.expireNeutronClient (tenant.getId (), cloudSiteId);
312 } catch (OpenStackBaseException e) {
313 // Convert Keystone OpenStackResponseException to MsoOpenstackException
314 throw keystoneErrorToMsoException (e, "Delete Tenant");
315 } catch (RuntimeException e) {
317 throw runtimeExceptionToMsoException (e, "DeleteTenant");
323 // -------------------------------------------------------------------
324 // PRIVATE UTILITY FUNCTIONS FOR USE WITHIN THIS CLASS
327 * Get a Keystone Admin client for the Openstack Identity service.
328 * This requires an 'admin'-level userId + password along with an 'admin' tenant
329 * in the target cloud. These values will be retrieved from properties based
330 * on the specified cloud ID.
332 * On successful authentication, the Keystone object will be cached for the cloudId
333 * so that it can be reused without going back to Openstack every time.
337 * @return an authenticated Keystone object
339 public Keystone getKeystoneAdminClient (CloudSite cloudSite) throws MsoException {
340 CloudIdentity cloudIdentity = cloudSite.getIdentityService ();
342 String cloudId = cloudIdentity.getId ();
343 String adminTenantName = cloudIdentity.getAdminTenant ();
344 String region = cloudSite.getRegionId ();
346 // Check first in the cache of previously authorized clients
347 KeystoneCacheEntry entry = adminClientCache.get (cloudId);
349 if (!entry.isExpired ()) {
350 return entry.getKeystoneClient ();
352 // Token is expired. Remove it from cache.
353 adminClientCache.remove (cloudId);
357 Keystone keystone = new Keystone (cloudIdentity.getKeystoneUrl (region, msoPropID));
359 // Must authenticate against the 'admin' tenant to get the services endpoints
360 Access access = null;
363 Authentication credentials = cloudIdentity.getAuthentication ();
364 OpenStackRequest <Access> request = keystone.tokens ()
365 .authenticate (credentials)
366 .withTenantName (adminTenantName);
367 access = executeAndRecordOpenstackRequest (request, msoProps);
368 token = access.getToken ().getId ();
369 } catch (OpenStackResponseException e) {
370 if (e.getStatus () == 401) {
371 // Authentication error. Can't access admin tenant - something is mis-configured
372 String error = "MSO Authentication Failed for " + cloudIdentity.getId ();
373 alarmLogger.sendAlarm ("MsoAuthenticationError", MsoAlarmLogger.CRITICAL, error);
374 throw new MsoAdapterException (error);
376 throw keystoneErrorToMsoException (e, "TokenAuth");
378 } catch (OpenStackConnectException e) {
379 // Connection to Openstack failed
380 throw keystoneErrorToMsoException (e, "TokenAuth");
383 // Get the Identity service URL. Throws runtime exception if not found per region.
384 String adminUrl = null;
386 // TODO: FOR TESTING!!!!
387 adminUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "identity", region, "public");
388 adminUrl = adminUrl.replaceFirst("5000", "35357");
389 } catch (RuntimeException e) {
390 String error = "Identity service not found: region=" + region + ",cloud=" + cloudIdentity.getId ();
391 alarmLogger.sendAlarm ("MsoConfigurationError", MsoAlarmLogger.CRITICAL, error);
392 LOGGER.error(MessageEnum.IDENTITY_SERVICE_NOT_FOUND, region, cloudIdentity.getId(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in findEndpointURL");
393 throw new MsoAdapterException (error, e);
396 // A new Keystone object is required for the new URL. Use the auth token from above.
397 // Note: this doesn't go back to Openstack, it's just a local object.
398 keystone = new Keystone (adminUrl);
399 keystone.token (token);
401 // Cache to avoid re-authentication for every call.
402 KeystoneCacheEntry cacheEntry = new KeystoneCacheEntry (adminUrl, token, access.getToken ().getExpires ());
403 adminClientCache.put (cloudId, cacheEntry);
409 * Find a tenant (or query its existance) by its Id.
411 * @param adminClient an authenticated Keystone object
413 * @param tenantName the tenant ID to query
415 * @return a Tenant object or null if not found
417 private Tenant findTenantById (Keystone adminClient, String tenantId) {
418 if (tenantId == null) {
423 OpenStackRequest <Tenant> request = adminClient.tenants ().show (tenantId);
424 return executeAndRecordOpenstackRequest (request, msoProps);
425 } catch (OpenStackResponseException e) {
426 if (e.getStatus () == 404) {
429 LOGGER.error(MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Error, GET Tenant by Id (" + tenantId + "): " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in Openstack GET tenant by Id");
436 * Find a tenant (or query its existance) by its Name. This method avoids an
437 * initial lookup by ID when it's known that we have the tenant Name.
439 * @param adminClient an authenticated Keystone object
441 * @param tenantName the tenant name to query
443 * @return a Tenant object or null if not found
445 public Tenant findTenantByName (Keystone adminClient, String tenantName) {
446 if (tenantName == null) {
451 OpenStackRequest <Tenant> request = adminClient.tenants ().show ("").queryParam ("name", tenantName);
452 return executeAndRecordOpenstackRequest (request, msoProps);
453 } catch (OpenStackResponseException e) {
454 if (e.getStatus () == 404) {
457 LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Error, GET Tenant By Name (" + tenantName + "): " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in Openstack GET Tenant By Name");
464 * Look up an Openstack User by Name or Openstack ID. Check the ID first, and if that
465 * fails, try the Name.
467 * @param adminClient an authenticated Keystone object
469 * @param userName the user name or ID to query
471 * @return a User object or null if not found
473 private User findUserByNameOrId (Keystone adminClient, String userNameOrId) {
474 if (userNameOrId == null) {
479 OpenStackRequest <User> request = adminClient.users ().show (userNameOrId);
480 return executeAndRecordOpenstackRequest (request, msoProps);
481 } catch (OpenStackResponseException e) {
482 if (e.getStatus () == 404) {
483 // Not found by ID. Search for name
484 return findUserByName (adminClient, userNameOrId);
486 LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Error, GET User (" + userNameOrId + "): " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in Openstack GET User");
493 * Look up an Openstack User by Name. This avoids initial Openstack query by ID
494 * if we know we have the User Name.
496 * @param adminClient an authenticated Keystone object
498 * @param userName the user name to query
500 * @return a User object or null if not found
502 public User findUserByName (Keystone adminClient, String userName) {
503 if (userName == null) {
508 OpenStackRequest <User> request = adminClient.users ().show ("").queryParam ("name", userName);
509 return executeAndRecordOpenstackRequest (request, msoProps);
510 } catch (OpenStackResponseException e) {
511 if (e.getStatus () == 404) {
514 LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Error, GET User By Name (" + userName + "): " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception in Openstack GET User By Name");
521 * Look up an Openstack Role by Name or Id. There is no direct query for Roles, so
522 * need to retrieve a full list from Openstack and look for a match. By default,
523 * Openstack should have a "_member_" role for normal VM-level privileges and an
524 * "admin" role for expanded privileges (e.g. administer tenants, users, and roles).
527 * @param adminClient an authenticated Keystone object
529 * @param roleNameOrId the Role name or ID to look up
531 * @return a Role object
533 private Role findRoleByNameOrId (Keystone adminClient, String roleNameOrId) {
534 if (roleNameOrId == null) {
538 // Search by name or ID. Must search in list
539 OpenStackRequest <Roles> request = adminClient.roles ().list ();
540 Roles roles = executeAndRecordOpenstackRequest (request, msoProps);
542 for (Role role : roles) {
543 if (roleNameOrId.equals (role.getName ()) || roleNameOrId.equals (role.getId ())) {
551 private static class KeystoneCacheEntry implements Serializable {
553 private static final long serialVersionUID = 1L;
555 private String keystoneUrl;
556 private String token;
557 private Calendar expires;
559 public KeystoneCacheEntry (String url, String token, Calendar expires) {
560 this.keystoneUrl = url;
562 this.expires = expires;
565 public Keystone getKeystoneClient () {
566 Keystone keystone = new Keystone (keystoneUrl);
567 keystone.token (token);
571 public boolean isExpired () {
572 return expires == null || System.currentTimeMillis() > expires.getTimeInMillis();
578 * Clean up the Admin client cache to remove expired entries.
580 public static void adminCacheCleanup () {
581 for (String cacheKey : adminClientCache.keySet ()) {
582 if (adminClientCache.get (cacheKey).isExpired ()) {
583 adminClientCache.remove (cacheKey);
584 LOGGER.debug ("Cleaned Up Cached Admin Client for " + cacheKey);
590 * Reset the Admin client cache.
591 * This may be useful if cached credentials get out of sync.
593 public static void adminCacheReset () {
594 adminClientCache = new HashMap<>();
598 public String getKeystoneUrl(String regionId, String msoPropID, CloudIdentity cloudIdentity) {
599 return cloudIdentity.getIdentityUrl();