Skip to content

Commit b9c1566

Browse files
authored
Fixes validate tag nullpointerexception. (#127)
* Fixes validate tag nullpointerexception. * Fixes ImageReference string form logging.
1 parent 5aaef64 commit b9c1566

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Changed
88

99
### Fixed
10+
- Null tag validation generating NullPointerException ([#125](https://github.com/google/jib/issues/125))
1011

1112
## 0.1.3
1213
### Added

jib-core/src/main/java/com/google/cloud/tools/jib/image/ImageReference.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ImageReference {
3333

3434
private static final String DOCKER_HUB_REGISTRY = "registry.hub.docker.com";
3535
private static final String DEFAULT_TAG = "latest";
36+
private static final String LIBRARY_REPOSITORY_PREFIX = "library/";
3637

3738
/**
3839
* Matches all sequences of alphanumeric characters possibly separated by any number of dashes in
@@ -112,7 +113,7 @@ public static ImageReference parse(String reference) throws InvalidImageReferenc
112113
* See https://docs.docker.com/engine/reference/commandline/pull/#pull-an-image-from-docker-hub
113114
*/
114115
if (DOCKER_HUB_REGISTRY.equals(registry) && repository.indexOf('/') < 0) {
115-
repository = "library/" + repository;
116+
repository = LIBRARY_REPOSITORY_PREFIX + repository;
116117
}
117118

118119
if (!Strings.isNullOrEmpty(tag)) {
@@ -132,10 +133,10 @@ public static ImageReference parse(String reference) throws InvalidImageReferenc
132133
/** Builds an image reference from a registry, repository, and tag. */
133134
public static ImageReference of(
134135
@Nullable String registry, String repository, @Nullable String tag) {
135-
if (registry == null) {
136+
if (Strings.isNullOrEmpty(registry)) {
136137
registry = DOCKER_HUB_REGISTRY;
137138
}
138-
if (tag == null) {
139+
if (Strings.isNullOrEmpty(tag)) {
139140
tag = DEFAULT_TAG;
140141
}
141142
return new ImageReference(registry, repository, tag);
@@ -178,4 +179,30 @@ public String getRepository() {
178179
public String getTag() {
179180
return tag;
180181
}
182+
183+
/** @return the image reference in Docker-readable format (inverse of {@link #parse}) */
184+
@Override
185+
public String toString() {
186+
StringBuilder referenceString = new StringBuilder();
187+
188+
if (!DOCKER_HUB_REGISTRY.equals(registry)) {
189+
// Use registry and repository if not Docker Hub.
190+
referenceString.append(registry).append('/').append(repository);
191+
192+
} else if (repository.startsWith(LIBRARY_REPOSITORY_PREFIX)) {
193+
// If Docker Hub and repository has 'library/' prefix, remove the 'library/' prefix.
194+
referenceString.append(repository.substring(LIBRARY_REPOSITORY_PREFIX.length()));
195+
196+
} else {
197+
// Use just repository if Docker Hub.
198+
referenceString.append(repository);
199+
}
200+
201+
// Use tag if not the default tag.
202+
if (!DEFAULT_TAG.equals(tag)) {
203+
referenceString.append(':').append(tag);
204+
}
205+
206+
return referenceString.toString();
207+
}
181208
}

jib-core/src/test/java/com/google/cloud/tools/jib/image/ImageReferenceTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ public void testOf_smoke() {
120120
expectedRepository, ImageReference.of(null, expectedRepository, null).getRepository());
121121
}
122122

123+
@Test
124+
public void testToString() {
125+
Assert.assertEquals("someimage", ImageReference.of(null, "someimage", null).toString());
126+
Assert.assertEquals("someimage", ImageReference.of("", "someimage", "").toString());
127+
Assert.assertEquals(
128+
"someotherimage", ImageReference.of(null, "library/someotherimage", null).toString());
129+
Assert.assertEquals(
130+
"someregistry/someotherimage",
131+
ImageReference.of("someregistry", "someotherimage", null).toString());
132+
Assert.assertEquals(
133+
"anotherregistry/anotherimage:sometag",
134+
ImageReference.of("anotherregistry", "anotherimage", "sometag").toString());
135+
}
136+
123137
private void verifyParse(String registry, String repository, String tagSeparator, String tag)
124138
throws InvalidImageReferenceException {
125139
// Gets the expected parsed components.

jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException;
3535
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
3636
import com.google.common.annotations.VisibleForTesting;
37+
import com.google.common.base.Strings;
3738
import java.io.IOException;
3839
import java.nio.file.Files;
3940
import java.nio.file.Path;
@@ -153,10 +154,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
153154
RegistryCredentials mavenSettingsCredentials =
154155
RegistryCredentials.from("Maven settings", registryCredentials);
155156

157+
ImageReference targetImageReference = ImageReference.of(registry, repository, tag);
156158
BuildConfiguration buildConfiguration =
157159
BuildConfiguration.builder(new MavenBuildLogger(getLog()))
158160
.setBaseImage(baseImage)
159-
.setTargetImage(ImageReference.of(registry, repository, tag))
161+
.setTargetImage(targetImageReference)
160162
.setCredentialHelperNames(credHelpers)
161163
.setKnownRegistryCredentials(mavenSettingsCredentials)
162164
.setMainClass(mainClass)
@@ -178,7 +180,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
178180
}
179181

180182
getLog().info("");
181-
getLog().info("Pushing image as " + registry + "/" + repository + ":" + tag);
183+
getLog().info("Pushing image as " + targetImageReference);
182184
getLog().info("");
183185

184186
// TODO: Instead of disabling logging, have authentication credentials be provided
@@ -192,7 +194,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
192194
buildImage(new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cacheDirectory));
193195

194196
getLog().info("");
195-
getLog().info("Built and pushed image as " + registry + "/" + repository + ":" + tag);
197+
getLog().info("Built and pushed image as " + targetImageReference);
196198
getLog().info("");
197199
}
198200

@@ -252,22 +254,24 @@ private Authorization getRegistryCredentialsFromSettings(String registry) {
252254
/** Checks validity of plugin parameters. */
253255
private void validateParameters() throws MojoFailureException {
254256
// Validates 'registry'.
255-
if (!ImageReference.isValidRegistry(registry)) {
257+
if (!Strings.isNullOrEmpty(registry) && !ImageReference.isValidRegistry(registry)) {
256258
getLog().error("Invalid format for 'registry'");
257259
}
258260
// Validates 'repository'.
259261
if (!ImageReference.isValidRepository(repository)) {
260262
getLog().error("Invalid format for 'repository'");
261263
}
262264
// Validates 'tag'.
263-
if (!ImageReference.isValidTag(tag)) {
264-
getLog().error("Invalid format for 'tag'");
265-
}
265+
if (!Strings.isNullOrEmpty(tag)) {
266+
if (!ImageReference.isValidTag(tag)) {
267+
getLog().error("Invalid format for 'tag'");
268+
}
266269

267-
// 'tag' must not contain backslashes.
268-
if (tag.indexOf('/') >= 0) {
269-
getLog().error("'tag' cannot contain backslashes");
270-
throw new MojoFailureException("Invalid configuration parameters");
270+
// 'tag' must not contain backslashes.
271+
if (tag.indexOf('/') >= 0) {
272+
getLog().error("'tag' cannot contain backslashes");
273+
throw new MojoFailureException("Invalid configuration parameters");
274+
}
271275
}
272276
}
273277

0 commit comments

Comments
 (0)