Files
runner/src/Runner.Worker/Container/DockerUtil.cs
David Kale 4e85b8f3b7 Allow registry credentials for job/service containers (#694)
* Log in with container credentials if given

* Stub in registry aware auth for later

* Fix hang if password is empty

* Remove default param to fix build

* PR Feedback. Add some tests and fix parse
2020-09-11 12:28:58 -04:00

66 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace GitHub.Runner.Worker.Container
{
public class DockerUtil
{
public static List<PortMapping> ParseDockerPort(IList<string> portMappingLines)
{
const string targetPort = "targetPort";
const string proto = "proto";
const string host = "host";
const string hostPort = "hostPort";
//"TARGET_PORT/PROTO -> HOST:HOST_PORT"
string pattern = $"^(?<{targetPort}>\\d+)/(?<{proto}>\\w+) -> (?<{host}>.+):(?<{hostPort}>\\d+)$";
List<PortMapping> portMappings = new List<PortMapping>();
foreach(var line in portMappingLines)
{
Match m = Regex.Match(line, pattern, RegexOptions.None, TimeSpan.FromSeconds(1));
if (m.Success)
{
portMappings.Add(new PortMapping(
m.Groups[hostPort].Value,
m.Groups[targetPort].Value,
m.Groups[proto].Value
));
}
}
return portMappings;
}
public static string ParsePathFromConfigEnv(IList<string> configEnvLines)
{
// Config format is VAR=value per line
foreach (var line in configEnvLines)
{
var keyValue = line.Split("=", 2);
if (keyValue.Length == 2 && string.Equals(keyValue[0], "PATH"))
{
return keyValue[1];
}
}
return "";
}
public static string ParseRegistryHostnameFromImageName(string name)
{
var nameSplit = name.Split('/');
// Single slash is implictly from Dockerhub, unless first part has .tld or :port
if (nameSplit.Length == 2 && (nameSplit[0].Contains(":") || nameSplit[0].Contains(".")))
{
return nameSplit[0];
}
// All other non Dockerhub registries
else if (nameSplit.Length > 2)
{
return nameSplit[0];
}
return "";
}
}
}