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

Fix 764 #2093

Merged
merged 6 commits into from
Sep 3, 2024
Merged

Fix 764 #2093

merged 6 commits into from
Sep 3, 2024

Conversation

mpoindexter
Copy link
Contributor

@mpoindexter mpoindexter commented Aug 31, 2024

When a zero time value is loaded from JSON or a DB in a way that assigns it the local timezone, it does not roudtrip in JSON as a value for which IsZero returns true. This causes KeyExpiry to be treated as a far past value instead of a nilish value.

See golang/go#57040

  • have read the CONTRIBUTING.md file
  • raised a GitHub issue or discussed it on the projects chat beforehand
  • added unit tests
  • added integration tests
  • updated documentation if needed
  • updated CHANGELOG.md

Fixes #764

Summary by CodeRabbit

  • New Features

    • Introduced new test cases to enhance testing for time-sensitive operations and PostgreSQL interactions.
    • Added a function to allow users to specify timezones for container configurations.
    • Enhanced test configurability with new options for flexible testing scenarios.
  • Bug Fixes

    • Resolved issues related to node registration in non-UTC timezones for PostgreSQL databases.
  • Documentation

    • Updated CHANGELOG to reflect recent updates and fixes.

When a zero time value is loaded from JSON or a DB in a way that
assigns it the local timezone, it does not roudtrip in JSON as a
value for which IsZero returns true. This causes KeyExpiry to be
treated as a far past value instead of a nilish value.

See golang/go#57040
@kradalby
Copy link
Collaborator

Thank you, much appreciated, for this change I would need a integration test that first verify the problem, so something that when ran in sqlite/postgres mode returns pass/fail respectively, and then when this change is applied change to pass/pass.

If you have time to look at it would be great, or else I can see what I have time to do next week.

I recon that a fix for this issue also would merit a changelog entry.

@mpoindexter
Copy link
Contributor Author

The unit test I added would fail prior to this change, and now passes. It really actually has nothing to do with the database, but rather the KeyExpiry time object having a non-UTC timezone. Just the postgresql driver being used (pgx) happens to put all times into the local timezone, and the sqllite driver in use does not - but I guess that could change on the part of either driver since the the time represents the same instant of the zero value, so I think the test I added is the appropriate verification of this. If you disagree I can look at adding an integration test. I'll update the PR to add a changelog entry.

@kradalby
Copy link
Collaborator

Integration tests to ensure full roundtrip via both drivers would be necessary. Both for this change and to ensure regressions, we have had libraries introduce silent failures before.

@mpoindexter
Copy link
Contributor Author

I guess it really depends on the timezone the tests run in - the existing integration tests should trip it as long as the timezone the headscale server runs in is non-UTC. I suspect on the integration tests the timezone is set to UTC and hence doesn't happen. I can take a look next week and see how hard it would be to introduce that, I'm not familiar with your integration test setup.

Copy link
Contributor

coderabbitai bot commented Aug 31, 2024

Walkthrough

The changes encompass the addition of new test cases and functions to enhance the testing framework, modifications to timestamp handling for consistency in UTC, and improvements in configuration options for PostgreSQL integration. The updates address a specific bug related to client activation with a PostgreSQL backend while broadening test coverage for time-sensitive operations across different time zones.

Changes

Files Change Summary
.github/workflows/test-integration.yaml Added TestEphemeralInAlternateTimezone to test ephemeral functionality in alternate time zones.
CHANGELOG.md Updated with entries for recent fixes and enhancements, including PostgreSQL issues and test coverage.
hscontrol/mapper/tail.go Modified tailNode function to handle KeyExpiry and Created fields in UTC format.
hscontrol/mapper/tail_test.go Introduced TestNodeExpiry to validate expiry handling in tailNode function.
integration/general_test.go Added TestEphemeralInAlternateTimezone and testEphemeralWithOptions for flexible testing.
integration/hsic/hsic.go Introduced WithTimezone function to set timezone for HeadscaleInContainer.
integration/run.sh Added HEADSCALE_INTEGRATION_POSTGRES environment variable for PostgreSQL configuration.
integration/scenario.go Modified Headscale method to conditionally use PostgreSQL backend based on usePostgresForTest.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Headscale
    participant PostgreSQL

    Client->>Headscale: Authenticate with payload
    Headscale->>PostgreSQL: Query for client data
    PostgreSQL-->>Headscale: Return client data
    Headscale-->>Client: Respond with authentication result
    Client->>Headscale: Send additional payload
    Headscale->>PostgreSQL: Validate key expiry
    PostgreSQL-->>Headscale: Return key expiry status
    Headscale-->>Client: Provide key expiry information
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix client activation with PostgreSQL backend (#764)
Ensure proper handling of time zones in tests
Improve test coverage for PostgreSQL interactions

Poem

🐇
In the meadow where time does play,
New tests hop in to save the day.
With UTC shining bright and clear,
PostgreSQL's woes now disappear!
A leap for bugs, a joyful cheer,
Hoppy changes, let’s all revere!
🌼


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.

@mpoindexter
Copy link
Contributor Author

OK, added an integration test for this, as I suspected it was enough to ensure that the headscale container was in an alternate timezone using existing test scenarios. I added a test method that forces the container into an alternate TZ and runs it that fails without this change. Also found that not all integration tests respected the env variable to use postgresql instead of sqlite, so fixed that as well, and added changelog entries.

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: 3

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1193a50 and 63fb232.

Files selected for processing (8)
  • .github/workflows/test-integration.yaml (1 hunks)
  • CHANGELOG.md (1 hunks)
  • hscontrol/mapper/tail.go (2 hunks)
  • hscontrol/mapper/tail_test.go (2 hunks)
  • integration/general_test.go (2 hunks)
  • integration/hsic/hsic.go (1 hunks)
  • integration/run.sh (1 hunks)
  • integration/scenario.go (2 hunks)
Additional comments not posted (8)
hscontrol/mapper/tail.go (1)

96-96: Approve the conversion of timestamps to UTC.

The changes to convert the KeyExpiry and Created timestamps to UTC are crucial for maintaining consistency and avoiding potential issues related to time zone discrepancies. This adjustment ensures that time-related data is handled robustly and is a best practice for systems that operate across multiple time zones.

Also applies to: 105-105

.github/workflows/test-integration.yaml (1)

48-48: Approve the addition of the new test case.

The inclusion of TestEphemeralInAlternateTimezone in the test matrix is a valuable addition, enhancing the robustness of the testing framework by ensuring that the application behaves correctly across different time zones. This is particularly important for applications that rely on time-sensitive operations.

hscontrol/mapper/tail_test.go (2)

4-4: Approved: Import statement for JSON operations.

The addition of the encoding/json package is necessary for the JSON operations performed in the new test function.


210-273: Approved: New test function TestNodeExpiry.

The function TestNodeExpiry is well-implemented and enhances the test coverage for the expiry logic in node management. It effectively handles various scenarios for the Expiry field of a Node object. Consider adding more test cases if other expiry scenarios are identified in the future.

integration/general_test.go (3)

218-219: Refactor: Utilize new test function for flexibility.

The modification of TestEphemeral to use testEphemeralWithOptions enhances the flexibility of the test by allowing variadic parameters to be passed. This change supports different configurations and is a good use of function abstraction to avoid code duplication.


221-223: New Test Function: Verify timezone handling.

TestEphemeralInAlternateTimezone is a new test function that specifically checks the ephemeral functionality in a different timezone ("America/Los_Angeles"). This is a valuable addition as it tests the system's behavior under timezone variations, ensuring robustness across different environments.


Line range hint 225-238: New Utility Function: Enhance test configurability.

The introduction of testEphemeralWithOptions allows for more dynamic configuration of tests through variadic parameters. This function is used to set up scenarios with specific options, making the tests more adaptable and easier to extend with additional parameters in the future.

Also applies to: 238-238

CHANGELOG.md (1)

73-74: Changelog Update: Document significant fixes and enhancements.

The new entries in the changelog correctly document the fix for registering nodes using preauthkeys with a PostgreSQL database in a non-UTC timezone, addressing issue #764. Additionally, the enhancement to ensure integration tests cover PostgreSQL for all scenarios is well-noted, reflecting a commitment to improve test coverage and reliability.

@kradalby kradalby merged commit 3101f89 into juanfont:main Sep 3, 2024
114 of 115 checks passed
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.

Headscale fails to activate clients with postgresql backend
2 participants