-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
session_persistence.go
228 lines (204 loc) · 6.22 KB
/
session_persistence.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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package democluster
import (
"bufio"
"context"
gosql "database/sql"
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/cockroachdb/cockroach/pkg/security/certnames"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server/pgurl"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/util/netutil/addr"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/oserror"
"github.com/cockroachdb/redact"
)
// saveWebSessions persists any currently active web session to disk,
// so they can be restored when the demo shell starts again.
func (c *transientCluster) saveWebSessions(ctx context.Context) error {
return c.doPersistence(ctx, "saving", c.saveWebSessionsInternal)
}
// restoreWebSessions restores any currently active web session from disk.
func (c *transientCluster) restoreWebSessions(ctx context.Context) error {
return c.doPersistence(ctx, "restoring", c.restoreWebSessionsInternal)
}
func (c *transientCluster) doPersistence(
ctx context.Context,
word redact.SafeString,
fn func(ctx context.Context, filename string, db *gosql.DB) error,
) error {
if c.demoDir == "" {
// No directory to save to. Bail.
return nil
}
// Lock the output dir to prevent concurrent demo sessions
// from stamping each other over.
cleanup, err := c.lockDir(ctx, c.demoDir, "sessions")
if err != nil {
return err
}
defer cleanup()
// We compose the connection URL anew because
// getNetworkURLForServer() uses the 'demo' account and we want a
// root connection, using client certs.
//
// This will bypass any blockage caused by a mistaken HBA
// configuration by the user.
caCert := filepath.Join(c.demoDir, certnames.CACertFilename())
rootCert := filepath.Join(c.demoDir, certnames.ClientCertFilename(username.RootUserName()))
rootKey := filepath.Join(c.demoDir, certnames.ClientKeyFilename(username.RootUserName()))
u := pgurl.New().
WithDatabase(catconstants.SystemDatabaseName).
WithUsername(username.RootUser).
WithAuthn(pgurl.AuthnClientCert(rootCert, rootKey)).
WithTransport(pgurl.TransportTLS(pgurl.TLSRequire, caCert))
apply := func(filename string, u *pgurl.URL) error {
db, err := gosql.Open("postgres", u.ToPQ().String())
if err != nil {
return err
}
defer db.Close()
return fn(ctx, filename, db)
}
if c.demoCtx.Multitenant && len(c.tenantServers) > 0 && c.tenantServers[0] != nil {
sqlAddr := c.tenantServers[0].SQLAddr()
host, port, _ := addr.SplitHostPort(sqlAddr, "")
u.WithNet(pgurl.NetTCP(host, port))
// When the server controller is not used, the tenant selection is
// done via the TCP port number and the options parameter is
// ignored. If the server controller is used, the options will
// select the tenant.
_ = u.SetOption("options", "-ccluster="+demoTenantName)
if err := apply("sessions.app.txt", u); err != nil {
return errors.Wrapf(err, "%s for application tenant", word)
}
}
if len(c.servers) > 0 && c.servers[0].TestServer != nil {
sqlAddr := c.servers[0].ServingSQLAddr()
host, port, _ := addr.SplitHostPort(sqlAddr, "")
u.WithNet(pgurl.NetTCP(host, port))
// See the comment above about options.
_ = u.SetOption("options", "-ccluster="+catconstants.SystemTenantName)
return errors.Wrapf(
apply("sessions.system.txt", u),
"%s for for system tenant", word)
}
return nil
}
type webSessionRow struct {
ID int64
HashedSecret []byte
Username string
ExpiresAt string
}
// saveWebSessionsInternal saves the sessions for just one tenant to
// the provided filename.
func (c *transientCluster) saveWebSessionsInternal(
ctx context.Context, filename string, db *gosql.DB,
) error {
c.infoLog(ctx, "saving sessions")
rows, err := db.QueryContext(ctx, `
SELECT id, "hashedSecret", username, "expiresAt"
FROM system.web_sessions
WHERE "expiresAt" > now()
AND "revokedAt" IS NULL`)
if err != nil {
return err
}
defer rows.Close()
outputFile, err := os.Create(filepath.Join(c.demoDir, filename))
if err != nil {
return err
}
defer func() {
if err := outputFile.Close(); err != nil {
c.warnLog(ctx, "%v", err)
}
}()
buf := bufio.NewWriter(outputFile)
defer func() {
if err := buf.Flush(); err != nil {
c.warnLog(ctx, "%v", err)
}
}()
numSessions := 0
for rows.Next() {
var row webSessionRow
if err := rows.Scan(&row.ID, &row.HashedSecret, &row.Username, &row.ExpiresAt); err != nil {
return err
}
j, err := json.Marshal(row)
if err != nil {
return err
}
j = append(j, '\n')
if _, err := buf.Write(j); err != nil {
return err
}
numSessions++
}
c.infoLog(ctx, "saved %d sessions to %q", numSessions, filename)
return nil
}
// restoreWebSessionsInternal restores the sessions for just one
// tenant from the provided filename.
func (c *transientCluster) restoreWebSessionsInternal(
ctx context.Context, filename string, db *gosql.DB,
) error {
c.infoLog(ctx, "restoring sessions")
inputFile, err := os.Open(filepath.Join(c.demoDir, filename))
if err != nil {
if oserror.IsNotExist(err) {
// No file yet. That's OK.
return nil
}
return err
}
defer func() {
if err := inputFile.Close(); err != nil {
c.warnLog(ctx, "%v", err)
}
}()
buf := bufio.NewReader(inputFile)
numSessions := 0
for {
j, err := buf.ReadBytes('\n')
if err != nil {
if errors.Is(err, io.EOF) {
// Done reading. Nothing more to do.
break
}
return err
}
var row webSessionRow
if err := json.Unmarshal(j, &row); err != nil {
return err
}
// TODO(yang): Update this to insert new user_id column.
if _, err := db.ExecContext(ctx, `
INSERT INTO system.web_sessions(id, "hashedSecret", username, "expiresAt")
VALUES ($1, $2, $3, $4)`,
row.ID,
row.HashedSecret,
row.Username,
row.ExpiresAt,
); err != nil {
return err
}
numSessions++
}
c.infoLog(ctx, "restored %d sessions from %q", numSessions, filename)
return nil
}