git.iko.soy/typeable/oauth2-proxy
on master help sign in
Watch the `htpasswd` file for changes and update the `htpasswdMap` (#1701)

* dynamically update the htpasswdMap based on the changes made to the htpasswd file * added tests to validate that htpasswdMap is updated after the htpasswd file is changed * refactored `htpasswd` and `watcher` to lower cognitive complexity * returned errors and refactored tests * added `CHANGELOG.md` entry for #1701 and fixed the codeclimate issue * Apply suggestions from code review Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk> * Fix lint issue from code suggestion * Wrap htpasswd load and watch errors with context * add the htpasswd wrapped error context to the test Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>

commit037cb041d3e72e91aa4a845e660192ce5c1741e0 authorAlexandru Ciobanu <67386493+aiciobanu@users.noreply.github.com> dateThu 1 Sep 2022, 21:46 (4 years ago) parentfcecbeb
8 files changed+244−117
CHANGELOG.md+2 oauthproxy.go+1−1 pkg/authentication/basic/htpasswd.go+78−22 pkg/authentication/basic/htpasswd_test.go+80−1 pkg/watcher/watcher.go+81 validator.go+2−1 watcher.go−81 watcher_unsupported.go−11
CHANGELOG.md+2
@@ -17,6 +17,8 @@ N/A
1717
1818- [#1669](https://github.com/oauth2-proxy/oauth2-proxy/pull/1699) Fix method deprecated error in lint (@t-katsumura)
1919
20+- [#1701](https://github.com/oauth2-proxy/oauth2-proxy/pull/1701) Watch the htpasswd file for changes and update the htpasswdMap (@aiciobanu)
21+
2022- [#1709](https://github.com/oauth2-proxy/oauth2-proxy/pull/1709) Show an alert message when basic auth credentials are invalid (@aiciobanu)
2123- [#1723](https://github.com/oauth2-proxy/oauth2-proxy/pull/1723) Added ability to specify allowed TLS cipher suites. (@crbednarz)
2224
oauthproxy.go+1−1
@@ -113,7 +113,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
113113 var err error
114114 basicAuthValidator, err = basic.NewHTPasswdValidator(opts.HtpasswdFile)
115115 if err != nil {
116− return nil, fmt.Errorf("could not load htpasswdfile: %v", err)
116+ return nil, fmt.Errorf("could not validate htpasswd: %v", err)
117117 }
118118 }
119119
pkg/authentication/basic/htpasswd.go+78−22
@@ -8,8 +8,10 @@ import (
88 "fmt"
99 "io"
1010 "os"
11+ "sync"
1112
1213 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/watcher"
1315 "golang.org/x/crypto/bcrypt"
1416)
1517
@@ -17,6 +19,7 @@ import (
1719// Passwords must be generated with -B for bcrypt or -s for SHA1.
1820type htpasswdMap struct {
1921 users map[string]interface{}
22+ rwm sync.RWMutex
2023}
2124
2225// bcryptPass is used to identify bcrypt passwords in the
@@ -30,10 +33,30 @@ type sha1Pass string
3033// NewHTPasswdValidator constructs an httpasswd based validator from the file
3134// at the path given.
3235func NewHTPasswdValidator(path string) (Validator, error) {
36+ h := &htpasswdMap{users: make(map[string]interface{})}
37+
38+ if err := h.loadHTPasswdFile(path); err != nil {
39+ return nil, fmt.Errorf("could not load htpasswd file: %v", err)
40+ }
41+
42+ if err := watcher.WatchFileForUpdates(path, nil, func() {
43+ err := h.loadHTPasswdFile(path)
44+ if err != nil {
45+ logger.Errorf("%v: no changes were made to the current htpasswd map", err)
46+ }
47+ }); err != nil {
48+ return nil, fmt.Errorf("could not watch htpasswd file: %v", err)
49+ }
50+
51+ return h, nil
52+}
53+
54+// loadHTPasswdFile loads htpasswd entries from an io.Reader (an opened file) into a htpasswdMap.
55+func (h *htpasswdMap) loadHTPasswdFile(filename string) error {
3356 // We allow HTPasswd location via config options
34− r, err := os.Open(path) // #nosec G304
57+ r, err := os.Open(filename) // #nosec G304
3558 if err != nil {
36− return nil, fmt.Errorf("could not open htpasswd file: %v", err)
59+ return fmt.Errorf("could not open htpasswd file: %v", err)
3760 }
3861 defer func(c io.Closer) {
3962 cerr := c.Close()
@@ -41,48 +64,81 @@ func NewHTPasswdValidator(path string) (Validator, error) {
4164 logger.Fatalf("error closing the htpasswd file: %v", cerr)
4265 }
4366 }(r)
44− return newHtpasswd(r)
45−}
4667
47−// newHtpasswd consctructs an htpasswd from an io.Reader (an opened file).
48−func newHtpasswd(file io.Reader) (*htpasswdMap, error) {
49− csvReader := csv.NewReader(file)
68+ csvReader := csv.NewReader(r)
5069 csvReader.Comma = ':'
5170 csvReader.Comment = '#'
5271 csvReader.TrimLeadingSpace = true
5372
5473 records, err := csvReader.ReadAll()
5574 if err != nil {
56− return nil, fmt.Errorf("could not read htpasswd file: %v", err)
75+ return fmt.Errorf("could not read htpasswd file: %v", err)
5776 }
5877
59− return createHtpasswdMap(records)
78+ updated, err := createHtpasswdMap(records)
79+ if err != nil {
80+ return fmt.Errorf("htpasswd entries error: %v", err)
81+ }
82+
83+ h.rwm.Lock()
84+ h.users = updated.users
85+ h.rwm.Unlock()
86+
87+ return nil
6088}
6189
6290// createHtasswdMap constructs an htpasswdMap from the given records
6391func createHtpasswdMap(records [][]string) (*htpasswdMap, error) {
6492 h := &htpasswdMap{users: make(map[string]interface{})}
93+ var invalidRecords, invalidEntries []string
6594 for _, record := range records {
66− user, realPassword := record[0], record[1]
67− shaPrefix := realPassword[:5]
68− if shaPrefix == "{SHA}" {
69− h.users[user] = sha1Pass(realPassword[5:])
70− continue
95+ // If a record is invalid or malformed don't panic with index out of range,
96+ // return a formatted error.
97+ lr := len(record)
98+ switch {
99+ case lr == 2:
100+ user, realPassword := record[0], record[1]
101+ invalidEntries = passShaOrBcrypt(h, user, realPassword)
102+ case lr == 1, lr > 2:
103+ invalidRecords = append(invalidRecords, record[0])
71104 }
105+ }
72106
73− bcryptPrefix := realPassword[:4]
74− if bcryptPrefix == "$2a$" || bcryptPrefix == "$2b$" || bcryptPrefix == "$2x$" || bcryptPrefix == "$2y$" {
75− h.users[user] = bcryptPass(realPassword)
76− continue
77− }
107+ if len(invalidRecords) > 0 {
108+ return h, fmt.Errorf("invalid htpasswd record(s) %+q", invalidRecords)
109+ }
110+
111+ if len(invalidEntries) > 0 {
112+ return h, fmt.Errorf("'%+q' user(s) could not be added: invalid password, must be a SHA or bcrypt entry", invalidEntries)
113+ }
78114
79− // Password is neither sha1 or bcrypt
80− // TODO(JoelSpeed): In the next breaking release, make this return an error.
81− logger.Errorf("Invalid htpasswd entry for %s. Must be a SHA or bcrypt entry.", user)
115+ if len(h.users) == 0 {
116+ return nil, fmt.Errorf("could not construct htpasswdMap: htpasswd file doesn't contain a single valid user entry")
82117 }
118+
83119 return h, nil
84120}
85121
122+// passShaOrBcrypt checks if a htpasswd entry is valid and the password is encrypted with SHA or bcrypt.
123+// Valid user entries are saved in the htpasswdMap, invalid records are reurned.
124+func passShaOrBcrypt(h *htpasswdMap, user, password string) (invalidEntries []string) {
125+ passLen := len(password)
126+ switch {
127+ case passLen > 6 && password[:5] == "{SHA}":
128+ h.users[user] = sha1Pass(password[5:])
129+ case passLen > 5 &&
130+ (password[:4] == "$2b$" ||
131+ password[:4] == "$2y$" ||
132+ password[:4] == "$2x$" ||
133+ password[:4] == "$2a$"):
134+ h.users[user] = bcryptPass(password)
135+ default:
136+ invalidEntries = append(invalidEntries, user)
137+ }
138+
139+ return invalidEntries
140+}
141+
86142// Validate checks a users password against the htpasswd entries
87143func (h *htpasswdMap) Validate(user string, password string) bool {
88144 realPassword, exists := h.users[user]
pkg/authentication/basic/htpasswd_test.go+80−1
@@ -1,8 +1,11 @@
11package basic
22
33import (
4+ "os"
5+
46 . "github.com/onsi/ginkgo"
57 . "github.com/onsi/gomega"
8+ . "github.com/onsi/gomega/types"
69)
710
811const (
@@ -84,13 +87,89 @@ var _ = Describe("HTPasswd Suite", func() {
8487 })
8588
8689 It("returns an error", func() {
87− Expect(err).To(MatchError("could not open htpasswd file: open ./test/htpasswd-doesnt-exist.txt: no such file or directory"))
90+ Expect(err).To(MatchError("could not load htpasswd file: could not open htpasswd file: open ./test/htpasswd-doesnt-exist.txt: no such file or directory"))
8891 })
8992
9093 It("returns a nil validator", func() {
9194 Expect(validator).To(BeNil())
9295 })
9396 })
97+
98+ Context("htpasswd file is updated", func() {
99+ const filePathPrefix = "htpasswd-file-updated-"
100+ const adminUserHtpasswdEntry = "admin:$2y$05$SXWrNM7ldtbRzBvUC3VXyOvUeiUcP45XPwM93P5eeGOEPIiAZmJjC"
101+ const user1HtpasswdEntry = "user1:$2y$05$/sZYJOk8.3Etg4V6fV7puuXfCJLmV5Q7u3xvKpjBSJUka.t2YtmmG"
102+ var fileNames []string
103+
104+ AfterSuite(func() {
105+ for _, v := range fileNames {
106+ err := os.Remove(v)
107+ Expect(err).ToNot(HaveOccurred())
108+ }
109+
110+ })
111+
112+ type htpasswdUpdate struct {
113+ testText string
114+ remove bool
115+ expectedLen int
116+ expectedGomegaMatcher GomegaMatcher
117+ }
118+
119+ assertHtpasswdMapUpdate := func(hu htpasswdUpdate) {
120+ var validator Validator
121+ var file *os.File
122+ var err error
123+
124+ // Create a temporary file with at least one entry
125+ file, err = os.CreateTemp("", filePathPrefix)
126+ Expect(err).ToNot(HaveOccurred())
127+ _, err = file.WriteString(adminUserHtpasswdEntry + "\n")
128+ Expect(err).ToNot(HaveOccurred())
129+
130+ validator, err = NewHTPasswdValidator(file.Name())
131+ Expect(err).ToNot(HaveOccurred())
132+
133+ htpasswd, ok := validator.(*htpasswdMap)
134+ Expect(ok).To(BeTrue())
135+
136+ if hu.remove {
137+ // Overwrite the original file with another entry
138+ err = os.WriteFile(file.Name(), []byte(user1HtpasswdEntry+"\n"), 0644)
139+ Expect(err).ToNot(HaveOccurred())
140+ } else {
141+ // Add another entry to the original file in append mode
142+ _, err = file.WriteString(user1HtpasswdEntry + "\n")
143+ Expect(err).ToNot(HaveOccurred())
144+ }
145+
146+ err = file.Close()
147+ Expect(err).ToNot(HaveOccurred())
148+
149+ fileNames = append(fileNames, file.Name())
150+
151+ It("has the correct number of users", func() {
152+ Expect(len(htpasswd.users)).To(Equal(hu.expectedLen))
153+ })
154+
155+ It(hu.testText, func() {
156+ Expect(htpasswd.Validate(adminUser, adminPassword)).To(hu.expectedGomegaMatcher)
157+ })
158+
159+ It("new entry is present", func() {
160+ Expect(htpasswd.Validate(user1, user1Password)).To(BeTrue())
161+ })
162+ }
163+
164+ Context("htpasswd entry is added", func() {
165+ assertHtpasswdMapUpdate(htpasswdUpdate{"initial entry is present", false, 2, BeTrue()})
166+ })
167+
168+ Context("htpasswd entry is removed", func() {
169+ assertHtpasswdMapUpdate(htpasswdUpdate{"initial entry is removed", true, 1, BeFalse()})
170+ })
171+
172+ })
94173 })
95174 })
96175})
pkg/watcher/watcher.gonew file+81
@@ -0,0 +1,81 @@
1+package watcher
2+
3+import (
4+ "fmt"
5+ "os"
6+ "path/filepath"
7+ "time"
8+
9+ "github.com/fsnotify/fsnotify"
10+
11+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
12+)
13+
14+// WatchFileForUpdates performs an action every time a file on disk is updated
15+func WatchFileForUpdates(filename string, done <-chan bool, action func()) error {
16+ filename = filepath.Clean(filename)
17+ watcher, err := fsnotify.NewWatcher()
18+ if err != nil {
19+ return fmt.Errorf("failed to create watcher for '%s': %s", filename, err)
20+ }
21+
22+ go func() {
23+ defer watcher.Close()
24+
25+ for {
26+ select {
27+ case <-done:
28+ logger.Printf("shutting down watcher for: %s", filename)
29+ return
30+ case event := <-watcher.Events:
31+ filterEvent(watcher, event, filename, action)
32+ case err = <-watcher.Errors:
33+ logger.Errorf("error watching '%s': %s", filename, err)
34+ }
35+ }
36+ }()
37+ if err := watcher.Add(filename); err != nil {
38+ return fmt.Errorf("failed to add '%s' to watcher: %v", filename, err)
39+ }
40+ logger.Printf("watching '%s' for updates", filename)
41+
42+ return nil
43+}
44+
45+// Filter file operations based on the events sent by the watcher.
46+// Execute the action() function when the following conditions are met:
47+// - the real path of the file was changed (Kubernetes ConfigMap/Secret)
48+// - the file is modified or created
49+func filterEvent(watcher *fsnotify.Watcher, event fsnotify.Event, filename string, action func()) {
50+ switch filepath.Clean(event.Name) == filename {
51+ // In Kubernetes the file path is a symlink, so we should take action
52+ // when the ConfigMap/Secret is replaced.
53+ case event.Op&fsnotify.Remove != 0:
54+ logger.Printf("watching interrupted on event: %s", event)
55+ WaitForReplacement(filename, event.Op, watcher)
56+ action()
57+ case event.Op&(fsnotify.Create|fsnotify.Write) != 0:
58+ logger.Printf("reloading after event: %s", event)
59+ action()
60+ }
61+}
62+
63+// WaitForReplacement waits for a file to exist on disk and then starts a watch
64+// for the file
65+func WaitForReplacement(filename string, op fsnotify.Op, watcher *fsnotify.Watcher) {
66+ const sleepInterval = 50 * time.Millisecond
67+
68+ // Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
69+ if op&fsnotify.Chmod != 0 {
70+ time.Sleep(sleepInterval)
71+ }
72+ for {
73+ if _, err := os.Stat(filename); err == nil {
74+ if err := watcher.Add(filename); err == nil {
75+ logger.Printf("watching resumed for '%s'", filename)
76+ return
77+ }
78+ }
79+ time.Sleep(sleepInterval)
80+ }
81+}
validator.go+2−1
@@ -9,6 +9,7 @@ import (
99 "unsafe"
1010
1111 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
12+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/watcher"
1213)
1314
1415// UserMap holds information from the authenticated emails file
@@ -26,7 +27,7 @@ func NewUserMap(usersFile string, done <-chan bool, onUpdate func()) *UserMap {
2627 atomic.StorePointer(&um.m, unsafe.Pointer(&m)) // #nosec G103
2728 if usersFile != "" {
2829 logger.Printf("using authenticated emails file %s", usersFile)
29− WatchForUpdates(usersFile, done, func() {
30+ watcher.WatchFileForUpdates(usersFile, done, func() {
3031 um.LoadAuthenticatedEmailsFile()
3132 onUpdate()
3233 })
watcher.godeleted−81
@@ -1,81 +0,0 @@
1−//go:build go1.3 && !plan9 && !solaris
2−// +build go1.3,!plan9,!solaris
3−
4−package main
5−
6−import (
7− "os"
8− "path/filepath"
9− "time"
10−
11− "github.com/fsnotify/fsnotify"
12−
13− "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14−)
15−
16−// WaitForReplacement waits for a file to exist on disk and then starts a watch
17−// for the file
18−func WaitForReplacement(filename string, op fsnotify.Op,
19− watcher *fsnotify.Watcher) {
20− const sleepInterval = 50 * time.Millisecond
21−
22− // Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
23− if op&fsnotify.Chmod != 0 {
24− time.Sleep(sleepInterval)
25− }
26− for {
27− if _, err := os.Stat(filename); err == nil {
28− if err := watcher.Add(filename); err == nil {
29− logger.Printf("watching resumed for %s", filename)
30− return
31− }
32− }
33− time.Sleep(sleepInterval)
34− }
35−}
36−
37−// WatchForUpdates performs an action every time a file on disk is updated
38−func WatchForUpdates(filename string, done <-chan bool, action func()) {
39− filename = filepath.Clean(filename)
40− watcher, err := fsnotify.NewWatcher()
41− if err != nil {
42− logger.Fatal("failed to create watcher for ", filename, ": ", err)
43− }
44− go func() {
45− defer func(w *fsnotify.Watcher) {
46− cerr := w.Close()
47− if cerr != nil {
48− logger.Fatalf("error closing watcher: %v", err)
49− }
50− }(watcher)
51− for {
52− select {
53− case <-done:
54− logger.Printf("Shutting down watcher for: %s", filename)
55− return
56− case event := <-watcher.Events:
57− // On Arch Linux, it appears Chmod events precede Remove events,
58− // which causes a race between action() and the coming Remove event.
59− // If the Remove wins, the action() (which calls
60− // UserMap.LoadAuthenticatedEmailsFile()) crashes when the file
61− // can't be opened.
62− if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
63− logger.Printf("watching interrupted on event: %s", event)
64− err = watcher.Remove(filename)
65− if err != nil {
66− logger.Printf("error removing watcher on %s: %v", filename, err)
67− }
68− WaitForReplacement(filename, event.Op, watcher)
69− }
70− logger.Printf("reloading after event: %s", event)
71− action()
72− case err = <-watcher.Errors:
73− logger.Errorf("error watching %s: %s", filename, err)
74− }
75− }
76− }()
77− if err = watcher.Add(filename); err != nil {
78− logger.Fatal("failed to add ", filename, " to watcher: ", err)
79− }
80− logger.Printf("watching %s for updates", filename)
81−}
watcher_unsupported.godeleted−11
@@ -1,11 +0,0 @@
1−//go:build !go1.3 || plan9 || solaris
2−// +build !go1.3 plan9 solaris
3−
4−package main
5−
6−import "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
7−
8−func WatchForUpdates(filename string, done <-chan bool, action func()) {
9− logger.Errorf("file watching not implemented on this platform")
10− go func() { <-done }()
11−}