Skip to content

Commit ddd9282

Browse files
authored
Merge branch 'main' into remove-gin-references
2 parents 2c73f8e + bd6282d commit ddd9282

15 files changed

+785
-93
lines changed

.github/workflows/test-integration.yml

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ jobs:
4848
retry_on: error
4949
command: nix develop --command -- make test_integration_derp
5050

51+
- name: Run OIDC integration tests
52+
if: steps.changed-files.outputs.any_changed == 'true'
53+
uses: nick-fields/retry@v2
54+
with:
55+
timeout_minutes: 240
56+
max_attempts: 5
57+
retry_on: error
58+
command: nix develop --command -- make test_integration_oidc
59+
5160
- name: Run general integration tests
5261
if: steps.changed-files.outputs.any_changed == 'true'
5362
uses: nick-fields/retry@v2

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dev: lint test build
2424
test:
2525
@go test -coverprofile=coverage.out ./...
2626

27-
test_integration: test_integration_cli test_integration_derp test_integration_general
27+
test_integration: test_integration_cli test_integration_derp test_integration_oidc test_integration_general
2828

2929
test_integration_cli:
3030
go test -failfast -tags integration_cli,integration -timeout 30m -count=1 ./...
@@ -35,6 +35,9 @@ test_integration_derp:
3535
test_integration_general:
3636
go test -failfast -tags integration_general,integration -timeout 30m -count=1 ./...
3737

38+
test_integration_oidc:
39+
go test -failfast -tags integration_oidc,integration -timeout 30m -count=1 ./...
40+
3841
coverprofile_func:
3942
go tool cover -func=coverage.out
4043

api_common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (h *Headscale) generateMapResponse(
1313
Str("func", "generateMapResponse").
1414
Str("machine", mapRequest.Hostinfo.Hostname).
1515
Msg("Creating Map response")
16-
node, err := machine.toNode(h.cfg.BaseDomain, h.cfg.DNSConfig, true)
16+
node, err := machine.toNode(h.cfg.BaseDomain, h.cfg.DNSConfig)
1717
if err != nil {
1818
log.Error().
1919
Caller().
@@ -37,7 +37,7 @@ func (h *Headscale) generateMapResponse(
3737

3838
profiles := getMapResponseUserProfiles(*machine, peers)
3939

40-
nodePeers, err := peers.toNodes(h.cfg.BaseDomain, h.cfg.DNSConfig, true)
40+
nodePeers, err := peers.toNodes(h.cfg.BaseDomain, h.cfg.DNSConfig)
4141
if err != nil {
4242
log.Error().
4343
Caller().

cmd/headscale/cli/mockoidc.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"os"
7+
"strconv"
8+
"time"
9+
10+
"github.com/oauth2-proxy/mockoidc"
11+
"github.com/rs/zerolog/log"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
const (
16+
errMockOidcClientIDNotDefined = Error("MOCKOIDC_CLIENT_ID not defined")
17+
errMockOidcClientSecretNotDefined = Error("MOCKOIDC_CLIENT_SECRET not defined")
18+
errMockOidcPortNotDefined = Error("MOCKOIDC_PORT not defined")
19+
accessTTL = 10 * time.Minute
20+
refreshTTL = 60 * time.Minute
21+
)
22+
23+
func init() {
24+
rootCmd.AddCommand(mockOidcCmd)
25+
}
26+
27+
var mockOidcCmd = &cobra.Command{
28+
Use: "mockoidc",
29+
Short: "Runs a mock OIDC server for testing",
30+
Long: "This internal command runs a OpenID Connect for testing purposes",
31+
Run: func(cmd *cobra.Command, args []string) {
32+
err := mockOIDC()
33+
if err != nil {
34+
log.Error().Err(err).Msgf("Error running mock OIDC server")
35+
os.Exit(1)
36+
}
37+
},
38+
}
39+
40+
func mockOIDC() error {
41+
clientID := os.Getenv("MOCKOIDC_CLIENT_ID")
42+
if clientID == "" {
43+
return errMockOidcClientIDNotDefined
44+
}
45+
clientSecret := os.Getenv("MOCKOIDC_CLIENT_SECRET")
46+
if clientSecret == "" {
47+
return errMockOidcClientSecretNotDefined
48+
}
49+
portStr := os.Getenv("MOCKOIDC_PORT")
50+
if portStr == "" {
51+
return errMockOidcPortNotDefined
52+
}
53+
54+
port, err := strconv.Atoi(portStr)
55+
if err != nil {
56+
return err
57+
}
58+
59+
mock, err := getMockOIDC(clientID, clientSecret)
60+
if err != nil {
61+
return err
62+
}
63+
64+
listener, err := net.Listen("tcp", fmt.Sprintf("mockoidc:%d", port))
65+
if err != nil {
66+
return err
67+
}
68+
69+
err = mock.Start(listener, nil)
70+
if err != nil {
71+
return err
72+
}
73+
log.Info().Msgf("Mock OIDC server listening on %s", listener.Addr().String())
74+
log.Info().Msgf("Issuer: %s", mock.Issuer())
75+
c := make(chan struct{})
76+
<-c
77+
78+
return nil
79+
}
80+
81+
func getMockOIDC(clientID string, clientSecret string) (*mockoidc.MockOIDC, error) {
82+
keypair, err := mockoidc.NewKeypair(nil)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
mock := mockoidc.MockOIDC{
88+
ClientID: clientID,
89+
ClientSecret: clientSecret,
90+
AccessTTL: accessTTL,
91+
RefreshTTL: refreshTTL,
92+
CodeChallengeMethodsSupported: []string{"plain", "S256"},
93+
Keypair: keypair,
94+
SessionStore: mockoidc.NewSessionStore(),
95+
UserQueue: &mockoidc.UserQueue{},
96+
ErrorQueue: &mockoidc.ErrorQueue{},
97+
}
98+
99+
return &mock, nil
100+
}

cmd/headscale/cli/root.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
var cfgFile string = ""
1616

1717
func init() {
18+
if len(os.Args) > 1 && os.Args[1] == "version" || os.Args[1] == "mockoidc" {
19+
return
20+
}
21+
1822
cobra.OnInitialize(initConfig)
1923
rootCmd.PersistentFlags().
2024
StringVarP(&cfgFile, "config", "c", "", "config file (default is /etc/headscale/config.yaml)")

flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# When updating go.mod or go.sum, a new sha will need to be calculated,
2626
# update this if you have a mismatch after doing a change to thos files.
27-
vendorSha256 = "sha256-kc8EU+TkwRlsKM2+ljm/88aWe5h2QMgd/ZGPSgdd9QQ=";
27+
vendorSha256 = "sha256-DosFCSiQ5FURbIrt4NcPGkExc84t2MGMqe9XLxNHdIM=";
2828

2929
ldflags = [ "-s" "-w" "-X github.com/juanfont/headscale/cmd/headscale/cli.Version=v${version}" ];
3030
};

0 commit comments

Comments
 (0)