Skip to content

Commit 38c90e6

Browse files
committed
Use native keychain bindings if possible
Switch from github.com/zalando/go-keyring to github.com/99designs/keyring that uses native bindings on macOS and Windows. On darwin, github.com/zalando/go-keyring encodes secret data in hex or base64, adding a prefix indicating that. If new library returns this prefix, convert the secret to plain data to keep using existing token and not require reauthentication. Originally suggested in cli#7023 (comment) Fixes cli#7123
1 parent 534f6d7 commit 38c90e6

6 files changed

Lines changed: 78 additions & 16 deletions

File tree

‎go.mod‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/cli/cli/v2
33
go 1.21
44

55
require (
6+
github.com/99designs/keyring v1.2.2
67
github.com/AlecAivazis/survey/v2 v2.3.7
78
github.com/MakeNowJust/heredoc v1.0.0
89
github.com/briandowns/spinner v1.18.1
@@ -47,6 +48,7 @@ require (
4748
)
4849

4950
require (
51+
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
5052
github.com/alecthomas/chroma v0.10.0 // indirect
5153
github.com/alessio/shellescape v1.4.1 // indirect
5254
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
@@ -56,11 +58,14 @@ require (
5658
github.com/danieljoos/wincred v1.2.0 // indirect
5759
github.com/davecgh/go-spew v1.1.1 // indirect
5860
github.com/dlclark/regexp2 v1.4.0 // indirect
61+
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
5962
github.com/fatih/color v1.7.0 // indirect
6063
github.com/gdamore/encoding v1.0.0 // indirect
64+
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
6165
github.com/godbus/dbus/v5 v5.1.0 // indirect
6266
github.com/golang/protobuf v1.5.3 // indirect
6367
github.com/gorilla/css v1.0.0 // indirect
68+
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
6469
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
6570
github.com/hashicorp/errwrap v1.0.0 // indirect
6671
github.com/inconshreveable/mousetrap v1.0.1 // indirect
@@ -70,6 +75,7 @@ require (
7075
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
7176
github.com/mattn/go-runewidth v0.0.14 // indirect
7277
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
78+
github.com/mtibben/percent v0.2.1 // indirect
7379
github.com/muesli/reflow v0.3.0 // indirect
7480
github.com/muesli/termenv v0.13.0 // indirect
7581
github.com/olekukonko/tablewriter v0.0.5 // indirect
@@ -85,5 +91,4 @@ require (
8591
golang.org/x/net v0.17.0 // indirect
8692
golang.org/x/sys v0.13.0 // indirect
8793
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
88-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
8994
)

‎go.sum‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
2+
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
3+
github.com/99designs/keyring v1.2.2 h1:pZd3neh/EmUzWONb35LxQfvuY7kiSXAq3HQd97+XBn0=
4+
github.com/99designs/keyring v1.2.2/go.mod h1:wes/FrByc8j7lFOAGLGSNEg8f/PaI3cgTBqhFkHUrPk=
15
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
26
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
37
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
@@ -46,6 +50,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
4650
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4751
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
4852
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
53+
github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM=
54+
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
4955
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
5056
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
5157
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
@@ -54,6 +60,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
5460
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
5561
github.com/gdamore/tcell/v2 v2.5.4 h1:TGU4tSjD3sCL788vFNeJnTdzpNKIw1H5dgLnJRQVv/k=
5662
github.com/gdamore/tcell/v2 v2.5.4/go.mod h1:dZgRy5v4iMobMEcWNYBtREnDZAT9DYmfqIkrgEMxLyw=
63+
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
64+
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
5765
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
5866
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5967
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
@@ -68,6 +76,8 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
6876
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
6977
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
7078
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
79+
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
80+
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
7181
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
7282
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
7383
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
@@ -90,8 +100,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
90100
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
91101
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
92102
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
93-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
94-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
103+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
104+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
95105
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
96106
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
97107
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -118,6 +128,8 @@ github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3r
118128
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
119129
github.com/microsoft/dev-tunnels v0.0.25 h1:UlMKUI+2O8cSu4RlB52ioSyn1LthYSVkJA+CSTsdKoA=
120130
github.com/microsoft/dev-tunnels v0.0.25/go.mod h1:frU++12T/oqxckXkDpTuYa427ncguEOodSPZcGCCrzQ=
131+
github.com/mtibben/percent v0.2.1 h1:5gssi8Nqo8QU/r2pynCm+hBQHpkB/uNK7BJCFogWdzs=
132+
github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ibNBTZrns=
121133
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
122134
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
123135
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
@@ -128,6 +140,8 @@ github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38 h1:0Fr
128140
github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU=
129141
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
130142
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
143+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
144+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
131145
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
132146
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
133147
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
@@ -241,8 +255,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
241255
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
242256
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
243257
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
244-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
245-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
258+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
259+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
246260
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
247261
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
248262
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎internal/keyring/keyring.go‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
package keyring
33

44
import (
5+
"strings"
56
"time"
67

7-
"github.com/zalando/go-keyring"
8+
"github.com/99designs/keyring"
9+
zkeyring "github.com/zalando/go-keyring"
810
)
911

12+
// Timeout for all operations with keyring.
13+
// Provide enough time for user to authenticate if necessary.
14+
const timeout = 30 * time.Second
15+
1016
type TimeoutError struct {
1117
message string
1218
}
@@ -15,54 +21,91 @@ func (e *TimeoutError) Error() string {
1521
return e.message
1622
}
1723

24+
var GetKeyring = func(service string) (keyring.Keyring, error) {
25+
return keyring.Open(keyring.Config{ServiceName: service})
26+
}
27+
28+
func MockInit() {
29+
keyrings := make(map[string]keyring.Keyring)
30+
GetKeyring = func(service string) (keyring.Keyring, error) {
31+
kr, ok := keyrings[service]
32+
if !ok {
33+
kr = keyring.NewArrayKeyring(nil)
34+
keyrings[service] = kr
35+
}
36+
return kr, nil
37+
}
38+
}
39+
1840
// Set secret in keyring for user.
1941
func Set(service, user, secret string) error {
42+
kr, err := GetKeyring(service)
43+
if err != nil {
44+
return err
45+
}
2046
ch := make(chan error, 1)
2147
go func() {
2248
defer close(ch)
23-
ch <- keyring.Set(service, user, secret)
49+
ch <- kr.Set(keyring.Item{Key: user, Data: []byte(secret)})
2450
}()
2551
select {
2652
case err := <-ch:
2753
return err
28-
case <-time.After(3 * time.Second):
54+
case <-time.After(timeout):
2955
return &TimeoutError{"timeout while trying to set secret in keyring"}
3056
}
3157
}
3258

3359
// Get secret from keyring given service and user name.
3460
func Get(service, user string) (string, error) {
61+
kr, err := GetKeyring(service)
62+
if err != nil {
63+
return "", err
64+
}
3565
ch := make(chan struct {
3666
val string
3767
err error
3868
}, 1)
3969
go func() {
4070
defer close(ch)
41-
val, err := keyring.Get(service, user)
71+
val, err := kr.Get(user)
72+
sval := string(val.Data)
73+
// go-keyring encodes secret data on darwin adding a prefix
74+
// convert it to plain data with zalando/keyring for backward compatibility
75+
if err == nil && strings.HasPrefix(sval, "go-keyring") {
76+
sval, err = zkeyring.Get(service, user)
77+
if err == nil {
78+
err = kr.Set(keyring.Item{Key: user, Data: []byte(sval)})
79+
}
80+
}
4281
ch <- struct {
4382
val string
4483
err error
45-
}{val, err}
84+
}{sval, err}
4685
}()
4786
select {
4887
case res := <-ch:
4988
return res.val, res.err
50-
case <-time.After(3 * time.Second):
89+
case <-time.After(timeout):
5190
return "", &TimeoutError{"timeout while trying to get secret from keyring"}
5291
}
5392
}
5493

5594
// Delete secret from keyring.
5695
func Delete(service, user string) error {
96+
kr, err := GetKeyring(service)
97+
if err != nil {
98+
return err
99+
}
57100
ch := make(chan error, 1)
58101
go func() {
59102
defer close(ch)
60-
ch <- keyring.Delete(service, user)
103+
ch <- kr.Remove(user)
61104
}()
62105
select {
63106
case err := <-ch:
64107
return err
65-
case <-time.After(3 * time.Second):
108+
case <-time.After(timeout):
66109
return &TimeoutError{"timeout while trying to delete secret from keyring"}
67110
}
68111
}

‎pkg/cmd/auth/login/login_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/MakeNowJust/heredoc"
1111
"github.com/cli/cli/v2/git"
1212
"github.com/cli/cli/v2/internal/config"
13+
"github.com/cli/cli/v2/internal/keyring"
1314
"github.com/cli/cli/v2/internal/prompter"
1415
"github.com/cli/cli/v2/internal/run"
1516
"github.com/cli/cli/v2/pkg/cmdutil"
1617
"github.com/cli/cli/v2/pkg/httpmock"
1718
"github.com/cli/cli/v2/pkg/iostreams"
1819
"github.com/google/shlex"
1920
"github.com/stretchr/testify/assert"
20-
"github.com/zalando/go-keyring"
2121
)
2222

2323
func stubHomeDir(t *testing.T, dir string) {

‎pkg/cmd/auth/logout/logout_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"testing"
99

1010
"github.com/cli/cli/v2/internal/config"
11+
"github.com/cli/cli/v2/internal/keyring"
1112
"github.com/cli/cli/v2/internal/prompter"
1213
"github.com/cli/cli/v2/pkg/cmdutil"
1314
"github.com/cli/cli/v2/pkg/httpmock"
1415
"github.com/cli/cli/v2/pkg/iostreams"
1516
"github.com/google/shlex"
1617
"github.com/stretchr/testify/assert"
17-
"github.com/zalando/go-keyring"
1818
)
1919

2020
func Test_NewCmdLogout(t *testing.T) {

‎pkg/cmd/auth/token/token_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"testing"
66

77
"github.com/cli/cli/v2/internal/config"
8+
"github.com/cli/cli/v2/internal/keyring"
89
"github.com/cli/cli/v2/pkg/cmdutil"
910
"github.com/cli/cli/v2/pkg/iostreams"
1011
"github.com/google/shlex"
1112
"github.com/stretchr/testify/assert"
12-
"github.com/zalando/go-keyring"
1313
)
1414

1515
func TestNewCmdToken(t *testing.T) {

0 commit comments

Comments
 (0)