@@ -41,48 +41,66 @@ class RetrieveRegistryCredentialsStep implements Callable<Authorization> {
4141 private static final ImmutableMap <String , String > COMMON_CREDENTIAL_HELPERS =
4242 ImmutableMap .of ("gcr.io" , "gcr" , "amazonaws.com" , "ecr-login" );
4343
44- private final BuildConfiguration buildConfiguration ;
44+ /** Retrieves credentials for the base image. */
45+ static RetrieveRegistryCredentialsStep forBaseImage (BuildConfiguration buildConfiguration ) {
46+ return new RetrieveRegistryCredentialsStep (
47+ buildConfiguration .getBuildLogger (),
48+ buildConfiguration .getBaseImageRegistry (),
49+ buildConfiguration .getBaseImageCredentialHelperName (),
50+ buildConfiguration .getKnownBaseRegistryCredentials ());
51+ }
52+
53+ /** Retrieves credentials for the target image. */
54+ static RetrieveRegistryCredentialsStep forTargetImage (BuildConfiguration buildConfiguration ) {
55+ return new RetrieveRegistryCredentialsStep (
56+ buildConfiguration .getBuildLogger (),
57+ buildConfiguration .getTargetImageRegistry (),
58+ buildConfiguration .getTargetImageCredentialHelperName (),
59+ buildConfiguration .getKnownTargetRegistryCredentials ());
60+ }
61+
62+ private final BuildLogger buildLogger ;
4563 private final String registry ;
4664 @ Nullable private final String credentialHelperSuffix ;
4765 @ Nullable private final RegistryCredentials knownRegistryCredentials ;
4866 private final DockerCredentialHelperFactory dockerCredentialHelperFactory ;
4967 private final DockerConfigCredentialRetriever dockerConfigCredentialRetriever ;
5068
51- RetrieveRegistryCredentialsStep (
52- BuildConfiguration buildConfiguration ,
53- String registry ,
54- @ Nullable String credentialHelperSuffix ,
55- @ Nullable RegistryCredentials knownRegistryCredentials ) {
56- this (
57- buildConfiguration ,
58- registry ,
59- credentialHelperSuffix ,
60- knownRegistryCredentials ,
61- new DockerCredentialHelperFactory (registry ),
62- new DockerConfigCredentialRetriever (registry ));
63- }
64-
6569 @ VisibleForTesting
6670 RetrieveRegistryCredentialsStep (
67- BuildConfiguration buildConfiguration ,
71+ BuildLogger buildLogger ,
6872 String registry ,
6973 @ Nullable String credentialHelperSuffix ,
7074 @ Nullable RegistryCredentials knownRegistryCredentials ,
7175 DockerCredentialHelperFactory dockerCredentialHelperFactory ,
7276 DockerConfigCredentialRetriever dockerConfigCredentialRetriever ) {
73- this .buildConfiguration = buildConfiguration ;
77+ this .buildLogger = buildLogger ;
7478 this .registry = registry ;
7579 this .credentialHelperSuffix = credentialHelperSuffix ;
7680 this .knownRegistryCredentials = knownRegistryCredentials ;
7781 this .dockerCredentialHelperFactory = dockerCredentialHelperFactory ;
7882 this .dockerConfigCredentialRetriever = dockerConfigCredentialRetriever ;
7983 }
8084
85+ /** Instantiate with {@link #forBaseImage} or {@link #forTargetImage}. */
86+ private RetrieveRegistryCredentialsStep (
87+ BuildLogger buildLogger ,
88+ String registry ,
89+ @ Nullable String credentialHelperSuffix ,
90+ @ Nullable RegistryCredentials knownRegistryCredentials ) {
91+ this (
92+ buildLogger ,
93+ registry ,
94+ credentialHelperSuffix ,
95+ knownRegistryCredentials ,
96+ new DockerCredentialHelperFactory (registry ),
97+ new DockerConfigCredentialRetriever (registry ));
98+ }
99+
81100 @ Override
82101 @ Nullable
83102 public Authorization call () throws IOException , NonexistentDockerCredentialHelperException {
84- try (Timer ignored =
85- new Timer (buildConfiguration .getBuildLogger (), String .format (DESCRIPTION , registry ))) {
103+ try (Timer ignored = new Timer (buildLogger , String .format (DESCRIPTION , registry ))) {
86104 // Tries to get registry credentials from Docker credential helpers.
87105 if (credentialHelperSuffix != null ) {
88106 Authorization authorization = retrieveFromCredentialHelper (credentialHelperSuffix );
@@ -101,14 +119,12 @@ public Authorization call() throws IOException, NonexistentDockerCredentialHelpe
101119 try {
102120 Authorization dockerConfigAuthorization = dockerConfigCredentialRetriever .retrieve ();
103121 if (dockerConfigAuthorization != null ) {
104- buildConfiguration
105- .getBuildLogger ()
106- .info ("Using credentials from Docker config for " + registry );
122+ buildLogger .info ("Using credentials from Docker config for " + registry );
107123 return dockerConfigAuthorization ;
108124 }
109125
110126 } catch (IOException ex ) {
111- buildConfiguration . getBuildLogger () .info ("Unable to parse Docker config" );
127+ buildLogger .info ("Unable to parse Docker config" );
112128 }
113129
114130 // Tries to infer common credential helpers for known registries.
@@ -128,7 +144,7 @@ public Authorization call() throws IOException, NonexistentDockerCredentialHelpe
128144 if (ex .getMessage () != null ) {
129145 // Warns the user that the specified (or inferred) credential helper is not on the
130146 // system.
131- buildConfiguration . getBuildLogger () .warn (ex .getMessage ());
147+ buildLogger .warn (ex .getMessage ());
132148 }
133149 }
134150 }
@@ -138,9 +154,7 @@ public Authorization call() throws IOException, NonexistentDockerCredentialHelpe
138154 * If no credentials found, give an info (not warning because in most cases, the base image is
139155 * public and does not need extra credentials) and return null.
140156 */
141- buildConfiguration
142- .getBuildLogger ()
143- .info ("No credentials could be retrieved for registry " + registry );
157+ buildLogger .info ("No credentials could be retrieved for registry " + registry );
144158 return null ;
145159 }
146160 }
@@ -153,9 +167,7 @@ public Authorization call() throws IOException, NonexistentDockerCredentialHelpe
153167 @ Nullable
154168 Authorization retrieveFromCredentialHelper (String credentialHelperSuffix )
155169 throws NonexistentDockerCredentialHelperException , IOException {
156- buildConfiguration
157- .getBuildLogger ()
158- .info ("Checking credentials from docker-credential-" + credentialHelperSuffix );
170+ buildLogger .info ("Checking credentials from docker-credential-" + credentialHelperSuffix );
159171
160172 try {
161173 Authorization authorization =
@@ -166,15 +178,13 @@ Authorization retrieveFromCredentialHelper(String credentialHelperSuffix)
166178 return authorization ;
167179
168180 } catch (NonexistentServerUrlDockerCredentialHelperException ex ) {
169- buildConfiguration
170- .getBuildLogger ()
171- .info (
172- "No credentials for " + registry + " in docker-credential-" + credentialHelperSuffix );
181+ buildLogger .info (
182+ "No credentials for " + registry + " in docker-credential-" + credentialHelperSuffix );
173183 return null ;
174184 }
175185 }
176186
177187 private void logGotCredentialsFrom (String credentialSource ) {
178- buildConfiguration . getBuildLogger () .info ("Using " + credentialSource + " for " + registry );
188+ buildLogger .info ("Using " + credentialSource + " for " + registry );
179189 }
180190}
0 commit comments