Skip to content

Commit

Permalink
Use docker-credential-helper to manage secrets (WIP)
Browse files Browse the repository at this point in the history
Store service credentials securely in the stores supported by docker:
- https://github.com/docker/docker-credential-helpers#available-programs

Introduces a top-level config property, "secretStore" and additional
command line arguments to manage the stored secrets.

The value of secretStore is used to find a helper command,
`docker-credential-<secretStore>`.

The docker project currently provides 4 store helpers:
- "osxkeychain" (OS X only)
- "secretservice" (Linux only)
- "wincred" (Windows only)
- "pass" (any OS supporting pass, which uses gpg2)

Docker-for-desktop installs the credential helpers above, as well as
"desktop" (docker-credential-desktop).

Generic installation instructions for the helpers:
- https://github.com/docker/docker-credential-helpers#installation

Users could provide additional helpers, the only requirement is that the
helper implements the credential store protocol:
- https://github.com/docker/docker-credential-helpers#development

The credential protocol is open, and new credential stores can be
implemented by any CLI satisfying the protocol:
- https://github.com/docker/docker-credential-helpers#development

The modifications to existing modules is not tested due to lack
of API keys, but demonstrates the unobtrusive changes required to
use the secret store.
  • Loading branch information
sam-github committed Apr 29, 2020
1 parent cc8f5f7 commit c6d19ac
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 326 deletions.
164 changes: 164 additions & 0 deletions cfg/secrets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package cfg

import (
"errors"
"fmt"
"runtime"

"github.com/docker/docker-credential-helpers/client"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/logger"
)

type Secret struct {
Service string
Secret string
Username string
Store string
}

// Configure the secret for a service.
//
// Does not overwrite explicitly configured values, so is safe to call
// if username and secret were explicitly set in module config.
//
// Input:
// * service: URL or identifier for service if configured by user. Not all
// modules support or need this. Optional, defaults to serviceDefault.
// * serviceDefault: Default URL or identifier for service. Must be unique,
// using the API URL is customary, but using the module name is reasonable.
// Required, secrets cannot be stored unless associated with a service.
//
// Output:
// * username: If a user/subdomain/identifier specific to the service is
// configurable, it can be saved as a "username". Optional.
// * secret: The secret for service. Optional.
func ConfigureSecret(
globalConfig *config.Config,
service string,
serviceDefault string,
username *string,
secret *string, // unfortunate order dependency...
) {
notWanted := func(out *string) bool {
return out == nil && *out != ""
}

// Don't try to fetch from cred store if nothing is wanted.
if notWanted(secret) && notWanted(username) {
return
}

if service == "" {
service = serviceDefault
}

if service == "" {
return
}

cred, err := FetchSecret(globalConfig, service)

if err != nil {
logger.Log(fmt.Sprintf("Loading secret failed: %s", err.Error()))
return
}

if cred == nil {
// No secret store configued.
return
}

if username != nil && *username == "" {
*username = cred.Username
}

if secret != nil && *secret == "" {
*secret = cred.Secret
}
}

// Fetch secret for `service`. Service is customarily a URL, but can be any
// identifier uniquely used by wtf to identify the service, such as the name
// of the module. nil is returned if the secretStore global property is not
// present or the secret is not found in that store.
func FetchSecret(globalConfig *config.Config, service string) (*Secret, error) {
prog := newProgram(globalConfig)

if prog == nil {
// No secret store configured.
return nil, nil
}

cred, err := client.Get(prog.runner, service)

if err != nil {
return nil, fmt.Errorf("get %v from %v: %w", service, prog.store, err)
}

return &Secret{
Service: cred.ServerURL,
Secret: cred.Secret,
Username: cred.Username,
Store: prog.store,
}, nil
}

func StoreSecret(globalConfig *config.Config, secret *Secret) error {
prog := newProgram(globalConfig)

if prog == nil {
return errors.New("Cannot store secrets: wtf.secretStore is not configured")
}

cred := &credentials.Credentials{
ServerURL: secret.Service,
Username: secret.Username,
Secret: secret.Secret,
}

// docker-credential requires a username, but it isn't necessary for
// all services. Use a default if a username was not set.
if cred.Username == "" {
cred.Username = "default"
}

err := client.Store(prog.runner, cred)

if err != nil {
return fmt.Errorf("store %v: %w", prog.store, err)
}

return nil
}

type program struct {
store string
runner client.ProgramFunc
}

func newProgram(globalConfig *config.Config) *program {
secretStore := globalConfig.UString("wtf.secretStore", "(none)")

if secretStore == "(none)" {
return nil
}

if secretStore == "" {
switch runtime.GOOS {
case "windows":
secretStore = "winrt"
case "darwin":
secretStore = "osxkeychain"
default:
secretStore = "secretservice"
}

}

return &program{
secretStore,
client.NewShellProgramFunc("docker-credential-" + secretStore),
}
}
87 changes: 87 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package flags

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/chzyer/readline"
goFlags "github.com/jessevdk/go-flags"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
Expand All @@ -17,10 +20,28 @@ type Flags struct {
Module string `short:"m" long:"module" optional:"yes" description:"Display info about a specific module, i.e.: 'wtfutil -m=todo'"`
Profile bool `short:"p" long:"profile" optional:"yes" description:"Profile application memory usage"`
Version bool `short:"v" long:"version" description:"Show version info"`
// Work-around go-flags misfeatures. If any sub-command is defined
// then `wtf` (no sub-commands, the common usage), is warned about.
Opt struct {
Cmd string `positional-arg-name:"command"`
Args []string `positional-arg-name:"args"`
} `positional-args:"yes"`

hasCustom bool
}

var EXTRA = `
Commands:
save-secret <service> <secret> [username]
service Service URL or name for secret.
secret Secret to be saved for the service.
username Username to associate with the service.
Save a secret into the secret store. Requires wtf.secretStore
to be configured. See individual modules for information on what
service, secret, and username means for their configuration. Not
all modules use secrets, and not all secrets require a username.
`

// NewFlags creates an instance of Flags
func NewFlags() *Flags {
flags := Flags{}
Expand All @@ -46,6 +67,71 @@ func (flags *Flags) RenderIf(version, date string, config *config.Config) {
fmt.Println(fmt.Sprintf("%s (%s)", version, date))
os.Exit(0)
}

if flags.Opt.Cmd == "" {
return
}

switch cmd := flags.Opt.Cmd; cmd {
case "save-secret":
var service, secret, username string
args := flags.Opt.Args

if len(args) < 1 || args[0] == "" {
fmt.Fprintf(os.Stderr, "save-secret: service required, see `%s --help`\n", os.Args[0])
os.Exit(1)
}

service = args[0]

if len(args) > 1 {
secret = args[1]
} else {
b, err := readline.Password("Secret (required): ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
secret = string(b)
}
secret = strings.TrimSpace(secret)

if secret == "" {
fmt.Fprintf(os.Stderr, "save-secret: secret required, see `%s --help`\n", os.Args[0])
os.Exit(1)
}

if len(args) > 2 {
username = args[2]
} else {
fmt.Printf("Username (optional): ")
reader := bufio.NewReader(os.Stdin)
var err error
username, err = reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
username = strings.TrimSpace(username)

err := cfg.StoreSecret(config, &cfg.Secret{
Service: service,
Secret: secret,
Username: username,
})

if err != nil {
fmt.Fprintf(os.Stderr, "Saving secret for service %q: %s\n", service, err.Error())
os.Exit(1)
}

fmt.Printf("Saved secret for service %q (username %q)\n", service, username)
os.Exit(0)
default:
fmt.Fprintf(os.Stderr, "Command `%s` is not supported, try `%s --help`\n", cmd, os.Args[0])
os.Exit(1)
}
}

// HasCustomConfig returns TRUE if a config path was passed in, FALSE if one was not
Expand All @@ -68,6 +154,7 @@ func (flags *Flags) Parse() {
parser := goFlags.NewParser(flags, goFlags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*goFlags.Error); ok && flagsErr.Type == goFlags.ErrHelp {
fmt.Println(EXTRA)
os.Exit(0)
}
}
Expand Down
54 changes: 18 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,48 @@ module github.com/wtfutil/wtf
go 1.14

require (
code.cloudfoundry.org/bytefmt v0.0.0-20190819182555-854d396b647c
github.com/Azure/go-autorest v11.1.2+incompatible // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/PagerDuty/go-pagerduty v0.0.0-20191002190746-f60f4fc45222
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48
github.com/PagerDuty/go-pagerduty v1.1.2
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/VictorAvelar/devto-api-go v1.0.0
github.com/adlio/trello v1.7.0
github.com/alecthomas/chroma v0.7.2
github.com/andygrunwald/go-gerrit v0.0.0-20190825170856-5959a9bf9ff8
github.com/briandowns/openweathermap v0.0.0-20180804155945-5f41b7c9d92d
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/andygrunwald/go-gerrit v0.0.0-20191101112536-3f5e365ccf57
github.com/briandowns/openweathermap v0.13.0
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/digitalocean/godo v1.35.1
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/docker-credential-helpers v0.6.3
github.com/dustin/go-humanize v1.0.0
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
github.com/gdamore/tcell v1.3.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/google/go-github/v26 v26.1.3
github.com/gophercloud/gophercloud v0.5.0 // indirect
github.com/hekmon/cunits v2.0.1+incompatible // indirect
github.com/hekmon/transmissionrpc v0.0.0-20190525133028-1d589625bacd
github.com/imdario/mergo v0.3.8 // indirect
github.com/hekmon/transmissionrpc v0.1.0
github.com/jessevdk/go-flags v1.4.0
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b
github.com/microsoft/azure-devops-go-api/azuredevops v0.0.0-20191014190507-26902c1d4325
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/microsoft/azure-devops-go-api/azuredevops v0.0.0-20200327121006-543de4815ec2
github.com/mmcdole/gofeed v1.0.0-beta2
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/nicklaw5/helix v0.5.8
github.com/olebedev/config v0.0.0-20190528211619-364964f3a8e4
github.com/olekukonko/tablewriter v0.0.4
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/ovh/cds v0.0.0-20200131143542-5e69464c6598
github.com/ovh/cds v0.0.0-20200428124128-1e25af6d3112
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.4.0
github.com/radovskyb/watcher v1.0.7
github.com/rivo/tview v0.0.0-20200108161608-1316ea7a4b35
github.com/rivo/tview v0.0.0-20200414130344-8e06c826b3a5
github.com/shirou/gopsutil v2.20.3+incompatible
github.com/stretchr/testify v1.5.1
github.com/wtfutil/spotigopher v0.0.0-20191127141047-7d8168fe103a
github.com/wtfutil/todoist v0.0.2-0.20191216004217-0ec29ceda61a
github.com/wtfutil/todoist v0.0.1
github.com/xanzy/go-gitlab v0.31.0
github.com/zmb3/spotify v0.0.0-20191010212056-e12fb981aacb
github.com/zmb3/spotify v0.0.0-20200422222148-5fe5f9535a2c
github.com/zorkian/go-datadog-api v2.28.0+incompatible
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/text v0.3.2
google.golang.org/api v0.22.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/jarcoal/httpmock.v1 v1.0.0-20181110093347-3be5f16b70eb // indirect
gopkg.in/yaml.v2 v2.2.8
gotest.tools v2.2.0+incompatible
k8s.io/apimachinery v0.0.0-20190223094358-dcb391cde5ca
k8s.io/client-go v10.0.0+incompatible
k8s.io/api v0.18.2 // indirect
k8s.io/apimachinery v0.18.2
k8s.io/client-go v11.0.0+incompatible
k8s.io/utils v0.0.0-20200414100711-2df71ebbae66 // indirect
)
Loading

0 comments on commit c6d19ac

Please sign in to comment.