Repository navigation
Expand file tree
/
Copy pathbootstrap.go
More file actions
81 lines (73 loc) · 2.47 KB
/
Copy pathbootstrap.go
File metadata and controls
81 lines (73 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"regexp"
"time"
"github.com/gin-gonic/gin"
resty "github.com/go-resty/resty/v2"
)
const bootstrapServiceAccount = "system:serviceaccount:os-network:tailscale"
const bootstrapAudience = "headscale-bootstrap"
const bootstrapKeyLifetime = 5 * time.Minute
var bootstrapKeyPattern = regexp.MustCompile(`^hskey-auth-[a-zA-Z0-9_-]+$`)
type bootstrapPreAuthKey struct {
Key string `json:"key"`
}
func registerBootstrap(router *gin.Engine) {
bootstrap := router.Group("/bootstrap")
bootstrap.Use(func(c *gin.Context) {
c.Header("Cache-Control", "no-store")
c.Header("Pragma", "no-cache")
c.Next()
})
bootstrap.Use(requireAllowedServiceAccount(bootstrapServiceAccount, bootstrapAudience))
bootstrap.POST("/tailscale-authkey", createBootstrapAuthKey)
}
// Do not include Headscale response bodies in errors: they can contain keys.
func bootstrapRequest(ctx context.Context, method, path string, body, result any) error {
if method != http.MethodGet {
if err := checkIdentityWrite(ctx); err != nil {
return err
}
}
resp, err := resty.New().SetTimeout(downstreamTimeout).R().SetContext(ctx).
SetHeaders(headers).SetBody(body).Execute(method, url+path)
if err != nil {
return fmt.Errorf("Headscale bootstrap request failed: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("Headscale bootstrap request returned HTTP %d", resp.StatusCode())
}
if result != nil {
if err := json.Unmarshal(resp.Body(), result); err != nil {
return errors.New("invalid Headscale bootstrap response")
}
}
return nil
}
func createBootstrapAuthKey(c *gin.Context) {
var result struct {
Key bootstrapPreAuthKey `json:"preAuthKey"`
}
request := createPreAuthKeyRequest{
Reusable: false, Ephemeral: false,
Expiration: time.Now().UTC().Add(bootstrapKeyLifetime).Format(time.RFC3339),
AclTags: []string{"tag:olares"},
}
err := bootstrapRequest(c.Request.Context(), http.MethodPost, "/preauthkey", request, &result)
if err == nil && !bootstrapKeyPattern.MatchString(result.Key.Key) {
err = errors.New("Headscale returned an invalid bootstrap key")
}
if err != nil {
log.Printf("bootstrap authkey failed error=%v", err)
c.JSON(http.StatusBadGateway, response{Code: requestHeadscaleError, Message: err.Error()})
return
}
c.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(result.Key.Key))
log.Printf("bootstrap authkey issued serviceAccount=%q", bootstrapServiceAccount)
}