VirtualMachine: Implement virtual machines using KubeVirt
Auto adds new VM into given user's Tailscale network
Change-Id: I16847a0b9eacc17b0e794d3b4913eb1d80a93f0a
diff --git a/core/installer/vpn.go b/core/installer/vpn.go
new file mode 100644
index 0000000..4739bf2
--- /dev/null
+++ b/core/installer/vpn.go
@@ -0,0 +1,34 @@
+package installer
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "io"
+ "net/http"
+)
+
+type VPNAuthKeyGenerator interface {
+ Generate(username string) (string, error)
+}
+
+type headscaleAPIClient struct {
+ apiAddr string
+}
+
+func NewHeadscaleAPIClient(apiAddr string) VPNAuthKeyGenerator {
+ return &headscaleAPIClient{apiAddr}
+}
+
+func (g *headscaleAPIClient) Generate(username string) (string, error) {
+ resp, err := http.Post(fmt.Sprintf("%s/user/%s/preauthkey", g.apiAddr, username), "application/json", nil)
+ if err != nil {
+ return "", err
+ }
+ var buf bytes.Buffer
+ io.Copy(&buf, resp.Body)
+ if resp.StatusCode != http.StatusOK {
+ return "", errors.New(buf.String())
+ }
+ return buf.String(), nil
+}