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

feat(crypto): v2 volume encryption support #424

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

mantissahz
Copy link
Contributor

Which issue(s) this PR fixes:

Issue # longhorn/longhorn#7355

What this PR does / why we need it:

Special notes for your reviewer:

Additional documentation or context

@mantissahz mantissahz self-assigned this Jan 10, 2025
Copy link

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request introduces a new dataEngine parameter across multiple components of the system to support volume management with different data engine types. This change involves updating function signatures, adding a new field to the Volume struct, and modifying device path generation logic. The primary goal is to enhance volume handling by allowing specification of data engine types (v1 and v2), with a focus on improving encrypted volume management and device path resolution.

Changes

File Change Summary
app/cmd/server.go - Added dataEngine CLI flag as a required string
- Updated Volume struct to include DataEngine field
pkg/crypto/crypto.go - Modified OpenVolume, CloseVolume, and ResizeEncryptoDevice functions to accept dataEngine parameter
- Updated function signatures and logging
pkg/rpc/server.go - Updated FilesystemTrim, FilesystemResize, and Mount methods to use vol.DataEngine in device path generation
pkg/server/share_manager.go - Modified device path retrieval methods to include dataEngine
- Updated crypto.OpenVolume and crypto.CloseVolume calls
pkg/types/types.go - Added constants for volume suffixes and data engine types
- Created new GetEncryptVolumeName function
- Modified GetVolumeDevicePath to support different data engines
pkg/volume/volume.go - Added DataEngine string field to Volume struct

Sequence Diagram

sequenceDiagram
    participant CLI as Command Line Interface
    participant Server as Server Command
    participant Volume as Volume Manager
    participant Crypto as Crypto Service
    participant RPC as RPC Server

    CLI->>Server: Specify dataEngine flag
    Server->>Volume: Create Volume with DataEngine
    Volume-->>Crypto: Open Volume with DataEngine
    Crypto-->>RPC: Perform Volume Operations
    RPC->>Volume: Generate Device Path
    Volume-->>Crypto: Close Volume with DataEngine
Loading

The sequence diagram illustrates the flow of data engine specification from CLI through volume management, crypto operations, and RPC server interactions, highlighting the new dataEngine parameter's role in volume handling.

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, 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. (Beta)
  • @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

@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

🧹 Nitpick comments (7)
pkg/types/types.go (2)

20-21: Consider using an enum-like type for DataEngine values

To prevent potential typos and invalid values, consider defining a custom type with constants:

-DataEngineTypeV1 = "v1"
-DataEngineTypeV2 = "v2"
+type DataEngineType string
+
+const (
+    DataEngineTypeV1 DataEngineType = "v1"
+    DataEngineTypeV2 DataEngineType = "v2"
+)

31-31: Follow Go naming conventions for parameters

The parameter EncryptedDevice should be encryptedDevice to follow Go naming conventions for function parameters.

pkg/volume/volume.go (1)

14-14: Add documentation for DataEngine field

Please add a comment explaining the valid values and purpose of this field.

+    // DataEngine specifies the volume data engine type (v1 or v2)
     DataEngine      string
pkg/crypto/crypto.go (1)

67-72: Improve error message clarity

The error message could be more descriptive about the actual issue.

-        return fmt.Errorf("volume %v encrypto device %s is closed for resizing", volume, encryptedDevPath)
+        return fmt.Errorf("cannot resize volume %v: encrypted device %s is not open", volume, encryptedDevPath)
pkg/server/share_manager.go (3)

204-206: Consider enhancing error handling for crypto operations

While the v2 volume support integration is correct, consider wrapping the crypto-related errors with more context to aid in debugging:

 cryptoDevice := types.GetVolumeDevicePath(vol.Name, vol.DataEngine, true)
 m.logger.Infof("Volume %s requires crypto device %s", vol.Name, cryptoDevice)
-if err := crypto.OpenVolume(vol.Name, vol.DataEngine, devicePath, vol.Passphrase); err != nil {
-    m.logger.WithError(err).Error("Failed to open encrypted volume")
-    return "", err
+if err := crypto.OpenVolume(vol.Name, vol.DataEngine, devicePath, vol.Passphrase); err != nil {
+    return "", errors.Wrapf(err, "failed to open encrypted volume %v with data engine %v", vol.Name, vol.DataEngine)
 }

220-225: Consider enhancing error handling for crypto device teardown

While the v2 volume support integration is correct, consider wrapping the crypto-related errors with more context:

 cryptoDevice := types.GetVolumeDevicePath(vol.Name, vol.DataEngine, true)
 if isOpen, err := crypto.IsDeviceOpen(cryptoDevice); err != nil {
-    return err
+    return errors.Wrapf(err, "failed to check if crypto device %v is open", cryptoDevice)
 } else if isOpen {
     m.logger.Infof("Volume %s has active crypto device %s", vol.Name, cryptoDevice)
     if err := crypto.CloseVolume(vol.Name, vol.DataEngine); err != nil {
-        return err
+        return errors.Wrapf(err, "failed to close crypto device %v with data engine %v", cryptoDevice, vol.DataEngine)
     }
     m.logger.Infof("Volume %s closed active crypto device %s", vol.Name, cryptoDevice)
 }

Line range hint 204-225: Architectural feedback on v2 volume encryption support

The integration of v2 volume support is well-structured and maintains a clear separation of concerns. The changes consistently handle the data engine parameter across all relevant operations (setup, teardown, and device path resolution).

Consider documenting the differences between v1 and v2 volume encryption in the code comments to help future maintainers understand the architectural decisions.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 17e95bc and 4af7e61.

📒 Files selected for processing (6)
  • app/cmd/server.go (2 hunks)
  • pkg/crypto/crypto.go (3 hunks)
  • pkg/rpc/server.go (4 hunks)
  • pkg/server/share_manager.go (3 hunks)
  • pkg/types/types.go (1 hunks)
  • pkg/volume/volume.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build binaries
  • GitHub Check: Summary
🔇 Additional comments (4)
pkg/rpc/server.go (3)

72-72: LGTM: Device path handling updated for v2 volume support

The addition of vol.DataEngine parameter is consistent with the v2 volume encryption support objective.


137-137: LGTM: Filesystem resize handling updated for v2 volume support

The changes properly integrate v2 volume support in both device path retrieval and crypto device resizing operations.

Also applies to: 164-164


312-312: LGTM: Mount operation updated for v2 volume support

The device path retrieval has been properly updated to support v2 volumes.

pkg/server/share_manager.go (1)

106-106: LGTM: Run method updated for v2 volume support

The device path retrieval has been properly updated to support v2 volumes.

pkg/types/types.go Show resolved Hide resolved
app/cmd/server.go Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
app/cmd/server.go Outdated Show resolved Hide resolved
Copy link

@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: 1

🧹 Nitpick comments (2)
pkg/crypto/crypto.go (2)

32-33: Enhance parameter documentation.

The function documentation should describe all parameters. Add documentation for volume, dataEngine, and passphrase parameters.

 // OpenVolume opens volume so that it can be used by the client.
 // attachingVolumeHostPath is the path of the volume on the host that will be opened.
+// volume is the unique identifier of the volume
+// dataEngine specifies the data engine type (v1 or v2)
+// passphrase is the encryption key for the LUKS device

73-73: Enhance error message clarity.

Consider including the data engine type in the error message for better debugging context.

-		return fmt.Errorf("volume %v encrypto device %s is closed for resizing", volume, encryptedDevPath)
+		return fmt.Errorf("volume %v encrypto device %s (data engine: %s) is closed for resizing", volume, encryptedDevPath, dataEngine)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4af7e61 and ffe68ae.

📒 Files selected for processing (6)
  • app/cmd/server.go (5 hunks)
  • pkg/crypto/crypto.go (3 hunks)
  • pkg/rpc/server.go (4 hunks)
  • pkg/server/share_manager.go (3 hunks)
  • pkg/types/types.go (1 hunks)
  • pkg/volume/volume.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • pkg/volume/volume.go
  • app/cmd/server.go
  • pkg/types/types.go
  • pkg/server/share_manager.go
  • pkg/rpc/server.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build binaries
  • GitHub Check: Summary
🔇 Additional comments (1)
pkg/crypto/crypto.go (1)

55-64: LGTM! Clean implementation of CloseVolume.

The changes to include the dataEngine parameter are consistent with the overall design, and the implementation maintains proper error handling.

pkg/crypto/crypto.go Outdated Show resolved Hide resolved
Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

In general, LGTM. Only the naming issue.

pkg/crypto/crypto.go Outdated Show resolved Hide resolved
app/cmd/server.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
pkg/crypto/crypto.go Outdated Show resolved Hide resolved
ref: longhorn/longhorn 7355

Signed-off-by: James Lu <jamesluhz@gmail.com>
Copy link

@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

🧹 Nitpick comments (4)
pkg/types/types.go (1)

24-29: Consider adding validation for dataEngine parameter.

The function logic is correct and aligns with the default behavior. However, consider adding validation for the dataEngine parameter to prevent runtime errors with invalid values.

 func GetEncryptVolumeName(volume, dataEngine string) string {
+    if dataEngine != DataEngineTypeV1 && dataEngine != DataEngineTypeV2 {
+        logrus.Warnf("Invalid data engine type: %s, defaulting to v1", dataEngine)
+        return volume
+    }
     if dataEngine == DataEngineTypeV2 {
         return volume + MapperV2VolumeSuffix
     }
     return volume
 }
pkg/crypto/crypto.go (3)

32-33: Document the dataEngine parameter.

The comment explains the devicePath parameter but should also document the dataEngine parameter and its expected values (v1/v2).

 // OpenVolume opens volume so that it can be used by the client.
 // devicePath is the path of the volume on the host that will be opened for instance '/dev/longhorn/volume1'
+// dataEngine specifies the volume data engine type (v1 or v2)

56-56: Add function documentation.

Add documentation for the function parameters to improve code maintainability.

+// CloseVolume closes encrypted volume so it can be detached.
+// volume is the name of the volume to close
+// dataEngine specifies the volume data engine type (v1 or v2)
 func CloseVolume(volume, dataEngine string) error {

75-75: Fix typo in error message.

"encrypto" should be "encrypted" in the error message.

-        return fmt.Errorf("volume %v encrypto device %s is closed for resizing", volume, devPath)
+        return fmt.Errorf("volume %v encrypted device %s is closed for resizing", volume, devPath)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 890c44b and 9aa843a.

📒 Files selected for processing (6)
  • app/cmd/server.go (5 hunks)
  • pkg/crypto/crypto.go (3 hunks)
  • pkg/rpc/server.go (4 hunks)
  • pkg/server/share_manager.go (3 hunks)
  • pkg/types/types.go (1 hunks)
  • pkg/volume/volume.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • pkg/volume/volume.go
  • app/cmd/server.go
  • pkg/server/share_manager.go
  • pkg/rpc/server.go
🧰 Additional context used
📓 Learnings (1)
pkg/types/types.go (1)
Learnt from: mantissahz
PR: longhorn/longhorn-share-manager#424
File: pkg/types/types.go:24-29
Timestamp: 2025-01-10T08:08:22.207Z
Learning: In the Longhorn Share Manager, the `dataEngine` parameter defaults to `v1`. This is reflected in the `GetEncryptVolumeName` function's behavior where any non-v2 data engine type returns the volume name without modification.
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
pkg/types/types.go (2)

15-21: LGTM! Well-documented constants.

The new constants are clearly documented and follow Go naming conventions.


31-37: LGTM! Well-documented device path handling.

The function clearly handles different device path scenarios with good documentation explaining the v2 volume behavior.

Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

In general, LGTM.

  • Please manually test the encrypted RWX volume attachment and detachment and update the result here.
  • Help create a ticket for moving the common functions to go-common-libs.

Thank you.

@derekbit derekbit merged commit 2929c79 into longhorn:master Jan 10, 2025
9 checks passed
@derekbit
Copy link
Member

@mergify backport v1.8.x

Copy link

mergify bot commented Jan 10, 2025

backport v1.8.x

✅ Backports have been created

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.

2 participants