Skip to content

Commit 352e9c3

Browse files
author
Nicole Schmidt
committed
Fix accessible typo and execute keypair migration through SQL only
1 parent 08773bf commit 352e9c3

File tree

8 files changed

+17
-56
lines changed

8 files changed

+17
-56
lines changed

api/src/main/java/org/apache/cloudstack/api/command/admin/user/RegisterUserKeysCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public List<Map<String, Object>> getRules() {
144144
@Override
145145
public long getEntityOwnerId() {
146146
User user = _entityMgr.findById(User.class, getUserId());
147-
List<Long> accessableUsers = _queryService.searchForAccessableUsers();
148-
if (user != null && accessableUsers.stream().anyMatch(u -> u == user.getId())) {
147+
List<Long> accessibleUsers = _queryService.searchForAccessibleUsers();
148+
if (user != null && accessibleUsers.stream().anyMatch(u -> u == user.getId())) {
149149
return user.getAccountId();
150150
}
151151
return Account.ACCOUNT_ID_SYSTEM;

api/src/main/java/org/apache/cloudstack/query/QueryService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public interface QueryService {
135135

136136
ListResponse<UserResponse> searchForUsers(Long domainId, boolean recursive) throws PermissionDeniedException;
137137

138-
List<Long> searchForAccessableUsers();
138+
List<Long> searchForAccessibleUsers();
139139

140140
ListResponse<EventResponse> searchForEvents(ListEventsCmd cmd);
141141

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42010to42100.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
import com.cloud.upgrade.SystemVmTemplateRegistration;
2020
import com.cloud.utils.db.TransactionLegacy;
2121
import com.cloud.utils.exception.CloudRuntimeException;
22-
import java.sql.Date;
2322
import java.sql.PreparedStatement;
24-
import java.sql.ResultSet;
2523
import java.sql.SQLException;
26-
import java.time.LocalDate;
27-
import java.util.UUID;
2824
import java.io.InputStream;
2925
import java.sql.Connection;
3026
import java.util.List;
@@ -60,52 +56,9 @@ public InputStream[] getPrepareScripts() {
6056
return new InputStream[] {script};
6157
}
6258

63-
protected void performKeyPairMigration(Connection conn) throws SQLException {
64-
try {
65-
logger.debug("Performing keypair migration from user table to api_keypair table.");
66-
PreparedStatement pstmt = conn.prepareStatement("SELECT u.id, u.api_key, u.secret_key, a.domain_id, u.id FROM `cloud`.`user` AS u JOIN `cloud`.`account` AS a " +
67-
"ON u.account_id = a.id WHERE u.api_key IS NOT NULL AND u.secret_key IS NOT NULL");
68-
ResultSet resultSet = pstmt.executeQuery();
69-
70-
while (resultSet.next()) {
71-
long id = resultSet.getLong(1);
72-
String apiKey = resultSet.getString(2);
73-
String secretKey = resultSet.getString(3);
74-
Long domainId = resultSet.getLong(4);
75-
Long accountId = resultSet.getLong(5);
76-
Date timestamp = Date.valueOf(LocalDate.now());
77-
78-
PreparedStatement preparedStatement = conn.prepareStatement("INSERT IGNORE INTO `cloud`.`api_keypair` (uuid, user_id, domain_id, account_id, api_key, secret_key, created, name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
79-
String uuid = UUID.randomUUID().toString();
80-
preparedStatement.setString(1, uuid);
81-
preparedStatement.setLong(2, id);
82-
preparedStatement.setLong(3, domainId);
83-
preparedStatement.setLong(4, accountId);
84-
85-
preparedStatement.setString(5, apiKey);
86-
preparedStatement.setString(6, secretKey);
87-
preparedStatement.setDate(7, timestamp);
88-
preparedStatement.setString(8, uuid);
89-
90-
preparedStatement.executeUpdate();
91-
}
92-
pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`user` DROP COLUMN api_key, DROP COLUMN secret_key;");
93-
pstmt.executeUpdate();
94-
logger.info("Successfully performed keypair migration.");
95-
} catch (SQLException ex) {
96-
logger.info("Unexpected exception in user keypair migration", ex);
97-
throw ex;
98-
}
99-
}
100-
10159
@Override
10260
public void performDataMigration(Connection conn) {
10361
migrateConfigurationScopeToBitmask(conn);
104-
try {
105-
performKeyPairMigration(conn);
106-
} catch (SQLException e) {
107-
throw new RuntimeException(e);
108-
}
10962
}
11063

11164
@Override

engine/schema/src/main/resources/META-INF/db/schema-42010to42100.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ CREATE TABLE IF NOT EXISTS `cloud`.`api_keypair_permissions` (
7171
PRIMARY KEY (`id`),
7272
CONSTRAINT `fk_keypair_permissions__api_keypair_id` FOREIGN KEY(`api_keypair_id`) REFERENCES `cloud`.`api_keypair`(`id`)
7373
);
74+
75+
INSERT INTO `cloud`.`api_keypair` (uuid, user_id, domain_id, account_id, api_key, secret_key, created, name)
76+
SELECT uuid(), user.id, account.domain_id, account.id, user.api_key, user.secret_key, now(), 'Active key pair'
77+
FROM `cloud`.`user` AS user
78+
JOIN `cloud`.`account` AS account ON user.account_id = account.id
79+
WHERE user.api_key IS NOT NULL
80+
AND user.secret_key IS NOT NULL;
81+
82+
ALTER TABLE `cloud`.`user` DROP COLUMN IF EXISTS api_key, DROP COLUMN IF EXISTS secret_key;

engine/schema/src/test/java/com/cloud/upgrade/dao/Upgrade42010to42100Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void testPerformDataMigration() throws SQLException {
4747
when(dbUpgradeUtils.getTableColumnType(conn, "configuration", "scope")).thenReturn("varchar(255)");
4848

4949
try (MockedStatic<TransactionLegacy> ignored2 = Mockito.mockStatic(TransactionLegacy.class)) {
50-
Mockito.doNothing().when(upgrade).performKeyPairMigration(conn);
5150
TransactionLegacy txn = Mockito.mock(TransactionLegacy.class);
5251
when(TransactionLegacy.currentTxn()).thenReturn(txn);
5352
PreparedStatement pstmt = Mockito.mock(PreparedStatement.class);

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ private Pair<List<UserAccountJoinVO>, Integer> getUserListInternal(Account calle
857857
}
858858

859859
@Override
860-
public List<Long> searchForAccessableUsers() {
860+
public List<Long> searchForAccessibleUsers() {
861861
List<Long> permittedAccounts = new ArrayList<>();
862862
Account callingAccount = CallContext.current().getCallingAccount();
863863
Filter searchFilter = new Filter(UserAccountJoinVO.class, "id", true);

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,7 +3016,7 @@ private Integer fetchMultipleKeypairs(List<ApiKeyPairResponse> responses, ListUs
30163016
users = List.of(cmd.getUserId());
30173017
} else {
30183018
User callerUser = CallContext.current().getCallingUser();
3019-
users = cmd.listAll() && isAdmin(callerUser.getAccountId()) ? queryService.searchForAccessableUsers() : List.of(callerUser.getId());
3019+
users = cmd.listAll() && isAdmin(callerUser.getAccountId()) ? queryService.searchForAccessibleUsers() : List.of(callerUser.getId());
30203020
}
30213021

30223022
Pair<List<ApiKeyPairVO>, Integer> keyPairs = apiKeyPairDao.listByUserIdsPaginated(users, cmd);
@@ -3119,7 +3119,7 @@ public void validateCallingUserHasAccessToDesiredUser(Long userId) {
31193119
if (!isAdmin(callerUser.getAccountId()) && callerUser.getId() != userId) {
31203120
throw new PermissionDeniedException("Only admins can operate on API keys owned by other users");
31213121
}
3122-
List<Long> accessibleUsers = queryService.searchForAccessableUsers();
3122+
List<Long> accessibleUsers = queryService.searchForAccessibleUsers();
31233123
User desiredUser = _userDao.getUser(userId);
31243124
if (accessibleUsers.stream().noneMatch(u -> Objects.equals(u, userId))) {
31253125
throw new PermissionDeniedException(String.format("Could not perform operation because calling user has less permissions " +

server/src/main/java/org/apache/cloudstack/acl/ApiKeyPairManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public ApiKeyPair findById(Long id) {
7373

7474
@Override
7575
public void validateCallingUserHasAccessToDesiredUser(Long userId) {
76-
List<Long> accessableUsers = queryService.searchForAccessableUsers();
76+
List<Long> accessibleUsers = queryService.searchForAccessibleUsers();
7777
User desiredUser = userDao.getUser(userId);
78-
if (accessableUsers.stream().noneMatch(u -> Objects.equals(u, userId))) {
78+
if (accessibleUsers.stream().noneMatch(u -> Objects.equals(u, userId))) {
7979
throw new InvalidParameterValueException(String.format("Could not perform operation because calling user has less permissions " +
8080
"than the informed user [%s].", desiredUser.getId()));
8181
}

0 commit comments

Comments
 (0)