Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from jglick/login-JENKINS-38018
[JENKINS-38018] Use docker-login wherever possible for registry credentials
- Loading branch information
Showing
9 changed files
with
180 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/org/jenkinsci/plugins/docker/commons/impl/RegistryKeyMaterialFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2018 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.docker.commons.impl; | ||
|
||
import hudson.AbortException; | ||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.Launcher; | ||
import hudson.model.TaskListener; | ||
import hudson.util.ArgumentListBuilder; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import javax.annotation.Nonnull; | ||
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterial; | ||
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
||
/** | ||
* Logs you in to a Docker registry. | ||
*/ | ||
@Restricted(NoExternalUse.class) | ||
public class RegistryKeyMaterialFactory extends KeyMaterialFactory { | ||
|
||
private final @Nonnull String username; | ||
private final @Nonnull String password; | ||
private final @Nonnull URL endpoint; | ||
private final @Nonnull Launcher launcher; | ||
private final @Nonnull TaskListener listener; | ||
private final @Nonnull String dockerExecutable; | ||
|
||
public RegistryKeyMaterialFactory(@Nonnull String username, @Nonnull String password, @Nonnull URL endpoint, @Nonnull Launcher launcher, @Nonnull TaskListener listener, @Nonnull String dockerExecutable) { | ||
this.username = username; | ||
this.password = password; | ||
this.endpoint = endpoint; | ||
this.launcher = launcher; | ||
this.listener = listener; | ||
this.dockerExecutable = dockerExecutable; | ||
} | ||
|
||
@Override | ||
public KeyMaterial materialize() throws IOException, InterruptedException { | ||
FilePath dockerConfig = createSecretsDirectory(); | ||
try { | ||
if (launcher.launch().cmds(new ArgumentListBuilder(dockerExecutable, "login", "-u", username, "-p").add(password, true).add(endpoint)).envs("DOCKER_CONFIG=" + dockerConfig).stdout(listener).join() != 0) { | ||
throw new AbortException("docker login failed"); | ||
} | ||
} catch (IOException | InterruptedException x) { | ||
try { | ||
dockerConfig.deleteRecursive(); | ||
} catch (Exception x2) { | ||
x.addSuppressed(x2); | ||
} | ||
throw x; | ||
} | ||
return new RegistryKeyMaterial(dockerConfig, new EnvVars("DOCKER_CONFIG", dockerConfig.getRemote())); | ||
} | ||
|
||
private static class RegistryKeyMaterial extends KeyMaterial { | ||
|
||
private final FilePath dockerConfig; | ||
|
||
RegistryKeyMaterial(FilePath dockerConfig, EnvVars envVars) { | ||
super(envVars); | ||
this.dockerConfig = dockerConfig; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
dockerConfig.deleteRecursive(); | ||
} catch (InterruptedException x) { | ||
// TODO would better have been thrown from KeyMaterial.close to begin with | ||
throw new IOException(x); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters