@Test @WithMockUser(authorities = { "DELETE_AUTH_KEY", "DELETE_SIGN_KEY", "DELETE_KEY", "SEND_AUTH_CERT_DEL_REQ" }) public void deleteKeyIgnoreWarningsFalse() throws Exception { try { keyService.deleteKey(AUTH_KEY_ID, false); fail("should throw exception"); } catch (UnhandledWarningsException expected) { assertEquals(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, expected.getWarningDeviations().iterator().next().getCode()); } }
public void deleteKey(String keyId, Boolean ignoreWarnings) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException, UnhandledWarningsException { TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId); auditDataHelper.put(tokenInfo); KeyInfo keyInfo = getKey(tokenInfo, keyId); auditDataHelper.put(keyInfo); if (keyInfo.getUsage() == null) { securityHelper.verifyAuthority("DELETE_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { securityHelper.verifyAuthority("DELETE_AUTH_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.SIGNING) { securityHelper.verifyAuthority("DELETE_SIGN_KEY"); } possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.DELETE, tokenInfo, keyInfo); if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { List<CertificateInfo> unregister = keyInfo.getCerts().stream().filter(this::shouldUnregister) .collect(Collectors.toList()); if (!unregister.isEmpty() && !ignoreWarnings) { throw new UnhandledWarningsException( new WarningDeviation(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, keyId)); } for (CertificateInfo certificateInfo : unregister) { unregisterAuthCert(certificateInfo); } } if (!auditDataHelper.dataIsForEvent(DELETE_ORPHANS)) { auditEventHelper.changeRequestScopedEvent(DELETE_KEY_FROM_TOKEN_AND_CONFIG); } try { signerProxyFacade.deleteKey(keyId, false); signerProxyFacade.deleteKey(keyId, true); } catch (CodedException e) { throw e; } catch (Exception other) { throw new SignerNotReachableException("delete key failed", other); } }
KeyService { public void deleteKey(String keyId, Boolean ignoreWarnings) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException, UnhandledWarningsException { TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId); auditDataHelper.put(tokenInfo); KeyInfo keyInfo = getKey(tokenInfo, keyId); auditDataHelper.put(keyInfo); if (keyInfo.getUsage() == null) { securityHelper.verifyAuthority("DELETE_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { securityHelper.verifyAuthority("DELETE_AUTH_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.SIGNING) { securityHelper.verifyAuthority("DELETE_SIGN_KEY"); } possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.DELETE, tokenInfo, keyInfo); if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { List<CertificateInfo> unregister = keyInfo.getCerts().stream().filter(this::shouldUnregister) .collect(Collectors.toList()); if (!unregister.isEmpty() && !ignoreWarnings) { throw new UnhandledWarningsException( new WarningDeviation(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, keyId)); } for (CertificateInfo certificateInfo : unregister) { unregisterAuthCert(certificateInfo); } } if (!auditDataHelper.dataIsForEvent(DELETE_ORPHANS)) { auditEventHelper.changeRequestScopedEvent(DELETE_KEY_FROM_TOKEN_AND_CONFIG); } try { signerProxyFacade.deleteKey(keyId, false); signerProxyFacade.deleteKey(keyId, true); } catch (CodedException e) { throw e; } catch (Exception other) { throw new SignerNotReachableException("delete key failed", other); } } }
KeyService { public void deleteKey(String keyId, Boolean ignoreWarnings) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException, UnhandledWarningsException { TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId); auditDataHelper.put(tokenInfo); KeyInfo keyInfo = getKey(tokenInfo, keyId); auditDataHelper.put(keyInfo); if (keyInfo.getUsage() == null) { securityHelper.verifyAuthority("DELETE_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { securityHelper.verifyAuthority("DELETE_AUTH_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.SIGNING) { securityHelper.verifyAuthority("DELETE_SIGN_KEY"); } possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.DELETE, tokenInfo, keyInfo); if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { List<CertificateInfo> unregister = keyInfo.getCerts().stream().filter(this::shouldUnregister) .collect(Collectors.toList()); if (!unregister.isEmpty() && !ignoreWarnings) { throw new UnhandledWarningsException( new WarningDeviation(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, keyId)); } for (CertificateInfo certificateInfo : unregister) { unregisterAuthCert(certificateInfo); } } if (!auditDataHelper.dataIsForEvent(DELETE_ORPHANS)) { auditEventHelper.changeRequestScopedEvent(DELETE_KEY_FROM_TOKEN_AND_CONFIG); } try { signerProxyFacade.deleteKey(keyId, false); signerProxyFacade.deleteKey(keyId, true); } catch (CodedException e) { throw e; } catch (Exception other) { throw new SignerNotReachableException("delete key failed", other); } } @Autowired  KeyService(TokenService tokenService, SignerProxyFacade signerProxyFacade,
            PossibleActionsRuleEngine possibleActionsRuleEngine,
            ManagementRequestSenderService managementRequestSenderService,
            SecurityHelper securityHelper,
            AuditDataHelper auditDataHelper,
            AuditEventHelper auditEventHelper,
            AuditEventLoggingFacade auditEventLoggingFacade); }
KeyService { public void deleteKey(String keyId, Boolean ignoreWarnings) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException, UnhandledWarningsException { TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId); auditDataHelper.put(tokenInfo); KeyInfo keyInfo = getKey(tokenInfo, keyId); auditDataHelper.put(keyInfo); if (keyInfo.getUsage() == null) { securityHelper.verifyAuthority("DELETE_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { securityHelper.verifyAuthority("DELETE_AUTH_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.SIGNING) { securityHelper.verifyAuthority("DELETE_SIGN_KEY"); } possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.DELETE, tokenInfo, keyInfo); if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { List<CertificateInfo> unregister = keyInfo.getCerts().stream().filter(this::shouldUnregister) .collect(Collectors.toList()); if (!unregister.isEmpty() && !ignoreWarnings) { throw new UnhandledWarningsException( new WarningDeviation(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, keyId)); } for (CertificateInfo certificateInfo : unregister) { unregisterAuthCert(certificateInfo); } } if (!auditDataHelper.dataIsForEvent(DELETE_ORPHANS)) { auditEventHelper.changeRequestScopedEvent(DELETE_KEY_FROM_TOKEN_AND_CONFIG); } try { signerProxyFacade.deleteKey(keyId, false); signerProxyFacade.deleteKey(keyId, true); } catch (CodedException e) { throw e; } catch (Exception other) { throw new SignerNotReachableException("delete key failed", other); } } @Autowired  KeyService(TokenService tokenService, SignerProxyFacade signerProxyFacade,
            PossibleActionsRuleEngine possibleActionsRuleEngine,
            ManagementRequestSenderService managementRequestSenderService,
            SecurityHelper securityHelper,
            AuditDataHelper auditDataHelper,
            AuditEventHelper auditEventHelper,
            AuditEventLoggingFacade auditEventLoggingFacade); KeyInfo getKey(String keyId); KeyInfo getKey(TokenInfo tokenInfo, String keyId); KeyInfo updateKeyFriendlyName(String id, String friendlyName); KeyInfo addKey(String tokenId, String keyLabel); void deleteKeyAndIgnoreWarnings(String keyId); void deleteKey(String keyId, Boolean ignoreWarnings); EnumSet<PossibleActionEnum> getPossibleActionsForKey(String keyId); }
KeyService { public void deleteKey(String keyId, Boolean ignoreWarnings) throws KeyNotFoundException, ActionNotPossibleException, GlobalConfOutdatedException, UnhandledWarningsException { TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId); auditDataHelper.put(tokenInfo); KeyInfo keyInfo = getKey(tokenInfo, keyId); auditDataHelper.put(keyInfo); if (keyInfo.getUsage() == null) { securityHelper.verifyAuthority("DELETE_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { securityHelper.verifyAuthority("DELETE_AUTH_KEY"); } else if (keyInfo.getUsage() == KeyUsageInfo.SIGNING) { securityHelper.verifyAuthority("DELETE_SIGN_KEY"); } possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.DELETE, tokenInfo, keyInfo); if (keyInfo.getUsage() == KeyUsageInfo.AUTHENTICATION) { List<CertificateInfo> unregister = keyInfo.getCerts().stream().filter(this::shouldUnregister) .collect(Collectors.toList()); if (!unregister.isEmpty() && !ignoreWarnings) { throw new UnhandledWarningsException( new WarningDeviation(WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED, keyId)); } for (CertificateInfo certificateInfo : unregister) { unregisterAuthCert(certificateInfo); } } if (!auditDataHelper.dataIsForEvent(DELETE_ORPHANS)) { auditEventHelper.changeRequestScopedEvent(DELETE_KEY_FROM_TOKEN_AND_CONFIG); } try { signerProxyFacade.deleteKey(keyId, false); signerProxyFacade.deleteKey(keyId, true); } catch (CodedException e) { throw e; } catch (Exception other) { throw new SignerNotReachableException("delete key failed", other); } } @Autowired  KeyService(TokenService tokenService, SignerProxyFacade signerProxyFacade,
            PossibleActionsRuleEngine possibleActionsRuleEngine,
            ManagementRequestSenderService managementRequestSenderService,
            SecurityHelper securityHelper,
            AuditDataHelper auditDataHelper,
            AuditEventHelper auditEventHelper,
            AuditEventLoggingFacade auditEventLoggingFacade); KeyInfo getKey(String keyId); KeyInfo getKey(TokenInfo tokenInfo, String keyId); KeyInfo updateKeyFriendlyName(String id, String friendlyName); KeyInfo addKey(String tokenId, String keyLabel); void deleteKeyAndIgnoreWarnings(String keyId); void deleteKey(String keyId, Boolean ignoreWarnings); EnumSet<PossibleActionEnum> getPossibleActionsForKey(String keyId); static final String WARNING_AUTH_KEY_REGISTERED_CERT_DETECTED; }