add better generate

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-03-20 01:33:56 -04:00
parent 3fc6abf56b
commit cdd93563f5
5655 changed files with 1187011 additions and 392 deletions

View file

@ -0,0 +1,186 @@
package credentials
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
// Credentials holds the information shared between docker and the credentials store.
type Credentials struct {
ServerURL string
Username string
Secret string
}
// 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
}
// CredsLabel holds the way Docker credentials should be labeled as such in credentials stores that allow labelling.
// 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"
// SetCredsLabel is a simple setter for CredsLabel
func SetCredsLabel(label string) {
CredsLabel = label
}
// Serve initializes the credentials helper and parses the action argument.
// 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) {
var err error
if len(os.Args) != 2 {
err = fmt.Errorf("Usage: %s <store|get|erase|list|version>", os.Args[0])
}
if err == nil {
err = HandleCommand(helper, os.Args[1], os.Stdin, os.Stdout)
}
if err != nil {
fmt.Fprintf(os.Stdout, "%v\n", err)
os.Exit(1)
}
}
// HandleCommand uses a helper and a key to run a credential action.
func HandleCommand(helper Helper, key string, in io.Reader, out io.Writer) error {
switch key {
case "store":
return Store(helper, in)
case "get":
return Get(helper, in, out)
case "erase":
return Erase(helper, in)
case "list":
return List(helper, out)
case "version":
return PrintVersion(out)
}
return fmt.Errorf("Unknown credential action `%s`", key)
}
// 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
}
if ok, err := creds.isValid(); !ok {
return err
}
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())
if len(serverURL) == 0 {
return NewErrCredentialsMissingServerURL()
}
username, secret, err := helper.Get(serverURL)
if err != nil {
return err
}
resp := Credentials{
ServerURL: serverURL,
Username: username,
Secret: secret,
}
buffer.Reset()
if err := json.NewEncoder(buffer).Encode(resp); err != nil {
return err
}
fmt.Fprint(writer, buffer.String())
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())
if len(serverURL) == 0 {
return NewErrCredentialsMissingServerURL()
}
return helper.Delete(serverURL)
}
//List returns all the serverURLs of keys in
//the OS store as a list of strings
func List(helper Helper, writer io.Writer) error {
accts, err := helper.List()
if err != nil {
return err
}
return json.NewEncoder(writer).Encode(accts)
}
//PrintVersion outputs the current version.
func PrintVersion(writer io.Writer) error {
fmt.Fprintln(writer, Version)
return nil
}

View file

@ -0,0 +1,249 @@
package credentials
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
)
type memoryStore struct {
creds map[string]*Credentials
}
func newMemoryStore() *memoryStore {
return &memoryStore{
creds: make(map[string]*Credentials),
}
}
func (m *memoryStore) Add(creds *Credentials) error {
m.creds[creds.ServerURL] = creds
return nil
}
func (m *memoryStore) Delete(serverURL string) error {
delete(m.creds, serverURL)
return nil
}
func (m *memoryStore) Get(serverURL string) (string, string, error) {
c, ok := m.creds[serverURL]
if !ok {
return "", "", fmt.Errorf("creds not found for %s", serverURL)
}
return c.Username, c.Secret, nil
}
func (m *memoryStore) List() (map[string]string, error) {
//Simply a placeholder to let memoryStore be a valid implementation of Helper interface
return nil, nil
}
func TestStore(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); err != nil {
t.Fatal(err)
}
c, ok := h.creds[serverURL]
if !ok {
t.Fatalf("creds not found for %s\n", serverURL)
}
if c.Username != "foo" {
t.Fatalf("expected username foo, got %s\n", c.Username)
}
if c.Secret != "bar" {
t.Fatalf("expected username bar, got %s\n", c.Secret)
}
}
func TestStoreMissingServerURL(t *testing.T) {
creds := &Credentials{
ServerURL: "",
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); IsCredentialsMissingServerURL(err) == false {
t.Fatal(err)
}
}
func TestStoreMissingUsername(t *testing.T) {
creds := &Credentials{
ServerURL: "https://index.docker.io/v1/",
Username: "",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); IsCredentialsMissingUsername(err) == false {
t.Fatal(err)
}
}
func TestGet(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); err != nil {
t.Fatal(err)
}
buf := strings.NewReader(serverURL)
w := new(bytes.Buffer)
if err := Get(h, buf, w); err != nil {
t.Fatal(err)
}
if w.Len() == 0 {
t.Fatalf("expected output in the writer, got %d", w.Len())
}
var c Credentials
if err := json.NewDecoder(w).Decode(&c); err != nil {
t.Fatal(err)
}
if c.Username != "foo" {
t.Fatalf("expected username foo, got %s\n", c.Username)
}
if c.Secret != "bar" {
t.Fatalf("expected username bar, got %s\n", c.Secret)
}
}
func TestGetMissingServerURL(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); err != nil {
t.Fatal(err)
}
buf := strings.NewReader("")
w := new(bytes.Buffer)
if err := Get(h, buf, w); IsCredentialsMissingServerURL(err) == false {
t.Fatal(err)
}
}
func TestErase(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); err != nil {
t.Fatal(err)
}
buf := strings.NewReader(serverURL)
if err := Erase(h, buf); err != nil {
t.Fatal(err)
}
w := new(bytes.Buffer)
if err := Get(h, buf, w); err == nil {
t.Fatal("expected error getting missing creds, got empty")
}
}
func TestEraseMissingServerURL(t *testing.T) {
serverURL := "https://index.docker.io/v1/"
creds := &Credentials{
ServerURL: serverURL,
Username: "foo",
Secret: "bar",
}
b, err := json.Marshal(creds)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(b)
h := newMemoryStore()
if err := Store(h, in); err != nil {
t.Fatal(err)
}
buf := strings.NewReader("")
if err := Erase(h, buf); IsCredentialsMissingServerURL(err) == false {
t.Fatal(err)
}
}
func TestList(t *testing.T) {
//This tests that there is proper input an output into the byte stream
//Individual stores are very OS specific and have been tested in osxkeychain and secretservice respectively
out := new(bytes.Buffer)
h := newMemoryStore()
if err := List(h, out); err != nil {
t.Fatal(err)
}
//testing that there is an output
if out.Len() == 0 {
t.Fatalf("expected output in the writer, got %d", 0)
}
}

View file

@ -0,0 +1,102 @@
package credentials
const (
// ErrCredentialsNotFound standardizes the not found error, so every helper returns
// the same message and docker can handle it properly.
errCredentialsNotFoundMessage = "credentials not found in native keychain"
// ErrCredentialsMissingServerURL and ErrCredentialsMissingUsername standardize
// invalid credentials or credentials management operations
errCredentialsMissingServerURLMessage = "no credentials server URL"
errCredentialsMissingUsernameMessage = "no credentials username"
)
// errCredentialsNotFound represents an error
// raised when credentials are not in the store.
type errCredentialsNotFound struct{}
// Error returns the standard error message
// for when the credentials are not in the store.
func (errCredentialsNotFound) Error() string {
return errCredentialsNotFoundMessage
}
// NewErrCredentialsNotFound creates a new error
// for when the credentials are not in the store.
func NewErrCredentialsNotFound() error {
return errCredentialsNotFound{}
}
// IsErrCredentialsNotFound returns true if the error
// was caused by not having a set of credentials in a store.
func IsErrCredentialsNotFound(err error) bool {
_, ok := err.(errCredentialsNotFound)
return ok
}
// IsErrCredentialsNotFoundMessage returns true if the error
// was caused by not having a set of credentials in a store.
//
// This function helps to check messages returned by an
// external program via its standard output.
func IsErrCredentialsNotFoundMessage(err string) bool {
return err == errCredentialsNotFoundMessage
}
// errCredentialsMissingServerURL represents an error raised
// when the credentials object has no server URL or when no
// server URL is provided to a credentials operation requiring
// one.
type errCredentialsMissingServerURL struct{}
func (errCredentialsMissingServerURL) Error() string {
return errCredentialsMissingServerURLMessage
}
// errCredentialsMissingUsername represents an error raised
// when the credentials object has no username or when no
// username is provided to a credentials operation requiring
// one.
type errCredentialsMissingUsername struct{}
func (errCredentialsMissingUsername) Error() string {
return errCredentialsMissingUsernameMessage
}
// NewErrCredentialsMissingServerURL creates a new error for
// errCredentialsMissingServerURL.
func NewErrCredentialsMissingServerURL() error {
return errCredentialsMissingServerURL{}
}
// NewErrCredentialsMissingUsername creates a new error for
// errCredentialsMissingUsername.
func NewErrCredentialsMissingUsername() error {
return errCredentialsMissingUsername{}
}
// IsCredentialsMissingServerURL returns true if the error
// was an errCredentialsMissingServerURL.
func IsCredentialsMissingServerURL(err error) bool {
_, ok := err.(errCredentialsMissingServerURL)
return ok
}
// IsCredentialsMissingServerURLMessage checks for an
// errCredentialsMissingServerURL in the error message.
func IsCredentialsMissingServerURLMessage(err string) bool {
return err == errCredentialsMissingServerURLMessage
}
// IsCredentialsMissingUsername returns true if the error
// was an errCredentialsMissingUsername.
func IsCredentialsMissingUsername(err error) bool {
_, ok := err.(errCredentialsMissingUsername)
return ok
}
// IsCredentialsMissingUsernameMessage checks for an
// errCredentialsMissingUsername in the error message.
func IsCredentialsMissingUsernameMessage(err string) bool {
return err == errCredentialsMissingUsernameMessage
}

View file

@ -0,0 +1,14 @@
package credentials
// Helper is the interface a credentials store helper must implement.
type Helper interface {
// Add appends credentials to the store.
Add(*Credentials) error
// Delete removes credentials from the store.
Delete(serverURL string) error
// Get retrieves credentials from the store.
// It returns username and secret as strings.
Get(serverURL string) (string, string, error)
// List returns the stored serverURLs and their associated usernames.
List() (map[string]string, error)
}

View file

@ -0,0 +1,4 @@
package credentials
// Version holds a string describing the current version
const Version = "0.6.0"