-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for including a client certificate for certificate vali…
…dation
- Loading branch information
Showing
6 changed files
with
212 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Serilog; | ||
using Serilog.Core; | ||
|
||
namespace Witsml; | ||
|
||
public interface IQueryLogger | ||
{ | ||
void LogQuery(string querySent, bool isSuccessful, string xmlReceived = null); | ||
} | ||
|
||
public class DefaultQueryLogger : IQueryLogger | ||
{ | ||
private readonly Logger _queryLogger; | ||
|
||
public DefaultQueryLogger() | ||
{ | ||
_queryLogger = new LoggerConfiguration() | ||
.WriteTo.File("queries.log", rollOnFileSizeLimit: true, retainedFileCountLimit: 1, fileSizeLimitBytes: 50000000) | ||
.CreateLogger(); | ||
} | ||
|
||
public void LogQuery(string querySent, bool isSuccessful, string xmlReceived = null) | ||
{ | ||
if (xmlReceived != null) | ||
{ | ||
_queryLogger.Information("Query: \n{Query}\nReceived: \n{Response}\nIsSuccessful: {IsSuccessful}", querySent, xmlReceived, isSuccessful); | ||
} | ||
else | ||
{ | ||
_queryLogger.Information("Query: \n{Query}\nIsSuccessful: {IsSuccessful}", querySent, isSuccessful); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Witsml; | ||
|
||
public class QueryResult | ||
{ | ||
public bool IsSuccessful { get; } | ||
public string Reason { get; } | ||
|
||
public QueryResult(bool isSuccessful, string reason = null) | ||
{ | ||
IsSuccessful = isSuccessful; | ||
Reason = reason; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Net; | ||
using System.ServiceModel; | ||
using System.ServiceModel.Channels; | ||
|
||
using Serilog; | ||
|
||
using Witsml.ServiceReference; | ||
|
||
namespace Witsml; | ||
|
||
public abstract class WitsmlClientBase | ||
{ | ||
internal static StoreSoapPortClient CreateSoapClient(WitsmlClientOptions options) | ||
{ | ||
EndpointAddress endpointAddress = new(options.Hostname); | ||
|
||
Binding serviceBinding = options.ClientCertificate == null | ||
? CreateBasicBinding(options.RequestTimeOut) | ||
: CreateCertificateAndBasicBinding(); | ||
|
||
var client = new StoreSoapPortClient(serviceBinding, endpointAddress); | ||
client.ClientCredentials.UserName.UserName = options.Credentials.Username; | ||
client.ClientCredentials.UserName.Password = options.Credentials.Password; | ||
|
||
if (options.ClientCertificate != null) | ||
{ | ||
client.ClientCredentials.ClientCertificate.Certificate = options.ClientCertificate; | ||
Log.Information($"Configured client to use client certificate. CN={options.ClientCertificate.SubjectName.Name}"); | ||
if (!options.ClientCertificate.HasPrivateKey) | ||
Log.Warning("Configured client certificate does not contain a private key"); | ||
} | ||
|
||
client.Endpoint.EndpointBehaviors.Add(new EndpointBehavior()); | ||
|
||
return client; | ||
} | ||
|
||
private static BasicHttpsBinding CreateBasicBinding(TimeSpan requestTimeout) | ||
{ | ||
return new BasicHttpsBinding | ||
{ | ||
Security = | ||
{ | ||
Mode = BasicHttpsSecurityMode.Transport, | ||
Transport = | ||
{ | ||
ClientCredentialType = HttpClientCredentialType.Basic | ||
} | ||
}, | ||
MaxReceivedMessageSize = int.MaxValue, | ||
SendTimeout = requestTimeout | ||
}; | ||
} | ||
|
||
private static CustomBinding CreateCertificateAndBasicBinding() | ||
{ | ||
return new CustomBinding | ||
{ | ||
Elements = | ||
{ | ||
new TextMessageEncodingBindingElement | ||
{ | ||
MessageVersion = MessageVersion.Soap11 | ||
}, | ||
new HttpsTransportBindingElement | ||
{ | ||
RequireClientCertificate = true, | ||
AuthenticationScheme = AuthenticationSchemes.Basic, | ||
MaxReceivedMessageSize = int.MaxValue | ||
} | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.