|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.iam; |
18 | 18 |
|
| 19 | +import java.sql.PreparedStatement; |
| 20 | +import java.sql.ResultSet; |
| 21 | +import java.sql.SQLException; |
19 | 22 | import java.util.ArrayList; |
20 | 23 | import java.util.Collections; |
21 | 24 | import java.util.HashMap; |
|
111 | 114 | import com.cloud.utils.component.ManagerBase; |
112 | 115 | import com.cloud.utils.db.DB; |
113 | 116 | import com.cloud.utils.db.EntityManager; |
| 117 | +import com.cloud.utils.db.Transaction; |
| 118 | +import com.cloud.utils.db.TransactionCallbackNoReturn; |
| 119 | +import com.cloud.utils.db.TransactionLegacy; |
| 120 | +import com.cloud.utils.db.TransactionStatus; |
| 121 | +import com.cloud.utils.exception.CloudRuntimeException; |
114 | 122 | import com.cloud.vm.InstanceGroupVO; |
115 | 123 | import com.cloud.vm.VMInstanceVO; |
116 | 124 | import com.cloud.vm.dao.NicIpAliasVO; |
@@ -190,6 +198,11 @@ public class IAMApiServiceImpl extends ManagerBase implements IAMApiService, Man |
190 | 198 |
|
191 | 199 | @Override |
192 | 200 | public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException { |
| 201 | + |
| 202 | + // populate group <-> account association if not present for CS admin |
| 203 | + // and system accounts |
| 204 | + populateIAMGroupAdminAccountMap(); |
| 205 | + |
193 | 206 | _messageBus.subscribe(AccountManager.MESSAGE_ADD_ACCOUNT_EVENT, new MessageSubscriber() { |
194 | 207 | @Override |
195 | 208 | public void onPublishMessage(String senderAddress, String subject, Object obj) { |
@@ -338,6 +351,88 @@ public void onPublishMessage(String senderAddress, String subject, Object obj) { |
338 | 351 | return super.configure(name, params); |
339 | 352 | } |
340 | 353 |
|
| 354 | + private void populateIAMGroupAdminAccountMap() { |
| 355 | + |
| 356 | + Transaction.execute(new TransactionCallbackNoReturn() { |
| 357 | + @Override |
| 358 | + public void doInTransactionWithoutResult(TransactionStatus status) { |
| 359 | + TransactionLegacy txn = TransactionLegacy.currentTxn(); |
| 360 | + |
| 361 | + String searchQuery = "Select id from `cloud`.`iam_group_account_map` where account_id = ? and removed is null"; |
| 362 | + ResultSet rs = null; |
| 363 | + PreparedStatement acctQuery = null; |
| 364 | + PreparedStatement acctInsert = null; |
| 365 | + // find if the system account is present in the map |
| 366 | + try { |
| 367 | + acctQuery = txn.prepareAutoCloseStatement(searchQuery); |
| 368 | + acctQuery.setLong(1, Account.ACCOUNT_ID_SYSTEM); |
| 369 | + |
| 370 | + rs = acctQuery.executeQuery(); |
| 371 | + if (!rs.next()) { |
| 372 | + acctInsert = txn |
| 373 | + .prepareAutoCloseStatement("INSERT INTO `cloud`.`iam_group_account_map` (group_id, account_id, created) values(?, ?, Now())"); |
| 374 | + // insert entry in iam_group_account_map table |
| 375 | + acctInsert.setLong(1, Account.ACCOUNT_TYPE_ADMIN + 1); |
| 376 | + acctInsert.setLong(2, Account.ACCOUNT_ID_SYSTEM); |
| 377 | + acctInsert.executeUpdate(); |
| 378 | + } |
| 379 | + } catch (SQLException ex) { |
| 380 | + String msg = "Unable to populate iam_group_account_map for SYSTEM account." + ex.getMessage(); |
| 381 | + s_logger.error(msg); |
| 382 | + throw new CloudRuntimeException(msg, ex); |
| 383 | + } finally { |
| 384 | + try { |
| 385 | + if (acctInsert != null) { |
| 386 | + acctInsert.close(); |
| 387 | + } |
| 388 | + if (rs != null) { |
| 389 | + rs.close(); |
| 390 | + } |
| 391 | + if (acctQuery != null) { |
| 392 | + acctQuery.close(); |
| 393 | + } |
| 394 | + } catch (SQLException e) { |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + // find if the admin account is present in the map |
| 399 | + try { |
| 400 | + acctQuery = txn.prepareAutoCloseStatement(searchQuery); |
| 401 | + acctQuery.setLong(1, Account.ACCOUNT_ID_SYSTEM + 1); |
| 402 | + |
| 403 | + rs = acctQuery.executeQuery(); |
| 404 | + if (!rs.next()) { |
| 405 | + acctInsert = txn |
| 406 | + .prepareAutoCloseStatement("INSERT INTO `cloud`.`iam_group_account_map` (group_id, account_id, created) values(?, ?, Now())"); |
| 407 | + // insert entry in iam_group_account_map table |
| 408 | + acctInsert.setLong(1, Account.ACCOUNT_TYPE_ADMIN + 1); |
| 409 | + acctInsert.setLong(2, Account.ACCOUNT_ID_SYSTEM + 1); |
| 410 | + acctInsert.executeUpdate(); |
| 411 | + } |
| 412 | + } catch (SQLException ex) { |
| 413 | + String msg = "Unable to populate iam_group_account_map for Admin account." + ex.getMessage(); |
| 414 | + s_logger.error(msg); |
| 415 | + throw new CloudRuntimeException(msg, ex); |
| 416 | + } finally { |
| 417 | + try { |
| 418 | + if (acctInsert != null) { |
| 419 | + acctInsert.close(); |
| 420 | + } |
| 421 | + if (rs != null) { |
| 422 | + rs.close(); |
| 423 | + } |
| 424 | + if (acctQuery != null) { |
| 425 | + acctQuery.close(); |
| 426 | + } |
| 427 | + } catch (SQLException e) { |
| 428 | + } |
| 429 | + } |
| 430 | + |
| 431 | + } |
| 432 | + }); |
| 433 | + |
| 434 | + } |
| 435 | + |
341 | 436 | private void addDomainWideResourceAccess(Map<String, Object> params) { |
342 | 437 |
|
343 | 438 | IAMEntityType entityType = (IAMEntityType)params.get(ApiConstants.ENTITY_TYPE); |
|
0 commit comments