Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2176 Added support a manual approval new nodes #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- TestACLNamedHostsCanReach
- TestACLDevice1CanAccessDevice2
- TestPolicyUpdateWhileRunningWithCLIInDatabase
- TestAuthNodeApproval
- TestOIDCAuthenticationPingAll
- TestOIDCExpireNodesBasedOnTokenExpiry
- TestAuthWebFlowAuthenticationPingAll
Expand All @@ -27,12 +28,14 @@ jobs:
- TestPreAuthKeyCommand
- TestPreAuthKeyCommandWithoutExpiry
- TestPreAuthKeyCommandReusableEphemeral
- TestPreAuthKeyCommandPreApproved
- TestPreAuthKeyCorrectUserLoggedInCommand
- TestApiKeyCommand
- TestNodeTagCommand
- TestNodeAdvertiseTagNoACLCommand
- TestNodeAdvertiseTagWithACLCommand
- TestNodeCommand
- TestNodeApproveCommand
- TestNodeExpireCommand
- TestNodeRenameCommand
- TestNodeMoveCommand
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

- Improved compatibilty of built-in DERP server with clients connecting over WebSocket.
- Allow nodes to use SSH agent forwarding [#2145](https://github.com/juanfont/headscale/pull/2145)

- Added manual approval of nodes in the network

## 0.23.0 (2024-09-18)

Expand Down
53 changes: 53 additions & 0 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func init() {
}
nodeCmd.AddCommand(registerNodeCmd)

approveNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = approveNodeCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(approveNodeCmd)

expireNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = expireNodeCmd.MarkFlagRequired("identifier")
if err != nil {
Expand Down Expand Up @@ -206,6 +213,43 @@ var listNodesCmd = &cobra.Command{
},
}

var approveNodeCmd = &cobra.Command{
Use: "approve",
Short: "Approve a node in your network",
Aliases: []string{"a"},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
identifier, err := cmd.Flags().GetUint64("identifier")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error converting ID to integer: %s", err),
output,
)
return
}
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
defer cancel()
defer conn.Close()
request := &v1.ApproveNodeRequest{
NodeId: identifier,
}
response, err := client.ApproveNode(ctx, request)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Cannot expire node: %s\n",
status.Convert(err).Message(),
),
output,
)
return
}
SuccessOutput(response.GetNode(), "Node approved", output)
},
}

var expireNodeCmd = &cobra.Command{
Use: "expire",
Short: "Expire (log out) a node in your network",
Expand Down Expand Up @@ -529,6 +573,7 @@ func nodesToPtables(
"Ephemeral",
"Last seen",
"Expiration",
"Approved",
"Connected",
"Expired",
}
Expand All @@ -554,6 +599,13 @@ func nodesToPtables(
lastSeenTime = lastSeen.Format("2006-01-02 15:04:05")
}

var approved string
if node.GetApproved() {
approved = pterm.LightGreen("yes")
} else {
approved = pterm.LightRed("no")
}

var expiry time.Time
var expiryTime string
if node.GetExpiry() != nil {
Expand Down Expand Up @@ -642,6 +694,7 @@ func nodesToPtables(
strconv.FormatBool(ephemeral),
lastSeenTime,
expiryTime,
approved,
online,
expired,
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/headscale/cli/preauthkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func init() {
Bool("reusable", false, "Make the preauthkey reusable")
createPreAuthKeyCmd.PersistentFlags().
Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
createPreAuthKeyCmd.PersistentFlags().
Bool("pre-approved", false, "Make the preauthkey with node pre-approval")
createPreAuthKeyCmd.Flags().
StringP("expiration", "e", DefaultPreAuthKeyExpiry, "Human-readable expiration of the key (e.g. 30m, 24h)")
createPreAuthKeyCmd.Flags().
Expand Down Expand Up @@ -91,6 +93,7 @@ var listPreAuthKeys = &cobra.Command{
"Key",
"Reusable",
"Ephemeral",
"Pre-Approved",
"Used",
"Expiration",
"Created",
Expand All @@ -116,6 +119,7 @@ var listPreAuthKeys = &cobra.Command{
key.GetKey(),
strconv.FormatBool(key.GetReusable()),
strconv.FormatBool(key.GetEphemeral()),
strconv.FormatBool(key.GetPreApproved()),
strconv.FormatBool(key.GetUsed()),
expiration,
key.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
Expand Down Expand Up @@ -148,13 +152,15 @@ var createPreAuthKeyCmd = &cobra.Command{

reusable, _ := cmd.Flags().GetBool("reusable")
ephemeral, _ := cmd.Flags().GetBool("ephemeral")
preApproved, _ := cmd.Flags().GetBool("pre-approved")
tags, _ := cmd.Flags().GetStringSlice("tags")

request := &v1.CreatePreAuthKeyRequest{
User: user,
Reusable: reusable,
Ephemeral: ephemeral,
AclTags: tags,
User: user,
Reusable: reusable,
Ephemeral: ephemeral,
PreApproved: preApproved,
AclTags: tags,
}

durationStr, _ := cmd.Flags().GetString("expiration")
Expand Down
1 change: 1 addition & 0 deletions cmd/headscale/headscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ func (*Suite) TestConfigLoading(c *check.C) {
)
c.Assert(viper.GetBool("logtail.enabled"), check.Equals, false)
c.Assert(viper.GetBool("randomize_client_port"), check.Equals, false)
c.Assert(viper.GetBool("node_management.manual_approve_new_node"), check.Equals, false)
}
6 changes: 6 additions & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,9 @@ logtail:
# default static port 41641. This option is intended as a workaround for some buggy
# firewall devices. See https://tailscale.com/kb/1181/firewalls/ for more information.
randomize_client_port: false

# Node management
# See https://tailscale.com/kb/1099/device-approval for more information.
node_management:
# Require new nodes to be approved by admins before they can access the network.
manual_approve_new_node: false
38 changes: 38 additions & 0 deletions docs/node-management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Node management

See https://tailscale.com/kb/1099/device-approval for more information.

## Setup

### 1. Change the configuration

1. Change the `config.yaml` to contain the desired records like so:

```yaml
node_management:
manual_approve_new_node: true
```

2. Restart your headscale instance.

## Usage

### Pre-approve a node using preauthkeys

1. Create preauthkey with a pre-approve option

```bash
headscale preauthkeys create --user=<USER_NAME> --pre-approved
```

2. Register a node on the headscale using preauthkey (with the pre-approval option enabled)

```bash
headscale nodes register --user=<USER_NAME> --mkey=mkey:<MACHINE_KEY> --auth-key=<PREAUTHKEY_PRE_APPROVED>
```

### Approve a node after registration without the pre-authkey pre-approval option

```bash
headscale nodes approve --identifier=<NODE_ID>
```
Loading
Loading