1616
1717package com .google .cloud .tools .jib .maven ;
1818
19- import com .google .api .client .util .Base64 ;
2019import com .google .cloud .tools .jib .builder .BuildConfiguration ;
2120import com .google .cloud .tools .jib .builder .RetrieveRegistryCredentialsStep ;
2221import com .google .cloud .tools .jib .http .Authorization ;
2322import com .google .cloud .tools .jib .http .Authorizations ;
2423import com .google .cloud .tools .jib .image .ImageReference ;
24+ import com .google .cloud .tools .jib .image .InvalidImageReferenceException ;
2525import com .google .cloud .tools .jib .registry .credentials .NonexistentDockerCredentialHelperException ;
2626import com .google .cloud .tools .jib .registry .credentials .RegistryCredentials ;
27- import java .io .IOException ;
28- import java .nio .charset .StandardCharsets ;
29- import java .util .HashMap ;
30- import java .util .List ;
31- import java .util .Map ;
32- import javax .annotation .Nullable ;
3327import org .apache .maven .execution .MavenSession ;
3428import org .apache .maven .plugin .AbstractMojo ;
3529import org .apache .maven .plugin .MojoExecutionException ;
3832import org .apache .maven .plugins .annotations .Parameter ;
3933import org .apache .maven .settings .Server ;
4034
41- @ Mojo (name = "targetImageCredentials" )
35+ import javax .annotation .Nullable ;
36+ import java .io .IOException ;
37+ import java .util .HashMap ;
38+ import java .util .List ;
39+ import java .util .Map ;
40+
41+ @ Mojo (name ="targetImageCredentials" )
4242public class TargetImageCredentialsMojo extends AbstractMojo {
4343
4444 @ Parameter (defaultValue = "${session}" , readonly = true )
4545 private MavenSession session ;
4646
47+ @ Parameter (defaultValue = "gcr.io/distroless/java" , required = true )
48+ private String from ;
49+
4750 @ Parameter private String registry ;
4851
4952 @ Parameter private List <String > credHelpers ;
5053
5154 @ Override
5255 public void execute () throws MojoExecutionException , MojoFailureException {
53- if (registry == null ) {
54- registry = ImageReference .getDefaultRegistry ();
55- }
56+ // Parses 'from' into image reference.
57+ ImageReference baseImage = getBaseImageReference ();
5658
5759 // Checks Maven settings for registry credentials.
60+ session .getSettings ().getServer (baseImage .getRegistry ());
5861 Map <String , Authorization > registryCredentials = new HashMap <>(2 );
62+ // Retrieves credentials for the base image registry.
63+ Authorization baseImageRegistryCredentials =
64+ getRegistryCredentialsFromSettings (baseImage .getRegistry ());
65+ if (baseImageRegistryCredentials != null ) {
66+ registryCredentials .put (baseImage .getRegistry (), baseImageRegistryCredentials );
67+ }
5968 // Retrieves credentials for the target registry.
6069 Authorization targetRegistryCredentials = getRegistryCredentialsFromSettings (registry );
6170 if (targetRegistryCredentials != null ) {
@@ -66,49 +75,34 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6675
6776 BuildConfiguration buildConfiguration =
6877 BuildConfiguration .builder (new MavenBuildLogger (getLog ()))
69- .setBaseImage (ImageReference . of ( null , "ignored" , null ) )
78+ .setBaseImage (baseImage )
7079 .setTargetImage (ImageReference .of (registry , "ignored" , null ))
7180 .setCredentialHelperNames (credHelpers )
7281 .setKnownRegistryCredentials (mavenSettingsCredentials )
7382 .setMainClass ("ignored" )
7483 .build ();
7584
76- RetrieveRegistryCredentialsStep retrieveRegistryCredentialsStep =
77- new RetrieveRegistryCredentialsStep (buildConfiguration , registry );
85+ RetrieveRegistryCredentialsStep retrieveRegistryCredentialsStep = new RetrieveRegistryCredentialsStep (buildConfiguration , registry );
7886 try {
7987 Authorization authorization = retrieveRegistryCredentialsStep .call ();
80- if (authorization == null ) {
81- throw new MojoExecutionException ("Could not retrieve credentials for " + registry );
82- }
83-
84- if (!"Basic" .equals (authorization .getScheme ())) {
85- throw new MojoExecutionException ("Credentials not retrieve correctly" );
86- }
87-
8888 System .out .println ("STARTAUTH" );
89-
90- // Split a token into username and secret using the 'username:secret' format. Uses first colon
91- // to split.
92- String token =
93- new String (Base64 .decodeBase64 (authorization .getToken ()), StandardCharsets .UTF_8 );
94- int colonIndex = token .indexOf (':' );
95- String username = token .substring (0 , colonIndex );
96- String secret = token .substring (colonIndex + 1 );
97-
98- System .out .println (username );
99- System .out .println (secret );
100-
101- Server server = new Server ();
102- server .setId ("_jib_credentials" );
103- server .setUsername (username );
104- server .setPassword (secret );
105- session .getSettings ().addServer (server );
89+ System .out .println (authorization );
10690
10791 } catch (IOException | NonexistentDockerCredentialHelperException ex ) {
10892 throw new MojoExecutionException ("Retrieve credentials failed" , ex );
10993 }
11094 }
11195
96+ /** @return the {@link ImageReference} parsed from {@link #from}. */
97+ private ImageReference getBaseImageReference () throws MojoFailureException {
98+ try {
99+ return ImageReference .parse (from );
100+
101+ } catch (InvalidImageReferenceException ex ) {
102+ throw new MojoFailureException ("Parameter 'from' is invalid" , ex );
103+ }
104+ }
105+
112106 /** Attempts to retrieve credentials for {@code registry} from Maven settings. */
113107 @ Nullable
114108 private Authorization getRegistryCredentialsFromSettings (String registry ) {
0 commit comments