94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/sandbox-kit-spec/v3/spec"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
|
)
|
|
|
|
type identityOAuthStore struct {
|
|
t *testing.T
|
|
token string
|
|
principal access.Principal
|
|
reads, writes int
|
|
}
|
|
|
|
func (s *identityOAuthStore) check(ctx context.Context) {
|
|
s.t.Helper()
|
|
p, _ := access.PrincipalFromContext(ctx)
|
|
if harnesssecret.RunCredential(ctx) != s.token || p.IdentityID != s.principal.IdentityID {
|
|
s.t.Fatal("OAuth operation lost the session's grant or principal")
|
|
}
|
|
}
|
|
|
|
func (s *identityOAuthStore) GetHarnessSecret(ctx context.Context, _, _ string) (harnesssecret.Secret, error) {
|
|
s.check(ctx)
|
|
s.reads++
|
|
return harnesssecret.Secret{AccessToken: "real-access", RefreshToken: "real-refresh"}, nil
|
|
}
|
|
|
|
func (s *identityOAuthStore) PutHarnessSecret(ctx context.Context, _ harnesssecret.Secret) error {
|
|
s.check(ctx)
|
|
s.writes++
|
|
return nil
|
|
}
|
|
|
|
func TestOAuthUsesSessionAuthority(t *testing.T) {
|
|
for _, setup := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "task grant", true: "setup principal"}[setup], func(t *testing.T) {
|
|
opts := SessionOptions{Token: "proxy-token", RunCredential: "run-grant", Org: "acme", Credentials: []spec.CredentialCapability{{Service: "provider", Phase: spec.Phases{"runtime"}, OAuth: &spec.OAuth{TokenEndpoint: &spec.TokenEndpoint{Host: "token.example"}, ResourceHosts: []string{"api.example"}, Sentinels: &spec.Sentinels{AccessToken: "access-sentinel", RefreshToken: "refresh-sentinel"}}}}}
|
|
if setup {
|
|
opts.RunCredential = ""
|
|
opts.Principal = access.Principal{IdentityID: "alice", Org: "acme"}
|
|
}
|
|
store := &identityOAuthStore{t: t, token: opts.RunCredential, principal: opts.Principal}
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if string(body) != "real-refresh" {
|
|
t.Errorf("refresh body %q", body)
|
|
}
|
|
_, _ = io.WriteString(w, `{"access_token":"new-access","refresh_token":"new-refresh"}`)
|
|
}))
|
|
defer upstream.Close()
|
|
proxy := NewProxy(nil, ProxyOptions{OAuthStore: store, Resolver: ResolverFunc(func(context.Context, string, string) (string, error) { return "", nil }), Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
|
}})
|
|
session, err := proxy.Register(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session.EnterRuntime()
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://api.example/data", nil)
|
|
req.Header.Set("Authorization", "Bearer access-sentinel")
|
|
if err := proxy.injectOAuth(req.Context(), session, req, "api.example", 80); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if req.Header.Get("Authorization") != "Bearer real-access" {
|
|
t.Fatal("access token not injected")
|
|
}
|
|
refresh := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "http://token.example/token", strings.NewReader("refresh-sentinel"))
|
|
response := httptest.NewRecorder()
|
|
proxy.interceptOAuth(refresh.Context(), response, refresh, session, session.oauth[0], "http", "token.example")
|
|
if response.Code != http.StatusOK || store.reads != 2 || store.writes != 1 {
|
|
t.Fatalf("status %d, reads %d, writes %d", response.Code, store.reads, store.writes)
|
|
}
|
|
if strings.Contains(response.Body.String(), "new-access") {
|
|
t.Fatal("real token escaped to container")
|
|
}
|
|
})
|
|
}
|
|
}
|