/
main.go
162 lines (138 loc) · 4.18 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sort"
lambdahandler "github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/function61/gokit/app/aws/lambdautils"
"github.com/function61/gokit/app/cli"
"github.com/function61/gokit/app/dynversion"
. "github.com/function61/gokit/builtin"
"github.com/function61/gokit/encoding/jsonfile"
"github.com/function61/gokit/net/http/ezhttp"
"github.com/function61/gokit/net/http/httputils"
"github.com/function61/gokit/os/osutil"
"github.com/function61/tailscale-discovery/pkg/tailscalediscoveryclient"
"github.com/spf13/cobra"
)
func main() {
if lambdautils.InLambda() {
handler, err := newServerHandler()
osutil.ExitIfError(err)
lambdahandler.StartHandler(lambdautils.NewLambdaHttpHandlerAdapter(handler))
return
}
app := &cobra.Command{
Use: os.Args[0],
Short: "Tailscale discovery",
Version: dynversion.Version,
Args: cobra.NoArgs,
Run: cli.RunnerNoArgs(server),
}
app.AddCommand(renewAPIKeyEntrypoint())
osutil.ExitIfError(app.Execute())
}
func server(ctx context.Context, _ *log.Logger) error {
handler, err := newServerHandler()
if err != nil {
return err
}
srv := &http.Server{
Handler: handler,
Addr: ":80",
ReadHeaderTimeout: httputils.DefaultReadHeaderTimeout,
}
return httputils.CancelableServer(ctx, srv, srv.ListenAndServe)
}
func newServerHandler() (http.Handler, error) {
apiKey, err := osutil.GetenvRequired("TAILSCALE_API_KEY")
if err != nil {
return nil, err
}
tailnet := coalesce(os.Getenv("TAILSCALE_TAILNET"), "-") // '-' means the default Tailnet the API key has access to
routes := http.NewServeMux()
routes.HandleFunc("/tailscale-discovery/api/devices", func(w http.ResponseWriter, r *http.Request) {
devs, err := getDevicesFromTailscale(r.Context(), tailnet, apiKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_ = jsonfile.Marshal(w, devs)
})
return routes, nil
}
type tailscaleAPIDevicesOutput struct {
Devices []struct {
Addresses []string `json:"addresses"`
Hostname string `json:"hostname"`
Tags []string `json:"tags"`
} `json:"devices"`
}
func getDevicesFromTailscale(ctx context.Context, tailnet string, apiKey string) ([]tailscalediscoveryclient.Device, error) {
devicesOutput := tailscaleAPIDevicesOutput{}
if _, err := ezhttp.Get(
ctx,
fmt.Sprintf("https://api.tailscale.com/api/v2/tailnet/%s/devices", tailnet),
ezhttp.AuthBasic(apiKey, ""), // <- unusual way
ezhttp.RespondsJSONAllowUnknownFields(&devicesOutput),
); err != nil {
return nil, fmt.Errorf("Tailscale API: %w", err)
}
devices := []tailscalediscoveryclient.Device{}
for _, apiDevice := range devicesOutput.Devices {
devices = append(devices, tailscalediscoveryclient.Device{
IPv4: apiDevice.Addresses[0], // first is always IPv4
Hostname: apiDevice.Hostname,
Tags: func() []string {
if apiDevice.Tags != nil {
return apiDevice.Tags
} else {
return []string{} // so it's not marshaled as "null"
}
}(),
})
}
// stable return order
sort.Slice(devices, func(i, j int) bool { return devices[i].Hostname < devices[j].Hostname })
return devices, nil
}
func renewAPIKeyEntrypoint() *cobra.Command {
return &cobra.Command{
Use: "renew-apikey",
Short: "Updates to Lambda the new API key",
Args: cobra.NoArgs,
Run: cli.RunnerNoArgs(func(ctx context.Context, _ *log.Logger) error {
newApiKey, err := osutil.GetenvRequired("NEW_TAILSCALE_API_KEY")
if err != nil {
return err
}
awsSession, err := session.NewSession()
if err != nil {
return err
}
lambdaSvc := lambda.New(awsSession, aws.NewConfig().WithRegion("eu-central-1"))
_, err = lambdaSvc.UpdateFunctionConfigurationWithContext(ctx, &lambda.UpdateFunctionConfigurationInput{
FunctionName: Pointer("WebTailscaleDiscovery"),
Environment: &lambda.Environment{
Variables: map[string]*string{
"TAILSCALE_API_KEY": &newApiKey,
},
},
})
return err
}),
}
}
func coalesce(a, b string) string {
if a != "" {
return a
} else {
return b
}
}