Skip to content

Changes to support external transport implementations #575

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions src/Docker.DotNet.BasicAuth/BasicAuthCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ private BasicAuthCredentials(MaybeSecureString username, MaybeSecureString passw
_password = password;
}

public override bool IsTlsCredentials()
{
return _isTls;
}

public override void Dispose()
{
_username.Dispose();
_password.Dispose();
}

public override bool SupportsScheme(string scheme)
{
return !(_isTls && scheme == "npipe");
}

public override bool IsTlsCredentials()
{
return _isTls;
}
}
}
}
7 changes: 6 additions & 1 deletion src/Docker.DotNet.X509/CertificateCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public override HttpMessageHandler GetHandler(HttpMessageHandler innerHandler)
return handler;
}

public override bool SupportsScheme(string scheme)
{
return scheme != "npipe";
}

public override bool IsTlsCredentials()
{
return true;
Expand All @@ -44,4 +49,4 @@ public override void Dispose()
{
}
}
}
}
2 changes: 1 addition & 1 deletion src/Docker.DotNet.X509/Docker.DotNet.X509.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
<ItemGroup>
<ProjectReference Include="..\Docker.DotNet\Docker.DotNet.csproj" />
</ItemGroup>
</Project>
</Project>
7 changes: 6 additions & 1 deletion src/Docker.DotNet/AnonymousCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public override bool IsTlsCredentials()
return false;
}

public override bool SupportsScheme(string scheme)
{
return true;
}

public override void Dispose()
{
}
Expand All @@ -18,4 +23,4 @@ public override HttpMessageHandler GetHandler(HttpMessageHandler innerHandler)
return innerHandler;
}
}
}
}
4 changes: 3 additions & 1 deletion src/Docker.DotNet/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Docker.DotNet
{
public abstract class Credentials : IDisposable
{
public abstract bool SupportsScheme(string scheme);

public abstract bool IsTlsCredentials();

public abstract HttpMessageHandler GetHandler(HttpMessageHandler innerHandler);
Expand All @@ -13,4 +15,4 @@ public virtual void Dispose()
{
}
}
}
}
75 changes: 2 additions & 73 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -47,78 +46,8 @@ internal DockerClient(DockerClientConfiguration configuration, Version requested
Plugin = new PluginOperations(this);
Exec = new ExecOperations(this);

ManagedHandler handler;
var uri = Configuration.EndpointBaseUri;
switch (uri.Scheme.ToLowerInvariant())
{
case "npipe":
if (Configuration.Credentials.IsTlsCredentials())
{
throw new Exception("TLS not supported over npipe");
}

var segments = uri.Segments;
if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
}

var serverName = uri.Host;
if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase))
{
// npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here.
serverName = ".";
}

var pipeName = uri.Segments[2];

uri = new UriBuilder("http", pipeName).Uri;
handler = new ManagedHandler(async (host, port, cancellationToken) =>
{
var timeout = (int)Configuration.NamedPipeConnectTimeout.TotalMilliseconds;
var stream = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
var dockerStream = new DockerPipeStream(stream);

await stream.ConnectAsync(timeout, cancellationToken)
.ConfigureAwait(false);

return dockerStream;
});
break;

case "tcp":
case "http":
var builder = new UriBuilder(uri)
{
Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
};
uri = builder.Uri;
handler = new ManagedHandler();
break;

case "https":
handler = new ManagedHandler();
break;

case "unix":
var pipeString = uri.LocalPath;
handler = new ManagedHandler(async (host, port, cancellationToken) =>
{
var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);

await sock.ConnectAsync(new Microsoft.Net.Http.Client.UnixDomainSocketEndPoint(pipeString))
.ConfigureAwait(false);

return sock;
});
uri = new UriBuilder("http", uri.Segments.Last()).Uri;
break;

default:
throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
}

_endpointBaseUri = uri;
var (url, handler) = Configuration.GetHandler();
_endpointBaseUri = url;

_client = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
_client.Timeout = SInfiniteTimeout;
Expand Down
83 changes: 82 additions & 1 deletion src/Docker.DotNet/DockerClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Net.Http.Client;

namespace Docker.DotNet
{
Expand Down Expand Up @@ -63,10 +67,87 @@ public void Dispose()
Credentials.Dispose();
}

public (Uri url, ManagedHandler handler) GetHandler()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be virtual so others can override it?

{
if (!Credentials.SupportsScheme(EndpointBaseUri.Scheme))
{
throw new Exception($"The provided credentials don't support the {EndpointBaseUri.Scheme} scheme.");
}

var uri = EndpointBaseUri;
ManagedHandler handler;

switch (EndpointBaseUri.Scheme.ToLowerInvariant())
{
case "npipe":
var segments = uri.Segments;
if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($"{uri} is not a valid npipe URI");
}

var serverName = uri.Host;
if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase))
{
// npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here.
serverName = ".";
}

var pipeName = uri.Segments[2];

uri = new UriBuilder("http", pipeName).Uri;
handler = new ManagedHandler(async (host, port, cancellationToken) =>
{
var timeout = (int)NamedPipeConnectTimeout.TotalMilliseconds;
var stream = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
var dockerStream = new DockerPipeStream(stream);

await stream.ConnectAsync(timeout, cancellationToken)
.ConfigureAwait(false);

return dockerStream;
});
break;

case "tcp":
case "http":
var builder = new UriBuilder(uri)
{
Scheme = Credentials.IsTlsCredentials() ? "https" : "http"
};
uri = builder.Uri;
handler = new ManagedHandler();
break;

case "https":
handler = new ManagedHandler();
break;

case "unix":
var pipeString = uri.LocalPath;
handler = new ManagedHandler(async (host, port, cancellationToken) =>
{
var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);

await sock.ConnectAsync(new Microsoft.Net.Http.Client.UnixDomainSocketEndPoint(pipeString))
.ConfigureAwait(false);

return sock;
});
uri = new UriBuilder("http", uri.Segments.Last()).Uri;
break;

default:
throw new Exception($"URL scheme {EndpointBaseUri.Scheme} is unsupported by this implementation.");
}

return (uri, handler);
}

private static Uri GetLocalDockerEndpoint()
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
return isWindows ? new Uri("npipe://./pipe/docker_engine") : new Uri("unix:/var/run/docker.sock");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private async Task<List<string>> ReadResponseLinesAsync(CancellationToken cancel
private HttpResponseMessage CreateResponseMessage(List<string> responseLines)
{
string responseLine = responseLines.First();

// HTTP/1.1 200 OK
string[] responseLineParts = responseLine.Split(new[] { ' ' }, 3);
// TODO: Verify HTTP/1.0 or 1.1.
Expand Down