-
Notifications
You must be signed in to change notification settings - Fork 140
CBG-4687 have sgcollect_info use a token #7596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
[project] | ||
name = "sgcollect_info" | ||
version = "0.0.1" | ||
requires-python = ">3.9,<3.10" | ||
|
||
[mypy] | ||
files = ["tools-tests/*.py"] | ||
mypy_path = "$MYPY_CONFIG_FILE_DIR/tools" | ||
|
||
[tool.ruff] | ||
extend-include = ["tools/sgcollect_info"] | ||
|
||
[tool.ruff.lint] | ||
extend-select = ["I","B006"] # isort, mutable default argument | ||
|
||
[dependency-groups] | ||
dev = [ | ||
"mypy>=1.16.1", | ||
"pytest>=8.4.0", | ||
"pytest-httpserver>=1.1.3", | ||
"trustme>=1.2.1", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1673,14 +1673,22 @@ func (h *handler) handleSGCollect() error { | |
return base.HTTPErrorf(http.StatusBadRequest, "Invalid options used for sgcollect_info: %v", multiError) | ||
} | ||
|
||
// Populate username and password used by sgcollect_info script for talking to Sync Gateway. | ||
params.syncGatewayUsername, params.syncGatewayPassword = h.getBasicAuth() | ||
addr, err := h.server.getServerAddr(adminServer) | ||
if err != nil { | ||
return base.HTTPErrorf(http.StatusInternalServerError, "Error getting admin server address: %v", err) | ||
} | ||
if h.server.Config.API.HTTPS.TLSCertPath != "" { | ||
addr = "https://" + addr | ||
} else { | ||
params.adminURL = "http://" + addr | ||
} | ||
params.adminURL = addr | ||
Comment on lines
+1676
to
+1685
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whilst I agree this might be useful to do to avoid the sgcollect_info script iterating to find a working localhost URL as it does today, this is not related to the token feature and we should evaluate and implement each separately. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try this URL and fall back to localhost There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved this into #7601 to be able to write the tests |
||
|
||
zipFilename := sgcollectFilename() | ||
|
||
logFilePath := h.server.Config.Logging.LogFilePath | ||
|
||
if err := sgcollectInstance.Start(logFilePath, h.serialNumber, zipFilename, params); err != nil { | ||
if err := sgcollectInstance.Start(logFilePath, h.serialNumber, zipFilename, params, h.server.sgcollectCookies); err != nil { | ||
return base.HTTPErrorf(http.StatusInternalServerError, "Error running sgcollect_info: %v", err) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import ( | |
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/couchbase/sync_gateway/auth" | ||
"github.com/couchbase/sync_gateway/db/functions" | ||
"github.com/google/uuid" | ||
"github.com/shirou/gopsutil/mem" | ||
|
||
"github.com/couchbase/gocbcore/v10" | ||
|
@@ -86,6 +87,7 @@ type ServerContext struct { | |
DatabaseInitManager *DatabaseInitManager // Manages database initialization (index creation and readiness) independent of database stop/start/reload, when using persistent config | ||
ActiveReplicationsCounter | ||
invalidDatabaseConfigTracking invalidDatabaseConfigs | ||
sgcollectCookies *sgcollectCookieManager | ||
} | ||
|
||
type ActiveReplicationsCounter struct { | ||
|
@@ -163,9 +165,10 @@ func NewServerContext(ctx context.Context, config *StartupConfig, persistentConf | |
BootstrapContext: &bootstrapContext{sgVersion: *base.ProductVersion}, | ||
hasStarted: make(chan struct{}), | ||
_httpServers: map[serverType]*serverInfo{}, | ||
} | ||
sc.invalidDatabaseConfigTracking = invalidDatabaseConfigs{ | ||
dbNames: map[string]*invalidConfigInfo{}, | ||
invalidDatabaseConfigTracking: invalidDatabaseConfigs{ | ||
dbNames: map[string]*invalidConfigInfo{}, | ||
}, | ||
sgcollectCookies: newSgcollectCookieManager(), | ||
} | ||
|
||
if base.ServerIsWalrus(sc.Config.Bootstrap.Server) { | ||
|
@@ -2277,3 +2280,31 @@ func (sc *ServerContext) getClusterUUID(ctx context.Context) (string, error) { | |
} | ||
return base.ParseClusterUUID(output) | ||
} | ||
|
||
type sgcollectCookieManager struct { | ||
tokens map[string]struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever expect to support more than 1 valid token at any one time? AFAIK since I think this is adding complexity by maintaining a map, since it's more likely that we can unintentionally build up a set of valid/usable tokens. It might be better with a single token string that has a timestamp to also allow expiry after a period of time. This covers the case where the sgcollect info script hangs indefinitely and keeps the token alive, or any situations where we may be unintentionally not deleting the token from the map. |
||
} | ||
|
||
func newSgcollectCookieManager() *sgcollectCookieManager { | ||
return &sgcollectCookieManager{ | ||
tokens: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (m *sgcollectCookieManager) createToken() (string, error) { | ||
u, err := uuid.NewUUID() | ||
if err != nil { | ||
return "", fmt.Errorf("error generating UUID for sgcollect token: %w", err) | ||
} | ||
m.tokens[u.String()] = struct{}{} | ||
return u.String(), nil | ||
} | ||
|
||
func (m *sgcollectCookieManager) deleteToken(token string) { | ||
delete(m.tokens, token) | ||
} | ||
|
||
func (m *sgcollectCookieManager) isValidToken(token string) bool { | ||
_, exists := m.tokens[token] | ||
return exists | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not super keen on changing dependency management for the sake of it but if we do want to do it - again we should do it independent of feature PRs.