This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
/
context.go
148 lines (134 loc) · 3.59 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
package main
import (
"fmt"
"gopkg.in/gcfg.v1"
"io"
"github.com/mozilla/mig"
migdb "github.com/mozilla/mig/database"
"os"
"strconv"
"strings"
"sync"
"time"
)
// Context contains all configuration variables as well as handlers for
// database and logging. It also contains some statistics.
// Context is intended as a single structure that can be passed around easily.
type Context struct {
Authentication struct {
Enabled bool
TokenDuration string
duration time.Duration
}
Channels struct {
Log chan mig.Log
}
DB migdb.DB
Keyring struct {
Reader io.ReadSeeker
Mutex sync.Mutex
UpdateTime time.Time
}
Manifest struct {
RequiredSignatures int
}
Postgres struct {
Host, User, Password, DBName, SSLMode string
Port, MaxConn int
}
Server struct {
IP string
Port int
Host, BaseRoute, BaseURL string
ClientPublicIP string
ClientPublicIPOffset int
}
Logging mig.Logging
}
// Init() initializes a context from a configuration file into an
// existing context struct
func Init(path string, debug bool) (ctx Context, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("Init() -> %v", e)
}
}()
ctx.Channels.Log = make(chan mig.Log, 37)
err = gcfg.ReadFileInto(&ctx, path)
if err != nil {
panic(err)
}
ctx.Server.BaseURL = ctx.Server.Host + ctx.Server.BaseRoute
ctx.Authentication.duration, err = time.ParseDuration(ctx.Authentication.TokenDuration)
if err != nil {
panic(err)
}
// Set the mode we will use to determine a client's public IP address
if ctx.Server.ClientPublicIP == "" {
ctx.Server.ClientPublicIP = "peer"
}
ctx.Server.ClientPublicIPOffset, err = parseClientPublicIP(ctx.Server.ClientPublicIP)
if err != nil {
fmt.Println(err)
panic(err)
}
if debug {
ctx.Logging.Level = "debug"
ctx.Logging.Mode = "stdout"
}
ctx.Logging, err = mig.InitLogger(ctx.Logging, "mig-api")
if err != nil {
panic(err)
}
if ctx.Manifest.RequiredSignatures < 1 {
panic("manifest:requiredsignatures must be at least 1 in config file")
}
ctx, err = initDB(ctx)
if err != nil {
panic(err)
}
return
}
// initDB() sets up the connection to the MongoDB backend database
func initDB(orig_ctx Context) (ctx Context, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("initDB() -> %v", e)
}
ctx.Channels.Log <- mig.Log{Desc: "leaving initDB()"}.Debug()
}()
fmt.Fprintf(os.Stdout, "Attempting to connect to postgresql database...")
ctx = orig_ctx
ctx.DB, err = migdb.Open(ctx.Postgres.DBName, ctx.Postgres.User, ctx.Postgres.Password,
ctx.Postgres.Host, ctx.Postgres.Port, ctx.Postgres.SSLMode)
if err != nil {
panic(err)
}
ctx.DB.SetMaxOpenConns(ctx.Postgres.MaxConn)
ctx.Channels.Log <- mig.Log{Desc: "Database connection opened"}
return
}
func parseClientPublicIP(s string) (ret int, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("parseClientPublicIP() -> %v", e)
}
}()
if s == "peer" {
return -1, nil
}
args := strings.Split(s, ":")
if len(args) != 2 || args[0] != "x-forwarded-for" {
panic("argument must be peer or x-forwarded-for:<int>")
}
ret, err = strconv.Atoi(args[1])
if err != nil || ret < 0 {
panic("x-forwarded-for argument must be positive integer or zero")
}
return ret, nil
}