Skip to content

Commit

Permalink
Store sender's IP address for inbound messages in DB (Draegerwerk#204)
Browse files Browse the repository at this point in the history
Store the IP addresses for inbound messages in the DB.

# Checklist

The following aspects have been respected by the author of this pull
request, confirmed by both pull request assignee **and** reviewer:

* Adherence to coding conventions
  * [x] Pull Request Assignee
  * [x] Reviewer
* Adherence to javadoc conventions
  * [x] Pull Request Assignee
  * [x] Reviewer
* Changelog update (necessity checked and entry added or not added
respectively)
  * [x] Pull Request Assignee
  * [x] Reviewer
* README update (necessity checked and entry added or not added
respectively)
  * [x] Pull Request Assignee
  * [x] Reviewer
* config update (necessity checked and entry added or not added
respectively)
  * [x] Pull Request Assignee
  * [x] Reviewer
* SDCcc executable ran against a test device (if necessary)
  * [x] Pull Request Assignee
  * [x] Reviewer

---------

Co-authored-by: Maximilian Pilz <[email protected]>
  • Loading branch information
ben-Draeger and maximilianpilz authored Sep 13, 2024
1 parent 0e8a98d commit 57292af
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- support for Kotlin
- preconditions which observe mdib changes during the test run
- storing of IP addresses of inbound messages in the database

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
Expand Down Expand Up @@ -84,13 +85,15 @@
import org.somda.sdc.dpws.soap.CommunicationContext;
import org.somda.sdc.dpws.soap.HttpApplicationInfo;
import org.somda.sdc.dpws.soap.SoapConstants;
import org.somda.sdc.dpws.soap.TransportInfo;
import org.somda.sdc.dpws.soap.wsaddressing.WsAddressingConstants;

/**
* Storage for incoming and outgoing messages.
*/
@Singleton
public class MessageStorage implements AutoCloseable {

private static final Logger LOG = LogManager.getLogger(MessageStorage.class);

private static final int FETCH_SIZE = 10;
Expand Down Expand Up @@ -310,7 +313,52 @@ protected MessageContent convertMessageToMessageContent(final Message message) {
mdibVersionGroups,
actions,
message.getID(),
isSOAP);
isSOAP,
getSender(message));
}

private String getSender(final Message message) {
final CommunicationContext communicationContext = message.getCommunicationContext();
if (communicationContext == null) {
// NOTE: this should never happen. If it does, this should be considered a bug.
LOG.trace("Encountered message (uuid={}) without a CommunicationContext.", message.getID());
testRunObserver.invalidateTestRun("Encountered message without a CommunicationContext.");
return null;
}
final TransportInfo transportInfo = communicationContext.getTransportInfo();
if (transportInfo == null) {
// NOTE: this should never happen. If it does, this should be considered a bug.
LOG.trace("Encountered message (uuid={}) without a TransportInfo.", message.getID());
testRunObserver.invalidateTestRun("Encountered message without a TransportInfo.");
return null;
}
if (message.getDirection() == CommunicationLog.Direction.INBOUND) {
final Optional<String> remoteAddress = transportInfo.getRemoteAddress();
if (remoteAddress.isEmpty()) {
// NOTE: this should never happen. If it does, this should be considered a bug.
LOG.trace("Encountered inbound message (uuid={}) without a remoteAddress.", message.getID());
testRunObserver.invalidateTestRun("Encountered inbound message without a remoteAddress.");
return null;
} else {
return remoteAddress.orElseThrow();
}
} else if (message.getDirection() == CommunicationLog.Direction.OUTBOUND) {
final Optional<String> localAddress = transportInfo.getLocalAddress();
if (localAddress.isEmpty()) {
// TODO: Fix: outbound messages often have a localAddress of 0.0.0.0 or null. After this is fixed,
// invalidate the TestRun in this case as well.
return null;
} else {
return localAddress.orElseThrow();
}
} else {
testRunObserver.invalidateTestRun("Encountered unknown direction in message.");
LOG.trace(
"Encountered unknown direction {} in message with uuid={}",
message.getDirection(),
message.getID());
return null;
}
}

private boolean processMessageBody(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
Expand Down Expand Up @@ -75,6 +76,9 @@ public class MessageContent {
private String uuid;
private boolean isSOAP;

@Column(nullable = true)
private String sender;

/**
* This will be used by hibernate when creating the POJO from database entries.
*/
Expand All @@ -94,6 +98,7 @@ public MessageContent() {}
* @param actions ws addressing actions
* @param uuid identifier for ensuring, that a message was written to the database
* @param isSOAP shall be true if a SOAP envelope was found and false otherwise
* @param sender the IP address of the message's sender or null if it could not be determined.
*/
public MessageContent(
final String body,
Expand All @@ -105,7 +110,8 @@ public MessageContent(
final List<MdibVersionGroupEntity.MdibVersionGroup> mdibVersionGroups,
final Set<String> actions,
final String uuid,
final boolean isSOAP) {
final boolean isSOAP,
@Nullable final String sender) {

this.body = body;
this.direction = direction;
Expand All @@ -115,6 +121,7 @@ public MessageContent(
this.actions = actions;
this.uuid = uuid;
this.isSOAP = isSOAP;
this.sender = sender;

this.messageHash = MessageUtil.hashMessage(this.body);
this.scheme = communicationContext.getTransportInfo().getScheme();
Expand Down Expand Up @@ -214,4 +221,8 @@ public String getRequestUri() {
public List<MdibVersionGroupEntity> getMdibVersionGroups() {
return this.mdibVersionGroups;
}

public String getSender() {
return this.sender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class MessageContent_ {
public static volatile SingularAttribute<MessageContent, String> scheme;
public static volatile SingularAttribute<MessageContent, String> uuid;
public static volatile SingularAttribute<MessageContent, Boolean> isSOAP;
public static volatile SingularAttribute<MessageContent, String> sender;

private MessageContent_() {}
}

0 comments on commit 57292af

Please sign in to comment.