1616
1717package com .google .cloud .tools .jib .maven ;
1818
19+ import com .google .api .client .util .Base64 ;
1920import com .google .cloud .tools .jib .builder .BuildConfiguration ;
2021import com .google .cloud .tools .jib .builder .RetrieveRegistryCredentialsStep ;
2122import com .google .cloud .tools .jib .http .Authorization ;
2223import com .google .cloud .tools .jib .http .Authorizations ;
2324import 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 ;
2733import org .apache .maven .execution .MavenSession ;
2834import org .apache .maven .plugin .AbstractMojo ;
2935import org .apache .maven .plugin .MojoExecutionException ;
3238import org .apache .maven .plugins .annotations .Parameter ;
3339import org .apache .maven .settings .Server ;
3440
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" )
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-
5047 @ Parameter private String registry ;
5148
5249 @ Parameter private List <String > credHelpers ;
5350
5451 @ Override
5552 public void execute () throws MojoExecutionException , MojoFailureException {
56- // Parses 'from' into image reference.
57- ImageReference baseImage = getBaseImageReference ();
53+ if (registry == null ) {
54+ registry = ImageReference .getDefaultRegistry ();
55+ }
5856
5957 // Checks Maven settings for registry credentials.
60- session .getSettings ().getServer (baseImage .getRegistry ());
6158 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- }
6859 // Retrieves credentials for the target registry.
6960 Authorization targetRegistryCredentials = getRegistryCredentialsFromSettings (registry );
7061 if (targetRegistryCredentials != null ) {
@@ -75,31 +66,46 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7566
7667 BuildConfiguration buildConfiguration =
7768 BuildConfiguration .builder (new MavenBuildLogger (getLog ()))
78- .setBaseImage (baseImage )
69+ .setBaseImage (ImageReference . of ( null , "ignored" , null ) )
7970 .setTargetImage (ImageReference .of (registry , "ignored" , null ))
8071 .setCredentialHelperNames (credHelpers )
8172 .setKnownRegistryCredentials (mavenSettingsCredentials )
8273 .setMainClass ("ignored" )
8374 .build ();
8475
85- RetrieveRegistryCredentialsStep retrieveRegistryCredentialsStep = new RetrieveRegistryCredentialsStep (buildConfiguration , registry );
76+ RetrieveRegistryCredentialsStep retrieveRegistryCredentialsStep =
77+ new RetrieveRegistryCredentialsStep (buildConfiguration , registry );
8678 try {
8779 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- System .out .println (authorization );
9089
91- } catch (IOException | NonexistentDockerCredentialHelperException ex ) {
92- throw new MojoExecutionException ("Retrieve credentials failed" , ex );
93- }
94- }
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 );
9597
96- /** @return the {@link ImageReference} parsed from {@link #from}. */
97- private ImageReference getBaseImageReference () throws MojoFailureException {
98- try {
99- return ImageReference .parse (from );
98+ System .out .println (username );
99+ System .out .println (secret );
100100
101- } catch (InvalidImageReferenceException ex ) {
102- throw new MojoFailureException ("Parameter 'from' is invalid" , ex );
101+ Server server = new Server ();
102+ server .setId ("_jib_credentials" );
103+ server .setUsername (username );
104+ server .setPassword (secret );
105+ session .getSettings ().addServer (server );
106+
107+ } catch (IOException | NonexistentDockerCredentialHelperException ex ) {
108+ throw new MojoExecutionException ("Retrieve credentials failed" , ex );
103109 }
104110 }
105111
0 commit comments