Skip to content

Commit 1c85af3

Browse files
author
Prachi Damle
committed
A production/QA Setup does not populate the admin and SYSTEM accounts during database setup. So IAM plugin needs to insert the necessary group <-> account map in the DB during startup
1 parent 748c090 commit 1c85af3

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

‎services/iam/plugin/src/org/apache/cloudstack/iam/IAMApiServiceImpl.java‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// under the License.
1717
package org.apache.cloudstack.iam;
1818

19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
1922
import java.util.ArrayList;
2023
import java.util.Collections;
2124
import java.util.HashMap;
@@ -111,6 +114,11 @@
111114
import com.cloud.utils.component.ManagerBase;
112115
import com.cloud.utils.db.DB;
113116
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;
114122
import com.cloud.vm.InstanceGroupVO;
115123
import com.cloud.vm.VMInstanceVO;
116124
import com.cloud.vm.dao.NicIpAliasVO;
@@ -190,6 +198,11 @@ public class IAMApiServiceImpl extends ManagerBase implements IAMApiService, Man
190198

191199
@Override
192200
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+
193206
_messageBus.subscribe(AccountManager.MESSAGE_ADD_ACCOUNT_EVENT, new MessageSubscriber() {
194207
@Override
195208
public void onPublishMessage(String senderAddress, String subject, Object obj) {
@@ -338,6 +351,88 @@ public void onPublishMessage(String senderAddress, String subject, Object obj) {
338351
return super.configure(name, params);
339352
}
340353

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+
341436
private void addDomainWideResourceAccess(Map<String, Object> params) {
342437

343438
IAMEntityType entityType = (IAMEntityType)params.get(ApiConstants.ENTITY_TYPE);

0 commit comments

Comments
 (0)