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

Commit 582eac5

Browse files
committed
Add passphrase fallback prompt
1 parent d918660 commit 582eac5

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

cmd/secrethub-http-proxy/main.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/signal"
99
"syscall"
1010

11+
"github.com/abiosoft/readline"
1112
"github.com/keylockerbv/secrethub-http-proxy/pkg/restproxy"
1213
"github.com/secrethub/secrethub-go/pkg/secrethub"
1314
)
@@ -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,20 @@ func main() {
5285
}
5386
}
5487

88+
func promptPassword() (string, error) {
89+
reader, err := readline.New("")
90+
password, err := reader.ReadPassword("Please put in the passphrase to unlock your credential:")
91+
if err == readline.ErrInterrupt {
92+
return "", nil
93+
}
94+
95+
if err != nil {
96+
return "", err
97+
}
98+
99+
return string(password), nil
100+
}
101+
55102
func gracefulShutdown(proxy restproxy.ClientProxy) {
56103
sigint := make(chan os.Signal, 1)
57104

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module github.com/keylockerbv/secrethub-http-proxy
22

33
require (
4+
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db
5+
github.com/chzyer/logex v1.1.10 // indirect
6+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
47
github.com/gorilla/mux v1.7.0
58
github.com/secrethub/secrethub-go v0.17.0
69
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
bitbucket.org/zombiezen/cardcpx v0.0.0-20150417151802-902f68ff43ef h1:Y5Zf3CYdrdGE7GOuK/MNN98GS1V8mOfeiJlISrKUcEo=
22
bitbucket.org/zombiezen/cardcpx v0.0.0-20150417151802-902f68ff43ef/go.mod h1:ZJR5FpaQx7Bt2bzIV3gBaCInI1+kG949WhNYYlRr8eA=
3+
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8=
4+
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530=
35
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco=
46
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
57
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4=
68
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
9+
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
10+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
11+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
12+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
713
github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk=
814
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
915
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=

0 commit comments

Comments
 (0)