27
27
import org .springframework .boot .buildpack .platform .docker .TotalProgressPushListener ;
28
28
import org .springframework .boot .buildpack .platform .docker .UpdateListener ;
29
29
import org .springframework .boot .buildpack .platform .docker .configuration .DockerConfiguration ;
30
+ import org .springframework .boot .buildpack .platform .docker .configuration .DockerRegistryAuthentication ;
30
31
import org .springframework .boot .buildpack .platform .docker .configuration .ResolvedDockerHost ;
31
32
import org .springframework .boot .buildpack .platform .docker .transport .DockerEngineException ;
32
33
import org .springframework .boot .buildpack .platform .docker .type .Binding ;
@@ -102,9 +103,8 @@ public void build(BuildRequest request) throws DockerEngineException, IOExceptio
102
103
Assert .notNull (request , "'request' must not be null" );
103
104
this .log .start (request );
104
105
validateBindings (request .getBindings ());
105
- String domain = request .getBuilder ().getDomain ();
106
106
PullPolicy pullPolicy = request .getPullPolicy ();
107
- ImageFetcher imageFetcher = new ImageFetcher (domain , getBuilderAuthHeader (), pullPolicy ,
107
+ ImageFetcher imageFetcher = new ImageFetcher (getBuilderRegistryAuthentication (), pullPolicy ,
108
108
request .getImagePlatform ());
109
109
Image builderImage = imageFetcher .fetchImage (ImageType .BUILDER , request .getBuilder ());
110
110
BuilderMetadata builderMetadata = BuilderMetadata .fromImage (builderImage );
@@ -203,64 +203,61 @@ private void pushImages(ImageReference name, List<ImageReference> tags) throws I
203
203
private void pushImage (ImageReference reference ) throws IOException {
204
204
Consumer <TotalProgressEvent > progressConsumer = this .log .pushingImage (reference );
205
205
TotalProgressPushListener listener = new TotalProgressPushListener (progressConsumer );
206
- this .docker .image ().push (reference , listener , getPublishAuthHeader ());
206
+ this .docker .image ().push (reference , listener , getPublishAuthHeader (reference ));
207
207
this .log .pushedImage (reference );
208
208
}
209
209
210
- private String getBuilderAuthHeader () {
211
- return (this .dockerConfiguration != null && this .dockerConfiguration .getBuilderRegistryAuthentication () != null )
212
- ? this .dockerConfiguration .getBuilderRegistryAuthentication ().getAuthHeader () : null ;
210
+ private DockerRegistryAuthentication getBuilderRegistryAuthentication () {
211
+ if (this .dockerConfiguration != null ) {
212
+ return this .dockerConfiguration .getBuilderRegistryAuthentication ();
213
+ }
214
+ return null ;
213
215
}
214
216
215
- private String getPublishAuthHeader () {
217
+ private String getPublishAuthHeader (ImageReference imageReference ) {
216
218
return (this .dockerConfiguration != null && this .dockerConfiguration .getPublishRegistryAuthentication () != null )
217
- ? this .dockerConfiguration .getPublishRegistryAuthentication ().getAuthHeader () : null ;
219
+ ? this .dockerConfiguration .getPublishRegistryAuthentication ().getAuthHeader (imageReference ) : null ;
218
220
}
219
221
220
222
/**
221
223
* Internal utility class used to fetch images.
222
224
*/
223
225
private class ImageFetcher {
224
226
225
- private final String domain ;
226
-
227
- private final String authHeader ;
227
+ private final DockerRegistryAuthentication authentication ;
228
228
229
229
private final PullPolicy pullPolicy ;
230
230
231
231
private ImagePlatform defaultPlatform ;
232
232
233
- ImageFetcher (String domain , String authHeader , PullPolicy pullPolicy , ImagePlatform platform ) {
234
- this .domain = domain ;
235
- this .authHeader = authHeader ;
233
+ ImageFetcher (DockerRegistryAuthentication authentication , PullPolicy pullPolicy , ImagePlatform platform ) {
234
+ this .authentication = authentication ;
236
235
this .pullPolicy = pullPolicy ;
237
236
this .defaultPlatform = platform ;
238
237
}
239
238
240
239
Image fetchImage (ImageType type , ImageReference reference ) throws IOException {
241
240
Assert .notNull (type , "'type' must not be null" );
242
241
Assert .notNull (reference , "'reference' must not be null" );
243
- Assert .state (this .authHeader == null || reference .getDomain ().equals (this .domain ),
244
- () -> String .format ("%s '%s' must be pulled from the '%s' authenticated registry" ,
245
- StringUtils .capitalize (type .getDescription ()), reference , this .domain ));
242
+ String authHeader = getAuthHeader (reference );
246
243
if (this .pullPolicy == PullPolicy .ALWAYS ) {
247
- return checkPlatformMismatch (pullImage (reference , type ), reference );
244
+ return checkPlatformMismatch (pullImage (authHeader , reference , type ), reference );
248
245
}
249
246
try {
250
247
return checkPlatformMismatch (Builder .this .docker .image ().inspect (reference ), reference );
251
248
}
252
249
catch (DockerEngineException ex ) {
253
250
if (this .pullPolicy == PullPolicy .IF_NOT_PRESENT && ex .getStatusCode () == 404 ) {
254
- return checkPlatformMismatch (pullImage (reference , type ), reference );
251
+ return checkPlatformMismatch (pullImage (authHeader , reference , type ), reference );
255
252
}
256
253
throw ex ;
257
254
}
258
255
}
259
256
260
- private Image pullImage (ImageReference reference , ImageType imageType ) throws IOException {
257
+ private Image pullImage (String authHeader , ImageReference reference , ImageType imageType ) throws IOException {
261
258
TotalProgressPullListener listener = new TotalProgressPullListener (
262
259
Builder .this .log .pullingImage (reference , this .defaultPlatform , imageType ));
263
- Image image = Builder .this .docker .image ().pull (reference , this .defaultPlatform , listener , this . authHeader );
260
+ Image image = Builder .this .docker .image ().pull (reference , this .defaultPlatform , listener , authHeader );
264
261
Builder .this .log .pulledImage (image , imageType );
265
262
if (this .defaultPlatform == null ) {
266
263
this .defaultPlatform = ImagePlatform .from (image );
@@ -278,6 +275,10 @@ private Image checkPlatformMismatch(Image image, ImageReference imageReference)
278
275
return image ;
279
276
}
280
277
278
+ private String getAuthHeader (ImageReference reference ) {
279
+ return (this .authentication != null ) ? this .authentication .getAuthHeader (reference ) : null ;
280
+ }
281
+
281
282
}
282
283
283
284
private static final class PlatformMismatchException extends RuntimeException {
0 commit comments