git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #93 from 18F/watcher-done
aca1fe8Jehiah Czebotar
validator.go+5−5
@@ -15,13 +15,13 @@ type UserMap struct {
15 m unsafe.Pointer
15 m unsafe.Pointer
16}
16}
17
17
18−func NewUserMap(usersFile string, onUpdate func()) *UserMap {
18+func NewUserMap(usersFile string, done <-chan bool, onUpdate func()) *UserMap {
19 um := &UserMap{usersFile: usersFile}
19 um := &UserMap{usersFile: usersFile}
20 m := make(map[string]bool)
20 m := make(map[string]bool)
21 atomic.StorePointer(&um.m, unsafe.Pointer(&m))
21 atomic.StorePointer(&um.m, unsafe.Pointer(&m))
22 if usersFile != "" {
22 if usersFile != "" {
23 log.Printf("using authenticated emails file %s", usersFile)
23 log.Printf("using authenticated emails file %s", usersFile)
24− started := WatchForUpdates(usersFile, func() {
24+ started := WatchForUpdates(usersFile, done, func() {
25 um.LoadAuthenticatedEmailsFile()
25 um.LoadAuthenticatedEmailsFile()
26 onUpdate()
26 onUpdate()
27 })
27 })
@@ -62,8 +62,8 @@ func (um *UserMap) LoadAuthenticatedEmailsFile() {
62}
62}
63
63
64func newValidatorImpl(domains []string, usersFile string,
64func newValidatorImpl(domains []string, usersFile string,
65− onUpdate func()) func(string) bool {
65+ done <-chan bool, onUpdate func()) func(string) bool {
66− validUsers := NewUserMap(usersFile, onUpdate)
66+ validUsers := NewUserMap(usersFile, done, onUpdate)
67
67
68 for i, domain := range domains {
68 for i, domain := range domains {
69 domains[i] = fmt.Sprintf("@%s", strings.ToLower(domain))
69 domains[i] = fmt.Sprintf("@%s", strings.ToLower(domain))
@@ -85,5 +85,5 @@ func newValidatorImpl(domains []string, usersFile string,
85}
85}
86
86
87func NewValidator(domains []string, usersFile string) func(string) bool {
87func NewValidator(domains []string, usersFile string) func(string) bool {
88− return newValidatorImpl(domains, usersFile, func() {})
88+ return newValidatorImpl(domains, usersFile, nil, func() {})
89}
89}
validator_test.go+20−5
@@ -9,6 +9,8 @@ import (
9
9
10type ValidatorTest struct {
10type ValidatorTest struct {
11 auth_email_file *os.File
11 auth_email_file *os.File
12+ done chan bool
13+ update_seen bool
12}
14}
13
15
14func NewValidatorTest(t *testing.T) *ValidatorTest {
16func NewValidatorTest(t *testing.T) *ValidatorTest {
@@ -18,13 +20,26 @@ func NewValidatorTest(t *testing.T) *ValidatorTest {
18 if err != nil {
20 if err != nil {
19 t.Fatal("failed to create temp file: " + err.Error())
21 t.Fatal("failed to create temp file: " + err.Error())
20 }
22 }
23+ vt.done = make(chan bool)
21 return vt
24 return vt
22}
25}
23
26
24func (vt *ValidatorTest) TearDown() {
27func (vt *ValidatorTest) TearDown() {
28+ vt.done <- true
25 os.Remove(vt.auth_email_file.Name())
29 os.Remove(vt.auth_email_file.Name())
26}
30}
27
31
32+func (vt *ValidatorTest) NewValidator(domains []string,
33+ updated chan<- bool) func(string) bool {
34+ return newValidatorImpl(domains, vt.auth_email_file.Name(),
35+ vt.done, func() {
36+ if vt.update_seen == false {
37+ updated <- true
38+ vt.update_seen = true
39+ }
40+ })
41+}
42+
28// This will close vt.auth_email_file.
43// This will close vt.auth_email_file.
29func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
44func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
30 defer vt.auth_email_file.Close()
45 defer vt.auth_email_file.Close()
@@ -41,7 +56,7 @@ func TestValidatorEmpty(t *testing.T) {
41
56
42 vt.WriteEmails(t, []string(nil))
57 vt.WriteEmails(t, []string(nil))
43 domains := []string(nil)
58 domains := []string(nil)
44− validator := NewValidator(domains, vt.auth_email_file.Name())
59+ validator := vt.NewValidator(domains, nil)
45
60
46 if validator("foo.bar@example.com") {
61 if validator("foo.bar@example.com") {
47 t.Error("nothing should validate when the email and " +
62 t.Error("nothing should validate when the email and " +
@@ -55,7 +70,7 @@ func TestValidatorSingleEmail(t *testing.T) {
55
70
56 vt.WriteEmails(t, []string{"foo.bar@example.com"})
71 vt.WriteEmails(t, []string{"foo.bar@example.com"})
57 domains := []string(nil)
72 domains := []string(nil)
58− validator := NewValidator(domains, vt.auth_email_file.Name())
73+ validator := vt.NewValidator(domains, nil)
59
74
60 if !validator("foo.bar@example.com") {
75 if !validator("foo.bar@example.com") {
61 t.Error("email should validate")
76 t.Error("email should validate")
@@ -72,7 +87,7 @@ func TestValidatorSingleDomain(t *testing.T) {
72
87
73 vt.WriteEmails(t, []string(nil))
88 vt.WriteEmails(t, []string(nil))
74 domains := []string{"example.com"}
89 domains := []string{"example.com"}
75− validator := NewValidator(domains, vt.auth_email_file.Name())
90+ validator := vt.NewValidator(domains, nil)
76
91
77 if !validator("foo.bar@example.com") {
92 if !validator("foo.bar@example.com") {
78 t.Error("email should validate")
93 t.Error("email should validate")
@@ -91,7 +106,7 @@ func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
91 "plugh@example.com",
106 "plugh@example.com",
92 })
107 })
93 domains := []string{"example0.com", "example1.com"}
108 domains := []string{"example0.com", "example1.com"}
94− validator := NewValidator(domains, vt.auth_email_file.Name())
109+ validator := vt.NewValidator(domains, nil)
95
110
96 if !validator("foo.bar@example0.com") {
111 if !validator("foo.bar@example0.com") {
97 t.Error("email from first domain should validate")
112 t.Error("email from first domain should validate")
@@ -117,7 +132,7 @@ func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
117
132
118 vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
133 vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
119 domains := []string{"Frobozz.Com"}
134 domains := []string{"Frobozz.Com"}
120− validator := NewValidator(domains, vt.auth_email_file.Name())
135+ validator := vt.NewValidator(domains, nil)
121
136
122 if !validator("foo.bar@example.com") {
137 if !validator("foo.bar@example.com") {
123 t.Error("loaded email addresses are not lower-cased")
138 t.Error("loaded email addresses are not lower-cased")
validator_watcher_copy_test.go+1−2
@@ -34,8 +34,7 @@ func TestValidatorOverwriteEmailListViaCopyingOver(t *testing.T) {
34 vt.WriteEmails(t, []string{"xyzzy@example.com"})
34 vt.WriteEmails(t, []string{"xyzzy@example.com"})
35 domains := []string(nil)
35 domains := []string(nil)
36 updated := make(chan bool)
36 updated := make(chan bool)
37− validator := newValidatorImpl(domains, vt.auth_email_file.Name(),
37+ validator := vt.NewValidator(domains, updated)
38− func() { updated <- true })
39
38
40 if !validator("xyzzy@example.com") {
39 if !validator("xyzzy@example.com") {
41 t.Error("email in list should validate")
40 t.Error("email in list should validate")
validator_watcher_test.go+2−4
@@ -51,8 +51,7 @@ func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
51 })
51 })
52 domains := []string(nil)
52 domains := []string(nil)
53 updated := make(chan bool)
53 updated := make(chan bool)
54− validator := newValidatorImpl(domains, vt.auth_email_file.Name(),
54+ validator := vt.NewValidator(domains, updated)
55− func() { updated <- true })
56
55
57 if !validator("xyzzy@example.com") {
56 if !validator("xyzzy@example.com") {
58 t.Error("first email in list should validate")
57 t.Error("first email in list should validate")
@@ -89,8 +88,7 @@ func TestValidatorOverwriteEmailListViaRenameAndReplace(t *testing.T) {
89 vt.WriteEmails(t, []string{"xyzzy@example.com"})
88 vt.WriteEmails(t, []string{"xyzzy@example.com"})
90 domains := []string(nil)
89 domains := []string(nil)
91 updated := make(chan bool)
90 updated := make(chan bool)
92− validator := newValidatorImpl(domains, vt.auth_email_file.Name(),
91+ validator := vt.NewValidator(domains, updated)
93− func() { updated <- true })
94
92
95 if !validator("xyzzy@example.com") {
93 if !validator("xyzzy@example.com") {
96 t.Error("email in list should validate")
94 t.Error("email in list should validate")
watcher.go+13−7
@@ -12,17 +12,18 @@ import (
12 "gopkg.in/fsnotify.v1"
12 "gopkg.in/fsnotify.v1"
13)
13)
14
14
15−func WaitForReplacement(event fsnotify.Event, watcher *fsnotify.Watcher) {
15+func WaitForReplacement(filename string, op fsnotify.Op,
16+ watcher *fsnotify.Watcher) {
16 const sleep_interval = 50 * time.Millisecond
17 const sleep_interval = 50 * time.Millisecond
17
18
18 // Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
19 // Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
19− if event.Op&fsnotify.Chmod != 0 {
20+ if op&fsnotify.Chmod != 0 {
20 time.Sleep(sleep_interval)
21 time.Sleep(sleep_interval)
21 }
22 }
22 for {
23 for {
23− if _, err := os.Stat(event.Name); err == nil {
24+ if _, err := os.Stat(filename); err == nil {
24− if err := watcher.Add(event.Name); err == nil {
25+ if err := watcher.Add(filename); err == nil {
25− log.Printf("watching resumed for %s", event.Name)
26+ log.Printf("watching resumed for %s", filename)
26 return
27 return
27 }
28 }
28 }
29 }
@@ -30,7 +31,7 @@ func WaitForReplacement(event fsnotify.Event, watcher *fsnotify.Watcher) {
30 }
31 }
31}
32}
32
33
33−func WatchForUpdates(filename string, action func()) bool {
34+func WatchForUpdates(filename string, done <-chan bool, action func()) bool {
34 filename = filepath.Clean(filename)
35 filename = filepath.Clean(filename)
35 watcher, err := fsnotify.NewWatcher()
36 watcher, err := fsnotify.NewWatcher()
36 if err != nil {
37 if err != nil {
@@ -40,6 +41,10 @@ func WatchForUpdates(filename string, action func()) bool {
40 defer watcher.Close()
41 defer watcher.Close()
41 for {
42 for {
42 select {
43 select {
44+ case _ = <-done:
45+ log.Printf("Shutting down watcher for: %s",
46+ filename)
47+ return
43 case event := <-watcher.Events:
48 case event := <-watcher.Events:
44 // On Arch Linux, it appears Chmod events precede Remove events,
49 // On Arch Linux, it appears Chmod events precede Remove events,
45 // which causes a race between action() and the coming Remove event.
50 // which causes a race between action() and the coming Remove event.
@@ -48,7 +53,8 @@ func WatchForUpdates(filename string, action func()) bool {
48 // can't be opened.
53 // can't be opened.
49 if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
54 if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
50 log.Printf("watching interrupted on event: %s", event)
55 log.Printf("watching interrupted on event: %s", event)
56+ watcher.Remove(filename)
51− WaitForReplacement(event, watcher)
57+ WaitForReplacement(filename, event.Op, watcher)
52 }
58 }
53 log.Printf("reloading after event: %s", event)
59 log.Printf("reloading after event: %s", event)
54 action()
60 action()