This repository has been archived by the owner on May 18, 2018. It is now read-only.
/
session.go
196 lines (163 loc) · 4.96 KB
/
session.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
package auth
import (
"context"
"net/http"
"os"
"github.com/joyent/triton-service-groups/accounts"
"github.com/joyent/triton-service-groups/keys"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// authSession a private struct which is only accessible by pulling out of the
// current request `context.Context`.
type Session struct {
*ParsedRequest
AccountID string
Fingerprint string
Datacenter string
TritonURL string
devMode bool
config Config
}
// NewSession constructs and returns a new Session by parsing the HTTP request,
// validating and pulling out authentication headers.
func NewSession(req *http.Request, cfg Config) (*Session, error) {
if devMode := os.Getenv("TSG_DEV_MODE"); devMode != "" {
log.Debug().
Str("account_id", testAccountID).
Str("fingerprint", testFingerprint).
Msg("auth: skipping authentication and using seeded defaults")
return &Session{
AccountID: testAccountID,
Fingerprint: testFingerprint,
Datacenter: cfg.Datacenter,
TritonURL: cfg.TritonURL,
devMode: true,
}, nil
}
parsedReq, err := ParseRequest(req)
if err != nil {
return &Session{}, errors.Wrap(err, "failed to parse auth request")
}
return &Session{
ParsedRequest: parsedReq,
Datacenter: cfg.Datacenter,
TritonURL: cfg.TritonURL,
config: cfg,
}, nil
}
func (s *Session) IsDevMode() bool {
return s.devMode
}
// IsAuthenticated represents whatever it means for an authSession to be deemed
// authenticated.
func (s *Session) IsAuthenticated() bool {
return s.AccountID != "" && s.Fingerprint != ""
}
// EnsureAccount ensures that a Triton account is authentic and an account has
// been created for it within the TSG database. Returns the TSG account that was
// either created or found.
func (s *Session) EnsureAccount(ctx context.Context, store *accounts.Store) (*accounts.Account, error) {
check := NewAccountCheck(s.ParsedRequest, store, s.config)
if err := check.OnTriton(ctx); err != nil {
err = errors.Wrap(err, "failed to check triton for account")
log.Error().Err(err)
return nil, err
}
if !check.HasTritonAccount() {
err := errors.New("could not authenticate account with triton")
log.Error().Err(err)
return nil, err
}
if err := check.SaveAccount(ctx); err != nil {
err := errors.Wrap(err, "failed to save account in database")
log.Error().Err(err)
return nil, err
}
s.AccountID = check.Account.ID
log.Debug().
Str("account_id", s.AccountID).
Str("account_name", check.Account.AccountName).
Msg("auth: session account has been authenticated")
return check.Account, nil
}
// EnsureKey checks Triton for an active TSG account key. If one cannot be found
// than a new key is created and stored it into the TSG database.
func (s *Session) EnsureKeys(ctx context.Context, acct *accounts.Account, store *keys.Store) error {
check := NewKeyCheck(s.ParsedRequest, acct, store, s.config)
if err := check.OnTriton(ctx); err != nil {
err = errors.Wrap(err, "failed to check triton for key")
log.Error().Err(err)
return err
}
if err := check.InDatabase(ctx); err != nil {
err = errors.Wrap(err, "failed to check database for key")
log.Error().Err(err)
return err
}
if check.HasKey() {
if check.HasTritonKey() {
if check.Key.Fingerprint == check.TritonKey.Fingerprint {
log.Debug().
Str("account_name", acct.AccountName).
Str("fingerprint", check.Key.Fingerprint).
Msg("auth: found existing keys with matching fingerprint")
s.Fingerprint = check.Key.Fingerprint
return nil
}
log.Error().
Str("account_name", acct.AccountName).
Err(ErrKeyConflict)
return ErrKeyConflict
} else {
keypair, err := DecodeKeyPair(check.Key.Material)
if err != nil {
err = errors.Wrap(err, "failed to generate new keypair")
log.Error().Err(err)
return err
}
err = check.AddTritonKey(ctx, keypair)
if err != nil {
err = errors.Wrap(err, "failed to add new key")
log.Error().Err(err)
return err
}
}
} else {
if check.HasTritonKey() {
err := errors.New("auth: found key in triton but not in tsg")
log.Error().
Str("account_name", acct.AccountName).
Str("fingerprint", check.TritonKey.Fingerprint).
Err(err)
return err
}
keypair, err := NewKeyPair(1024)
if err != nil {
err = errors.Wrap(err, "failed to generate new keypair")
log.Error().Err(err)
return err
}
if !check.HasTritonKey() {
if err := check.AddTritonKey(ctx, keypair); err != nil {
err = errors.Wrap(err, "failed to add new key")
log.Error().Err(err)
return err
}
}
if err := check.InsertKey(ctx, keypair); err != nil {
err = errors.Wrap(err, "failed to save new key")
log.Error().Err(err)
return err
}
}
if check.Key.Fingerprint == check.TritonKey.Fingerprint {
log.Debug().
Str("account_name", acct.AccountName).
Str("fingerprint", check.Key.Fingerprint).
Msg("auth: detected matching fingerprints for service keys")
s.Fingerprint = check.Key.Fingerprint
return nil
}
return nil
}