use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerLoginException in project aws-greengrass-nucleus by aws-greengrass.
the class DefaultDockerClient method login.
/**
* Login to given docker registry.
*
* @param registry Registry to log into, with credentials encapsulated
* @throws DockerLoginException error in authenticating with the registry
* @throws UserNotAuthorizedForDockerException when current user is not authorized to use docker
* @throws DockerServiceUnavailableException an error that can be potentially fixed through retries
* @throws IOException unexpected error
*/
public void login(Registry registry) throws DockerLoginException, UserNotAuthorizedForDockerException, DockerServiceUnavailableException, IOException {
Map<String, String> credEnvMap = new HashMap<>();
credEnvMap.put("dockerUsername", registry.getCredentials().getUsername());
credEnvMap.put("dockerPassword", registry.getCredentials().getPassword());
Platform platform = Platform.getInstance();
String loginCommand = String.format("docker login %s -u %s -p %s", registry.getEndpoint(), platform.formatEnvironmentVariableCmd("dockerUsername"), platform.formatEnvironmentVariableCmd("dockerPassword"));
CliResponse response = runDockerCmd(loginCommand, credEnvMap);
Optional<UserNotAuthorizedForDockerException> userAuthorizationError = checkUserAuthorizationError(response);
if (userAuthorizationError.isPresent()) {
throw userAuthorizationError.get();
}
if (response.exit.isPresent()) {
if (response.exit.get() == 0) {
return;
} else {
if (response.getOut().contains("Service Unavailable")) {
// engine has issues or proxy config is bad etc. Not entirely reliable to determine retry behavior
throw new DockerServiceUnavailableException(String.format("Error logging into the registry using credentials - %s", response.err));
}
throw new DockerLoginException(String.format("Error logging into the registry using credentials - %s", response.err));
}
} else {
throw new IOException("Unexpected error while trying to perform docker login", response.failureCause);
}
}
Aggregations