use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class SignatureAddingWonMessageProcessor method signWithDefaultKey.
/**
* Used by the WoN node. Uses its default key for signing any outgoing message.
*
* @param message
* @return
* @throws WonMessageProcessingException
*/
public WonMessage signWithDefaultKey(final WonMessage message) throws WonMessageProcessingException {
// use default key for signing
PrivateKey privateKey = cryptographyService.getDefaultPrivateKey();
String webId = cryptographyService.getDefaultPrivateKeyAlias();
PublicKey publicKey = cryptographyService.getPublicKey(webId);
try {
List<WonMessage> ret = new ArrayList<WonMessage>();
for (WonMessage part : message.getAllMessages()) {
ret.add(processWithKey(part, webId, privateKey, publicKey));
}
return WonMessage.of(ret);
} catch (Exception e) {
logger.error("Failed to sign", e);
throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString());
}
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class SignatureAddingWonMessageProcessor method signWithAtomKey.
/**
* Used by owners - they find the key alias in the message and get the key from
* the cryptography service.
*
* @param wonMessage
* @return
* @throws WonMessageProcessingException
*/
public WonMessage signWithAtomKey(final WonMessage wonMessage) throws WonMessageProcessingException {
List<WonMessage> ret = new ArrayList<WonMessage>();
for (WonMessage message : wonMessage.getAllMessages()) {
// use senderAtom key for signing
Optional<URI> senderAtomURI = Optional.of(message.getSenderAtomURIRequired());
String alias = keyPairAliasDerivationStrategy.getAliasForAtomUri(senderAtomURI.get().toString());
PrivateKey privateKey = cryptographyService.getPrivateKey(alias);
PublicKey publicKey = cryptographyService.getPublicKey(alias);
if (privateKey == null || publicKey == null) {
throw new WonMessageProcessingException(String.format("Cannot sign message with key for atom %s: key not found", senderAtomURI));
}
try {
ret.add(processWithKey(message, senderAtomURI.get().toString(), privateKey, publicKey));
} catch (Exception e) {
logger.error("Failed to sign", e);
throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString(), e);
}
}
return WonMessage.of(ret);
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class DeactivateAtomMessageFromOwnerReactionProcessor method process.
public void process(final Exchange exchange) throws Exception {
WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
URI recipientAtomURI = wonMessage.getRecipientAtomURI();
logger.debug("DEACTIVATING atom. atomURI:{}", recipientAtomURI);
if (recipientAtomURI == null)
throw new WonMessageProcessingException("recipientAtomURI is not set");
Atom atom = atomService.getAtomRequired(recipientAtomURI);
matcherProtocolMatcherClient.atomDeactivated(atom.getAtomURI(), wonMessage);
// close all connections
Collection<Connection> conns = connectionRepository.findByAtomURIAndNotStateForUpdate(atom.getAtomURI(), ConnectionState.CLOSED);
for (Connection con : conns) {
entityManager.refresh(con);
closeConnection(atom, con);
}
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class UriConsistencyCheckingWonMessageProcessor method process.
@Override
public WonMessage process(WonMessage message) {
StopWatch sw = new StopWatch();
if (message == null) {
throw new WonMessageProcessingException("No WonMessage object found in exchange");
}
sw.start("validMessageURI");
if (!WonMessageUtils.isValidMessageUri(message.getMessageURIRequired())) {
throw new WonMessageNotWellFormedException("Not a valid message URI: " + message.getMessageURI());
}
sw.stop();
sw.start("envelopePropertyCheck");
EnvelopePropertyCheckResult result = message.checkEnvelopeProperties();
if (!result.isValid()) {
throw new WonMessageNotWellFormedException(result.getMessage());
}
sw.stop();
sw.start("getNodeInfo");
Optional<URI> senderNode = messageRoutingInfoService.senderNode(message);
Optional<URI> recipientNode = messageRoutingInfoService.recipientNode(message);
if (!senderNode.isPresent() && !message.getMessageTypeRequired().isHintMessage()) {
throw new WonMessageProcessingException("Cannot determine sender node for " + message.toShortStringForDebug());
}
if (!recipientNode.isPresent()) {
throw new WonMessageProcessingException("Cannot determine recipient node for " + message.toShortStringForDebug());
}
WonNodeInfo senderNodeInfo = null;
WonNodeInfo recipientNodeInfo = null;
if (senderNode.isPresent() && !message.getMessageType().isHintMessage()) {
// do not check the sender node for a hint
// TODO: change this behaviour as soon as a matcher uses a WoN node
senderNodeInfo = wonNodeInformationService.getWonNodeInformation(senderNode.get());
}
if (recipientNode != null) {
recipientNodeInfo = wonNodeInformationService.getWonNodeInformation(recipientNode.get());
}
if (senderNodeInfo == null && !message.getMessageType().isHintMessage()) {
throw new WonMessageProcessingException("Could not load sender WonNodeInfo (won node " + senderNode.get() + ")");
}
if (recipientNodeInfo == null) {
throw new WonMessageProcessingException("Could not load recipient WonNodeInfo (won node " + recipientNode.get() + ")");
}
sw.stop();
if (!message.getMessageType().isHintMessage()) {
sw.start("senderAtomUriCheck");
checkAtomUri(message.getSenderAtomURI(), senderNodeInfo);
sw.stop();
sw.start("senderSocketUriCheck");
checkSocketUri(message.getSenderSocketURI(), senderNodeInfo);
sw.stop();
// there is no way atom or connection uri can be on the recipient node and the
// recipient node is different from the sender node
sw.start("atomUriCheck");
checkAtomUri(message.getAtomURI(), senderNodeInfo);
sw.stop();
sw.start("connectionUriCheck");
checkConnectionUri(message.getConnectionURI(), senderNodeInfo);
// Check that atom URI for create_atom message corresponds to local pattern
sw.stop();
sw.start("createMsgAtomUriCheck");
checkCreateMsgAtomURI(message, senderNodeInfo);
sw.stop();
}
sw.start("recipientAtomUriCheck");
checkAtomUri(message.getRecipientAtomURI(), recipientNodeInfo);
sw.stop();
sw.start("recipientSocketUriCheck");
checkSocketUri(message.getRecipientSocketURI(), recipientNodeInfo);
sw.stop();
sw.start("signerCheck");
WonMessageDirection statedDirection = message.getEnvelopeType();
if (statedDirection.isFromSystem()) {
if (!Objects.equals(message.getSenderNodeURIRequired(), message.getSignerURIRequired())) {
RDFDataMgr.write(System.out, message.getCompleteDataset(), Lang.TRIG);
throw new WonMessageNotWellFormedException("WonMessage " + message.toShortStringForDebug() + " is FROM_SYSTEM but not signed by its node");
}
}
sw.stop();
if (logger.isDebugEnabled()) {
logger.debug(LogMarkers.TIMING, "URI Consistency check timing for message {}:\n{}", message.getMessageURIRequired(), sw.prettyPrint());
}
return message;
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class SignatureAddingWonMessageProcessor method signWithOtherKey.
public WonMessage signWithOtherKey(final WonMessage wonMessage, URI webId) throws WonMessageProcessingException {
List<WonMessage> ret = new ArrayList<>();
for (WonMessage message : wonMessage.getAllMessages()) {
// use senderAtom key for signing
String alias = keyPairAliasDerivationStrategy.getAliasForAtomUri(webId.toString());
PrivateKey privateKey = cryptographyService.getPrivateKey(alias);
PublicKey publicKey = cryptographyService.getPublicKey(alias);
Objects.requireNonNull(publicKey);
Objects.requireNonNull(publicKey);
try {
ret.add(processWithKey(message, webId.toString(), privateKey, publicKey));
} catch (Exception e) {
logger.error("Failed to sign", e);
throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString(), e);
}
}
return WonMessage.of(ret);
}
Aggregations