-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathContainerImageReferenceUtility.cs
More file actions
173 lines (146 loc) · 5.98 KB
/
ContainerImageReferenceUtility.cs
File metadata and controls
173 lines (146 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// transcribed from https://github.com/containers/image/blob/c1a5f92d0ebbf9e0bf187b3353dd400472b388eb/docker/reference/reference.go
// Package reference provides a general type to represent any way of referencing images within the registry.
// Its main purpose is to abstract tags and digests (content-addressable hash).
//
// Grammar
//
// reference := name [ ":" tag ] [ "@" digest ]
// name := [domain '/'] path-component ['/' path-component]*
// domain := domain-component ['.' domain-component]* [':' port-number]
// domain-component := /([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])/
// port-number := /[0-9]+/
// path-component := alpha-numeric [separator alpha-numeric]*
// alpha-numeric := /[a-z0-9]+/
// separator := /[_.]|__|[-]*/
//
// tag := /[\w][\w.-]{0,127}/
//
// digest := digest-algorithm ":" digest-hex
// digest-algorithm := digest-algorithm-component [ digest-algorithm-separator digest-algorithm-component ]*
// digest-algorithm-separator := /[+.-_]/
// digest-algorithm-component := /[A-Za-z][A-Za-z0-9]*/
// digest-hex := /[0-9a-fA-F]{32,}/ ; At least 128 bit digest value
//
// identifier := /[a-f0-9]{64}/
// short-identifier := /[a-f0-9]{6,64}/
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.ComponentDetection.Contracts;
public static class ContainerImageReferenceUtility
{
// NameTotalLengthMax is the maximum total number of characters in a repository name.
private const int NameTotalLengthMax = 255;
private const string DEFAULTDOMAIN = "docker.io";
private const string LEGACYDEFAULTDOMAIN = "index.docker.io";
private const string OFFICIALREPOSITORYNAME = "library";
public static ContainerImageReference ParseQualifiedName(string qualifiedName)
{
var regexp = ContainerImageRegex.ReferenceRegexp;
if (!regexp.IsMatch(qualifiedName))
{
if (string.IsNullOrWhiteSpace(qualifiedName))
{
throw new ReferenceNameEmptyException(qualifiedName);
}
if (regexp.IsMatch(qualifiedName.ToLower()))
{
throw new ReferenceNameContainsUppercaseException(qualifiedName);
}
throw new ReferenceInvalidFormatException(qualifiedName);
}
var matches = regexp.Match(qualifiedName).Groups;
var name = matches[1].Value;
if (name.Length > NameTotalLengthMax)
{
throw new ReferenceNameTooLongException(name);
}
var reference = new Reference();
var nameMatch = ContainerImageRegex.AnchoredNameRegexp.Match(name).Groups;
if (nameMatch.Count == 3)
{
reference.Domain = nameMatch[1].Value;
reference.Repository = nameMatch[2].Value;
}
else
{
reference.Domain = string.Empty;
reference.Repository = matches[1].Value;
}
reference.Tag = matches[2].Value;
if (matches.Count > 3 && !string.IsNullOrEmpty(matches[3].Value))
{
DigestUtility.CheckDigest(matches[3].Value, true);
reference.Digest = matches[3].Value;
}
return CreateContainerImageReference(reference);
}
public static (string Domain, string Remainder) SplitDockerDomain(string name)
{
string domain;
string remainder;
var indexOfSlash = name.IndexOf('/');
if (indexOfSlash == -1 || !(
name.LastIndexOf('.', indexOfSlash) != -1 ||
name.LastIndexOf(':', indexOfSlash) != -1 ||
name.StartsWith("localhost/")))
{
domain = DEFAULTDOMAIN;
remainder = name;
}
else
{
domain = name[..indexOfSlash];
remainder = name[(indexOfSlash + 1)..];
}
if (domain == LEGACYDEFAULTDOMAIN)
{
domain = DEFAULTDOMAIN;
}
if (domain == DEFAULTDOMAIN && indexOfSlash == -1)
{
remainder = $"{OFFICIALREPOSITORYNAME}/{remainder}";
}
return (domain, remainder);
}
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Explicitly checks for character case.")]
public static ContainerImageReference ParseFamiliarName(string name)
{
if (ContainerImageRegex.AnchoredIdentifierRegexp.IsMatch(name))
{
throw new ReferenceNameNotCanonicalException(name);
}
(var domain, var remainder) = SplitDockerDomain(name);
string remoteName;
var tagSeparatorIndex = remainder.IndexOf(':');
if (tagSeparatorIndex > -1)
{
remoteName = remainder[..tagSeparatorIndex];
}
else
{
remoteName = remainder;
}
if (!string.Equals(remoteName.ToLowerInvariant(), remoteName, StringComparison.InvariantCulture))
{
throw new ReferenceNameContainsUppercaseException(name);
}
return ParseQualifiedName($"{domain}/{remainder}");
}
public static ContainerImageReference ParseAll(string name)
{
if (ContainerImageRegex.AnchoredIdentifierRegexp.IsMatch(name))
{
return CreateContainerImageReference(new Reference { Digest = $"sha256:{name}" });
}
if (DigestUtility.CheckDigest(name, false))
{
return CreateContainerImageReference(new Reference { Digest = name });
}
return ParseFamiliarName(name);
}
private static ContainerImageReference CreateContainerImageReference(Reference options)
{
return ContainerImageReference.CreateContainerImageReference(options.Repository, options.Domain, options.Digest, options.Tag);
}
}