mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-17 07:47:29 +00:00
Compare commits
39 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a9a1a8fb3c | ||
![]() |
85ddc0db33 | ||
![]() |
fddc2aa8fa | ||
![]() |
be3a379d10 | ||
![]() |
d0daff180e | ||
![]() |
be36480a64 | ||
![]() |
9f52a64a6a | ||
![]() |
52511af8e4 | ||
![]() |
ddb6bd795c | ||
![]() |
271660a284 | ||
![]() |
0b0f7db534 | ||
![]() |
5a7b377f6f | ||
![]() |
654d2b9910 | ||
![]() |
829a8c4381 | ||
![]() |
5807562b56 | ||
![]() |
985c6e7cc9 | ||
![]() |
0d13e16fed | ||
![]() |
91d135e069 | ||
![]() |
3e1e07e8c1 | ||
![]() |
6c4c761408 | ||
![]() |
abfb1791f1 | ||
![]() |
7ce8c4c649 | ||
![]() |
2ddca366f2 | ||
![]() |
9a6ac6e3e6 | ||
![]() |
cc3e8705bd | ||
![]() |
809a5b84e7 | ||
![]() |
06ae2a6c50 | ||
![]() |
93517aa6f8 | ||
![]() |
5f0f3705c0 | ||
![]() |
70ae18c3a8 | ||
![]() |
6aa763a1ae | ||
![]() |
ebfb8c8c5e | ||
![]() |
30788e1a70 | ||
![]() |
27947c6746 | ||
![]() |
6924b7bf4c | ||
![]() |
fa8cd96108 | ||
![]() |
dd1e425d02 | ||
![]() |
7f2027d7f2 | ||
![]() |
48f5a9a18c |
@@ -20,6 +20,7 @@ builds:
|
|||||||
- -mod=readonly
|
- -mod=readonly
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=v{{.Version}}
|
- -s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=v{{.Version}}
|
||||||
|
|
||||||
- id: linux-armhf
|
- id: linux-armhf
|
||||||
main: ./cmd/headscale/headscale.go
|
main: ./cmd/headscale/headscale.go
|
||||||
mod_timestamp: '{{ .CommitTimestamp }}'
|
mod_timestamp: '{{ .CommitTimestamp }}'
|
||||||
@@ -49,9 +50,16 @@ builds:
|
|||||||
- linux
|
- linux
|
||||||
goarch:
|
goarch:
|
||||||
- amd64
|
- amd64
|
||||||
goarm:
|
main: ./cmd/headscale/headscale.go
|
||||||
- 6
|
mod_timestamp: '{{ .CommitTimestamp }}'
|
||||||
- 7
|
ldflags:
|
||||||
|
- -s -w -X github.com/juanfont/headscale/cmd/headscale/cli.Version=v{{.Version}}
|
||||||
|
|
||||||
|
- id: linux-arm64
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
goarch:
|
||||||
|
- arm64
|
||||||
main: ./cmd/headscale/headscale.go
|
main: ./cmd/headscale/headscale.go
|
||||||
mod_timestamp: '{{ .CommitTimestamp }}'
|
mod_timestamp: '{{ .CommitTimestamp }}'
|
||||||
ldflags:
|
ldflags:
|
||||||
@@ -63,6 +71,7 @@ archives:
|
|||||||
- darwin-amd64
|
- darwin-amd64
|
||||||
- linux-armhf
|
- linux-armhf
|
||||||
- linux-amd64
|
- linux-amd64
|
||||||
|
- linux-arm64
|
||||||
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
|
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
|
||||||
format: binary
|
format: binary
|
||||||
|
|
||||||
|
3
api.go
3
api.go
@@ -395,6 +395,9 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key,
|
|||||||
m.RegisterMethod = "authKey"
|
m.RegisterMethod = "authKey"
|
||||||
db.Save(&m)
|
db.Save(&m)
|
||||||
|
|
||||||
|
pak.Used = true
|
||||||
|
db.Save(&pak)
|
||||||
|
|
||||||
resp.MachineAuthorized = true
|
resp.MachineAuthorized = true
|
||||||
resp.User = *pak.Namespace.toUser()
|
resp.User = *pak.Namespace.toUser()
|
||||||
respBody, err := encode(resp, &idKey, h.privateKey)
|
respBody, err := encode(resp, &idKey, h.privateKey)
|
||||||
|
@@ -129,6 +129,7 @@ var deleteNodeCmd = &cobra.Command{
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
output, _ := cmd.Flags().GetString("output")
|
||||||
h, err := getHeadscaleApp()
|
h, err := getHeadscaleApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
@@ -143,21 +144,32 @@ var deleteNodeCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
confirm := false
|
confirm := false
|
||||||
prompt := &survey.Confirm{
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
Message: fmt.Sprintf("Do you want to remove the node %s?", m.Name),
|
if !force {
|
||||||
}
|
prompt := &survey.Confirm{
|
||||||
err = survey.AskOne(prompt, &confirm)
|
Message: fmt.Sprintf("Do you want to remove the node %s?", m.Name),
|
||||||
if err != nil {
|
}
|
||||||
return
|
err = survey.AskOne(prompt, &confirm)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if confirm {
|
if confirm || force {
|
||||||
err = h.DeleteMachine(m)
|
err = h.DeleteMachine(m)
|
||||||
|
if strings.HasPrefix(output, "json") {
|
||||||
|
JsonOutput(map[string]string{"Result": "Node deleted"}, err, output)
|
||||||
|
return
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error deleting node: %s", err)
|
log.Fatalf("Error deleting node: %s", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Node deleted\n")
|
fmt.Printf("Node deleted\n")
|
||||||
} else {
|
} else {
|
||||||
|
if strings.HasPrefix(output, "json") {
|
||||||
|
JsonOutput(map[string]string{"Result": "Node not deleted"}, err, output)
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Printf("Node not deleted\n")
|
fmt.Printf("Node not deleted\n")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@@ -57,7 +57,7 @@ var listPreAuthKeys = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
d := pterm.TableData{{"ID", "Key", "Reusable", "Ephemeral", "Expiration", "Created"}}
|
d := pterm.TableData{{"ID", "Key", "Reusable", "Ephemeral", "Used", "Expiration", "Created"}}
|
||||||
for _, k := range *keys {
|
for _, k := range *keys {
|
||||||
expiration := "-"
|
expiration := "-"
|
||||||
if k.Expiration != nil {
|
if k.Expiration != nil {
|
||||||
@@ -76,6 +76,7 @@ var listPreAuthKeys = &cobra.Command{
|
|||||||
k.Key,
|
k.Key,
|
||||||
reusable,
|
reusable,
|
||||||
strconv.FormatBool(k.Ephemeral),
|
strconv.FormatBool(k.Ephemeral),
|
||||||
|
fmt.Sprintf("%v", k.Used),
|
||||||
expiration,
|
expiration,
|
||||||
k.CreatedAt.Format("2006-01-02 15:04:05"),
|
k.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
})
|
})
|
||||||
@@ -130,7 +131,7 @@ var createPreAuthKeyCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var expirePreAuthKeyCmd = &cobra.Command{
|
var expirePreAuthKeyCmd = &cobra.Command{
|
||||||
Use: "expire",
|
Use: "expire KEY",
|
||||||
Short: "Expire a preauthkey",
|
Short: "Expire a preauthkey",
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
@@ -152,6 +153,10 @@ var expirePreAuthKeyCmd = &cobra.Command{
|
|||||||
|
|
||||||
k, err := h.GetPreAuthKey(n, args[0])
|
k, err := h.GetPreAuthKey(n, args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if strings.HasPrefix(o, "json") {
|
||||||
|
JsonOutput(k, err, o)
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Fatalf("Error getting the key: %s", err)
|
log.Fatalf("Error getting the key: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'")
|
rootCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'")
|
||||||
|
rootCmd.PersistentFlags().Bool("force", false, "Disable prompts and forces the execution")
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
|
@@ -262,3 +262,12 @@ func JsonOutput(result interface{}, errResult error, outputFormat string) {
|
|||||||
}
|
}
|
||||||
fmt.Println(string(j))
|
fmt.Println(string(j))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasJsonOutputFlag() bool {
|
||||||
|
for _, arg := range os.Args {
|
||||||
|
if arg == "json" || arg == "json-line" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@@ -62,7 +62,8 @@ func main() {
|
|||||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !viper.GetBool("disable_check_updates") {
|
jsonOutput := cli.HasJsonOutputFlag()
|
||||||
|
if !viper.GetBool("disable_check_updates") && !jsonOutput {
|
||||||
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && cli.Version != "dev" {
|
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && cli.Version != "dev" {
|
||||||
githubTag := &latest.GithubTag{
|
githubTag := &latest.GithubTag{
|
||||||
Owner: "juanfont",
|
Owner: "juanfont",
|
||||||
|
@@ -504,43 +504,43 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
|
|||||||
assert.Contains(s.T(), result, hostname)
|
assert.Contains(s.T(), result, hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(kradalby): Figure out why these connections are not set up
|
// TODO(juanfont): We have to find out why do we need to wait
|
||||||
// // TODO: See if we can have a more deterministic wait here.
|
time.Sleep(100 * time.Second) // Wait for the nodes to receive updates
|
||||||
// time.Sleep(100 * time.Second)
|
|
||||||
|
|
||||||
// mainIps, err := getIPs(main.tailscales)
|
mainIps, err := getIPs(main.tailscales)
|
||||||
// assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
// sharedIps, err := getIPs(shared.tailscales)
|
sharedIps, err := getIPs(shared.tailscales)
|
||||||
// assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
// for hostname, tailscale := range main.tailscales {
|
for hostname, tailscale := range main.tailscales {
|
||||||
// for peername, ip := range sharedIps {
|
for peername, ip := range sharedIps {
|
||||||
// s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
||||||
// // We currently cant ping ourselves, so skip that.
|
// We currently cant ping ourselves, so skip that.
|
||||||
// if peername != hostname {
|
if peername != hostname {
|
||||||
// // We are only interested in "direct ping" which means what we
|
// We are only interested in "direct ping" which means what we
|
||||||
// // might need a couple of more attempts before reaching the node.
|
// might need a couple of more attempts before reaching the node.
|
||||||
// command := []string{
|
command := []string{
|
||||||
// "tailscale", "ping",
|
"tailscale", "ping",
|
||||||
// "--timeout=1s",
|
"--timeout=15s",
|
||||||
// "--c=20",
|
"--c=20",
|
||||||
// "--until-direct=true",
|
"--until-direct=true",
|
||||||
// ip.String(),
|
ip.String(),
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fmt.Printf("Pinging from %s (%s) to %s (%s)\n", hostname, mainIps[hostname], peername, ip)
|
fmt.Printf("Pinging from %s (%s) to %s (%s)\n", hostname, mainIps[hostname], peername, ip)
|
||||||
// result, err := executeCommand(
|
result, err := executeCommand(
|
||||||
// &tailscale,
|
&tailscale,
|
||||||
// command,
|
command,
|
||||||
// )
|
[]string{},
|
||||||
// assert.Nil(t, err)
|
)
|
||||||
// fmt.Printf("Result for %s: %s\n", hostname, result)
|
assert.Nil(t, err)
|
||||||
// assert.Contains(t, result, "pong")
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
||||||
// }
|
assert.Contains(t, result, "pong")
|
||||||
// })
|
}
|
||||||
// }
|
})
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IntegrationTestSuite) TestTailDrop() {
|
func (s *IntegrationTestSuite) TestTailDrop() {
|
||||||
|
57
machine.go
57
machine.go
@@ -78,13 +78,13 @@ func (h *Headscale) getDirectPeers(m *Machine) (Machines, error) {
|
|||||||
return machines, nil
|
return machines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getShared fetches machines that are shared to the `Namespace` of the machine we are getting peers for
|
||||||
func (h *Headscale) getShared(m *Machine) (Machines, error) {
|
func (h *Headscale) getShared(m *Machine) (Machines, error) {
|
||||||
log.Trace().
|
log.Trace().
|
||||||
Str("func", "getShared").
|
Str("func", "getShared").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
Msg("Finding shared peers")
|
Msg("Finding shared peers")
|
||||||
|
|
||||||
// We fetch here machines that are shared to the `Namespace` of the machine we are getting peers for
|
|
||||||
sharedMachines := []SharedMachine{}
|
sharedMachines := []SharedMachine{}
|
||||||
if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?",
|
if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?",
|
||||||
m.NamespaceID).Find(&sharedMachines).Error; err != nil {
|
m.NamespaceID).Find(&sharedMachines).Error; err != nil {
|
||||||
@@ -105,6 +105,37 @@ func (h *Headscale) getShared(m *Machine) (Machines, error) {
|
|||||||
return peers, nil
|
return peers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getSharedTo fetches the machines of the namespaces this machine is shared in
|
||||||
|
func (h *Headscale) getSharedTo(m *Machine) (Machines, error) {
|
||||||
|
log.Trace().
|
||||||
|
Str("func", "getSharedTo").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Msg("Finding peers in namespaces this machine is shared with")
|
||||||
|
|
||||||
|
sharedMachines := []SharedMachine{}
|
||||||
|
if err := h.db.Preload("Namespace").Preload("Machine").Where("machine_id = ?",
|
||||||
|
m.ID).Find(&sharedMachines).Error; err != nil {
|
||||||
|
return Machines{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
peers := make(Machines, 0)
|
||||||
|
for _, sharedMachine := range sharedMachines {
|
||||||
|
namespaceMachines, err := h.ListMachinesInNamespace(sharedMachine.Namespace.Name)
|
||||||
|
if err != nil {
|
||||||
|
return Machines{}, err
|
||||||
|
}
|
||||||
|
peers = append(peers, *namespaceMachines...)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
||||||
|
|
||||||
|
log.Trace().
|
||||||
|
Str("func", "getSharedTo").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Msgf("Found peers we are shared with: %s", peers.String())
|
||||||
|
return peers, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Headscale) getPeers(m *Machine) (Machines, error) {
|
func (h *Headscale) getPeers(m *Machine) (Machines, error) {
|
||||||
direct, err := h.getDirectPeers(m)
|
direct, err := h.getDirectPeers(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -118,13 +149,24 @@ func (h *Headscale) getPeers(m *Machine) (Machines, error) {
|
|||||||
shared, err := h.getShared(m)
|
shared, err := h.getShared(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Str("func", "getDirectPeers").
|
Str("func", "getShared").
|
||||||
|
Err(err).
|
||||||
|
Msg("Cannot fetch peers")
|
||||||
|
return Machines{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sharedTo, err := h.getSharedTo(m)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Str("func", "sharedTo").
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("Cannot fetch peers")
|
Msg("Cannot fetch peers")
|
||||||
return Machines{}, err
|
return Machines{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
peers := append(direct, shared...)
|
peers := append(direct, shared...)
|
||||||
|
peers = append(peers, sharedTo...)
|
||||||
|
|
||||||
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
||||||
|
|
||||||
log.Trace().
|
log.Trace().
|
||||||
@@ -179,6 +221,11 @@ func (h *Headscale) UpdateMachine(m *Machine) error {
|
|||||||
|
|
||||||
// DeleteMachine softs deletes a Machine from the database
|
// DeleteMachine softs deletes a Machine from the database
|
||||||
func (h *Headscale) DeleteMachine(m *Machine) error {
|
func (h *Headscale) DeleteMachine(m *Machine) error {
|
||||||
|
err := h.RemoveSharedMachineFromAllNamespaces(m)
|
||||||
|
if err != nil && err != errorMachineNotShared {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
m.Registered = false
|
m.Registered = false
|
||||||
namespaceID := m.NamespaceID
|
namespaceID := m.NamespaceID
|
||||||
h.db.Save(&m) // we mark it as unregistered, just in case
|
h.db.Save(&m) // we mark it as unregistered, just in case
|
||||||
@@ -191,10 +238,16 @@ func (h *Headscale) DeleteMachine(m *Machine) error {
|
|||||||
|
|
||||||
// HardDeleteMachine hard deletes a Machine from the database
|
// HardDeleteMachine hard deletes a Machine from the database
|
||||||
func (h *Headscale) HardDeleteMachine(m *Machine) error {
|
func (h *Headscale) HardDeleteMachine(m *Machine) error {
|
||||||
|
err := h.RemoveSharedMachineFromAllNamespaces(m)
|
||||||
|
if err != nil && err != errorMachineNotShared {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
namespaceID := m.NamespaceID
|
namespaceID := m.NamespaceID
|
||||||
if err := h.db.Unscoped().Delete(&m).Error; err != nil {
|
if err := h.db.Unscoped().Delete(&m).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.RequestMapUpdates(namespaceID)
|
return h.RequestMapUpdates(namespaceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -91,7 +91,7 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
machines := []Machine{}
|
machines := []Machine{}
|
||||||
if err := h.db.Preload("AuthKey").Preload("Namespace").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil {
|
if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &machines, nil
|
return &machines, nil
|
||||||
|
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
const errorAuthKeyNotFound = Error("AuthKey not found")
|
const errorAuthKeyNotFound = Error("AuthKey not found")
|
||||||
const errorAuthKeyExpired = Error("AuthKey expired")
|
const errorAuthKeyExpired = Error("AuthKey expired")
|
||||||
const errorAuthKeyNotReusableAlreadyUsed = Error("AuthKey not reusable already used")
|
const errSingleUseAuthKeyHasBeenUsed = Error("AuthKey has already been used")
|
||||||
|
|
||||||
// PreAuthKey describes a pre-authorization key usable in a particular namespace
|
// PreAuthKey describes a pre-authorization key usable in a particular namespace
|
||||||
type PreAuthKey struct {
|
type PreAuthKey struct {
|
||||||
@@ -21,6 +21,7 @@ type PreAuthKey struct {
|
|||||||
Namespace Namespace
|
Namespace Namespace
|
||||||
Reusable bool
|
Reusable bool
|
||||||
Ephemeral bool `gorm:"default:false"`
|
Ephemeral bool `gorm:"default:false"`
|
||||||
|
Used bool `gorm:"default:false"`
|
||||||
|
|
||||||
CreatedAt *time.Time
|
CreatedAt *time.Time
|
||||||
Expiration *time.Time
|
Expiration *time.Time
|
||||||
@@ -110,11 +111,10 @@ func (h *Headscale) checkKeyValidity(k string) (*PreAuthKey, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(machines) != 0 {
|
if len(machines) != 0 || pak.Used {
|
||||||
return nil, errorAuthKeyNotReusableAlreadyUsed
|
return nil, errSingleUseAuthKeyHasBeenUsed
|
||||||
}
|
}
|
||||||
|
|
||||||
// missing here validation on current usage
|
|
||||||
return &pak, nil
|
return &pak, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -87,7 +87,7 @@ func (*Suite) TestAlreadyUsedKey(c *check.C) {
|
|||||||
h.db.Save(&m)
|
h.db.Save(&m)
|
||||||
|
|
||||||
p, err := h.checkKeyValidity(pak.Key)
|
p, err := h.checkKeyValidity(pak.Key)
|
||||||
c.Assert(err, check.Equals, errorAuthKeyNotReusableAlreadyUsed)
|
c.Assert(err, check.Equals, errSingleUseAuthKeyHasBeenUsed)
|
||||||
c.Assert(p, check.IsNil)
|
c.Assert(p, check.IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,3 +180,16 @@ func (*Suite) TestExpirePreauthKey(c *check.C) {
|
|||||||
c.Assert(err, check.Equals, errorAuthKeyExpired)
|
c.Assert(err, check.Equals, errorAuthKeyExpired)
|
||||||
c.Assert(p, check.IsNil)
|
c.Assert(p, check.IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
|
||||||
|
n, err := h.CreateNamespace("test6")
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
pak.Used = true
|
||||||
|
h.db.Save(&pak)
|
||||||
|
|
||||||
|
_, err = h.checkKeyValidity(pak.Key)
|
||||||
|
c.Assert(err, check.Equals, errSingleUseAuthKeyHasBeenUsed)
|
||||||
|
}
|
||||||
|
11
sharing.go
11
sharing.go
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
|||||||
|
|
||||||
const errorSameNamespace = Error("Destination namespace same as origin")
|
const errorSameNamespace = Error("Destination namespace same as origin")
|
||||||
const errorMachineAlreadyShared = Error("Node already shared to this namespace")
|
const errorMachineAlreadyShared = Error("Node already shared to this namespace")
|
||||||
|
const errorMachineNotShared = Error("Machine not shared to this namespace")
|
||||||
|
|
||||||
// SharedMachine is a join table to support sharing nodes between namespaces
|
// SharedMachine is a join table to support sharing nodes between namespaces
|
||||||
type SharedMachine struct {
|
type SharedMachine struct {
|
||||||
@@ -35,3 +36,13 @@ func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveSharedMachineFromAllNamespaces removes a machine as a shared node from all namespaces
|
||||||
|
func (h *Headscale) RemoveSharedMachineFromAllNamespaces(m *Machine) error {
|
||||||
|
sharedMachine := SharedMachine{}
|
||||||
|
if result := h.db.Where("machine_id = ?", m.ID).Unscoped().Delete(&sharedMachine); result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
142
sharing_test.go
142
sharing_test.go
@@ -274,7 +274,7 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
c.Assert(err, check.NotNil)
|
c.Assert(err, check.NotNil)
|
||||||
|
|
||||||
m1 := &Machine{
|
m1 := &Machine{
|
||||||
ID: 0,
|
ID: 1,
|
||||||
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
@@ -291,7 +291,7 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
m2 := &Machine{
|
m2 := &Machine{
|
||||||
ID: 1,
|
ID: 2,
|
||||||
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
@@ -308,7 +308,7 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
m3 := &Machine{
|
m3 := &Machine{
|
||||||
ID: 2,
|
ID: 3,
|
||||||
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
@@ -325,7 +325,7 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
m4 := &Machine{
|
m4 := &Machine{
|
||||||
ID: 3,
|
ID: 4,
|
||||||
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
@@ -341,6 +341,129 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
_, err = h.GetMachine(n1.Name, m4.Name)
|
_, err = h.GetMachine(n1.Name, m4.Name)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
p1s, err := h.getPeers(m1)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(p1s), check.Equals, 1) // node1 can see node4
|
||||||
|
c.Assert(p1s[0].Name, check.Equals, "test_get_shared_nodes_4")
|
||||||
|
|
||||||
|
err = h.AddSharedMachineToNamespace(m2, n1)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
p1sAfter, err := h.getPeers(m1)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(p1sAfter), check.Equals, 2) // node1 can see node2 (shared) and node4 (same namespace)
|
||||||
|
c.Assert(p1sAfter[0].Name, check.Equals, "test_get_shared_nodes_2")
|
||||||
|
c.Assert(p1sAfter[1].Name, check.Equals, "test_get_shared_nodes_4")
|
||||||
|
|
||||||
|
node1shared, err := h.getShared(m1)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(node1shared), check.Equals, 1) // node1 can see node2 as shared
|
||||||
|
c.Assert(node1shared[0].Name, check.Equals, "test_get_shared_nodes_2")
|
||||||
|
|
||||||
|
pAlone, err := h.getPeers(m3)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(pAlone), check.Equals, 0) // node3 is alone
|
||||||
|
|
||||||
|
pSharedTo, err := h.getPeers(m2)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(pSharedTo), check.Equals, 2) // node2 should see node1 (sharedTo) and node4 (sharedTo), as is shared in namespace1
|
||||||
|
c.Assert(pSharedTo[0].Name, check.Equals, "test_get_shared_nodes_1")
|
||||||
|
c.Assert(pSharedTo[1].Name, check.Equals, "test_get_shared_nodes_4")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Suite) TestDeleteSharedMachine(c *check.C) {
|
||||||
|
n1, err := h.CreateNamespace("shared1")
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
n2, err := h.CreateNamespace("shared2")
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
n3, err := h.CreateNamespace("shared3")
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
pak1n1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
pak2n2, err := h.CreatePreAuthKey(n2.Name, false, false, nil)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
pak3n3, err := h.CreatePreAuthKey(n3.Name, false, false, nil)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
pak4n1, err := h.CreatePreAuthKey(n1.Name, false, false, nil)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
_, err = h.GetMachine(n1.Name, "test_get_shared_nodes_1")
|
||||||
|
c.Assert(err, check.NotNil)
|
||||||
|
|
||||||
|
m1 := &Machine{
|
||||||
|
ID: 1,
|
||||||
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
||||||
|
Name: "test_get_shared_nodes_1",
|
||||||
|
NamespaceID: n1.ID,
|
||||||
|
Registered: true,
|
||||||
|
RegisterMethod: "authKey",
|
||||||
|
IPAddress: "100.64.0.1",
|
||||||
|
AuthKeyID: uint(pak1n1.ID),
|
||||||
|
}
|
||||||
|
h.db.Save(m1)
|
||||||
|
|
||||||
|
_, err = h.GetMachine(n1.Name, m1.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
m2 := &Machine{
|
||||||
|
ID: 2,
|
||||||
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
Name: "test_get_shared_nodes_2",
|
||||||
|
NamespaceID: n2.ID,
|
||||||
|
Registered: true,
|
||||||
|
RegisterMethod: "authKey",
|
||||||
|
IPAddress: "100.64.0.2",
|
||||||
|
AuthKeyID: uint(pak2n2.ID),
|
||||||
|
}
|
||||||
|
h.db.Save(m2)
|
||||||
|
|
||||||
|
_, err = h.GetMachine(n2.Name, m2.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
m3 := &Machine{
|
||||||
|
ID: 3,
|
||||||
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
Name: "test_get_shared_nodes_3",
|
||||||
|
NamespaceID: n3.ID,
|
||||||
|
Registered: true,
|
||||||
|
RegisterMethod: "authKey",
|
||||||
|
IPAddress: "100.64.0.3",
|
||||||
|
AuthKeyID: uint(pak3n3.ID),
|
||||||
|
}
|
||||||
|
h.db.Save(m3)
|
||||||
|
|
||||||
|
_, err = h.GetMachine(n3.Name, m3.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
m4 := &Machine{
|
||||||
|
ID: 4,
|
||||||
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
||||||
|
Name: "test_get_shared_nodes_4",
|
||||||
|
NamespaceID: n1.ID,
|
||||||
|
Registered: true,
|
||||||
|
RegisterMethod: "authKey",
|
||||||
|
IPAddress: "100.64.0.4",
|
||||||
|
AuthKeyID: uint(pak4n1.ID),
|
||||||
|
}
|
||||||
|
h.db.Save(m4)
|
||||||
|
|
||||||
|
_, err = h.GetMachine(n1.Name, m4.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
p1s, err := h.getPeers(m1)
|
p1s, err := h.getPeers(m1)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(len(p1s), check.Equals, 1) // nodes 1 and 4
|
c.Assert(len(p1s), check.Equals, 1) // nodes 1 and 4
|
||||||
@@ -363,4 +486,15 @@ func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
|
|||||||
pAlone, err := h.getPeers(m3)
|
pAlone, err := h.getPeers(m3)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(len(pAlone), check.Equals, 0) // node 3 is alone
|
c.Assert(len(pAlone), check.Equals, 0) // node 3 is alone
|
||||||
|
|
||||||
|
sharedMachines, err := h.ListSharedMachinesInNamespace(n1.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(*sharedMachines), check.Equals, 1)
|
||||||
|
|
||||||
|
err = h.DeleteMachine(m2)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
sharedMachines, err = h.ListSharedMachinesInNamespace(n1.Name)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
c.Assert(len(*sharedMachines), check.Equals, 0)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user