@@ -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
1516var (
@@ -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+
4376func 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+
5599func gracefulShutdown (proxy restproxy.ClientProxy ) {
56100 sigint := make (chan os.Signal , 1 )
57101
0 commit comments