- Fix compile errors
- MOve consumer key split into Subscription to allow better testing
- move models to extra file
- adopt API changes
This commit is contained in:
Peter 2021-03-05 16:55:25 +01:00
parent c85fb08205
commit 0e086b3b6c
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
7 changed files with 89 additions and 43 deletions

View file

@ -7,8 +7,8 @@ stages:
variables: variables:
DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_CERTDIR: "/certs"
DOCKER_CERT_PATH: "/certs/client" DOCKER_CERT_PATH: "/certs/client"
DOCKER_TLS_VERIFY: 0 DOCKER_TLS_VERIFY: 1
DOCKER_HOST: 'tcp://docker:2375' DOCKER_HOST: 'tcp://docker:2376'
test: test:
stage: test stage: test

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
namespace INetMock.Client.Audit namespace INetMock.Client.Audit
@ -60,11 +61,11 @@ namespace INetMock.Client.Audit
Headers = new INetMockHttpHeaders(detailsEntity.Headers); Headers = new INetMockHttpHeaders(detailsEntity.Headers);
} }
public HttpMethod Method { get; init; } public HttpMethod Method { get; init; } = HttpMethod.Get;
public string Host { get; init; } public string Host { get; init; } = string.Empty;
public string Uri { get; init; } public string Uri { get; init; } = string.Empty;
public string Proto { get; init; } public string Proto { get; init; } = string.Empty;
public HttpHeaders Headers { get; init; } public HttpHeaders Headers { get; init; } = new INetMockHttpHeaders(new MapField<string, HTTPHeaderValue>());
} }
public record DnsDetails : EventDetails public record DnsDetails : EventDetails
@ -73,9 +74,9 @@ namespace INetMock.Client.Audit
{ {
} }
public DNSOpCode OpCode { get; init; } public DNSOpCode OpCode { get; init; } = DNSOpCode.Query;
public IReadOnlyList<DNSQuestionEntity> Questions { get; init; } public IReadOnlyList<DNSQuestionEntity> Questions { get; init; } = Array.Empty<DNSQuestionEntity>();
public DnsDetails(Any? any) public DnsDetails(Any? any)
{ {

View file

@ -37,11 +37,11 @@ namespace INetMock.Client.Audit
public DateTimeOffset Timestamp { get; init; } public DateTimeOffset Timestamp { get; init; }
public TransportProtocol Transport { get; init; } public TransportProtocol Transport { get; init; }
public AppProtocol Application { get; init; } public AppProtocol Application { get; init; }
public IPAddress SourceIp { get; init; } public IPAddress SourceIp { get; init; } = IPAddress.Any;
public IPAddress DestinationIp { get; init; } public IPAddress DestinationIp { get; init; } = IPAddress.Any;
public ushort SourcePort { get; init; } public ushort SourcePort { get; init; }
public ushort DestinationPort { get; init; } public ushort DestinationPort { get; init; }
public TLSDetailsEntity TlsDetails { get; init; } public TLSDetailsEntity TlsDetails { get; init; } = new();
public T? Details { get; init; } public T? Details { get; init; }
public bool CanConvert<TTarget>() where TTarget : EventDetails, new() public bool CanConvert<TTarget>() where TTarget : EventDetails, new()

View file

@ -48,7 +48,9 @@ namespace INetMock.Client.PCAP.Client
public async Task<IReadOnlyList<Subscription>> ListActiveRecordingsAsync(CancellationToken token = default) public async Task<IReadOnlyList<Subscription>> ListActiveRecordingsAsync(CancellationToken token = default)
{ {
var recordings = await _pcapServiceClient.ListActiveRecordingsAsync(new(), Metadata.Empty, null, token); var recordings = await _pcapServiceClient.ListActiveRecordingsAsync(new(), Metadata.Empty, null, token);
return recordings.Subscriptions.Select(SplitKeyIntoSubscription).ToList(); return recordings.Subscriptions
.Select(consumerKey => new Subscription(consumerKey))
.ToList();
} }
public async Task<string> StartPcapFileRecordingAsync(RecordingRequest request, public async Task<string> StartPcapFileRecordingAsync(RecordingRequest request,
@ -71,10 +73,10 @@ namespace INetMock.Client.PCAP.Client
return result.ResolvedPath; return result.ResolvedPath;
} }
public async Task<bool> StopPcapFileRecord(string consumerKey, CancellationToken token = default) public async Task<bool> StopPcapFileRecording(string consumerKey, CancellationToken token = default)
{ {
var clientRequest = new StopPCAPFileRecordRequest {ConsumerKey = consumerKey}; var clientRequest = new StopPCAPFileRecordingRequest {ConsumerKey = consumerKey};
var result = await _pcapServiceClient.StopPCAPFileRecordAsync( var result = await _pcapServiceClient.StopPCAPFileRecordingAsync(
clientRequest, clientRequest,
Metadata.Empty, Metadata.Empty,
null, null,
@ -82,16 +84,5 @@ namespace INetMock.Client.PCAP.Client
); );
return result.Removed; return result.Removed;
} }
private static Subscription SplitKeyIntoSubscription(string key)
{
var splitIndex = key.IndexOf(':');
if (splitIndex < 0)
{
throw new ArgumentOutOfRangeException();
}
return new Subscription(key.Substring(splitIndex+1), key.Substring(0, splitIndex), key);
}
} }
} }

View file

@ -1,25 +1,9 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace INetMock.Client.PCAP namespace INetMock.Client.PCAP
{ {
public record RecordingDevice(string Name, IReadOnlyList<IPAddress> Addresses);
public record Subscription(string ConsumerName, string Device, string ConsumerKey);
public record RecordingRequest(string Device, string TargetPath, bool Promiscuous = false)
{
public RecordingRequest(string device, string targetPath, bool promiscuous, TimeSpan readTimeout) : this(device,
targetPath, promiscuous)
{
ReadTimeout = readTimeout;
}
public TimeSpan ReadTimeout { get; } = TimeSpan.FromSeconds(30);
}
public interface IPcapApiClient public interface IPcapApiClient
{ {
Task<IReadOnlyList<RecordingDevice>> ListAvailableDevicesAsync(CancellationToken token = default); Task<IReadOnlyList<RecordingDevice>> ListAvailableDevicesAsync(CancellationToken token = default);

View file

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Net;
namespace INetMock.Client.PCAP
{
public record RecordingDevice(string Name, IReadOnlyList<IPAddress> Addresses);
public record Subscription
{
public Subscription(string consumerKey)
{
ConsumerKey = consumerKey;
(ConsumerName, Device) = SplitConsumerKey(consumerKey);
}
public Subscription(string consumerKey, string consumerName, string device)
{
ConsumerKey = consumerKey;
ConsumerName = consumerName;
Device = device;
}
public string ConsumerKey { get; init; }
public string ConsumerName { get; init; }
public string Device { get; init; }
private static (string name, string key) SplitConsumerKey(string consumerKey)
{
var splitIndex = consumerKey.IndexOf(':');
if (splitIndex < 0)
{
throw new ArgumentOutOfRangeException(
nameof(consumerKey),
"The given consumer key could not be split into components"
);
}
return (consumerKey[(splitIndex + 1)..], consumerKey[..splitIndex]);
}
}
public record RecordingRequest(string Device, string TargetPath, bool Promiscuous = false)
{
public RecordingRequest(string device, string targetPath, bool promiscuous, TimeSpan readTimeout) : this(device,
targetPath, promiscuous)
{
ReadTimeout = readTimeout;
}
public TimeSpan ReadTimeout { get; } = TimeSpan.FromSeconds(30);
}
}

View file

@ -0,0 +1,17 @@
using INetMock.Client.PCAP;
using Xunit;
namespace INetMock.Client.Test.PCAP
{
public class SubscriptionTests
{
[Theory]
[InlineData("lo:test.pcap", "test.pcap", "lo")]
public void Constructor_ConsumerKey_SplitIntoComponents(string key, string expectedName, string expectedDevice)
{
var sub = new Subscription(key);
Assert.Equal(sub, new Subscription(key, expectedName, expectedDevice));
}
}
}