Skip to content

CBG-4687 have sgcollect_info use a token #7604

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

Merged
merged 12 commits into from
Jun 24, 2025
Merged

CBG-4687 have sgcollect_info use a token #7604

merged 12 commits into from
Jun 24, 2025

Conversation

torcolvin
Copy link
Collaborator

@torcolvin torcolvin commented Jun 24, 2025

CBG-4687 have sgcollect_info use a token

  • Created a field on handlers that marks if is sgcollect that indicates whether sgcollect will be called.
  • Create a specific token for performing these operations
  • Switch sgcollect code pass auth headers rather than explicit username/password

For the reviewer:

  • Manual testing with real SG and /_sgcollect_info endpoint and sgcollect_info executable

This was done on top of #7601. Will turn into a non draft PR when that is merged.

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Dependencies (if applicable)

  • Link upstream PRs

Integration Tests

@bbrks bbrks requested review from Copilot and bbrks June 24, 2025 09:13
@bbrks bbrks self-assigned this Jun 24, 2025
Copilot

This comment was marked as outdated.

Copy link
Member

@bbrks bbrks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small suggestions - otherwise looks good to me.

rest/handler.go Outdated
Comment on lines 297 to 298
if h.sgcollect && h.server.sgcollect.hasValidToken(h.rq.Header) {
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth detecting when a handler is called with an sgcollect token and the handler doesn't have h.sgcollect set?

It indicates we have added a new endpoint to sgcollect but forgotten to set the handler option which seems like an easy thing to forget.

AssertfCtx should let us catch this at dev time via the existing sgcollect tests we have.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this with tests at some complexity of the code.

if !strings.HasPrefix(auth, authPrefix) {
return false
}
if time.Since(sg.tokenAge) > 30*time.Minute {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this token timeout into a named const for clarity.

if !strings.HasPrefix(auth, authPrefix) {
return false
}
if time.Since(sg.tokenAge) > 30*time.Minute {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether 30 minutes is long enough, or whether we'll actually get cases where sgcollect_info takes longer to run if there's a really big set of logs to process?

Do we have any information about how long we expect sgcollect_info to run for? Even locally it takes ~2 minutes to run.

There is probably data we could gather from previous collections to be sure this is long enough not to cause problems, but the easier alternative is to give us much more headroom to eliminate the chance of exceeding during a valid run, yet still have an upper-limit token lifespan (e.g. 6 hours) to avoid indefinite token lifespan in the case where we were unable to clear the token manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would potentially like a third opinion on this - the token expiry was a suggestion which I think is a useful defensive measure in case we or sgcollect screws up and keeps a token active for a long period, but I'm not quite sure what the expiry value should be.

/cc @adamcfraser

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12hr

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided on 12 hours offline.

@bbrks bbrks assigned torcolvin and unassigned bbrks Jun 24, 2025
Base automatically changed from CBG-4712 to main June 24, 2025 17:36
@torcolvin torcolvin marked this pull request as ready for review June 24, 2025 18:34
@torcolvin torcolvin requested review from bbrks and Copilot June 24, 2025 18:34
@torcolvin torcolvin assigned bbrks and unassigned torcolvin Jun 24, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements token‐based authentication for sgcollect_info by replacing explicit username/password usage with auth headers throughout the code and tests, and by adding token generation, validation, and clearance in sgcollect_info.

  • Replace basic auth functions with token-based auth and update function signatures accordingly.
  • Update various task creation, HTTP client, and handler functions to use auth_headers.
  • Update tests and routing to reflect these changes.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/tasks.py & tools/sgcollect.py Update functions to accept auth_headers instead of user/pass.
tools-tests/* Modify test mocks and calls to support auth_headers usage.
rest/sgcollect.go Introduce token management for sgcollect_info and update env.
rest/routing.go & rest/handler.go Update handler creation to pass sgcollect option and auth info.
base/* Minor updates to test harness assertions and logging detail.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
bbrks
bbrks previously approved these changes Jun 24, 2025
Copy link
Member

@bbrks bbrks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
// with valid sgcollect token, but sgcollect on the handler is disabled
require.NoError(t, sc.sgcollect.createNewToken())
adminHandler = newHandler(sc, adminPrivs, adminServer, httptest.NewRecorder(), &http.Request{Header: http.Header{"Authorization": []string{"SGCollect invalid"}}}, handlerOptions{sgcollect: false})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should ideally be a valid token but the current implementation for the assertion doesn't care whether the token was valid or not, so it's fine

@torcolvin torcolvin enabled auto-merge (squash) June 24, 2025 19:17
@torcolvin torcolvin merged commit ac4581d into main Jun 24, 2025
45 of 46 checks passed
@torcolvin torcolvin deleted the CBG-4687-take2 branch June 24, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants