// Path: src/main/java/org/jenkinsci/plugins/docker/commons/impl/ServerHostKeyMaterialFactory.java
// @Restricted(NoExternalUse.class)
// public class ServerHostKeyMaterialFactory extends KeyMaterialFactory{
// 
//     /**
//      * The host.
//      */
//     @Nonnull
//     private final String host;
// 
//     public ServerHostKeyMaterialFactory(@Nonnull String host) {
//         this.host = host;
//     }
// 
//     /** {@inheritDoc} */
//     @Override
//     public KeyMaterial materialize() throws IOException, InterruptedException {
//         EnvVars env = new EnvVars();
//         env.put("DOCKER_HOST", host);
//         return new KeyMaterialImpl(env);
//     }
// 
//     /**
//      * Our implementation.
//      */
//     private static class KeyMaterialImpl extends KeyMaterial {
//         /**
//          * Standardize serialization
//          */
//         private static final long serialVersionUID = 1L;
// 
//         /** {@inheritDoc} */
//         private KeyMaterialImpl(EnvVars envVars) {
//             super(envVars);
//         }
// 
//         /** {@inheritDoc} */
//         @Override
//         public void close() throws IOException {
//             
//         }
//     }
// }


import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Run;
import hudson.remoting.VirtualChannel;
import hudson.util.ListBoxModel;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.docker.commons.impl.ServerHostKeyMaterialFactory;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import static hudson.Util.fixEmpty;

    /**
     * Makes the key materials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
     * needed to access it.
     */
    public KeyMaterialFactory newKeyMaterialFactory(@Nonnull Run context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
        DockerServerCredentials creds=null;
        if (credentialsId!=null) {
            List<DomainRequirement> domainRequirements = URIRequirementBuilder.fromUri(getUri()).build();
            domainRequirements.add(new DockerServerDomainRequirement());
            creds = CredentialsProvider.findCredentialById(credentialsId, DockerServerCredentials.class, context,
                    domainRequirements);
        }

        // the directory needs to be outside workspace to avoid prying eyes
        FilePath dotDocker = dotDocker(target);
        dotDocker.mkdirs();
        // ServerKeyMaterialFactory.materialize creates a random subdir if one is needed:
        return newKeyMaterialFactory(dotDocker, creds);
    }

    static FilePath dotDocker(@Nonnull VirtualChannel target) throws IOException, InterruptedException {
        // TODO this is wrong, should be using WorkspaceList.tempDir
        return FilePath.getHomeDirectory(target).child(".docker");
    }

    /**
     * Create a {@link KeyMaterialFactory} for connecting to the docker server/host. 
     */
    public KeyMaterialFactory newKeyMaterialFactory(FilePath dir, @Nullable DockerServerCredentials credentials) throws IOException, InterruptedException {
// Path: src/main/java/org/jenkinsci/plugins/docker/commons/impl/ServerHostKeyMaterialFactory.java
// @Restricted(NoExternalUse.class)
// public class ServerHostKeyMaterialFactory extends KeyMaterialFactory{
// 
//     /**
//      * The host.
//      */
//     @Nonnull
//     private final String host;
// 
//     public ServerHostKeyMaterialFactory(@Nonnull String host) {
//         this.host = host;
//     }
// 
//     /** {@inheritDoc} */
//     @Override
//     public KeyMaterial materialize() throws IOException, InterruptedException {
//         EnvVars env = new EnvVars();
//         env.put("DOCKER_HOST", host);
//         return new KeyMaterialImpl(env);
//     }
// 
//     /**
//      * Our implementation.
//      */
//     private static class KeyMaterialImpl extends KeyMaterial {
//         /**
//          * Standardize serialization
//          */
//         private static final long serialVersionUID = 1L;
// 
//         /** {@inheritDoc} */
//         private KeyMaterialImpl(EnvVars envVars) {
//             super(envVars);
//         }
// 
//         /** {@inheritDoc} */
//         @Override
//         public void close() throws IOException {
//             
//         }
//     }
// }


// Path: src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerEndpoint.java
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Run;
import hudson.remoting.VirtualChannel;
import hudson.util.ListBoxModel;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.docker.commons.impl.ServerHostKeyMaterialFactory;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import static hudson.Util.fixEmpty;


    /**
     * Makes the key materials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
     * needed to access it.
     */
    public KeyMaterialFactory newKeyMaterialFactory(@Nonnull Run context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
        DockerServerCredentials creds=null;
        if (credentialsId!=null) {
            List<DomainRequirement> domainRequirements = URIRequirementBuilder.fromUri(getUri()).build();
            domainRequirements.add(new DockerServerDomainRequirement());
            creds = CredentialsProvider.findCredentialById(credentialsId, DockerServerCredentials.class, context,
                    domainRequirements);
        }

        // the directory needs to be outside workspace to avoid prying eyes
        FilePath dotDocker = dotDocker(target);
        dotDocker.mkdirs();
        // ServerKeyMaterialFactory.materialize creates a random subdir if one is needed:
        return newKeyMaterialFactory(dotDocker, creds);
    }

    static FilePath dotDocker(@Nonnull VirtualChannel target) throws IOException, InterruptedException {
        // TODO this is wrong, should be using WorkspaceList.tempDir
        return FilePath.getHomeDirectory(target).child(".docker");
    }

    /**
     * Create a {@link KeyMaterialFactory} for connecting to the docker server/host. 
     */
    public KeyMaterialFactory newKeyMaterialFactory(FilePath dir, @Nullable DockerServerCredentials credentials) throws IOException, InterruptedException {
        return (uri == null ? KeyMaterialFactory.NULL : new ServerHostKeyMaterialFactory(uri))