Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Add an early validation of repo names specified in the pom.
Browse files Browse the repository at this point in the history
An invalid name will now cause an early build failure with a clear message in the build log, rather than the incomprehensible “broken pipe” error at docker build time.
  • Loading branch information
seamonstr committed Mar 11, 2019
1 parent b039ea7 commit c0ce28f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plugin/src/main/java/com/spotify/plugin/dockerfile/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -221,6 +222,7 @@ static String buildImage(@Nonnull DockerClient dockerClient,
log.info(""); // Spacing around build progress
try {
if (repository != null) {
validateRepository(repository);
final String name = formatImageName(repository, tag);
log.info(MessageFormat.format("Image will be built as {0}", name));
log.info(""); // Spacing around build progress
Expand All @@ -238,6 +240,20 @@ static String buildImage(@Nonnull DockerClient dockerClient,
return progressHandler.builtImageId();
}

private static String VALID_REPO_REGEX =
"^([a-z0-9_.-])+(\\/(?=[a-z0-9_.-])[a-z0-9_.-]+)*$";


static void validateRepository(@Nonnull String repository)
throws MojoFailureException {
Pattern pattern = Pattern.compile(VALID_REPO_REGEX);
if (!pattern.matcher(repository).matches()) {
throw new MojoFailureException(
"Repo name \"" + repository +
"\" must contain only lowercase, numbers, '-', '_' or '.'.");
}
}

private static void requireValidDockerFilePath(@Nonnull Log log,
@Nonnull Path contextDirectory,
@Nullable Path dockerfile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*-
* -\-\-
* Dockerfile Maven Plugin
* --
* Copyright (C) 2019 Simon Woodward
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.plugin.dockerfile;

import static org.junit.Assert.assertTrue;

import org.apache.maven.plugin.MojoFailureException;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TestRepoNameValidation {
public ExpectedException thrown = ExpectedException.none();

@Test
public void testSuccess() throws MojoFailureException {
BuildMojo.validateRepository("alllowercase");
BuildMojo.validateRepository("with000numbers");
BuildMojo.validateRepository("00withnumbers");
BuildMojo.validateRepository("00757383");
BuildMojo.validateRepository("withnumbers34343");
BuildMojo.validateRepository("with-hyphens");
BuildMojo.validateRepository("with_underscores");
BuildMojo.validateRepository("with.dots");
BuildMojo.validateRepository("______");
BuildMojo.validateRepository("------");
BuildMojo.validateRepository("......");
BuildMojo.validateRepository("example.com/okay./.path");
BuildMojo.validateRepository(".start.and.end.");
BuildMojo.validateRepository("-start-and-end-");
BuildMojo.validateRepository("_start_and_end_");
// Forward slash delimits the repo user from the repo name; strictly speaking,
// you're allowed only one slash, somewhere in the middle.
BuildMojo.validateRepository("with/forwardslash");
BuildMojo.validateRepository("with/multiple/forwardslash");
}

private boolean throwsMojoFailure(String repoName) {
boolean threw = false;
try {
BuildMojo.validateRepository(repoName);
} catch (MojoFailureException e) {
threw = true;
}
return threw;
}

@Test
public void testFailCases() {
assertTrue("Mixed case didn't fail", throwsMojoFailure("ddddddDddddd"));
assertTrue("Symbols didn't fail", throwsMojoFailure("ddddddDd+dddd"));
assertTrue("Starting slash didn't fail", throwsMojoFailure("/atstart"));
assertTrue("Ending slash didn't fail", throwsMojoFailure("atend/"));
}
}

0 comments on commit c0ce28f

Please sign in to comment.