- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.1k
          Contextualize credentials used by GitSCMFileSystem when possible
          #1802
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            dwnusbaum
  wants to merge
  1
  commit into
  jenkinsci:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dwnusbaum:contextualize-scmfilesystem-credentials
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +74
        
        
          −0
        
        
          
        
      
    
  
  
     Draft
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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 | 
|---|---|---|
|  | @@ -25,11 +25,18 @@ | |
|  | ||
| package jenkins.plugins.git; | ||
|  | ||
| import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
| import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; | ||
| import com.cloudbees.plugins.credentials.domains.Domain; | ||
| import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; | ||
| import hudson.EnvVars; | ||
| import hudson.Extension; | ||
| import hudson.model.Run; | ||
| import hudson.model.TaskListener; | ||
| import hudson.plugins.git.BranchSpec; | ||
| import hudson.plugins.git.GitSCM; | ||
| import hudson.plugins.git.GitException; | ||
| import hudson.util.Secret; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.util.Collections; | ||
|  | @@ -47,12 +54,15 @@ | |
| import org.eclipse.jgit.lib.ObjectId; | ||
| import org.jenkinsci.plugins.gitclient.Git; | ||
| import org.jenkinsci.plugins.gitclient.GitClient; | ||
| import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition; | ||
| import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.jvnet.hudson.test.Issue; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
|  | ||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|  | @@ -477,6 +487,67 @@ public void null_pointer_exception() throws Exception { | |
| assertEquals(Constants.R_HEADS, result1.prefix); | ||
| } | ||
|  | ||
| @Test | ||
| public void filesystem_supports_credential_contextualization() throws Exception { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I confirm that the test fails without the credentials contextualization. | ||
| sampleRepo.init(); | ||
| sampleRepo.git("checkout", "-b", "dev"); | ||
| sampleRepo.write("Jenkinsfile", "echo 'Hello, world!'"); | ||
| sampleRepo.git("add", "Jenkinsfile"); | ||
| sampleRepo.git("commit", "--all", "--message=dev"); | ||
| var store = CredentialsProvider.lookupStores(r.jenkins).iterator().next(); | ||
| store.addCredentials(Domain.global(), new ContextualizableCredentials("my-creds")); | ||
| var job = r.createProject(WorkflowJob.class); | ||
| var scm = new GitSCM( | ||
| GitSCM.createRepoList(sampleRepo.toString(), "my-creds"), | ||
| Collections.singletonList(new BranchSpec("*/dev")), | ||
| null, | ||
| null, | ||
| Collections.emptyList()); | ||
| var flowDefinition = new CpsScmFlowDefinition(scm, "Jenkinsfile"); | ||
| flowDefinition.setLightweight(true); // Triggers use of GitSCMFileSystem | ||
| job.setDefinition(flowDefinition); | ||
| var run = r.buildAndAssertSuccess(job); | ||
| } | ||
|  | ||
| private static class ContextualizableCredentials extends BaseStandardCredentials implements StandardUsernamePasswordCredentials { | ||
| private final boolean contextualized; | ||
|  | ||
| @DataBoundConstructor | ||
| public ContextualizableCredentials(String id) { | ||
| super(id, null); | ||
| this.contextualized = false; | ||
| } | ||
|  | ||
| private ContextualizableCredentials(ContextualizableCredentials base) { | ||
| super(base.getId(), null); | ||
| this.contextualized = true; | ||
| } | ||
|  | ||
| @Override | ||
| public String getUsername() { | ||
| if (contextualized) { | ||
| return "username"; | ||
| } | ||
| throw new IllegalStateException("Requires contextualization"); | ||
| } | ||
|  | ||
| @Override | ||
| public Secret getPassword() { | ||
| if (contextualized) { | ||
| return Secret.fromString("s3cr3t"); | ||
| } | ||
| throw new IllegalStateException("Requires contextualization"); | ||
| } | ||
|  | ||
| @Override | ||
| public ContextualizableCredentials forRun(Run<?, ?> run) { | ||
| return new ContextualizableCredentials(this); | ||
| } | ||
|  | ||
| @Extension | ||
| public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {} | ||
| } | ||
|  | ||
| /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ | ||
| private boolean isWindows() { | ||
| return java.io.File.pathSeparatorChar==';'; | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: No sure if
GitSCMTelescopeshould contextualize the credentials similarly?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I looked into that, but IDK. There is also this credentials lookup in
AbstractGitSCMSourcewhich won't work, and I don't think it can be made to work:git-plugin/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Lines 1333 to 1338 in de7f436
Going by https://github.com/search?type=code&q=+owner%3Ajenkinsci+gitscmtelescope, I think the answer here would just be that
GitSCMSourcedoesn't support inference-based options forGitHubAppCredentials, you must useGitHubSCMSource. IDK if there would ever be a reason that you would have to useGitSCMSourceoverGitHubSCMSource, but I don't think so.For
GitSCMand its use ofGitSCMFileSystem, the situation is different, since there is noGitHubSCM.