Search in sources :

Example 1 with DockerLoginException

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);
    }
}
Also used : DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) Platform(com.aws.greengrass.util.platforms.Platform) HashMap(java.util.HashMap) IOException(java.io.IOException) DockerLoginException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerLoginException) UserNotAuthorizedForDockerException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.UserNotAuthorizedForDockerException)

Aggregations

DockerLoginException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerLoginException)1 DockerServiceUnavailableException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException)1 UserNotAuthorizedForDockerException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.UserNotAuthorizedForDockerException)1 Platform (com.aws.greengrass.util.platforms.Platform)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1