2017-04-17 18:08:24 -04:00
|
|
|
package credentials
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// Action defines the name of an action (sub-command) supported by a
|
|
|
|
// credential-helper binary. It is an alias for "string", and mostly
|
|
|
|
// for convenience.
|
|
|
|
type Action = string
|
|
|
|
|
|
|
|
// List of actions (sub-commands) supported by credential-helper binaries.
|
|
|
|
const (
|
|
|
|
ActionStore Action = "store"
|
|
|
|
ActionGet Action = "get"
|
|
|
|
ActionErase Action = "erase"
|
|
|
|
ActionList Action = "list"
|
|
|
|
ActionVersion Action = "version"
|
|
|
|
)
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// Credentials holds the information shared between docker and the credentials store.
|
|
|
|
type Credentials struct {
|
|
|
|
ServerURL string
|
|
|
|
Username string
|
|
|
|
Secret string
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
// isValid checks the integrity of Credentials object such that no credentials lack
|
|
|
|
// a server URL or a username.
|
|
|
|
// It returns whether the credentials are valid and the error if it isn't.
|
|
|
|
// error values can be errCredentialsMissingServerURL or errCredentialsMissingUsername
|
|
|
|
func (c *Credentials) isValid() (bool, error) {
|
|
|
|
if len(c.ServerURL) == 0 {
|
|
|
|
return false, NewErrCredentialsMissingServerURL()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Username) == 0 {
|
|
|
|
return false, NewErrCredentialsMissingUsername()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-08-16 16:02:38 -04:00
|
|
|
// CredsLabel holds the way Docker credentials should be labeled as such in credentials stores that allow labelling.
|
2017-04-17 18:08:24 -04:00
|
|
|
// That label allows to filter out non-Docker credentials too at lookup/search in macOS keychain,
|
|
|
|
// Windows credentials manager and Linux libsecret. Default value is "Docker Credentials"
|
|
|
|
var CredsLabel = "Docker Credentials"
|
|
|
|
|
2017-08-16 16:02:38 -04:00
|
|
|
// SetCredsLabel is a simple setter for CredsLabel
|
2017-04-17 18:08:24 -04:00
|
|
|
func SetCredsLabel(label string) {
|
|
|
|
CredsLabel = label
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// Serve initializes the credentials-helper and parses the action argument.
|
2017-04-17 18:08:24 -04:00
|
|
|
// This function is designed to be called from a command line interface.
|
|
|
|
// It uses os.Args[1] as the key for the action.
|
|
|
|
// It uses os.Stdin as input and os.Stdout as output.
|
|
|
|
// This function terminates the program with os.Exit(1) if there is an error.
|
|
|
|
func Serve(helper Helper) {
|
|
|
|
if len(os.Args) != 2 {
|
2023-07-17 10:06:44 -04:00
|
|
|
_, _ = fmt.Fprintln(os.Stdout, usage())
|
|
|
|
os.Exit(1)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
switch os.Args[1] {
|
|
|
|
case "--version", "-v":
|
|
|
|
_ = PrintVersion(os.Stdout)
|
|
|
|
os.Exit(0)
|
|
|
|
case "--help", "-h":
|
|
|
|
_, _ = fmt.Fprintln(os.Stdout, usage())
|
|
|
|
os.Exit(0)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
if err := HandleCommand(helper, os.Args[1], os.Stdin, os.Stdout); err != nil {
|
|
|
|
_, _ = fmt.Fprintln(os.Stdout, err)
|
2017-04-17 18:08:24 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
func usage() string {
|
|
|
|
return fmt.Sprintf("Usage: %s <store|get|erase|list|version>", Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleCommand runs a helper to execute a credential action.
|
|
|
|
func HandleCommand(helper Helper, action Action, in io.Reader, out io.Writer) error {
|
|
|
|
switch action {
|
|
|
|
case ActionStore:
|
2017-04-17 18:08:24 -04:00
|
|
|
return Store(helper, in)
|
2023-07-17 10:06:44 -04:00
|
|
|
case ActionGet:
|
2017-04-17 18:08:24 -04:00
|
|
|
return Get(helper, in, out)
|
2023-07-17 10:06:44 -04:00
|
|
|
case ActionErase:
|
2017-04-17 18:08:24 -04:00
|
|
|
return Erase(helper, in)
|
2023-07-17 10:06:44 -04:00
|
|
|
case ActionList:
|
2017-04-17 18:08:24 -04:00
|
|
|
return List(helper, out)
|
2023-07-17 10:06:44 -04:00
|
|
|
case ActionVersion:
|
2017-08-16 16:02:38 -04:00
|
|
|
return PrintVersion(out)
|
2023-07-17 10:06:44 -04:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown action: %s", Name, action)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store uses a helper and an input reader to save credentials.
|
|
|
|
// The reader must contain the JSON serialization of a Credentials struct.
|
|
|
|
func Store(helper Helper, reader io.Reader) error {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buffer.Write(scanner.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var creds Credentials
|
|
|
|
if err := json.NewDecoder(buffer).Decode(&creds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
if ok, err := creds.isValid(); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
return helper.Add(&creds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves the credentials for a given server url.
|
|
|
|
// The reader must contain the server URL to search.
|
|
|
|
// The writer is used to write the JSON serialization of the credentials.
|
|
|
|
func Get(helper Helper, reader io.Reader, writer io.Writer) error {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buffer.Write(scanner.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverURL := strings.TrimSpace(buffer.String())
|
2017-06-05 18:23:21 -04:00
|
|
|
if len(serverURL) == 0 {
|
|
|
|
return NewErrCredentialsMissingServerURL()
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
username, secret, err := helper.Get(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
buffer.Reset()
|
|
|
|
err = json.NewEncoder(buffer).Encode(Credentials{
|
2017-06-05 18:23:21 -04:00
|
|
|
ServerURL: serverURL,
|
2017-08-16 16:02:38 -04:00
|
|
|
Username: username,
|
|
|
|
Secret: secret,
|
2023-07-17 10:06:44 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
_, _ = fmt.Fprint(writer, buffer.String())
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase removes credentials from the store.
|
|
|
|
// The reader must contain the server URL to remove.
|
|
|
|
func Erase(helper Helper, reader io.Reader) error {
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buffer.Write(scanner.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverURL := strings.TrimSpace(buffer.String())
|
2017-06-05 18:23:21 -04:00
|
|
|
if len(serverURL) == 0 {
|
|
|
|
return NewErrCredentialsMissingServerURL()
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
return helper.Delete(serverURL)
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:00:20 -04:00
|
|
|
// List returns all the serverURLs of keys in
|
|
|
|
// the OS store as a list of strings
|
2017-04-17 18:08:24 -04:00
|
|
|
func List(helper Helper, writer io.Writer) error {
|
|
|
|
accts, err := helper.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.NewEncoder(writer).Encode(accts)
|
|
|
|
}
|
2017-08-16 16:02:38 -04:00
|
|
|
|
2022-08-29 10:00:20 -04:00
|
|
|
// PrintVersion outputs the current version.
|
2017-08-16 16:02:38 -04:00
|
|
|
func PrintVersion(writer io.Writer) error {
|
2023-07-17 10:06:44 -04:00
|
|
|
_, _ = fmt.Fprintf(writer, "%s (%s) %s\n", Name, Package, Version)
|
2017-08-16 16:02:38 -04:00
|
|
|
return nil
|
|
|
|
}
|