@@ -3168,17 +3168,17 @@ public ListResponse<ApiKeyPairResponse> listKeys(ListUserKeysCmd cmd) {
31683168 List <ApiKeyPairResponse > responses = new ArrayList <>();
31693169
31703170 if (cmd .getKeyId () != null || cmd .getApiKeyFilter () != null ) {
3171- fetchOnlyOneKeypair (responses , cmd );
3171+ fetchOnlyOneKeyPair (responses , cmd );
31723172 finalResponse .setResponses (responses );
31733173 return finalResponse ;
31743174 }
31753175
3176- Integer total = fetchMultipleKeypairs (responses , cmd );
3176+ Integer total = fetchMultipleKeyPairs (responses , cmd );
31773177 finalResponse .setResponses (responses , total );
31783178 return finalResponse ;
31793179 }
31803180
3181- private void fetchOnlyOneKeypair (List <ApiKeyPairResponse > responses , ListUserKeysCmd cmd ) {
3181+ private void fetchOnlyOneKeyPair (List <ApiKeyPairResponse > responses , ListUserKeysCmd cmd ) {
31823182 ApiKeyPair keyPair ;
31833183 if (cmd .getKeyId () != null ) {
31843184 keyPair = _accountService .getKeyPairById (cmd .getKeyId ());
@@ -3188,14 +3188,13 @@ private void fetchOnlyOneKeypair(List<ApiKeyPairResponse> responses, ListUserKey
31883188
31893189 validateKeyPairIsNotNull (keyPair );
31903190 validateAccessingKeyPairPermissionsIsSupersetOfAccessedKeyPair (keyPair , cmd );
3191-
31923191 _accountService .validateCallingUserHasAccessToDesiredUser (keyPair .getUserId ());
3193- markExpiredKeysWithStateExpired (keyPair );
3192+ removeApiKeyPairIfExpired (keyPair );
31943193
31953194 addKeypairResponse (keyPair , responses , cmd );
31963195 }
31973196
3198- private Integer fetchMultipleKeypairs (List <ApiKeyPairResponse > responses , ListUserKeysCmd cmd ) {
3197+ private Integer fetchMultipleKeyPairs (List <ApiKeyPairResponse > responses , ListUserKeysCmd cmd ) {
31993198 List <Long > users ;
32003199 if (cmd .getUserId () != null ) {
32013200 _accountService .validateCallingUserHasAccessToDesiredUser (cmd .getUserId ());
@@ -3210,7 +3209,7 @@ private Integer fetchMultipleKeypairs(List<ApiKeyPairResponse> responses, ListUs
32103209 .filter (keyPair -> isAccessingKeypairSuperset (keyPair , cmd ))
32113210 .forEach (keyPair -> {
32123211 addKeypairResponse (keyPair , responses , cmd );
3213- markExpiredKeysWithStateExpired (keyPair );
3212+ removeApiKeyPairIfExpired (keyPair );
32143213 });
32153214
32163215 return keyPairs .second ();
@@ -3281,7 +3280,7 @@ private Boolean isApiKeySupersetOfPermission(List<RolePermissionEntity> baseKeyP
32813280 return roleService .roleHasPermission (apiNameToBaseKeyPermissions , comparedPermissions );
32823281 }
32833282
3284- private void markExpiredKeysWithStateExpired (ApiKeyPair apiKeyPair ) {
3283+ private void removeApiKeyPairIfExpired (ApiKeyPair apiKeyPair ) {
32853284 if (apiKeyPair .hasEndDatePassed ()) {
32863285 internalDeleteApiKey (apiKeyPair );
32873286 }
@@ -3406,7 +3405,7 @@ public ApiKeyPair createApiKeyAndSecretKey(RegisterUserKeysCmd cmd) {
34063405 Account caller = getCurrentCallingAccount ();
34073406 User user = _userDao .findById (cmd .getUserId ());
34083407 if (user == null ) {
3409- throw new InvalidParameterValueException (String .format ("Unable to find user by id : %d" , cmd .getUserId ()));
3408+ throw new InvalidParameterValueException (String .format ("Unable to find user by ID : %d" , cmd .getUserId ()));
34103409 }
34113410
34123411 final String name = cmd .getName ();
@@ -3415,32 +3414,29 @@ public ApiKeyPair createApiKeyAndSecretKey(RegisterUserKeysCmd cmd) {
34153414 final Date startDate = cmd .getStartDate ();
34163415 final Date endDate = cmd .getEndDate ();
34173416 final List <Map <String , Object >> rules = cmd .getRules ();
3418- final RegisterUserKeysCmd registerCmd = cmd ;
34193417
34203418 Account account = _accountDao .findById (user .getAccountId ());
34213419 checkAccess (caller , null , true , account );
34223420 verifyCallerPrivilegeForUserOrAccountOperations (user );
34233421
3424- // don't allow baremetal or system user
34253422 if (BaremetalUtils .BAREMETAL_SYSTEM_ACCOUNT_NAME .equals (user .getUsername ()) || user .getId () == User .UID_SYSTEM ) {
3426- throw new PermissionDeniedException (String .format ("User id : [%s] is system account, update is not allowed." , user .getId ()));
3423+ throw new PermissionDeniedException (String .format ("User ID : [%s] is a system account and, thus, the operation is not allowed." , user .getId ()));
34273424 }
34283425
34293426 Date now = DateTime .now ().toDate ();
34303427
34313428 if (endDate != null && endDate .compareTo (now ) <= 0 ) {
3432- throw new InvalidParameterValueException ("Keypair cannot be created with expired date, please input a date on the future." );
3429+ throw new InvalidParameterValueException ("Keypair cannot be created with expired date, please input a date in the future." );
34333430 }
34343431 if (ObjectUtils .allNotNull (startDate , endDate ) && startDate .compareTo (endDate ) > -1 ) {
34353432 throw new InvalidParameterValueException ("Please specify an end date that is after the start date." );
34363433 }
34373434
3438- // generate both an api key and a secret key, return the keypair to the user
34393435 final ApiKeyPairVO newApiKeyPair = new ApiKeyPairVO (name , userId , description , startDate , endDate , account );
34403436 return Transaction .execute ((TransactionCallback <ApiKeyPairVO >) status -> {
34413437 createUserApiKey (userId , newApiKeyPair );
34423438 createUserSecretKey (userId , newApiKeyPair );
3443- return validateAndPersistKeyPairAndPermissions (account , newApiKeyPair , rules , registerCmd );
3439+ return validateAndPersistKeyPairAndPermissions (account , newApiKeyPair , rules , cmd );
34443440 });
34453441 }
34463442
@@ -3486,28 +3482,22 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
34863482 @ DB
34873483 private ApiKeyPairVO validateAndPersistKeyPairAndPermissions (Account account , ApiKeyPairVO newApiKeyPair ,
34883484 List <Map <String , Object >> rules , RegisterUserKeysCmd cmd ) {
3489- // this is only used to determine if we should use api key permissions or account permissions to base our new key on
34903485 String accessingApiKey = getAccessingApiKey (cmd );
34913486 final Role accountRole = roleService .findRole (account .getRoleId ());
3487+ List <RolePermissionEntity > allPermissions = accessingApiKey == null ?
3488+ roleService .findAllRolePermissionsEntityBy (accountRole .getId (), true ) : getAllKeypairPermissions (accessingApiKey );
34923489
3493- List <RolePermissionEntity > allPermissions = accessingApiKey == null ? getAllAccountRolePermissions (accountRole ) : getAllKeypairPermissions (accessingApiKey );
3494- List <RolePermissionEntity > permissions ;
3495- if (CollectionUtils .isEmpty (rules )) {
3496- permissions = allPermissions .stream ()
3497- .map (permission -> new ApiKeyPairPermissionVO (0 , permission .getRule ().toString (), permission .getPermission (), permission .getDescription ()))
3498- .collect (Collectors .toList ());
3499- } else {
3500- permissions = new ArrayList <>();
3501- for (Map <String , Object > ruleDetail : rules ) {
3502- String rule = ruleDetail .get (ApiConstants .RULE ).toString ();
3503- RolePermission .Permission rulePermission = (RolePermission .Permission ) ruleDetail .get (ApiConstants .PERMISSION );
3504- String ruleDescription = (String ) ruleDetail .get (ApiConstants .DESCRIPTION );
3505- permissions .add (new ApiKeyPairPermissionVO (0 , rule , rulePermission , ruleDescription ));
3506- }
3507- if (!isApiKeySupersetOfPermission (allPermissions , permissions )) {
3508- throw new InvalidParameterValueException (String .format ("The keypair being created has a bigger set of permissions than the account [%s] that owns it. This is " +
3509- "not allowed." , account .getUuid ()));
3510- }
3490+ List <RolePermissionEntity > permissions = new ArrayList <>();
3491+ for (Map <String , Object > ruleDetail : rules ) {
3492+ String rule = ruleDetail .get (ApiConstants .RULE ).toString ();
3493+ RolePermission .Permission rulePermission = (RolePermission .Permission ) ruleDetail .get (ApiConstants .PERMISSION );
3494+ String ruleDescription = (String ) ruleDetail .get (ApiConstants .DESCRIPTION );
3495+ permissions .add (new ApiKeyPairPermissionVO (0 , rule , rulePermission , ruleDescription ));
3496+ }
3497+
3498+ if (!isApiKeySupersetOfPermission (allPermissions , permissions )) {
3499+ throw new InvalidParameterValueException (String .format ("The keypair being created has a bigger set of permissions than the account [%s] that owns it. This is " +
3500+ "not allowed." , account .getUuid ()));
35113501 }
35123502
35133503 ApiKeyPairVO savedApiKeyPair = apiKeyPairDao .persist (newApiKeyPair );
@@ -3519,16 +3509,6 @@ private ApiKeyPairVO validateAndPersistKeyPairAndPermissions(Account account, Ap
35193509 return savedApiKeyPair ;
35203510 }
35213511
3522- /**
3523- * Gets all account role permissions
3524- * @param accountRole base account role of the user.
3525- */
3526- private List <RolePermissionEntity > getAllAccountRolePermissions (Role accountRole ) {
3527- List <RolePermission > allAccountRolePermissions = roleService .findAllPermissionsBy (accountRole .getId ());
3528- return allAccountRolePermissions .stream ().map (permission -> (RolePermissionEntity ) permission )
3529- .collect (Collectors .toList ());
3530- }
3531-
35323512 /**
35333513 * Gets all API keypair permissions for the given apiKey
35343514 */
@@ -3539,7 +3519,8 @@ private List<RolePermissionEntity> getAllKeypairPermissions(String apiKey) {
35393519 ApiKeyPair apiKeyPair = keyPairManager .findByApiKey (apiKey );
35403520 Account account = _accountDao .findById (apiKeyPair .getAccountId ());
35413521 List <ApiKeyPairPermission > allApiKeyRolePermissions = keyPairManager .findAllPermissionsByKeyPairId (apiKeyPair .getId (), account .getRoleId ());
3542- return allApiKeyRolePermissions .stream ().map (permission -> (RolePermissionEntity ) permission )
3522+ return allApiKeyRolePermissions .stream ()
3523+ .map (permission -> (RolePermissionEntity ) permission )
35433524 .collect (Collectors .toList ());
35443525 }
35453526
0 commit comments