-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(livestream): recheck current project membership #108135
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
Open
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f7bb61
fix(livestream): recheck current project membership
pauldambra 624a1a7
chore: update live stream branch from master
pauldambra 93afc13
chore(livestream): use typed authorization test arguments
pauldambra f696fcc
fix(livestream): handle response close result
pauldambra df2f0e0
fix(livestream): harden periodic authorization checks
pauldambra 34d0001
chore(livestream): simplify test authorization responses
pauldambra 0a06d4c
fix(livestream): keep notifications flowing during access checks
pauldambra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| var authorizationClient = &http.Client{ | ||
| Timeout: 3 * time.Second, | ||
| CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
| return http.ErrUseLastResponse | ||
| }, | ||
| } | ||
|
|
||
| func CheckAccess(ctx context.Context, header http.Header) error { | ||
| url := viper.GetString("jwt.authorization_url") | ||
| if url == "" { | ||
| return nil | ||
| } | ||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return echo.NewHTTPError(http.StatusServiceUnavailable, "live stream authorization unavailable") | ||
| } | ||
| request.Header.Set("Authorization", header.Get("Authorization")) | ||
| response, err := authorizationClient.Do(request) | ||
| if err != nil { | ||
| return echo.NewHTTPError(http.StatusServiceUnavailable, "live stream authorization unavailable") | ||
| } | ||
| defer func() { _ = response.Body.Close() }() | ||
| if response.StatusCode == http.StatusNoContent { | ||
| return nil | ||
| } | ||
| if response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden { | ||
| return echo.NewHTTPError(http.StatusUnauthorized, "live stream access denied") | ||
| } | ||
| return echo.NewHTTPError(http.StatusServiceUnavailable, "live stream authorization unavailable") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCheckAccess(t *testing.T) { | ||
| for _, test := range []struct { | ||
| status int | ||
| want int | ||
| }{ | ||
| {http.StatusNoContent, 0}, | ||
| {http.StatusOK, http.StatusServiceUnavailable}, | ||
| {http.StatusFound, http.StatusServiceUnavailable}, | ||
| {http.StatusUnauthorized, http.StatusUnauthorized}, | ||
| {http.StatusForbidden, http.StatusUnauthorized}, | ||
| {http.StatusInternalServerError, http.StatusServiceUnavailable}, | ||
| } { | ||
| t.Run(http.StatusText(test.status), func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| assert.Equal(t, "Bearer test-live-stream-token", r.Header.Get("Authorization")) | ||
| if r.URL.Path == "/redirected" { | ||
| w.WriteHeader(http.StatusNoContent) | ||
| return | ||
| } | ||
| w.Header().Set("Location", "/redirected") | ||
| w.WriteHeader(test.status) | ||
| })) | ||
| defer server.Close() | ||
| viper.Set("jwt.authorization_url", server.URL) | ||
| t.Cleanup(func() { viper.Set("jwt.authorization_url", "") }) | ||
| err := CheckAccess(context.Background(), http.Header{"Authorization": {"Bearer test-live-stream-token"}}) | ||
| if test.want == 0 { | ||
| require.NoError(t, err) | ||
| } else { | ||
| var httpError *echo.HTTPError | ||
| require.ErrorAs(t, err, &httpError) | ||
| assert.Equal(t, test.want, httpError.Code) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckAccessFailsClosedOnConnectionFailure(t *testing.T) { | ||
| server := httptest.NewServer(http.NotFoundHandler()) | ||
| server.Close() | ||
| viper.Set("jwt.authorization_url", server.URL) | ||
| t.Cleanup(func() { viper.Set("jwt.authorization_url", "") }) | ||
| var httpError *echo.HTTPError | ||
| require.ErrorAs(t, CheckAccess(context.Background(), http.Header{}), &httpError) | ||
| assert.Equal(t, http.StatusServiceUnavailable, httpError.Code) | ||
| } | ||
|
|
||
| func TestCheckAccessBeforeRollout(t *testing.T) { | ||
| viper.Set("jwt.authorization_url", "") | ||
| require.NoError(t, CheckAccess(context.Background(), http.Header{})) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When the Live Events page is open, it polls
/statsevery 1,500 ms (LiveEventsTable.tsx:32,74), so this new call generates about 40 synchronous Django authorization requests per minute per viewer. Each request performs User and Team lookups and, for access-control organizations,UserPermissionsloads every project access-control row across all organizations the user belongs to (posthog/user_permissions.py:113-134), despite checking one team. This multiplies organization-wide database scans by active viewers and can make the three-second fail-closed timeout disconnect users under load; cache or throttle the stats authorization decision to the 15–30-second recheck cadence, or use a team-scoped resolver.Useful? React with 👍 / 👎.