use of ee.ria.xroad.signer.protocol.dto.KeyInfo in project X-Road by nordic-institute.
the class KeyAndCertificateRequestService method addKeyAndCertRequest.
/**
* Add a new key and create a csr for it
* @param tokenId
* @param keyLabel
* @param memberId
* @param keyUsageInfo
* @param caName
* @param subjectFieldValues
* @param csrFormat
* @return
* @throws ActionNotPossibleException if add key or generate csr was not possible
* @throws ClientNotFoundException if client with {@code memberId} id was not found
* @throws CertificateAuthorityNotFoundException if ca authority with name {@code caName} does not exist
* @throws TokenNotFoundException if token with {@code tokenId} was not found
* @throws DnFieldHelper.InvalidDnParameterException if required dn parameters were missing, or if there
* were some extra parameters
*/
public KeyAndCertRequestInfo addKeyAndCertRequest(String tokenId, String keyLabel, ClientId memberId, KeyUsageInfo keyUsageInfo, String caName, Map<String, String> subjectFieldValues, CertificateRequestFormat csrFormat) throws ActionNotPossibleException, ClientNotFoundException, CertificateAuthorityNotFoundException, TokenNotFoundException, DnFieldHelper.InvalidDnParameterException {
KeyInfo keyInfo = keyService.addKey(tokenId, keyLabel);
GeneratedCertRequestInfo csrInfo;
boolean csrGenerateSuccess = false;
Exception csrGenerateException = null;
try {
csrInfo = tokenCertificateService.generateCertRequest(keyInfo.getId(), memberId, keyUsageInfo, caName, subjectFieldValues, csrFormat);
csrGenerateSuccess = true;
} catch (KeyNotFoundException | WrongKeyUsageException e) {
csrGenerateException = e;
// create key & generateCertRequest
throw new DeviationAwareRuntimeException(e, e.getErrorDeviation());
} catch (Exception e) {
csrGenerateException = e;
throw e;
} finally {
// In case of Errors, we do not want to attempt rollback
if (csrGenerateException != null) {
tryRollbackCreateKey(csrGenerateException, keyInfo.getId());
} else if (!csrGenerateSuccess) {
log.error("csr generate failed -create key rollback was not attempted since failure " + "was not due to an Exception (we do not catch Errors)");
}
}
// get a new keyInfo that contains the csr
KeyInfo refreshedKeyInfo;
try {
refreshedKeyInfo = keyService.getKey(keyInfo.getId());
} catch (KeyNotFoundException e) {
throw new DeviationAwareRuntimeException(e, e.getErrorDeviation());
}
KeyAndCertRequestInfo info = new KeyAndCertRequestInfo(refreshedKeyInfo, csrInfo.getCertReqId(), csrInfo.getCertRequest(), csrInfo.getFormat(), csrInfo.getMemberId(), csrInfo.getKeyUsage());
return info;
}
use of ee.ria.xroad.signer.protocol.dto.KeyInfo in project X-Road by nordic-institute.
the class KeyService method updateKeyFriendlyName.
/**
* Updates key friendly name
* @throws KeyNotFoundException if key was not found
* @throws ActionNotPossibleException if friendly name could not be updated for this key
*/
public KeyInfo updateKeyFriendlyName(String id, String friendlyName) throws KeyNotFoundException, ActionNotPossibleException {
// check that updating friendly name is possible
TokenInfo tokenInfo = tokenService.getTokenForKeyId(id);
KeyInfo keyInfo = getKey(tokenInfo, id);
auditDataHelper.put(RestApiAuditProperty.KEY_ID, keyInfo.getId());
auditDataHelper.put(RestApiAuditProperty.KEY_FRIENDLY_NAME, friendlyName);
possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.EDIT_FRIENDLY_NAME, tokenInfo, keyInfo);
try {
signerProxyFacade.setKeyFriendlyName(id, friendlyName);
keyInfo = getKey(id);
} catch (KeyNotFoundException e) {
throw e;
} catch (CodedException e) {
if (isCausedByKeyNotFound(e)) {
throw new KeyNotFoundException(e);
} else {
throw e;
}
} catch (Exception e) {
throw new SignerNotReachableException("Update key friendly name failed", e);
}
return keyInfo;
}
use of ee.ria.xroad.signer.protocol.dto.KeyInfo in project X-Road by nordic-institute.
the class KeyService method addKey.
/**
* Generate a new key for selected token
* @param tokenId
* @param keyLabel
* @return {@link KeyInfo}
* @throws TokenNotFoundException if token was not found
* @throws ActionNotPossibleException if generate key was not possible for this token
*/
public KeyInfo addKey(String tokenId, String keyLabel) throws TokenNotFoundException, ActionNotPossibleException {
// check that adding a key is possible
TokenInfo tokenInfo = tokenService.getToken(tokenId);
auditDataHelper.put(tokenInfo);
possibleActionsRuleEngine.requirePossibleTokenAction(PossibleActionEnum.GENERATE_KEY, tokenInfo);
KeyInfo keyInfo = null;
try {
keyInfo = signerProxyFacade.generateKey(tokenId, keyLabel);
} catch (CodedException e) {
throw e;
} catch (Exception other) {
throw new SignerNotReachableException("adding a new key failed", other);
}
auditDataHelper.put(RestApiAuditProperty.KEY_ID, keyInfo.getId());
auditDataHelper.put(RestApiAuditProperty.KEY_LABEL, keyInfo.getLabel());
auditDataHelper.put(RestApiAuditProperty.KEY_FRIENDLY_NAME, keyInfo.getFriendlyName());
return keyInfo;
}
Aggregations