mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-01 13:46:49 +02:00
Preauthkey can assign machine to subnet
This commit is contained in:
parent
e9ea698130
commit
137b327795
23
api.go
23
api.go
@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"inet.af/netaddr"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/klauspost/compress/zstd"
|
"github.com/klauspost/compress/zstd"
|
||||||
@ -342,7 +343,27 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key,
|
|||||||
Str("func", "handleAuthKey").
|
Str("func", "handleAuthKey").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
Msg("Authentication key was valid, proceeding to acquire an IP address")
|
Msg("Authentication key was valid, proceeding to acquire an IP address")
|
||||||
ip, err := h.getAvailableIP()
|
|
||||||
|
var prefix netaddr.IPPrefix
|
||||||
|
var prefixErr error
|
||||||
|
if pak.Subnet != "" {
|
||||||
|
prefix, prefixErr = netaddr.ParseIPPrefix(pak.Subnet)
|
||||||
|
|
||||||
|
if prefixErr != nil {
|
||||||
|
log.Debug().
|
||||||
|
Str("func", "handleAuthKey").
|
||||||
|
Str("machine", m.Name).
|
||||||
|
Msg("Subnet was not valid, using default")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ip *netaddr.IP
|
||||||
|
if pak.Subnet != "" && prefixErr == nil {
|
||||||
|
ip, err = h.getAvailableIPForPrefix(prefix)
|
||||||
|
} else {
|
||||||
|
ip, err = h.getAvailableIP()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Str("func", "handleAuthKey").
|
Str("func", "handleAuthKey").
|
||||||
|
@ -25,6 +25,7 @@ func init() {
|
|||||||
createPreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
createPreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
||||||
createPreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
|
createPreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
|
||||||
createPreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
createPreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
||||||
|
createPreAuthKeyCmd.Flags().StringP("subnet", "s", "", "Subnet to assign new nodes to")
|
||||||
}
|
}
|
||||||
|
|
||||||
var preauthkeysCmd = &cobra.Command{
|
var preauthkeysCmd = &cobra.Command{
|
||||||
@ -116,7 +117,8 @@ var createPreAuthKeyCmd = &cobra.Command{
|
|||||||
expiration = &exp
|
expiration = &exp
|
||||||
}
|
}
|
||||||
|
|
||||||
k, err := h.CreatePreAuthKey(n, reusable, ephemeral, expiration)
|
subnet, _ := cmd.Flags().GetString("subnet")
|
||||||
|
k, err := h.CreatePreAuthKeyWithSubnet(n, reusable, ephemeral, expiration, subnet)
|
||||||
if strings.HasPrefix(o, "json") {
|
if strings.HasPrefix(o, "json") {
|
||||||
JsonOutput(k, err, o)
|
JsonOutput(k, err, o)
|
||||||
return
|
return
|
||||||
|
@ -21,13 +21,19 @@ type PreAuthKey struct {
|
|||||||
Namespace Namespace
|
Namespace Namespace
|
||||||
Reusable bool
|
Reusable bool
|
||||||
Ephemeral bool `gorm:"default:false"`
|
Ephemeral bool `gorm:"default:false"`
|
||||||
|
Subnet string `gorm:"default:''"`
|
||||||
|
|
||||||
CreatedAt *time.Time
|
CreatedAt *time.Time
|
||||||
Expiration *time.Time
|
Expiration *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreatePreAuthKey creates a new PreAuthKey in a namespace, and returns it
|
// CreatePreAuthKey creates a new PreAuthKey in a namespace for the default subnet, and returns it
|
||||||
func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, ephemeral bool, expiration *time.Time) (*PreAuthKey, error) {
|
func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, ephemeral bool, expiration *time.Time) (*PreAuthKey, error) {
|
||||||
|
return h.CreatePreAuthKeyWithSubnet(namespaceName, reusable, ephemeral, expiration, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatePreAuthKey creates a new PreAuthKey in a namespace with a subnet, and returns it
|
||||||
|
func (h *Headscale) CreatePreAuthKeyWithSubnet(namespaceName string, reusable bool, ephemeral bool, expiration *time.Time, subnet string) (*PreAuthKey, error) {
|
||||||
n, err := h.GetNamespace(namespaceName)
|
n, err := h.GetNamespace(namespaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -47,6 +53,7 @@ func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, epheme
|
|||||||
Ephemeral: ephemeral,
|
Ephemeral: ephemeral,
|
||||||
CreatedAt: &now,
|
CreatedAt: &now,
|
||||||
Expiration: expiration,
|
Expiration: expiration,
|
||||||
|
Subnet: subnet,
|
||||||
}
|
}
|
||||||
h.db.Save(&k)
|
h.db.Save(&k)
|
||||||
|
|
||||||
@ -55,6 +62,7 @@ func (h *Headscale) CreatePreAuthKey(namespaceName string, reusable bool, epheme
|
|||||||
|
|
||||||
// GetPreAuthKeys returns the list of PreAuthKeys for a namespace
|
// GetPreAuthKeys returns the list of PreAuthKeys for a namespace
|
||||||
func (h *Headscale) GetPreAuthKeys(namespaceName string) (*[]PreAuthKey, error) {
|
func (h *Headscale) GetPreAuthKeys(namespaceName string) (*[]PreAuthKey, error) {
|
||||||
|
h.getAvailableIP() // temp
|
||||||
n, err := h.GetNamespace(namespaceName)
|
n, err := h.GetNamespace(namespaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
4
utils.go
4
utils.go
@ -75,8 +75,10 @@ func encodeMsg(b []byte, pubKey *wgkey.Key, privKey *wgkey.Private) ([]byte, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Headscale) getAvailableIP() (*netaddr.IP, error) {
|
func (h *Headscale) getAvailableIP() (*netaddr.IP, error) {
|
||||||
ipPrefix := h.cfg.IPPrefix
|
return h.getAvailableIPForPrefix(h.cfg.IPPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) getAvailableIPForPrefix(ipPrefix netaddr.IPPrefix) (*netaddr.IP, error) {
|
||||||
usedIps, err := h.getUsedIPs()
|
usedIps, err := h.getUsedIPs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user