-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AUTH-5682 Org token flow in Access logins should pass CF_AppSession c…
…ookie - Refactor HandleRedirects function and add unit tests - Move signal test to its own file because of OS specific instructions
- Loading branch information
Showing
3 changed files
with
163 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
//go:build linux || darwin | ||
|
||
package token | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSignalHandler(t *testing.T) { | ||
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}} | ||
handlerRan := false | ||
done := make(chan struct{}) | ||
timer := time.NewTimer(time.Second) | ||
sigHandler.register(func() { | ||
handlerRan = true | ||
done <- struct{}{} | ||
}) | ||
|
||
p, err := os.FindProcess(os.Getpid()) | ||
require.Nil(t, err) | ||
p.Signal(syscall.SIGUSR1) | ||
|
||
// Blocks for up to one second to make sure the handler callback runs before the assert. | ||
select { | ||
case <-done: | ||
assert.True(t, handlerRan) | ||
case <-timer.C: | ||
t.Fail() | ||
} | ||
sigHandler.deregister() | ||
} | ||
|
||
func TestSignalHandlerClose(t *testing.T) { | ||
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}} | ||
done := make(chan struct{}) | ||
timer := time.NewTimer(time.Second) | ||
sigHandler.register(func() { done <- struct{}{} }) | ||
sigHandler.deregister() | ||
|
||
p, err := os.FindProcess(os.Getpid()) | ||
require.Nil(t, err) | ||
p.Signal(syscall.SIGUSR1) | ||
select { | ||
case <-done: | ||
t.Fail() | ||
case <-timer.C: | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,54 +1,82 @@ | ||
//go:build linux | ||
|
||
package token | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSignalHandler(t *testing.T) { | ||
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}} | ||
handlerRan := false | ||
done := make(chan struct{}) | ||
timer := time.NewTimer(time.Second) | ||
sigHandler.register(func() { | ||
handlerRan = true | ||
done <- struct{}{} | ||
}) | ||
|
||
p, err := os.FindProcess(os.Getpid()) | ||
require.Nil(t, err) | ||
p.Signal(syscall.SIGUSR1) | ||
|
||
// Blocks for up to one second to make sure the handler callback runs before the assert. | ||
select { | ||
case <-done: | ||
assert.True(t, handlerRan) | ||
case <-timer.C: | ||
t.Fail() | ||
} | ||
sigHandler.deregister() | ||
func TestHandleRedirects_AttachOrgToken(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/login", nil) | ||
via := []*http.Request{} | ||
orgToken := "orgTokenValue" | ||
|
||
handleRedirects(req, via, orgToken) | ||
|
||
// Check if the orgToken cookie is attached | ||
cookies := req.Cookies() | ||
found := false | ||
for _, cookie := range cookies { | ||
if cookie.Name == tokenCookie && cookie.Value == orgToken { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
t.Errorf("OrgToken cookie not attached to the request.") | ||
} | ||
} | ||
|
||
func TestHandleRedirects_AttachAppSessionCookie(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/authorized", nil) | ||
via := []*http.Request{ | ||
{ | ||
URL: &url.URL{Path: "/cdn-cgi/access/login"}, | ||
Response: &http.Response{ | ||
Header: http.Header{"Set-Cookie": {"CF_AppSession=appSessionValue"}}, | ||
}, | ||
}, | ||
} | ||
orgToken := "orgTokenValue" | ||
|
||
err := handleRedirects(req, via, orgToken) | ||
|
||
// Check if the appSessionCookie is attached to the request | ||
cookies := req.Cookies() | ||
found := false | ||
for _, cookie := range cookies { | ||
if cookie.Name == appSessionCookie && cookie.Value == "appSessionValue" { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
t.Errorf("AppSessionCookie not attached to the request.") | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
} | ||
|
||
func TestSignalHandlerClose(t *testing.T) { | ||
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}} | ||
done := make(chan struct{}) | ||
timer := time.NewTimer(time.Second) | ||
sigHandler.register(func() { done <- struct{}{} }) | ||
sigHandler.deregister() | ||
|
||
p, err := os.FindProcess(os.Getpid()) | ||
require.Nil(t, err) | ||
p.Signal(syscall.SIGUSR1) | ||
select { | ||
case <-done: | ||
t.Fail() | ||
case <-timer.C: | ||
func TestHandleRedirects_StopAtAuthorizedEndpoint(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/authorized", nil) | ||
via := []*http.Request{ | ||
{ | ||
URL: &url.URL{Path: "other"}, | ||
}, | ||
{ | ||
URL: &url.URL{Path: AccessAuthorizedWorkerPath}, | ||
}, | ||
} | ||
orgToken := "orgTokenValue" | ||
|
||
err := handleRedirects(req, via, orgToken) | ||
|
||
// Check if ErrUseLastResponse is returned | ||
if err != http.ErrUseLastResponse { | ||
t.Errorf("Expected ErrUseLastResponse, got %v", err) | ||
} | ||
} |