- 
                Notifications
    
You must be signed in to change notification settings  - Fork 34
 
Add support for git https credentials #23
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
          
     Open
      
      
            NikolausDemmel
  wants to merge
  6
  commits into
  dockito:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
NikolausDemmel:feature/git-https-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.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9a06da8
              
                Never use proxy when communicating with the vault.
              
              
                NikolausDemmel 3cd68cc
              
                remove temporary tared archive of .ssh folder
              
              
                NikolausDemmel a57692e
              
                make sure /vault/.ssh exists (e.g. in case we start the container wit…
              
              
                NikolausDemmel b1f1438
              
                make sure ONVAULT works with empty .ssh folder
              
              
                NikolausDemmel 743cf01
              
                Add .dockerignore for all except the actually needed files.
              
              
                NikolausDemmel 8b7587a
              
                Add support for git https credentials
              
              
                NikolausDemmel 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| * | ||
| !index.js | ||
| !ONVAULT | ||
| !package.json | ||
| !credentials | 
  
    
      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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -22,6 +22,10 @@ log () { | |
| echo -e "${GREEN}[Dockito Vault]${NC} $@" | ||
| } | ||
| 
     | 
||
| # don't go through proxy for accessing vault | ||
| no_proxy_old="$no_proxy" | ||
| export no_proxy="$VAULT_HOST" | ||
| 
     | 
||
| if ! curl -s "${VAULT_URI}/_ping"; then | ||
| COUNTER=0 | ||
| echo 'Waiting 10s for dockito/vault to be ready...' | ||
| 
        
          
        
         | 
    @@ -43,28 +47,60 @@ if curl -s "${VAULT_URI}/_ping"; then | |
| cp -r ~/.ssh/* $tmp_ssh_vault | ||
| fi | ||
| 
     | 
||
| log "Downloading private keys..." | ||
| log "Downloading private keys and git credentials ..." | ||
| curl -s "${VAULT_URI}/ssh.tgz" | tar -C ~/.ssh/ -zxf - | ||
| chown `whoami` ~/.ssh/* | ||
| chmod 600 ~/.ssh/* | ||
| chown -f `whoami` ~/.ssh/* || true | ||
| chmod -f 600 ~/.ssh/* || true | ||
| 
     | 
||
| log "Using ssh key: $VAULT_SSH_KEY" | ||
| if [ ! -f ~/.ssh/$VAULT_SSH_KEY ]; then | ||
| log "Did not find ssh key: $VAULT_SSH_KEY" | ||
| else | ||
| log "Using ssh key: $VAULT_SSH_KEY" | ||
| fi | ||
| if [[ "$VAULT_SSH_KEY" != "id_rsa" ]]; then | ||
| # configure the ssh to any host to use this ssh key | ||
| echo -e "\nHost *\nIdentityFile ~/.ssh/$VAULT_SSH_KEY" >> ~/.ssh/config | ||
| fi | ||
| 
     | 
||
| # download git credential store to temporary location | ||
| tmp_git_credential_store=`mktemp ~/.git-credentials-XXXXXX` | ||
| curl -s "${VAULT_URI}/git-credentials" > "$tmp_git_credential_store" | ||
| 
     | 
||
| # check number of credentials (== non-blank lines) for seeing if something is wrong | ||
| credential_count=`cat "$tmp_git_credential_store" | sed '/^\s*$/d' | wc -l` | ||
| if [ $credential_count -eq 0 ]; then | ||
| log "Did not find any git https credentials" | ||
| else | ||
| log "Using $credential_count git https credential(s)" | ||
| fi | ||
| 
     | 
||
| # configure git | ||
| git_config_old_credential_helper=`git config --global credential.helper` || true | ||
| git_config_old_core_askpass=`git config --global core.askpass` || true | ||
| git config --global credential.helper "store --file=$tmp_git_credential_store" | ||
| git config --global core.askpass true | ||
| 
     | 
||
| # restore 'no_proxy' for executing the actual command | ||
| export no_proxy="$no_proxy_old" | ||
| 
     | 
||
| log "Executing command: $@" | ||
| eval $@ | ||
| 
     | 
||
| log "Removing private keys..." | ||
| log "Removing private keys and git credentials..." | ||
| rm -rf ~/.ssh/* | ||
| 
     | 
||
| # copying backup to ssh directory | ||
| if [[ -n "$ssh_backup_enabled" ]]; then | ||
| cp -r $tmp_ssh_vault/* ~/.ssh | ||
| rm -rf $tmp_ssh_vault | ||
| fi | ||
| 
     | 
||
| rm $tmp_git_credential_store | ||
| 
     | 
||
| # restoring old setting | ||
| git config --global credential.helper "$git_config_old_credential_helper" | ||
| git config --global core.askpass "$git_config_old_core_askpass" | ||
| 
         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 to self: I need to test this "restoring" of the git config.  | 
||
| 
     | 
||
| else | ||
| log "ERROR: Start the dockito/vault container before using ONVAULT!" | ||
| log "ex: docker run -d -p ${VAULT_HOST}:14242:3000 -v ~/.ssh:/vault/.ssh dockito/vault" | ||
| 
          
            
          
           | 
    ||
  
    
      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 | 
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/bin/sh | ||
| 
     | 
||
| # allow overriding location of credential store | ||
| : ${CREDENTIALFILE:=/vault/store/git-credentials} | ||
| 
     | 
||
| usage () { | ||
| echo "Usage: | ||
| $(basename $0) set URL | ||
| $(basename $0) remove URL | ||
| $(basename $0) list | ||
| $(basename $0) clear | ||
| 
     | 
||
| Examples: | ||
| $(basename $0) set github.com | ||
| $(basename $0) set gitlab.com/foo/bar | ||
| $(basename $0) remove github.com | ||
| " | ||
| } | ||
| 
     | 
||
| # parse arguments | ||
| while [ $# -ge 1 ]; do | ||
| case "$1" in | ||
| set|remove) | ||
| if [ $# -eq 2 ]; then | ||
| CMD="credentials_$1 $2" | ||
| shift | ||
| fi | ||
| break | ||
| ;; | ||
| list|clear) | ||
| CMD="credentials_$1" | ||
| break | ||
| ;; | ||
| -h|--help) | ||
| usage | ||
| exit 0 | ||
| ;; | ||
| *) | ||
| usage | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| 
     | 
||
| shift | ||
| done | ||
| 
     | 
||
| if [ $# -ne 1 ] || [ -z "$CMD" ]; then | ||
| usage | ||
| exit 1 | ||
| fi | ||
| 
     | 
||
| # helper to url-encode username and password using javascript | ||
| # proper escaping for username & password, see: https://gist.github.com/pierrevalade/6025241 | ||
| encodeurl () { | ||
| node <<EOF | ||
| var escape = function(str) { | ||
| return encodeURIComponent(str).replace(/[!*()']/g, function(character) { | ||
| return '%' + character.charCodeAt(0).toString(16); | ||
| }); | ||
| }; | ||
| console.log(escape('$1')) | ||
| EOF | ||
| } | ||
| 
     | 
||
| mask_passwords () { | ||
| # replace passwords with ****** and discard non-matching lines | ||
| sed -n 's|^\(.*://.*:\)\(.*\)\(@.*\)$|\1******\3|p' | ||
| } | ||
| 
     | 
||
| credentials_remove () { | ||
| # remove leading https:// | ||
| URL="$( echo "$1" | sed -e 's#^.*://##' )" | ||
| 
     | 
||
| # remove existing entry | ||
| matching=`grep "@$URL\$" "$CREDENTIALFILE" 2>/dev/null` | ||
| if [ -n "$matching" ]; then | ||
| tempfile=`mktemp -t git-credentials-XXXXXX` | ||
| grep -v "@$URL\$" "$CREDENTIALFILE" > "$tempfile" | ||
| mv "$tempfile" "$CREDENTIALFILE" | ||
| else | ||
| echo "No credentials for '$URL'" | ||
| exit 1 | ||
| fi | ||
| } | ||
| 
     | 
||
| credentials_set () { | ||
| # remove leading https:// | ||
| URL="$( echo "$1" | sed -e 's#^.*://##' )" | ||
| 
     | 
||
| # remove existing entry | ||
| matching=`grep "@$URL\$" "$CREDENTIALFILE" 2>/dev/null` | ||
| if [ -n "$matching" ]; then | ||
| echo "Removing existing entries:" | ||
| echo "$matching" | mask_passwords | ||
| read -p "Continue? [yN]: " CONFIRM | ||
| if ! [ "$CONFIRM" = "y" ]; then | ||
| exit 1 | ||
| fi | ||
| credentials_remove $URL | ||
| echo "" | ||
| fi | ||
| 
     | 
||
| echo "Adding credentials for 'https://$URL'." | ||
| read -p "Username: " USER | ||
| read -p "Password: " -s PASSWORD | ||
| echo "" | ||
| 
     | 
||
| USER=`encodeurl "$USER"` | ||
| PASSWORD=`encodeurl "$PASSWORD"` | ||
| 
     | 
||
| echo "https://$USER:$PASSWORD@$URL" >> "$CREDENTIALFILE" | ||
| chmod 600 "$CREDENTIALFILE" | ||
| } | ||
| 
     | 
||
| credentials_list () { | ||
| if [ -f "$CREDENTIALFILE" ]; then | ||
| # mask passwords and sort by URL (which starts after the "@") | ||
| cat "$CREDENTIALFILE" | mask_passwords | sort -k 2 -t "@" | ||
| fi | ||
| } | ||
| 
     | 
||
| credentials_clear () { | ||
| echo "" > "$CREDENTIALFILE" | ||
| } | ||
| 
     | 
||
| 
     | 
||
| $CMD | 
  
    
      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
    
  
  
    
              
  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.
I have removed the mktemp from the script to allow using it with Linux distros without this command.
Please take a look at c202988.