Java Code Examples for org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint#getUrl()

The following examples show how to use org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint#getUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FolderConfig.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public String getRegistryUrl(@Nullable Run run) {
    if (run != null) {
        Job job = run.getParent();
        ItemGroup parent = job.getParent();
        while (parent != null) {

            if (parent instanceof AbstractFolder) {
                AbstractFolder folder = (AbstractFolder) parent;
                FolderConfig config = (FolderConfig) folder.getProperties().get(FolderConfig.class);
                if (config != null) {
                    DockerRegistryEndpoint registry = config.getRegistry();
                    if (registry != null && !StringUtils.isBlank(registry.getUrl())) {
                        return registry.getUrl();
                    }
                }
            }

            if (parent instanceof Item) {
                parent = ((Item) parent).getParent();
            } else {
                parent = null;
            }
        }
    }
    return null;
}
 
Example 2
Source File: DockerCloud.java    From docker-plugin with MIT License 5 votes vote down vote up
@Restricted(NoExternalUse.class)
public static AuthConfig getAuthConfig(DockerRegistryEndpoint registry, ItemGroup context) {
    AuthConfig auth = new AuthConfig();

    // we can't use DockerRegistryEndpoint#getToken as this one do check domainRequirement based on registry URL
    // but in some context (typically, passing registry auth for `docker build`) we just can't guess this one.

    final Credentials c = firstOrNull(CredentialsProvider.lookupCredentials(
            IdCredentials.class, context, ACL.SYSTEM, Collections.EMPTY_LIST),
            withId(registry.getCredentialsId()));
    final DockerRegistryToken t = c == null ? null : AuthenticationTokens.convert(DockerRegistryToken.class, c);
    if (t == null) {
        throw new IllegalArgumentException("Invalid Credential ID " + registry.getCredentialsId());
    }
    final String token = t.getToken();
    // What docker-commons claim to be a "token" is actually configuration storage
    // see https://github.com/docker/docker-ce/blob/v17.09.0-ce/components/cli/cli/config/configfile/file.go#L214
    // i.e base64 encoded username : password
    final String decode = new String(Base64.decodeBase64(token), StandardCharsets.UTF_8);
    int i = decode.indexOf(':');
    if (i > 0) {
        String username = decode.substring(0, i);
        auth.withUsername(username);
    }
    auth.withPassword(decode.substring(i+1));
    if (registry.getUrl() != null) {
        auth.withRegistryAddress(registry.getUrl());
    }
    return auth;
}