Use Headscale container to run mockoidc

This commit is contained in:
Juan Font 2022-09-20 20:42:50 +00:00
parent 2e97119db8
commit 1563d7555f

View File

@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -19,7 +18,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/oauth2-proxy/mockoidc"
"github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker" "github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -36,10 +34,10 @@ type IntegrationOIDCTestSuite struct {
suite.Suite suite.Suite
stats *suite.SuiteInformation stats *suite.SuiteInformation
oidc *mockoidc.MockOIDC
pool dockertest.Pool pool dockertest.Pool
network dockertest.Network network dockertest.Network
headscale dockertest.Resource headscale dockertest.Resource
mockOidc dockertest.Resource
saveLogs bool saveLogs bool
tailscales map[string]dockertest.Resource tailscales map[string]dockertest.Resource
@ -75,6 +73,11 @@ func TestOIDCIntegrationTestSuite(t *testing.T) {
log.Printf("Could not save log: %s\n", err) log.Printf("Could not save log: %s\n", err)
} }
} }
if err := s.pool.Purge(&s.mockOidc); err != nil {
log.Printf("Could not purge resource: %s\n", err)
}
if err := s.pool.Purge(&s.headscale); err != nil { if err := s.pool.Purge(&s.headscale); err != nil {
t.Logf("Could not purge resource: %s\n", err) t.Logf("Could not purge resource: %s\n", err)
} }
@ -109,36 +112,43 @@ func (s *IntegrationOIDCTestSuite) SetupSuite() {
log.Printf("Network config: %v", s.network.Network.IPAM.Config[0]) log.Printf("Network config: %v", s.network.Network.IPAM.Config[0])
s.Suite.T().Log("Setting up mock OIDC") s.Suite.T().Log("Setting up mock OIDC")
oidc, _ := mockoidc.NewServer(nil) mockOidcOptions := &dockertest.RunOptions{
ln, err := net.Listen("tcp", fmt.Sprintf("%s:0", s.network.Network.IPAM.Config[0].Gateway)) Name: "mockoidc",
if err != nil { Hostname: "mockoidc",
s.FailNow(fmt.Sprintf("Could not listen on port: %s", err), "") Cmd: []string{"headscale", "mockoidc"},
ExposedPorts: []string{"10000/tcp"},
Networks: []*dockertest.Network{&s.network},
PortBindings: map[docker.Port][]docker.PortBinding{
"10000/tcp": {{HostPort: "10000"}},
},
Env: []string{
"MOCKOIDC_PORT=10000",
"MOCKOIDC_CLIENT_ID=superclient",
"MOCKOIDC_CLIENT_SECRET=supersecret",
},
} }
oidc.Start(ln, nil)
s.oidc = oidc
// we now parse the Issuer URL and replace the host with the docker internal hostname
// urlIssuer, _ := url.Parse(s.oidc.Issuer())
// urlIssuer.Host = fmt.Sprintf("host-gateway:%s", urlIssuer.Port())
// issuer := urlIssuer.String()
oidcCfg := fmt.Sprintf(`
oidc:
issuer: %s
client_id: %s
client_secret: %s
strip_email_domain: true`,
s.oidc.Issuer(),
s.oidc.Config().ClientID,
s.oidc.Config().ClientSecret)
fmt.Println(oidcCfg)
headscaleBuildOptions := &dockertest.BuildOptions{ headscaleBuildOptions := &dockertest.BuildOptions{
Dockerfile: "Dockerfile.debug", Dockerfile: "Dockerfile.debug",
ContextDir: ".", ContextDir: ".",
} }
if pmockoidc, err := s.pool.BuildAndRunWithBuildOptions(
headscaleBuildOptions,
mockOidcOptions,
DockerRestartPolicy); err == nil {
s.mockOidc = *pmockoidc
} else {
s.FailNow(fmt.Sprintf("Could not start mockOIDC container: %s", err), "")
}
oidcCfg := fmt.Sprintf(`
oidc:
issuer: http://%s:10000/oidc
client_id: superclient
client_secret: supersecret
strip_email_domain: true`, s.mockOidc.GetIPInNetwork(&s.network))
currentPath, err := os.Getwd() currentPath, err := os.Getwd()
if err != nil { if err != nil {
s.FailNow(fmt.Sprintf("Could not determine current path: %s", err), "") s.FailNow(fmt.Sprintf("Could not determine current path: %s", err), "")
@ -160,7 +170,8 @@ oidc:
} }
headscaleOptions := &dockertest.RunOptions{ headscaleOptions := &dockertest.RunOptions{
Name: oidcHeadscaleHostname, Name: oidcHeadscaleHostname,
Networks: []*dockertest.Network{&s.network},
Mounts: []string{ Mounts: []string{
path.Join(currentPath, path.Join(currentPath,
"integration_test/etc_oidc:/etc/headscale", "integration_test/etc_oidc:/etc/headscale",
@ -368,8 +379,6 @@ func (s *IntegrationOIDCTestSuite) tailscaleContainer(
} }
func (s *IntegrationOIDCTestSuite) TearDownSuite() { func (s *IntegrationOIDCTestSuite) TearDownSuite() {
s.oidc.Shutdown()
if !s.saveLogs { if !s.saveLogs {
for _, tailscale := range s.tailscales { for _, tailscale := range s.tailscales {
if err := s.pool.Purge(&tailscale); err != nil { if err := s.pool.Purge(&tailscale); err != nil {
@ -381,6 +390,10 @@ func (s *IntegrationOIDCTestSuite) TearDownSuite() {
log.Printf("Could not purge resource: %s\n", err) log.Printf("Could not purge resource: %s\n", err)
} }
if err := s.pool.Purge(&s.mockOidc); err != nil {
log.Printf("Could not purge resource: %s\n", err)
}
if err := s.network.Close(); err != nil { if err := s.network.Close(); err != nil {
log.Printf("Could not close network: %s\n", err) log.Printf("Could not close network: %s\n", err)
} }