...

Source file src/github.com/docker/docker-credential-helpers/client/client.go

Documentation: github.com/docker/docker-credential-helpers/client

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/docker/docker-credential-helpers/credentials"
    10  )
    11  
    12  // isValidCredsMessage checks if 'msg' contains invalid credentials error message.
    13  // It returns whether the logs are free of invalid credentials errors and the error if it isn't.
    14  // error values can be errCredentialsMissingServerURL or errCredentialsMissingUsername.
    15  func isValidCredsMessage(msg string) error {
    16  	if credentials.IsCredentialsMissingServerURLMessage(msg) {
    17  		return credentials.NewErrCredentialsMissingServerURL()
    18  	}
    19  
    20  	if credentials.IsCredentialsMissingUsernameMessage(msg) {
    21  		return credentials.NewErrCredentialsMissingUsername()
    22  	}
    23  
    24  	return nil
    25  }
    26  
    27  // Store uses an external program to save credentials.
    28  func Store(program ProgramFunc, creds *credentials.Credentials) error {
    29  	cmd := program(credentials.ActionStore)
    30  
    31  	buffer := new(bytes.Buffer)
    32  	if err := json.NewEncoder(buffer).Encode(creds); err != nil {
    33  		return err
    34  	}
    35  	cmd.Input(buffer)
    36  
    37  	out, err := cmd.Output()
    38  	if err != nil {
    39  		t := strings.TrimSpace(string(out))
    40  
    41  		if isValidErr := isValidCredsMessage(t); isValidErr != nil {
    42  			err = isValidErr
    43  		}
    44  
    45  		return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, t)
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // Get executes an external program to get the credentials from a native store.
    52  func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error) {
    53  	cmd := program(credentials.ActionGet)
    54  	cmd.Input(strings.NewReader(serverURL))
    55  
    56  	out, err := cmd.Output()
    57  	if err != nil {
    58  		t := strings.TrimSpace(string(out))
    59  
    60  		if credentials.IsErrCredentialsNotFoundMessage(t) {
    61  			return nil, credentials.NewErrCredentialsNotFound()
    62  		}
    63  
    64  		if isValidErr := isValidCredsMessage(t); isValidErr != nil {
    65  			err = isValidErr
    66  		}
    67  
    68  		return nil, fmt.Errorf("error getting credentials - err: %v, out: `%s`", err, t)
    69  	}
    70  
    71  	resp := &credentials.Credentials{
    72  		ServerURL: serverURL,
    73  	}
    74  
    75  	if err := json.NewDecoder(bytes.NewReader(out)).Decode(resp); err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return resp, nil
    80  }
    81  
    82  // Erase executes a program to remove the server credentials from the native store.
    83  func Erase(program ProgramFunc, serverURL string) error {
    84  	cmd := program(credentials.ActionErase)
    85  	cmd.Input(strings.NewReader(serverURL))
    86  	out, err := cmd.Output()
    87  	if err != nil {
    88  		t := strings.TrimSpace(string(out))
    89  
    90  		if isValidErr := isValidCredsMessage(t); isValidErr != nil {
    91  			err = isValidErr
    92  		}
    93  
    94  		return fmt.Errorf("error erasing credentials - err: %v, out: `%s`", err, t)
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  // List executes a program to list server credentials in the native store.
   101  func List(program ProgramFunc) (map[string]string, error) {
   102  	cmd := program(credentials.ActionList)
   103  	cmd.Input(strings.NewReader("unused"))
   104  	out, err := cmd.Output()
   105  	if err != nil {
   106  		t := strings.TrimSpace(string(out))
   107  
   108  		if isValidErr := isValidCredsMessage(t); isValidErr != nil {
   109  			err = isValidErr
   110  		}
   111  
   112  		return nil, fmt.Errorf("error listing credentials - err: %v, out: `%s`", err, t)
   113  	}
   114  
   115  	var resp map[string]string
   116  	if err = json.NewDecoder(bytes.NewReader(out)).Decode(&resp); err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return resp, nil
   121  }
   122  

View as plain text