Skip to content

Commit 514e57b

Browse files
cleaning code
1 parent 00dc78e commit 514e57b

File tree

8 files changed

+38
-45
lines changed

8 files changed

+38
-45
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
@APICommand(name = "deleteUserKeys", description = "Deletes a keypair from a user", responseObject = SuccessResponse.class,
3232
since = "4.23.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
3333
public class DeleteUserKeysCmd extends BaseAsyncCmd {
34-
3534
@ACL
3635
@Parameter(name = ApiConstants.KEYPAIR_ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, required = true, description = "ID of the keypair to be deleted.")
3736
private Long id;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@
3636
import java.util.List;
3737

3838
@APICommand(name = "listUserKeyRules",
39-
description = "This command allows the user to query the rules defined for a API access keypair.",
39+
description = "Lists the rules defined for a API key pair.",
4040
responseObject = BaseRolePermissionResponse.class,
4141
requestHasSensitiveInfo = false,
4242
responseHasSensitiveInfo = false,
4343
since = "4.23.0")
44-
4544
public class ListUserKeyRulesCmd extends BaseListDomainResourcesCmd {
46-
4745
@ACL
48-
@Parameter(name=ApiConstants.KEYPAIR_ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, description = "ID of the keypair.", required = true)
46+
@Parameter(name = ApiConstants.KEYPAIR_ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, description = "ID of the key pair.", required = true)
4947
private Long id;
5048

5149
public Long getId() {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@
3838
responseHasSensitiveInfo = true,
3939
authorized = {RoleType.User, RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin},
4040
since = "4.23.0")
41-
4241
public class ListUserKeysCmd extends BaseListDomainResourcesCmd {
43-
4442
@ACL
4543
@Parameter(name = ApiConstants.USER_ID, type = CommandType.UUID, entityType = UserResponse.class, description = "ID of the user that owns the keys.")
4644
private Long userId;
4745

4846
@ACL
49-
@Parameter(name = ApiConstants.KEYPAIR_ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, description = "ID of the keypair.")
47+
@Parameter(name = ApiConstants.KEYPAIR_ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, description = "ID of the key pair.")
5048
private Long keyPairId;
5149

5250
@ACL
53-
@Parameter(name = ApiConstants.API_KEY_FILTER, type = CommandType.STRING, description = "API Key of the keypair.")
51+
@Parameter(name = ApiConstants.API_KEY_FILTER, type = CommandType.STRING, description = "API key of the key pair.")
5452
private String apiKeyFilter;
5553

5654
@Parameter(name = ApiConstants.SHOW_PERMISSIONS, type = CommandType.BOOLEAN, description = "Whether API Key rules should be returned.")
@@ -67,6 +65,7 @@ public Long getKeyId() {
6765
public String getApiKeyFilter() {
6866
return apiKeyFilter;
6967
}
68+
7069
public Boolean getShowPermissions() {
7170
return showPermissions;
7271
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.cloudstack.api.command.admin.user;
1818

1919
import com.cloud.event.EventTypes;
20+
import com.cloud.exception.InvalidParameterValueException;
2021
import com.cloud.user.Account;
2122
import com.cloud.user.User;
2223
import org.apache.cloudstack.acl.Rule;
@@ -41,29 +42,31 @@
4142

4243
@APICommand(name = "registerUserKeys",
4344
responseObject = ApiKeyPairResponse.class,
44-
description = "This command allows a user to register for the developer API, returning a secret key and an API key. This request is made through the integration API port, so it is a privileged command and must be made on behalf of a user. It is up to the implementer just how the username and password are entered, and then how that translates to an integration API request. Both secret key and API key should be returned to the user",
45+
description = "Registers an API key pair (API and secret keys) for a user.",
4546
requestHasSensitiveInfo = false, responseHasSensitiveInfo = true)
4647
public class RegisterUserKeysCmd extends BaseAsyncCmd {
47-
48-
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = UserResponse.class, required = true, description = "User ID.")
48+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = UserResponse.class, required = true, description = "ID of the user.")
4949
private Long id;
5050

51-
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "API keypair name.")
51+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "API key pair name.")
5252
private String name;
5353

54-
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "API keypair description.")
54+
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "API key pair description.")
5555
private String description;
5656

57-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "Start date of the API keypair. " +
57+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "Start date of the API key pair. " +
5858
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
5959
private Date startDate;
6060

61-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "Expiration date of the API keypair. " +
61+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "Expiration date of the API key pair. " +
6262
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
6363
private Date endDate;
6464

65-
@Parameter(name = ApiConstants.RULES, type = CommandType.MAP, description = "Rules param list, lower indexed rules take precedence over higher. If no rules are informed, " +
66-
"defaults to allowing all account permissions. Example input: rules[0].rule=* rules[0].permission=allow")
65+
@Parameter(name = ApiConstants.RULES, type = CommandType.MAP, description = "The rules of the API key pair. If no rules are informed, " +
66+
"defaults to allowing all account permissions. Otherwise, only the explicitly informed permissions for the key pair will be " +
67+
"considered. Lower indexed rules take precedence over higher. Thus, in the following example: " +
68+
"\"rules[0].rule=deleteUserKeys rules[0].permission=deny rules[1].rule=*UserKeys* rules[1].permission=allow\", all rules matching " +
69+
"the expression \"*UserKeys*\" will be allowed, except for \"deleteUserKeys\".")
6770
private Map rules;
6871

6972
public void setUserId(Long userId) {
@@ -126,8 +129,8 @@ public List<Map<String, Object>> getRules() {
126129

127130
String permission = detail.get(ApiConstants.PERMISSION);
128131
if (StringUtils.isEmpty(permission)) {
129-
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Rule %s has no permission associated with it," +
130-
" please specify if it is either allow or deny.", rule));
132+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Rule [%s] has no permission associated with it," +
133+
" please specify if it is either [allow] or [deny].", rule));
131134
}
132135
ruleDetails.put(ApiConstants.PERMISSION, roleService.getRolePermission(permission));
133136

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class UpdateUserCmd extends BaseCmd {
4646
//////////////// API parameters /////////////////////
4747
/////////////////////////////////////////////////////
4848

49-
@Parameter(name = ApiConstants.USER_API_KEY, type = CommandType.STRING, description = "The API key for the user. Must be specified with usersecretkey")
49+
@Parameter(name = ApiConstants.USER_API_KEY, type = CommandType.STRING, description = "Updates the latest API key of the user. Must be specified with usersecretkey")
5050
private String userApiKey;
5151

5252
@Parameter(name = ApiConstants.EMAIL, type = CommandType.STRING, description = "Email")
@@ -70,7 +70,7 @@ public class UpdateUserCmd extends BaseCmd {
7070
@Parameter(name = ApiConstants.CURRENT_PASSWORD, type = CommandType.STRING, description = "Current password that was being used by the user. You must inform the current password when updating the password.", acceptedOnAdminPort = false)
7171
private String currentPassword;
7272

73-
@Parameter(name = ApiConstants.USER_SECRET_KEY, type = CommandType.STRING, description = "The secret key for the user. Must be specified with userapikey.")
73+
@Parameter(name = ApiConstants.USER_SECRET_KEY, type = CommandType.STRING, description = "Updates the latest secret key of the user. Must be specified with userapikey.")
7474
private String userSecretKey;
7575

7676
@Parameter(name = ApiConstants.API_KEY_ACCESS, type = CommandType.STRING, description = "Determines if Api key access for this user is enabled, disabled or inherits the value from its parent, the owning account", since = "4.20.1.0", authorized = {RoleType.Admin})

engine/schema/src/main/java/org/apache/cloudstack/acl/dao/ApiKeyPairDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Pair<List<ApiKeyPairVO>, Integer> listByUserIdsPaginated(List<Long> userI
8181

8282
Pair<List<ApiKeyPairVO>, Integer> apiKeyPairVOList = searchAndCount(sc, searchFilter);
8383
if (CollectionUtils.isEmpty(apiKeyPairVOList.first())) {
84-
return new Pair(List.of(), 0);
84+
return new Pair<>(List.of(), 0);
8585
}
8686
return apiKeyPairVOList;
8787
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,6 @@ public List<Long> searchForAccessibleUsers() {
861861
try {
862862
accountMgr.checkCallerRoleTypeAllowedForUserOrAccountOperations(accountVO, userVO);
863863
} catch (PermissionDeniedException exception) {
864-
logger.debug(exception.getMessage());
865864
return false;
866865
}
867866
return allowedRolesId.contains(userAccount.getAccountRoleId());

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,7 +3170,6 @@ public Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd) {
31703170
verifyCallerPrivilegeForUserOrAccountOperations(user);
31713171

31723172
String accessingApiKey = getAccessingApiKey(cmd);
3173-
31743173
ApiKeyPair keyPair;
31753174
if (accessingApiKey != null) {
31763175
ApiKeyPair accessingKeyPair = apiKeyPairService.findByApiKey(accessingApiKey);
@@ -3241,7 +3240,7 @@ private Integer fetchMultipleKeyPairs(List<ApiKeyPairResponse> responses, ListUs
32413240
users = cmd.listAll() && isAdmin(callerUser.getAccountId()) ? queryService.searchForAccessibleUsers() : List.of(callerUser.getId());
32423241
}
32433242

3244-
Pair<List<ApiKeyPairVO>, Integer> keyPairs = apiKeyPairDao.listByUserIdsPaginated(users, cmd);
3243+
Pair<List<ApiKeyPairVO>, Integer> keyPairs = apiKeyPairDao.listByUserIdsPaginated(users, cmd);
32453244
keyPairs.first().stream()
32463245
.filter(keyPair -> isAccessingKeypairSuperset(keyPair, cmd))
32473246
.forEach(keyPair -> {
@@ -3273,7 +3272,7 @@ private void validateKeyPairIsNotNull(ApiKeyPair keyPair) {
32733272

32743273
private void validateAccessingKeyPairPermissionsIsSupersetOfAccessedKeyPair(ApiKeyPair keyPair, BaseCmd cmd) {
32753274
if (!isAccessingKeypairSuperset(keyPair, cmd)) {
3276-
logger.info("Accessing API keypair has less permissions than accessed API keypair.");
3275+
logger.info("Accessing API key pair [{}] has less permissions than accessed API key pair.", keyPair.getId());
32773276
throw new PermissionDeniedException("Could not complete request.");
32783277
}
32793278
}
@@ -3345,7 +3344,7 @@ public void validateCallingUserHasAccessToDesiredUser(Long userId) {
33453344
public void deleteApiKey(ApiKeyPair keyPair) {
33463345
User user = _userDao.findByIdIncludingRemoved(keyPair.getUserId());
33473346
if (user == null) {
3348-
throw new InvalidParameterValueException("User associated to the key does not exist.");
3347+
throw new InvalidParameterValueException("User associated with the API key pair does not exist.");
33493348
}
33503349

33513350
if ((BaremetalUtils.BAREMETAL_SYSTEM_ACCOUNT_NAME.equals(user.getUsername()) || user.getId() == User.UID_SYSTEM)
@@ -3501,15 +3500,14 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
35013500
return keys;
35023501
}
35033502

3504-
/***
3505-
* Validates if the Keypair has at least one rule, then gets all account role permissions and calls a method that
3506-
* validates if the user permissions are a superset of permissions of the Keypair that is being created
3507-
* @param account is the user's account, from which the default permissions are pulled.
3508-
* @param newApiKeyPair is the new keypair being created
3509-
* @param rules are the rules passed to the API which are being validated, if no rules were passed, defaults to all
3510-
* account permissions
3511-
* @throws InvalidParameterValueException if the user's permissions are not a superset of the Keypair, or there are
3512-
* no rules associated with the Keypair
3503+
/**
3504+
* Persists the API key pair and its corresponding permissions. Verifies whether
3505+
* the key pair being created is a superset of its owner's permissions.
3506+
* @param account Account owner of the key pair.
3507+
* @param newApiKeyPair The key pair object to be persisted.
3508+
* @param rules The set of rules of the key pair.
3509+
* @param cmd The API's command.
3510+
* @return The persisted key pair object.
35133511
*/
35143512
@DB
35153513
private ApiKeyPairVO validateAndPersistKeyPairAndPermissions(Account account, ApiKeyPairVO newApiKeyPair,
@@ -3528,8 +3526,8 @@ private ApiKeyPairVO validateAndPersistKeyPairAndPermissions(Account account, Ap
35283526
}
35293527

35303528
if (!isApiKeySupersetOfPermission(allPermissions, permissions)) {
3531-
throw new InvalidParameterValueException(String.format("The keypair being created has a bigger set of permissions than the account [%s] that owns it. This is " +
3532-
"not allowed.", account.getUuid()));
3529+
throw new InvalidParameterValueException(String.format("The key pair being created has a bigger set of permissions than the account [%s] " +
3530+
"that owns it. This is not allowed.", account.getUuid()));
35333531
}
35343532

35353533
ApiKeyPairVO savedApiKeyPair = apiKeyPairDao.persist(newApiKeyPair);
@@ -3541,18 +3539,15 @@ private ApiKeyPairVO validateAndPersistKeyPairAndPermissions(Account account, Ap
35413539
return savedApiKeyPair;
35423540
}
35433541

3544-
/**
3545-
* Gets all API keypair permissions for the given apiKey
3546-
*/
35473542
@Override
35483543
public List<RolePermissionEntity> getAllKeypairPermissions(String apiKey) {
35493544
if (apiKey == null) {
3550-
throw new InvalidParameterValueException("API key not present in URL, cannot fetch API key rules.");
3545+
throw new InvalidParameterValueException("API key not present in the request's URL and, thus, unable to fetch API key rules.");
35513546
}
35523547
ApiKeyPair apiKeyPair = keyPairManager.findByApiKey(apiKey);
35533548
Account account = _accountDao.findById(apiKeyPair.getAccountId());
3554-
List<ApiKeyPairPermission> allApiKeyRolePermissions = keyPairManager.findAllPermissionsByKeyPairId(apiKeyPair.getId(), account.getRoleId());
3555-
return new ArrayList<>(allApiKeyRolePermissions);
3549+
List<ApiKeyPairPermission> keyPairPermissions = keyPairManager.findAllPermissionsByKeyPairId(apiKeyPair.getId(), account.getRoleId());
3550+
return new ArrayList<>(keyPairPermissions);
35563551
}
35573552

35583553
private String createUserApiKey(long userId, ApiKeyPairVO newApiKeyPair) {

0 commit comments

Comments
 (0)