Skip to content

Commit fc95995

Browse files
committed
Refactor Tailscale service to streamline OAuth configuration and remove custom transport
- Simplified OAuth setup by utilizing the built-in OAuth support from the Tailscale client. - Removed the custom HTTP transport for setting headers, enhancing code clarity. - Updated deployment configuration to maintain Tailscale tailnet reference.
1 parent 67c250d commit fc95995

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

backend/internal/services/tailscale.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,15 @@ import (
1313
"github.com/rajsinghtech/tsflow/backend/internal/config"
1414
"github.com/rajsinghtech/tsflow/backend/internal/utils"
1515
tailscale "tailscale.com/client/tailscale/v2"
16-
"golang.org/x/oauth2"
17-
"golang.org/x/oauth2/clientcredentials"
1816
)
1917

20-
type secFetchSiteTransport struct {
21-
rt http.RoundTripper
22-
}
23-
24-
func (t *secFetchSiteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
25-
req.Header.Set("Sec-Fetch-Site", "cross-site")
26-
req.Header.Set("Sec-Fetch-Mode", "cors")
27-
return t.rt.RoundTrip(req)
28-
}
29-
3018
type TailscaleService struct {
31-
apiKey string
32-
oauthConfig *clientcredentials.Config
33-
tailnet string
34-
baseURL string
35-
client *http.Client
36-
useOAuth bool
37-
tsClient *tailscale.Client
19+
apiKey string
20+
tailnet string
21+
baseURL string
22+
client *http.Client
23+
useOAuth bool
24+
tsClient *tailscale.Client
3825
}
3926

4027
type Device struct {
@@ -83,29 +70,18 @@ func NewTailscaleService(cfg *config.Config) *TailscaleService {
8370
}
8471

8572
if cfg.TailscaleOAuthClientID != "" && cfg.TailscaleOAuthClientSecret != "" {
86-
ts.oauthConfig = &clientcredentials.Config{
73+
// Use the Tailscale client's built-in OAuth support
74+
oauthConfig := tailscale.OAuthConfig{
8775
ClientID: cfg.TailscaleOAuthClientID,
8876
ClientSecret: cfg.TailscaleOAuthClientSecret,
8977
Scopes: cfg.TailscaleOAuthScopes,
90-
TokenURL: cfg.TailscaleAPIURL + "/oauth/token",
9178
}
9279

93-
// Create custom transport to add required headers
94-
transport := &http.Transport{}
95-
httpClient := &http.Client{
96-
Transport: &secFetchSiteTransport{rt: transport},
97-
Timeout: 5 * time.Minute,
98-
}
99-
100-
// Create context with custom HTTP client
101-
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
102-
oauthClient := ts.oauthConfig.Client(ctx)
103-
10480
ts.tsClient = &tailscale.Client{
105-
HTTP: oauthClient,
81+
HTTP: oauthConfig.HTTPClient(),
10682
Tailnet: cfg.TailscaleTailnet,
10783
}
108-
ts.client = oauthClient
84+
ts.client = oauthConfig.HTTPClient()
10985
ts.useOAuth = true
11086
} else if cfg.TailscaleAPIKey != "" {
11187
ts.apiKey = cfg.TailscaleAPIKey

k8s/deployment.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,4 @@ spec:
3232
valueFrom:
3333
secretKeyRef:
3434
name: tsflow
35-
key: TAILSCALE_TAILNET
36-
- name: ENVIRONMENT
37-
value: production
35+
key: TAILSCALE_TAILNET

0 commit comments

Comments
 (0)