Skip to content

Commit 9f5b990

Browse files
committed
now storing credentials in store when available
1 parent 12b0b0b commit 9f5b990

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Client
10+
#### Added
11+
- Password is now stored into the OS credential manager upon successful interactive authentication.
912

1013
## [3.1.0] - 2021-08-19
1114
### Server

client/interactive.go

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"github.com/zalando/go-keyring"
78
"io"
89
"io/ioutil"
910
"net/http"
@@ -16,6 +17,8 @@ import (
1617
"vbouchaud/k8s-ldap-auth/types"
1718
)
1819

20+
const credentialIdentifier = "k8s-ldap-auth"
21+
1922
func readData(readLine func(screen io.ReadWriter) (string, error)) (string, error) {
2023
if !isatty.IsTerminal(os.Stdin.Fd()) && !isatty.IsCygwinTerminal(os.Stdin.Fd()) {
2124
return "", fmt.Errorf("stdin should be terminal")
@@ -74,6 +77,8 @@ func performAuth(addr, user, pass string) ([]byte, error) {
7477
res *http.Response
7578
)
7679

80+
interactiveMode := false
81+
7782
if user == "" {
7883
log.Info().Msg("Username was not provided, asking for input")
7984
user, err = readData(username)
@@ -85,6 +90,14 @@ func performAuth(addr, user, pass string) ([]byte, error) {
8590
log.Info().Str("username", user).Msg("Username exists.")
8691

8792
if pass == "" {
93+
pass, err = keyring.Get(credentialIdentifier, user)
94+
if err != nil {
95+
log.Error().Err(err).Msg("Error while fetching credentials from store.")
96+
}
97+
}
98+
99+
if pass == "" {
100+
interactiveMode = true
88101
log.Info().Msg("Password was not provided, asking for input")
89102
pass, err = readData(password)
90103
print("\n")
@@ -110,6 +123,9 @@ func performAuth(addr, user, pass string) ([]byte, error) {
110123
defer res.Body.Close()
111124

112125
if res.StatusCode != http.StatusOK {
126+
if err := keyring.Delete(credentialIdentifier, user); err != nil {
127+
log.Error().Err(err).Msg("Error while removing credentials from store.")
128+
}
113129
return nil, fmt.Errorf(http.StatusText(res.StatusCode))
114130
}
115131

@@ -119,5 +135,11 @@ func performAuth(addr, user, pass string) ([]byte, error) {
119135
return nil, err
120136
}
121137

138+
if interactiveMode {
139+
if err = keyring.Set(credentialIdentifier, user, pass); err != nil {
140+
log.Error().Err(err).Msg("Error while registering credentials into store.")
141+
}
142+
}
143+
122144
return body, nil
123145
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/mattn/go-isatty v0.0.14
1212
github.com/rs/zerolog v1.25.0
1313
github.com/urfave/cli/v2 v2.3.0
14+
github.com/zalando/go-keyring v0.1.1
1415
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72
1516
k8s.io/api v0.22.2
1617
k8s.io/apimachinery v0.22.2

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
4747
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
4848
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4949
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
50+
github.com/danieljoos/wincred v1.1.0 h1:3RNcEpBg4IhIChZdFRSdlQt1QjCp1sMAPIrOnm7Yf8g=
51+
github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7hqDjlFjiygg=
5052
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5153
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5254
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -80,6 +82,8 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
8082
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
8183
github.com/goccy/go-json v0.7.8 h1:CvMH7LotYymYuLGEohBM1lTZWX4g6jzWUUl2aLFuBoE=
8284
github.com/goccy/go-json v0.7.8/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
85+
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
86+
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
8387
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
8488
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
8589
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
@@ -210,6 +214,7 @@ github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzu
210214
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
211215
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
212216
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
217+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
213218
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
214219
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
215220
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@@ -222,6 +227,8 @@ github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/X
222227
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
223228
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
224229
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
230+
github.com/zalando/go-keyring v0.1.1 h1:w2V9lcx/Uj4l+dzAf1m9s+DJ1O8ROkEHnynonHjTcYE=
231+
github.com/zalando/go-keyring v0.1.1/go.mod h1:OIC+OZ28XbmwFxU/Rp9V7eKzZjamBJwRzC8UFJH9+L8=
225232
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
226233
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
227234
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

0 commit comments

Comments
 (0)