22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Diagnostics ;
5+ #if NET
56using System . Formats . Tar ;
7+ #endif
68using System . Text . Json ;
79using System . Text . Json . Nodes ;
810using Microsoft . DotNet . Cli . Utils ;
1214namespace Microsoft . NET . Build . Containers ;
1315
1416// Wraps the 'docker'/'podman' cli.
15- internal sealed class DockerCli : ILocalRegistry
17+ internal sealed class DockerCli
18+ #if NET
19+ : ILocalRegistry
20+ #endif
1621{
1722 public const string DockerCommand = "docker" ;
1823 public const string PodmanCommand = "podman" ;
@@ -21,7 +26,10 @@ internal sealed class DockerCli : ILocalRegistry
2126
2227 private readonly ILogger _logger ;
2328 private string ? _command ;
29+
30+ #if NET
2431 private string ? _fullCommandPath ;
32+ #endif
2533
2634 public DockerCli ( string ? command , ILoggerFactory loggerFactory )
2735 {
@@ -53,6 +61,7 @@ private static string FindFullPathFromPath(string command)
5361 return command ;
5462 }
5563
64+ #if NET
5665 private async ValueTask < string > FindFullCommandPath ( CancellationToken cancellationToken )
5766 {
5867 if ( _fullCommandPath != null )
@@ -162,6 +171,7 @@ public bool IsAvailable()
162171
163172 public string ? GetCommand ( )
164173 => GetCommandAsync ( default ) . GetAwaiter ( ) . GetResult ( ) ;
174+ #endif
165175
166176 /// <summary>
167177 /// Gets docker configuration.
@@ -183,7 +193,6 @@ internal static JsonDocument GetDockerConfig()
183193 dockerCommand . CaptureStdErr ( ) ;
184194 CommandResult dockerCommandResult = dockerCommand . Execute ( ) ;
185195
186-
187196 if ( dockerCommandResult . ExitCode != 0 )
188197 {
189198 throw new DockerLoadException ( Resource . FormatString (
@@ -194,17 +203,68 @@ internal static JsonDocument GetDockerConfig()
194203 }
195204
196205 return JsonDocument . Parse ( dockerCommandResult . StdOut ) ;
197-
198-
199206 }
200207 catch ( Exception e ) when ( e is not DockerLoadException )
201208 {
202209 throw new DockerLoadException ( Resource . FormatString ( nameof ( Strings . DockerInfoFailed_Ex ) , e . Message ) ) ;
203210 }
204211 }
212+ /// <summary>
213+ /// Checks if the registry is marked as insecure in the docker/podman config.
214+ /// </summary>
215+ /// <param name="registryDomain"></param>
216+ /// <returns></returns>
217+ public static bool IsInsecureRegistry ( string registryDomain )
218+ {
219+ try
220+ {
221+ //check the docker config to see if the registry is marked as insecure
222+ var rootElement = GetDockerConfig ( ) . RootElement ;
223+
224+ //for docker
225+ if ( rootElement . TryGetProperty ( "RegistryConfig" , out var registryConfig ) && registryConfig . ValueKind == JsonValueKind . Object )
226+ {
227+ if ( registryConfig . TryGetProperty ( "IndexConfigs" , out var indexConfigs ) && indexConfigs . ValueKind == JsonValueKind . Object )
228+ {
229+ foreach ( var property in indexConfigs . EnumerateObject ( ) )
230+ {
231+ if ( property . Value . ValueKind == JsonValueKind . Object && property . Value . TryGetProperty ( "Secure" , out var secure ) && ! secure . GetBoolean ( ) )
232+ {
233+ if ( property . Name . Equals ( registryDomain , StringComparison . Ordinal ) )
234+ {
235+ return true ;
236+ }
237+ }
238+ }
239+ }
240+ }
241+
242+ //for podman
243+ if ( rootElement . TryGetProperty ( "registries" , out var registries ) && registries . ValueKind == JsonValueKind . Object )
244+ {
245+ foreach ( var property in registries . EnumerateObject ( ) )
246+ {
247+ if ( property . Value . ValueKind == JsonValueKind . Object && property . Value . TryGetProperty ( "Insecure" , out var insecure ) && insecure . GetBoolean ( ) )
248+ {
249+ if ( property . Name . Equals ( registryDomain , StringComparison . Ordinal ) )
250+ {
251+ return true ;
252+ }
253+ }
254+ }
255+ }
256+ return false ;
257+ }
258+ catch ( DockerLoadException )
259+ {
260+ //if docker load fails, we can't check the config so we assume the registry is secure
261+ return false ;
262+ }
263+ }
205264
206265 private static void Proc_OutputDataReceived ( object sender , DataReceivedEventArgs e ) => throw new NotImplementedException ( ) ;
207266
267+ #if NET
208268 public static async Task WriteImageToStreamAsync ( BuiltImage image , SourceImageReference sourceReference , DestinationImageReference destinationReference , Stream imageStream , CancellationToken cancellationToken )
209269 {
210270 cancellationToken . ThrowIfCancellationRequested ( ) ;
@@ -310,6 +370,7 @@ await Task.WhenAll(
310370
311371 return _command ;
312372 }
373+ #endif
313374
314375 private static bool IsPodmanAlias ( )
315376 {
@@ -331,6 +392,7 @@ private static bool IsPodmanAlias()
331392 }
332393 }
333394
395+ #if NET
334396 private async Task < bool > TryRunVersionCommandAsync ( string command , CancellationToken cancellationToken )
335397 {
336398 try
@@ -353,6 +415,7 @@ private async Task<bool> TryRunVersionCommandAsync(string command, CancellationT
353415 return false ;
354416 }
355417 }
418+ #endif
356419
357420 public override string ToString ( )
358421 {
0 commit comments