Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 50a7d15

Browse files
authored
Merge pull request #16 from keylockerbv/feature/passphrase-prompt
Add passphrase fallback prompt
2 parents a42c3c6 + 2279cc9 commit 50a7d15

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ RUN apk add --no-cache ca-certificates && update-ca-certificates
55

66
EXPOSE 8080
77

8-
CMD secrethub-http-proxy -C ${SECRETHUB_CREDENTIAL:-$(cat /secrethub/credential)} -P ${SECRETHUB_CREDENTIAL_PASSPHRASE} -h 0.0.0.0 -p 8080
8+
CMD secrethub-http-proxy -C ${SECRETHUB_CREDENTIAL:-$(cat /secrethub/credential)} -P ${SECRETHUB_CREDENTIAL_PASSPHRASE:-""} -h 0.0.0.0 -p 8080

cmd/secrethub-http-proxy/main.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/keylockerbv/secrethub-http-proxy/pkg/restproxy"
1212
"github.com/secrethub/secrethub-go/pkg/secrethub"
13+
"golang.org/x/crypto/ssh/terminal"
1314
)
1415

1516
var (
@@ -32,14 +33,46 @@ func init() {
3233
exit(fmt.Errorf("credential is required"))
3334
}
3435

35-
cred, err := secrethub.NewCredential(credential, credentialPassphrase)
36+
cred, err := findCredential(credential, credentialPassphrase)
3637
if err != nil {
3738
exit(err)
3839
}
3940

4041
client = secrethub.NewClient(cred, nil)
4142
}
4243

44+
func findCredential(credential string, passphrase string) (secrethub.Credential, error) {
45+
parser := secrethub.NewCredentialParser(secrethub.DefaultCredentialDecoders)
46+
47+
encoded, err := parser.Parse(credential)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
if encoded.IsEncrypted() {
53+
if passphrase == "" {
54+
passphrase, err = promptPassword()
55+
if err != nil {
56+
return nil, err
57+
}
58+
}
59+
60+
key, err := secrethub.NewPassBasedKey([]byte(passphrase))
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
credential, err := encoded.DecodeEncrypted(key)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
return credential, err
71+
}
72+
73+
return encoded.Decode()
74+
}
75+
4376
func main() {
4477
proxy := restproxy.NewRESTProxy(client, host, port)
4578

@@ -52,6 +85,17 @@ func main() {
5285
}
5386
}
5487

88+
func promptPassword() (string, error) {
89+
fmt.Printf("Please put in the passphrase to unlock your credential:")
90+
password, err := terminal.ReadPassword(int(syscall.Stdin))
91+
fmt.Println()
92+
if err != nil {
93+
return "", err
94+
}
95+
96+
return string(password), nil
97+
}
98+
5599
func gracefulShutdown(proxy restproxy.ClientProxy) {
56100
sigint := make(chan os.Signal, 1)
57101

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ module github.com/keylockerbv/secrethub-http-proxy
33
require (
44
github.com/gorilla/mux v1.7.0
55
github.com/secrethub/secrethub-go v0.17.0
6+
golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b
7+
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 // indirect
68
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ github.com/secrethub/secrethub-go v0.17.0 h1:AlRbFlLofhzY7Onv1QRI0u0iB4UTr8PCrNW
3131
github.com/secrethub/secrethub-go v0.17.0/go.mod h1:gqrxdTNcVowCy/Bo49Y+7En12aV24wnoG3ktUF9xW/k=
3232
golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b h1:+/WWzjwW6gidDJnMKWLKLX1gxn7irUTF1fLpQovfQ5M=
3333
golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
34+
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 h1:hOY+O8MxdkPV10pNf7/XEHaySCiPKxixMKUshfHsGn0=
35+
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3436
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
3537
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)