Skip to content

Commit

Permalink
Add ExtendNodeExpiration functionality to gRPC and CLI
Browse files Browse the repository at this point in the history
Implement ExtendNodeExpiration RPC, allowing users to extend a node's expiration. Update the CLI to include the `extend-expiration` command with required fields. Validate inputs, update the database, and notify peers of expiration changes.
  • Loading branch information
FlorinPeter committed Mar 4, 2025
1 parent b6fbd37 commit c73ba08
Show file tree
Hide file tree
Showing 14 changed files with 2,289 additions and 890 deletions.
61 changes: 61 additions & 0 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"tailscale.com/types/key"
)

Expand Down Expand Up @@ -57,6 +58,18 @@ func init() {
}
nodeCmd.AddCommand(expireNodeCmd)

extendNodeExpirationCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = extendNodeExpirationCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatal(err.Error())
}
extendNodeExpirationCmd.Flags().StringP("new-expiry", "e", "", "New expiration time in RFC3339 format, e.g., 2024-01-01T15:04:05Z")
err = extendNodeExpirationCmd.MarkFlagRequired("new-expiry")
if err != nil {
log.Fatal(err.Error())
}
nodeCmd.AddCommand(extendNodeExpirationCmd)

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

var extendNodeExpirationCmd = &cobra.Command{
Use: "extend-expiration",
Short: "Extends the expiration of a node by setting a new expiration time",
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
nodeID, err := cmd.Flags().GetUint64("identifier")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting identifier from flag: %s", err), output)
}

newExpiryStr, err := cmd.Flags().GetString("new-expiry")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting new-expiry from flag: %s", err), output)
}

// Parse the new-expiry timestamp from string to time.Time
newExpiry, err := time.Parse(time.RFC3339, newExpiryStr)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Invalid expiration time format: must be in RFC3339 format, %s", err), output)
}

// Set up context and gRPC client
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
defer cancel()
defer conn.Close()

// Build the gRPC request
request := &v1.ExtendNodeExpirationRequest{
NodeId: nodeID,
NewExpiration: timestamppb.New(newExpiry), // convert time.Time to protobuf timestamp
}

// Make the gRPC call
response, err := client.ExtendNodeExpiration(ctx, request)
if err != nil {
st, ok := status.FromError(err)
if ok {
ErrorOutput(st.Err(), st.Message(), output)
} else {
ErrorOutput(err, "Unexpected error during ExtendNodeExpiration RPC call", output)
}
}

// Print the result
SuccessOutput(response, "Node expiration extended successfully", output)
},
}

var renameNodeCmd = &cobra.Command{
Use: "rename NEW_NAME",
Short: "Renames a node in your network",
Expand Down
Loading

0 comments on commit c73ba08

Please sign in to comment.