mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Authentication

Open rusher opened this issue 10 months ago • 10 comments

Description

authentication has been revamp. this is a simplification of https://github.com/go-sql-driver/mysql/pull/1694, without new authentication plugin addition, since it's already a big change.

Current implementation was requiring authentication switch plugins data to begin with iAuthMoreData, but that's only required for "caching_sha2_password" and "sha256_password" plugins. Additionally, now permits multi-authentication (in mariadb: https://mariadb.com/kb/en/create-user/#identified-viawith-authentication_plugin / mysql: https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html)

goal is to add MariaDB main authentication plugins (like parsec, PAM, GSSAPI, ...) based on plugin like this

Checklist

  • [x] Code compiles correctly
  • [x] Created tests which fail without the change (if possible)
  • [x] All tests passing
  • [x] Extended the README / documentation, if necessary
  • [x] Added myself / the copyright holder to the AUTHORS file

Summary by CodeRabbit

  • New Features

    • Introduced a pluggable authentication system supporting multiple MySQL and MariaDB authentication methods, including native password, caching_sha2_password, cleartext, old password, sha256_password, and client_ed25519.
    • Added detailed documentation describing the available authentication plugins and their usage.
  • Bug Fixes

    • Improved error handling and authentication flow to prevent infinite loops and ensure immediate error reporting on authentication failure.
  • Refactor

    • Centralized authentication handling behind a unified plugin interface for greater modularity and extensibility.
  • Tests

    • Enhanced and expanded authentication tests, including new scenarios for multi-step authentication processes.

rusher avatar Apr 16 '25 17:04 rusher

Walkthrough

This set of changes introduces a modular authentication plugin system to the Go MySQL driver. The authentication logic is refactored to use a generic plugin interface, allowing multiple authentication methods to be implemented as separate plugins. Each plugin encapsulates its own protocol, including native password, caching SHA2, cleartext, old password, SHA256, and Ed25519 authentication. The driver now maintains a global plugin registry, and authentication flows are managed through this registry and the plugin interface. Extensive cryptographic and protocol-specific logic is moved from the core authentication code into the respective plugins. Tests are updated to use the new plugin-based interface.

Changes

File(s) Change Summary
README.md Added a new section describing the authentication plugin system and listing supported plugins.
auth.go Refactored to remove direct authentication logic; introduced generic plugin-based authentication handling and helper for parsing auth switch data. Removed all legacy cryptographic and plugin-specific code.
auth_plugin.go Introduced the AuthPlugin interface, a simple plugin registry, and global plugin registration functions.
auth_mysql_native.go Added implementation of the mysql_native_password plugin with SHA1-based password scrambling.
auth_caching_sha2.go Added implementation of the caching_sha2_password plugin supporting fast and full authentication with SHA256 and RSA encryption.
auth_cleartext.go Added implementation of the mysql_clear_password plugin for cleartext password authentication.
auth_old_password.go Added implementation of the mysql_old_password plugin using the legacy pre-4.1 password hashing scheme.
auth_sha256.go Added implementation of the sha256_password plugin with RSA encryption and SHA256 hashing.
auth_ed25519.go Added implementation of the client_ed25519 plugin using Ed25519 signatures.
auth_test.go Refactored tests to use plugin instances and the new plugin interface. Added tests for multi-step authentication switch flows.
connector.go Changed authentication to use plugin registry and plugin interface. Removed fallback to default plugin. Introduced authMaximumSwitch constant.
driver.go Added a global plugin registry.
packets.go Removed the readAuthResult method, as authentication result handling is now managed by plugins.
const.go Removed constants related to caching SHA2 authentication states, as this logic is now internal to the plugin.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant Connector
    participant mysqlConn
    participant PluginRegistry
    participant AuthPlugin
    participant Server

    App->>Connector: Connect()
    Connector->>mysqlConn: initiate connection
    mysqlConn->>PluginRegistry: GetPlugin(plugin_name)
    PluginRegistry-->>mysqlConn: AuthPlugin instance
    mysqlConn->>AuthPlugin: InitAuth(authData, config)
    AuthPlugin-->>mysqlConn: auth response
    mysqlConn->>Server: send auth response
    loop (up to max switches)
        Server-->>mysqlConn: auth result / switch request
        alt OK or ERR
            mysqlConn-->>Connector: authentication success/failure
        else Auth Switch
            mysqlConn->>PluginRegistry: GetPlugin(new_plugin_name)
            PluginRegistry-->>mysqlConn: AuthPlugin instance
            mysqlConn->>AuthPlugin: continuationAuth(packet, authData, conn)
            AuthPlugin-->>mysqlConn: next auth response
            mysqlConn->>Server: send next auth response
        end
    end

Suggested reviewers

  • methane

Poem

In the warren of code, plugins now hop,
Each with a secret, a key, or a lock.
Native and cleartext, Ed25519 too,
Old and new passwords, all hopping through.
The registry garden keeps every bun,
Securely authenticating, one by one.
🐇✨ The driver now dances—authentication is fun!

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

coderabbitai[bot] avatar Apr 16 '25 17:04 coderabbitai[bot]

Coverage Status

coverage: 82.772% (-0.2%) from 82.961% when pulling 05b4bcf9960b30ffb842fceac28a41b8697e29e9 on rusher:authentication into 0fd55eb45dae058584cc7a2c80f721dadc991117 on go-sql-driver:master.

coveralls avatar Apr 19 '25 05:04 coveralls

~Make "test stability improvement." PR first.~ I did it by myself.

methane avatar Apr 19 '25 11:04 methane

Maybe, its time to use internal package.

methane avatar Apr 21 '25 09:04 methane

rebase on current master, and corrected the point above

rusher avatar Apr 22 '25 13:04 rusher

rebase on current master, and added https://github.com/go-sql-driver/mysql/pull/1696/commits/fde44b6541b7e93a427aca029662657c383f7969 to ensure public key casting

rusher avatar Apr 25 '25 13:04 rusher

Do you have any thoughts or concerns about when this can be merged?

rusher avatar May 07 '25 08:05 rusher

since this PR affects public API, I need a lot of time for DeepThink. No schedule for now.

methane avatar May 08 '25 13:05 methane

maybe a first step would be to make that not pluggable ( i mean not public ) ?

rusher avatar May 26 '25 13:05 rusher

@methane, how can I help?

grooverdan avatar Jun 18 '25 23:06 grooverdan