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

move logic for validating node names #2127

Merged
merged 2 commits into from
Sep 11, 2024

Conversation

kradalby
Copy link
Collaborator

@kradalby kradalby commented Sep 11, 2024

this commits moves the generation of "given names" of nodes into the registration function, and adds validation of renames to RenameNode using the same logic.

Fixes #2121

Summary by CodeRabbit

  • New Features

    • Enhanced node registration with checks for unique identifiers, improving data integrity.
    • Simplified node creation process by removing the GivenName field.
  • Bug Fixes

    • Ensured that node renaming adheres to uniqueness constraints, preventing conflicts.
  • Tests

    • Introduced new tests for renaming nodes to validate uniqueness and conflict resolution.
    • Removed outdated tests related to the generation of given names.

this commits moves the generation of "given names" of nodes
into the registration function, and adds validation of renames
to RenameNode using the same logic.

Fixes juanfont#2121

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
@kradalby kradalby requested a review from juanfont as a code owner September 11, 2024 12:45
Copy link
Contributor

coderabbitai bot commented Sep 11, 2024

Walkthrough

The changes involve the removal of the GivenName field from the Node struct and the associated logic in the handleRegister and handleAuthKey functions. The listNodesByGivenName function has been eliminated, and the RenameNode function has been updated for type safety. New functions for ensuring unique node names have been introduced, and tests have been adjusted to reflect these changes.

Changes

Files Change Summary
hscontrol/auth.go, hscontrol/db/node.go, hscontrol/grpcv1.go Removed GivenName from node creation logic; eliminated listNodesByGivenName; updated RenameNode for type safety; introduced functions for unique name validation.
hscontrol/db/node_test.go Removed TestGenerateGivenName; added TestRenameNode to validate renaming functionality and uniqueness constraints.

Assessment against linked issues

Objective Addressed Explanation
Ensure unique identification for nodes (e.g., #2121)
Prevent conflicts when renaming nodes (e.g., #2121)
Maintain consistent behaviour in node resolution (e.g., #2121) Changes may affect how nodes are identified.

Poem

In the burrow deep, where changes bloom,
Nodes now dance without the gloom.
Given names have hopped away,
Unique and safe, they’re here to stay.
A rabbit's cheer for code so bright,
Hopping forth into the light! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
hscontrol/grpcv1.go (1)

Impact of Removing GivenName Assignment:

The GivenName field is referenced in multiple parts of the codebase, including tests and CLI operations. Its removal from the DebugCreateNode function could potentially break tests or affect CLI functionality. The presence of tests that assert the GivenName suggests that it is an expected part of the node's attributes and likely used for identification or display purposes.

  • Integration Tests: The GivenName is used in assertions in integration/cli_test.go.
  • Node Tests: The GivenName is referenced in hscontrol/types/node_test.go.
  • CLI Commands: The GivenName is used in cmd/headscale/cli/routes.go and cmd/headscale/cli/nodes.go.

Please ensure that removing the GivenName assignment does not break any functionality or negatively impact the user experience. Consider reviewing the tests and CLI commands that rely on GivenName to confirm that they still function correctly.

Analysis chain

Line range hint 1-1000: Verify the impact of removing the GivenName assignment.

The removal of the givenName variable and its assignment suggests a simplification of the node creation process. However, this change could lead to a loss of functionality if the GivenName was critical for node identification or user interaction.

Please verify that removing the GivenName assignment does not break any functionality or negatively impact the user experience. Consider running the following script to search for any references to GivenName in the codebase:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for references to `GivenName` in the codebase.

# Test: Search for `GivenName` in Go files. 
# Expect: No critical references to `GivenName`.
rg --type go 'GivenName'

Length of output: 6112

hscontrol/db/node.go (2)

649-656: LGTM, but fix the typo in the function name.

The new isUnqiueName function looks good:

  • The function correctly queries the database to check for uniqueness.
  • The function correctly returns true if no nodes are found and false otherwise.
  • The function correctly handles the error returned by the database query.

However, there is a typo in the function name. Apply this diff to fix it:

-func isUnqiueName(tx *gorm.DB, name string) (bool, error) {
+func isUniqueName(tx *gorm.DB, name string) (bool, error) {

Line range hint 659-681: LGTM, but update the function to use the fixed function name.

The new ensureUniqueGivenName function looks good:

  • The function correctly generates a unique name for a node.
  • The function correctly handles the error returned by generateGivenName.
  • The function correctly handles the error returned by isUnqiueName.
  • The function correctly returns the generated name.

However, the function should be updated to use the fixed function name isUniqueName. Apply this diff to fix it:

-	unique, err := isUnqiueName(tx, givenName)
+	unique, err := isUniqueName(tx, givenName)
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 64319f7 and 225f8d4.

Files selected for processing (4)
  • hscontrol/auth.go (3 hunks)
  • hscontrol/db/node.go (5 hunks)
  • hscontrol/db/node_test.go (3 hunks)
  • hscontrol/grpcv1.go (2 hunks)
Additional comments not posted (6)
hscontrol/auth.go (1)

69-69: LGTM!

The change to set up logging for the handleRegister function using the logAuthFunc helper function is approved.

hscontrol/grpcv1.go (1)

376-376: LGTM!

The change to wrap request.GetNodeId() with types.NodeID() is a good move towards a more type-safe approach. It enhances clarity and ensures that the function receives the correct type, potentially reducing runtime errors related to type mismatches.

hscontrol/db/node.go (2)

Line range hint 231-250: LGTM!

The changes to the RenameNode function look good:

  • The function signature update to use types.NodeID enhances type safety and clarity.
  • The new uniqueness check for the node name is a necessary validation to maintain data integrity.
  • The function correctly returns an error if the new name is not unique.

413-420: LGTM!

The changes to the RegisterNode function look good:

  • The new check to ensure that the node has a unique GivenName proactively maintains data integrity upon registration.
  • The function correctly handles the error returned by ensureUniqueGivenName.
hscontrol/db/node_test.go (2)

Line range hint 1-1: LGTM!

The removal of the TODO comment is approved, as it indicates that the previously missing tests have been added.


738-833: Brilliant test for the node renaming functionality!

The TestRenameNode function is a comprehensive test that covers all the important scenarios for node renaming:

  • Registering nodes with the same hostname for different users and verifying that they are assigned unique given names.
  • Renaming a node to a unique name.
  • Reusing a name that is no longer in use.
  • Attempting to rename a node to a name that is currently in use and expecting an error.

The test is well-structured, uses clear assertions to verify the expected behavior, and leverages database transactions to maintain data integrity during the rename operations.

Excellent work on this test! It provides great coverage for the node renaming functionality.

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (2)
hscontrol/db/node.go (2)

649-656: LGTM, but fix the typo in the function name.

The isUnqiueName function is correctly implemented and returns the expected result.

However, the function name has a typo:

-func isUnqiueName(tx *gorm.DB, name string) (bool, error) {
+func isUniqueName(tx *gorm.DB, name string) (bool, error) {

Please fix the typo in the function name and update all references to it.


Line range hint 659-681: LGTM, but update the function to use the corrected name isUniqueName.

The refactoring of ensureUniqueGivenName is approved:

  • The refactoring improves code readability and maintainability by separating the concerns of generating a name and checking its uniqueness.
  • The function correctly handles the error returned by generateGivenName and isUnqiueName.
  • The function ensures that a unique name is generated by appending a random suffix if necessary.

However, please update the function to use the corrected name isUniqueName:

-	unique, err := isUnqiueName(tx, givenName)
+	unique, err := isUniqueName(tx, givenName)
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 225f8d4 and d8c0432.

Files selected for processing (1)
  • hscontrol/db/node.go (5 hunks)
Additional comments not posted (3)
hscontrol/db/node.go (3)

Line range hint 231-250: LGTM!

The changes to the RenameNode function are approved:

  • The nodeID parameter type has been updated to types.NodeID, which enhances type safety and clarity.
  • The function now includes a check for the uniqueness of the new name by calling isUnqiueName, which is a necessary validation to maintain data integrity.
  • The function correctly returns an error if the new name is not unique.

413-420: LGTM!

The changes to the RegisterNode function are approved:

  • The function now includes a check to ensure that if the GivenName field of a node is empty, it will be populated by calling ensureUniqueGivenName.
  • This change proactively ensures that a node has a unique given name upon registration, which enhances data integrity.
  • The function correctly handles the error returned by ensureUniqueGivenName.

Line range hint 1-1: Verify the impact of removing listNodesByGivenName.

The removal of listNodesByGivenName is a significant change that affects how nodes are queried by their given names.

To ensure that this change does not introduce any issues, please verify that the functionality of listNodesByGivenName has been correctly replaced or refactored elsewhere in the codebase.

Run the following script to search for potential usages of listNodesByGivenName in the codebase:

If the script returns any occurrences of listNodesByGivenName, please investigate and ensure that its functionality has been correctly replaced or refactored.

@kradalby kradalby merged commit 064c46f into juanfont:main Sep 11, 2024
118 of 119 checks passed
@kradalby kradalby deleted the kradalby/2121-move-given-name-logic branch September 11, 2024 16:27
@coderabbitai coderabbitai bot mentioned this pull request Sep 18, 2024
kradalby added a commit to kradalby/headscale that referenced this pull request Sep 30, 2024
* move logic for validating node names

this commits moves the generation of "given names" of nodes
into the registration function, and adds validation of renames
to RenameNode using the same logic.

Fixes juanfont#2121

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

* fix double arg

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>

---------

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Different A record for nodes with the same name
2 participants