/
server.go
243 lines (206 loc) · 6.2 KB
/
server.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright © 2021 Yale University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package api
import (
"context"
"encoding/json"
"errors"
"math/rand"
"net/http"
"os"
"time"
"github.com/YaleSpinup/datasync-api/common"
"github.com/YaleSpinup/datasync-api/iam"
"github.com/YaleSpinup/datasync-api/session"
"github.com/YaleSpinup/flywheel"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// apiVersion is the API version
type apiVersion struct {
// The version of the API
Version string `json:"version"`
// The git hash of the API
GitHash string `json:"githash"`
// The build timestamp of the API
BuildStamp string `json:"buildstamp"`
}
type server struct {
router *mux.Router
version *apiVersion
context context.Context
session session.Session
sessionCache *cache.Cache
flywheel *flywheel.Manager
orgPolicy string
org string
}
// NewServer creates a new server and starts it
func NewServer(config common.Config) error {
// setup server context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if config.Org == "" {
return errors.New("'org' cannot be empty in the configuration")
}
s := server{
router: mux.NewRouter(),
context: ctx,
org: config.Org,
sessionCache: cache.New(600*time.Second, 900*time.Second),
}
s.version = &apiVersion{
Version: config.Version.Version,
GitHash: config.Version.GitHash,
BuildStamp: config.Version.BuildStamp,
}
orgPolicy, err := orgTagAccessPolicy(config.Org)
if err != nil {
return err
}
s.orgPolicy = orgPolicy
manager, err := newFlywheelManager(config.Flywheel)
if err != nil {
return err
}
s.flywheel = manager
// Create a new session used for authentication and assuming cross account roles
log.Debugf("Creating new session with key '%s' in region '%s'", config.Account.Akid, config.Account.Region)
s.session = session.New(
session.WithCredentials(config.Account.Akid, config.Account.Secret, ""),
session.WithRegion(config.Account.Region),
session.WithExternalID(config.Account.ExternalID),
session.WithExternalRoleName(config.Account.Role),
)
publicURLs := map[string]string{
"/v1/datasync/ping": "public",
"/v1/datasync/version": "public",
"/v1/datasync/metrics": "public",
}
// load routes
s.routes()
if config.ListenAddress == "" {
config.ListenAddress = ":8080"
}
handler := handlers.RecoveryHandler()(handlers.LoggingHandler(os.Stdout, TokenMiddleware([]byte(config.Token), publicURLs, s.router)))
srv := &http.Server{
Handler: handler,
Addr: config.ListenAddress,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Infof("Starting listener on %s", config.ListenAddress)
if err := srv.ListenAndServe(); err != nil {
return err
}
return nil
}
// LogWriter is an http.ResponseWriter
type LogWriter struct {
http.ResponseWriter
}
// Write log message if http response writer returns an error
func (w LogWriter) Write(p []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(p)
if err != nil {
log.Errorf("Write failed: %v", err)
}
return
}
type rollbackFunc func(ctx context.Context) error
// rollBack executes functions from a stack of rollback functions
func rollBack(t *[]rollbackFunc) {
if t == nil {
return
}
timeout, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
done := make(chan string, 1)
go func() {
tasks := *t
log.Errorf("executing rollback of %d tasks", len(tasks))
for i := len(tasks) - 1; i >= 0; i-- {
f := tasks[i]
if funcerr := f(timeout); funcerr != nil {
log.Errorf("rollback task error: %s, continuing rollback", funcerr)
}
log.Infof("executed rollback task %d of %d", len(tasks)-i, len(tasks))
}
done <- "success"
}()
// wait for a done context
select {
case <-timeout.Done():
log.Error("timeout waiting for successful rollback")
case <-done:
log.Info("successfully rolled back")
}
}
type stop struct {
error
}
// retry was originally borrowed from https://upgear.io/blog/simple-golang-retry-function/
// it will retry the given code _f_ for the specified number of _attempts_
// sleeping after each attempt, starting with _sleep_ time and doubling it
// for the first _doubling_ times, then keeping it constant (+ jitter)
func retry(attempts int, doubling int, sleep time.Duration, f func() error) error {
if err := f(); err != nil {
if s, ok := err.(stop); ok {
// return the original error for later checking
return s.error
}
if attempts--; attempts > 0 {
// add some randomness to prevent creating a Thundering Herd
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
if doubling--; doubling > 0 {
return retry(attempts, doubling, 2*sleep, f)
}
// stop doubling the sleep interval after some point to prevent it from getting too big
return retry(attempts, 0, sleep, f)
}
return err
}
return nil
}
// orgTagAccessPolicy generates the org tag conditional policy to be passed inline when assuming a role
func orgTagAccessPolicy(org string) (string, error) {
log.Debugf("generating org policy document")
policy := iam.PolicyDocument{
Version: "2012-10-17",
Statement: []iam.StatementEntry{
{
Effect: "Allow",
Action: []string{"*"},
Resource: "*",
Condition: iam.Condition{
"StringEquals": iam.ConditionStatement{
"aws:ResourceTag/spinup:org": org,
},
},
},
},
}
j, err := json.Marshal(policy)
if err != nil {
return "", err
}
return string(j), nil
}