Skip to content

Commit 00b02e3

Browse files
committed
Reactor auth.File and instantiate authenticators in main.go
1 parent d0c9dc3 commit 00b02e3

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

auth/file.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212

1313
// Structs
1414

15-
// FileAuthenticator contains file based authentication
15+
// File contains file based authentication
1616
// information including the in-memory map of username to
1717
// password mapping.
18-
type FileAuthenticator struct {
18+
type File struct {
1919
Users []User
2020
}
2121

@@ -28,20 +28,20 @@ type User struct {
2828

2929
// Functions
3030

31-
// NewFileAuthenticator takes in a file name and a separator,
31+
// NewFile takes in a file name and a separator,
3232
// reads in specified file and parses it line by line as
3333
// username - password elements separated by the separator.
3434
// At the end, the returned struct contains the information
3535
// and an in-memory map of username mapped to password.
36-
func NewFileAuthenticator(file string, sep string) (*FileAuthenticator, error) {
36+
func NewFile(file string, sep string) (*File, error) {
3737

3838
// Reserve space for the ordered users list in memory.
3939
users := make([]User, 0, 50)
4040

4141
// Open file with authentication information.
4242
handle, err := os.Open(file)
4343
if err != nil {
44-
return nil, fmt.Errorf("[auth.NewFileAuthenticator] Could not open supplied authentication file: %v", err)
44+
return nil, fmt.Errorf("[auth.NewFile] Could not open supplied authentication file: %v", err)
4545
}
4646
defer handle.Close()
4747

@@ -71,22 +71,22 @@ func NewFileAuthenticator(file string, sep string) (*FileAuthenticator, error) {
7171

7272
// If the scanner ended with an error, report it.
7373
if err := scanner.Err(); err != nil {
74-
return nil, fmt.Errorf("[auth.NewFileAuthenticator] Experienced error while scanning authentication file: %v", err)
74+
return nil, fmt.Errorf("[auth.NewFile] Experienced error while scanning authentication file: %v", err)
7575
}
7676

7777
// Sort users list to search it efficiently later on.
7878
sort.Slice(users, func(i, j int) bool {
7979
return users[i].Name < users[j].Name
8080
})
8181

82-
return &FileAuthenticator{
82+
return &File{
8383
Users: users,
8484
}, nil
8585
}
8686

8787
// GetWorkerForUser returns the name of the worker node
8888
// that is responsible for handling the user's mailbox.
89-
func (f *FileAuthenticator) GetWorkerForUser(workers map[string]config.Worker, id int) (string, error) {
89+
func (f *File) GetWorkerForUser(workers map[string]config.Worker, id int) (string, error) {
9090

9191
for name, worker := range workers {
9292

@@ -105,7 +105,7 @@ func (f *FileAuthenticator) GetWorkerForUser(workers map[string]config.Worker, i
105105
// process by taking supplied credentials and attempting
106106
// to find a matching entry the in-memory list taken from
107107
// the authentication file.
108-
func (f *FileAuthenticator) AuthenticatePlain(username string, password string, clientAddr string) (int, string, error) {
108+
func (f *File) AuthenticatePlain(username string, password string, clientAddr string) (int, string, error) {
109109

110110
// Search in user list for user matching supplied name.
111111
i := sort.Search(len(f.Users), func(i int) bool {

imap/distributor.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"crypto/tls"
1111

12-
"github.com/numbleroot/pluto/auth"
1312
"github.com/numbleroot/pluto/config"
1413
"github.com/numbleroot/pluto/crypto"
1514
)
@@ -50,34 +49,18 @@ type Distributor struct {
5049
// opened up on supplied IP address and port as well as initializes
5150
// connections to involved worker nodes. It returns those
5251
// information bundeled in above Distributor struct.
53-
func InitDistributor(config *config.Config) (*Distributor, error) {
52+
func InitDistributor(config *config.Config, auth PlainAuthenticator) (*Distributor, error) {
5453

5554
var err error
5655

5756
// Initialize and set fields.
5857
distr := &Distributor{
5958
lock: new(sync.RWMutex),
59+
AuthAdapter: auth,
6060
Connections: make(map[string]*tls.Conn),
6161
Config: config,
6262
}
6363

64-
// As the distributor is responsible for the authentication
65-
// of incoming requests, connect to provided auth mechanism.
66-
if config.Distributor.AuthAdapter == "AuthFile" {
67-
68-
// Open authentication file and read user information.
69-
distr.AuthAdapter, err = auth.NewFileAuthenticator(config.Distributor.AuthFile.File, config.Distributor.AuthFile.Separator)
70-
71-
} else if config.Distributor.AuthAdapter == "AuthPostgres" {
72-
73-
// Connect to PostgreSQL database.
74-
distr.AuthAdapter, err = auth.NewPostgresAuthenticator(config.Distributor.AuthPostgres.IP, config.Distributor.AuthPostgres.Port, config.Distributor.AuthPostgres.Database, config.Distributor.AuthPostgres.User, config.Distributor.AuthPostgres.Password, config.Distributor.AuthPostgres.UseTLS)
75-
76-
}
77-
if err != nil {
78-
return nil, err
79-
}
80-
8164
// Load internal TLS config.
8265
distr.IntlTLSConfig, err = crypto.NewInternalTLSConfig(config.Distributor.InternalTLS.CertLoc, config.Distributor.InternalTLS.KeyLoc, config.RootCertLoc)
8366
if err != nil {

main.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,37 @@ import (
66
"os"
77
"runtime"
88

9+
"github.com/numbleroot/pluto/auth"
910
"github.com/numbleroot/pluto/config"
1011
"github.com/numbleroot/pluto/imap"
1112
)
1213

1314
// Functions
1415

16+
// initAuthenticator of the correct implementation specified in the config
17+
// to be used in the imap.Distributor.
18+
func initAuthenticator(config *config.Config) (imap.PlainAuthenticator, error) {
19+
20+
switch config.Distributor.AuthAdapter {
21+
case "AuthPostgres":
22+
// Connect to PostgreSQL database.
23+
return auth.NewPostgresAuthenticator(
24+
config.Distributor.AuthPostgres.IP,
25+
config.Distributor.AuthPostgres.Port,
26+
config.Distributor.AuthPostgres.Database,
27+
config.Distributor.AuthPostgres.User,
28+
config.Distributor.AuthPostgres.Password,
29+
config.Distributor.AuthPostgres.UseTLS,
30+
)
31+
default: // AuthFile
32+
// Open authentication file and read user information.
33+
return auth.NewFile(
34+
config.Distributor.AuthFile.File,
35+
config.Distributor.AuthFile.Separator,
36+
)
37+
}
38+
}
39+
1540
func main() {
1641

1742
var err error
@@ -37,8 +62,13 @@ func main() {
3762
// system based on passed command line flag.
3863
if *distributorFlag {
3964

65+
authenticator, err := initAuthenticator(conf)
66+
if err != nil {
67+
log.Fatal(err)
68+
}
69+
4070
// Initialize distributor.
41-
distr, err := imap.InitDistributor(conf)
71+
distr, err := imap.InitDistributor(conf, authenticator)
4272
if err != nil {
4373
log.Fatal(err)
4474
}

0 commit comments

Comments
 (0)