Skip to content

Commit

Permalink
Fix EA image check to work wiht local registries (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpuranik authored May 15, 2019
1 parent 378e831 commit 2086d4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Docker
{
using System;
using System.Linq;
using System.Text.RegularExpressions;
using global::Docker.DotNet.Models;
using Microsoft.Azure.Devices.Edge.Util;
using Newtonsoft.Json;
Expand All @@ -12,6 +13,10 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Docker
[JsonConverter(typeof(DockerConfigJsonConverter))]
public class DockerConfig : IEquatable<DockerConfig>
{
// This is not the actual docker image regex, but a less strict version.
const string ImageRegexPattern = @"^(?<repo>([^/]*/)*)(?<image>[^/:]+)(?<tag>:[^/:]+)?$";

static readonly Regex ImageRegex = new Regex(ImageRegexPattern);
readonly CreateContainerParameters createOptions;

public DockerConfig(string image)
Expand Down Expand Up @@ -78,22 +83,22 @@ internal static CreateContainerParameters GetCreateOptions(string createOptions)
internal static string ValidateAndGetImage(string image)
{
image = Preconditions.CheckNonWhiteSpace(image, nameof(image)).Trim();
string[] parts = image.Split(':');
string imageWithTag;
if (parts.Length == 2)
{
imageWithTag = image;
}
else if (parts.Length == 1)
Match match = ImageRegex.Match(image);
if (match.Success)
{
imageWithTag = Invariant($"{image}:{Constants.DefaultTag}");
if (match.Groups["tag"]?.Length > 0)
{
return image;
}
else
{
return Invariant($"{image}:{Constants.DefaultTag}");
}
}
else
{
throw new ArgumentException($"Image {image} is not in the right format");
}

return imageWithTag;
}

internal static bool CompareCreateOptions(CreateContainerParameters a, CreateContainerParameters b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ public void TestSerialization()
[InlineData("ea:t1", "ea:t1", null)]
[InlineData(" ea:t1 ", "ea:t1", null)]
[InlineData("mcr.ms.com/ea:t1", "mcr.ms.com/ea:t1", null)]
[InlineData("mcr.ms.com/ms/ea:t1", "mcr.ms.com/ms/ea:t1", null)]
[InlineData("mcr.ms.com/ea", "mcr.ms.com/ea:latest", null)]
[InlineData(" ubuntu ", "ubuntu:latest", null)]
[InlineData("localhost:9000/ea", "localhost:9000/ea:latest", null)]
[InlineData("localhost:9000/ea:tag1", "localhost:9000/ea:tag1", null)]
[InlineData("localhost:9000/comp/ea:tag1", "localhost:9000/comp/ea:tag1", null)]
public void TestValidateAndGetImage(string image, string result, Type expectedException)
{
if (expectedException != null)
Expand Down

0 comments on commit 2086d4b

Please sign in to comment.