Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store sender's IP address for inbound messages in DB #204

Merged
merged 17 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 @@
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 {

public static final String UNKNOWN_SENDER = "UNKNOWN";
public static final String DEFAULT_OUTBOUND_SENDER = "SDCcc";
private static final Logger LOG = LogManager.getLogger(MessageStorage.class);

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

private String getSender(final Message message) {
if (message.getDirection() == CommunicationLog.Direction.INBOUND) {
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);
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);
testRunObserver.invalidateTestRun("Encountered message without a TransportInfo.");
return null;
}
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);
ben-Draeger marked this conversation as resolved.
Show resolved Hide resolved
testRunObserver.invalidateTestRun("Encountered inbound message without a remoteAddress.");
return null;
} else {
return remoteAddress.orElseThrow();
}
} else if (message.getDirection() == CommunicationLog.Direction.OUTBOUND) {
return DEFAULT_OUTBOUND_SENDER;
maximilianpilz marked this conversation as resolved.
Show resolved Hide resolved
} 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 @@ -75,6 +75,9 @@ public class MessageContent {
private String uuid;
private boolean isSOAP;

@Column(nullable = true)
private String sender;
maximilianpilz marked this conversation as resolved.
Show resolved Hide resolved

/**
* This will be used by hibernate when creating the POJO from database entries.
*/
Expand All @@ -94,6 +97,8 @@ 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 either the String "SDCcc" for outbound messages or the IP Address of the
maximilianpilz marked this conversation as resolved.
Show resolved Hide resolved
* message Sender for inbound messages.
*/
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,
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_() {}
}
Loading