-
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
Conversation
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 |
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
moved this into #7601 to be able to write the tests
@@ -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 comment
The 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 /_sgcollect_info
is a singleton and has sync mechanisms in place to avoid multiple runs, this isn't and won't be required.
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.
uv run -- pytest | ||
- name: Run mypy | ||
run: | | ||
uv run -- mypy tools/sgcollect_info tools |
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.
return args | ||
} | ||
|
||
// sgCollectPaths attempts to return the absolute paths to Sync Gateway and to sgcollect_info binaries. | ||
func sgCollectPaths() (sgBinary, sgCollectBinary string, err error) { | ||
func sgCollectPaths() (sgBinary string, sgCollectBinary []string, err error) { |
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.
In what circumstances does sgCollectBinary
need to be a slice? I'm confused...
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.
It is ["python", "tools/sgcollect_info"] when called in tests (only!) but always just sgcollect_info
which is a self extracting binary in prod.
@@ -28,6 +28,10 @@ import ( | |||
"github.com/pkg/errors" | |||
) | |||
|
|||
const ( | |||
adminAPITokenHeader = "SG_BEARER_TOKEN" |
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.
Header
->EnvVar
return {"Authorization": f"Bearer {session_token}"} | ||
elif username: | ||
base64string = base64.b64encode( | ||
(f"{username}: {password}").encode("utf-8") |
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.
leading space is breaking password here
session_token_name = "SG_BEARER_TOKEN" | ||
session_token = os.environ.get(session_token_name) | ||
if session_token: | ||
return {"Authorization": f"Bearer {session_token}"} |
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.
Replace Bearer
with SGCollectToken
""" | ||
Return the headers for authentication. | ||
""" | ||
session_token_name = "SG_BEARER_TOKEN" |
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.
SG_BEARER_TOKEN
-> SG_COLLECT_TOKEN
@torcolvin ready for a rebase now the other changes have been split out and merged (#7597) |
CBG-4687 have sgcollect_info use a token
sgcollect
that indicates whether sgcollect will be called.Risk mitigation/testing strategy:
--sync-gateway-url
since it will try to connect on http://127.0.0.1:4985 and then https://127.0.0.1:4985sgcollectPath
andsgcollectPathErr
for testing. I don't know why these are globals in the first place. To run the tests on windows, you need to runpython tools/sgcollect_info
instead oftools/sgcollect_info
since executing the file works on mac/linux by reading the shebang line. This lead to the weird modification ofsgcollectPath
to be a []string{} to fit in for testing. In productionsgcollect_info
is a self extracting binary that runs python so this is not used in a non testing path.sgCollect.stdout
for testing only! I think there's some value in looking for errors, I found an exception we should handle in pythonget_paths_from_expvars
if there was no file named*.json
in the command line.Possible future refactors:
sgcollectPath
globals, this is really weird and if you are executing a subprocess doing a path manipulation is so fast. If we really did want to optimize it, you could add async.Once
to ServerContext.For the reviewer:
TODO
stdout
to a channel in the test instanceio.Pipe
might cause dangling go routinesPre-review checklist
fmt.Print
,log.Print
, ...)base.UD(docID)
,base.MD(dbName)
)docs/api
Integration Tests
GSI=true,xattrs=true
https://jenkins.sgwdev.com/job/SyncGateway-Integration/000/