1- using System ;
1+ using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . IO ;
5+ using System . Linq ;
46using Microsoft . Extensions . Configuration ;
7+ using Newtonsoft . Json ;
8+ using JsonSerializer = System . Text . Json . JsonSerializer ;
59
610namespace Squadron
711{
8- /// <summary>
9- /// Abstract base class for container resource options
10- /// </summary>
12+ /// <summary>
13+ /// Abstract base class for container resource options
14+ /// </summary>
1115 public abstract class ContainerResourceOptions
1216 {
13- /// <summary>
14- /// Configures the resource
15- /// </summary>
16- /// <param name="builder">The builder.</param>
17+ /// <summary>
18+ /// Configures the resource
19+ /// </summary>
20+ /// <param name="builder">The builder.</param>
1721 public abstract void Configure ( ContainerResourceBuilder builder ) ;
1822
19-
23+
2024 public static DockerConfiguration DefaultDockerConfigResolver ( )
2125 {
2226 IConfigurationRoot configuration = new ConfigurationBuilder ( )
@@ -27,8 +31,78 @@ public static DockerConfiguration DefaultDockerConfigResolver()
2731
2832 IConfigurationSection section = configuration . GetSection ( "Squadron:Docker" ) ;
2933
30- DockerConfiguration containerConfig = section . Get < DockerConfiguration > ( ) ;
31- return containerConfig ?? new DockerConfiguration ( ) ;
34+ DockerConfiguration containerConfig = section . Get < DockerConfiguration > ( ) ?? new DockerConfiguration ( ) ;
35+
36+ AddLocalDockerAuthentication ( containerConfig ) ;
37+
38+ return containerConfig ;
39+ }
40+
41+ private static void AddLocalDockerAuthentication ( DockerConfiguration containerConfig )
42+ {
43+ var dockerAuthRootObject = TryGetDockerAuthRootObject ( ) ;
44+
45+ if ( dockerAuthRootObject != null )
46+ {
47+ foreach ( var auth in dockerAuthRootObject . Auths )
48+ {
49+ if ( ! Uri . TryCreate ( auth . Key , UriKind . RelativeOrAbsolute , out Uri address ) )
50+ {
51+ continue ;
52+ }
53+
54+ if ( containerConfig . Registries . Any ( p =>
55+ p . Address . Equals ( address . ToString ( ) , StringComparison . InvariantCultureIgnoreCase ) ) ||
56+ string . IsNullOrEmpty ( auth . Value . Email ) ||
57+ string . IsNullOrEmpty ( auth . Value . Auth ) )
58+ {
59+ continue ;
60+ }
61+
62+ var decryptedToken = Convert . FromBase64String ( auth . Value . Auth ) ;
63+ var token = System . Text . Encoding . UTF8 . GetString ( decryptedToken ) ;
64+ var parts = token . Split ( ':' ) ;
65+
66+ if ( parts . Length != 2 )
67+ {
68+ continue ;
69+ }
70+
71+ containerConfig . Registries . Add ( new DockerRegistryConfiguration
72+ {
73+ Name = address . Host ,
74+ Address = address . ToString ( ) ,
75+ Username = parts [ 0 ] ,
76+ Password = parts [ 1 ]
77+ } ) ;
78+ }
79+ }
80+ }
81+
82+ private static DockerAuthRootObject ? TryGetDockerAuthRootObject ( )
83+ {
84+ var dockerConfigPath = Environment . GetEnvironmentVariable ( "DOCKER_CONFIG" ) ;
85+ if ( string . IsNullOrEmpty ( dockerConfigPath ) )
86+ {
87+ return null ;
88+ }
89+
90+ var configFilePath = Path . Combine ( dockerConfigPath , "config.json" ) ;
91+
92+ if ( ! File . Exists ( configFilePath ) )
93+ {
94+ return null ;
95+ }
96+
97+ try
98+ {
99+ var jsonString = File . ReadAllText ( configFilePath ) ;
100+
101+ return JsonSerializer . Deserialize < DockerAuthRootObject > ( jsonString ) ;
102+ }
103+ catch { }
104+
105+ return null ;
32106 }
33107 }
34108}
0 commit comments