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: update codis fe / proxy with tls; topom updated #2383

Open
wants to merge 18 commits into
base: unstable
Choose a base branch
from

Conversation

kolinfluence
Copy link

codis fe / proxy tls + topom update

@github-actions github-actions bot added 🧹 Updates This will not be worked on Invalid PR Title labels Feb 1, 2024
@kolinfluence kolinfluence changed the title codis fe / proxy tls + topom update feat: update codis fe / proxy with tls; topom updated Feb 2, 2024
@sprappcom
Copy link

@AlexStocks please look into it.

codis/config/dashboard.toml Outdated Show resolved Hide resolved
codis/pkg/proxy/.proxy.go.swo Outdated Show resolved Hide resolved
codis/admin/.codis-fe-admin.sh.swo Outdated Show resolved Hide resolved
codis/cert.pem Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better not to provide the public key file. Instead, you can add a generation command in the readme.

Copy link
Author

Choose a reason for hiding this comment

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

@machinly the public key file is definitely a dummy one.
you put "filesystem" as default, surely a dummy version is fine right? ... maybe you can modify with error message generated when there's no cert / key files present after you merge this and delete them. will that do fine?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not modify the default configuration

Copy link
Author

Choose a reason for hiding this comment

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

can you only pull what is needed?

codis/key.pem Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better not to provide the private key file. Instead, you can add a generation command in the readme.

jodis_name = ""
jodis_addr = ""
jodis_name = "etcd"
jodis_addr = "127.0.0.1:2379"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not modify the default parameter values

Copy link
Author

Choose a reason for hiding this comment

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

can you only pull what is needed?

@@ -25,8 +25,10 @@ const DefaultConfig = `
# Set Coordinator, only accept "zookeeper" & "etcd" & "filesystem".
# for zookeeper/etcd, coorinator_auth accept "user:password"
# Quick Start
coordinator_name = "filesystem"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not modify the default parameter values

Copy link
Author

Choose a reason for hiding this comment

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

can you only pull what is needed?

codis/pkg/models/store.go Outdated Show resolved Hide resolved
# slow command list e.g. hgetall, mset
slow_cmd_list = ""
# quick command list
quick_cmd_list = "get,set"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not modify the default configuration

CODIS_FE_ADDR="0.0.0.0:9090"
CODIS_FE_TLS=1
CODIS_FE_TLS_KEY="/usr/local/src/pika/codis/key.pem"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment this code

Copy link
Author

Choose a reason for hiding this comment

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

Can you help add the rest?

@kolinfluence
Copy link
Author

noted. will revert.

@github-actions github-actions bot added the ✏️ Feature New feature or request label Mar 14, 2024
@kolinfluence
Copy link
Author

@machinly pls check again, i've done all. sorry for the issues.
for code generation, do provide what examples you think is good. for the cert is just dummy test like your "filesystem" thing.

@luky116
Copy link
Collaborator

luky116 commented Apr 21, 2024

codis-fe cannot access, have you tried in you local env?
image

@luky116
Copy link
Collaborator

luky116 commented Apr 21, 2024

proxy cannot connect?

image

@luky116
Copy link
Collaborator

luky116 commented Apr 21, 2024

Judging from your code, you want to add the TLS protocol to the redis protocol port of the proxy layer. If so, can dashboard and fe not add TLS? Because they interact through the http protocol, the proxy's redis protocol port is only responsible for the user's redis client connection.

@kolinfluence
Copy link
Author

kolinfluence commented Apr 21, 2024

@luky116 sorry was busy playing with lru benchmark tests update here so a bit lagged behind on pika (but it's on my radar daily).
https://github.com/cloudxaas/gocache/tree/main/lrux/bytes
need fast lru. (promoting it coz may be able to be used in codis too and would hope for improvement to make it faster, kind of applicable to cache)

back to your question...

  1. the dashboard and fe is added for remote users to the dashboard and fe. it's optional configuration parameter. if you set tls = true, then it will do the tls feature, else wont affect performance at runtime.

  2. it'll be useful for wider adoption considering a lot of dev can remotely access their dashboard / fe etc knowing it's more secure. basically it wont affect the runtime performance.

  3. dashboard and fe why not add tls to interact with https protocol too?
    ok i see your point. so maybe we can also do a tlsfe = 1, tlsdashboard = 1, tlspika = 1 etc kind of parameter too. we can allow http/https to run concurrently without affecting performance and the fe/dashboard choosing which to connect to.

good?

@kolinfluence
Copy link
Author

kolinfluence commented Apr 21, 2024

proxy cannot connect?

image

you need to use redis-cli with secure tls to connect put
redis-cli --tls
as option parameter

pls remember it will only use tls for proxy if it's set to true. so accordingly, we will need to put tls-something for everything else etc where we want tls to have. so maybe tls-pika = true in future when pika has tls? but proxy shld definitely have tls.

@AlexStocks
Copy link
Collaborator

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

PR Review 🔍

⏱️ Estimated effort to review [1-5]

4, because the PR involves multiple files with significant changes including the introduction of TLS in the network layer, modifications to configuration handling, and updates to the proxy and topom modules. The complexity of the changes and the security implications of correctly implementing TLS require careful review to ensure reliability and security.

🧪 Relevant tests

No

⚡ Possible issues

Possible Bug: The error handling in the TLS setup could be improved by providing more specific error messages or handling specific types of errors differently.

Performance Concern: The introduction of TLS might impact the performance due to encryption overhead. It would be beneficial to benchmark the performance before and after these changes to ensure that the impact is within acceptable limits.

🔒 Security concerns

No

Code feedback:
relevant filecodis/cmd/fe/main.go
suggestion      

Consider setting more fields in the tls.Config to ensure better security, such as MinVersion to enforce the use of TLS 1.2 or higher. This helps prevent the use of older, less secure versions of TLS. [important]

relevant lineCertificates: []tls.Certificate{cert},

relevant filecodis/pkg/proxy/proxy.go
suggestion      

Add error logging before returning the error when the TLS certificate fails to load. This will help in debugging issues related to TLS configuration by providing a clear log message about what went wrong. [important]

relevant linereturn errors.Trace(err)

relevant filecodis/pkg/models/store.go
suggestion      

Implement error handling for the case when Decode returns nil but no error. This could potentially lead to a situation where the function proceeds without a valid token, which might not be the intended behavior. [medium]

relevant lineif t.Token == token {

relevant filecodis/pkg/topom/topom.go
suggestion      

Add validation for the input byte slice b in the Decode function to check if it's nil or empty before attempting to decode it. This prevents unnecessary processing and potential errors when dealing with empty data. [medium]

relevant lineif err := jsonDecode(s, b); err != nil {

@AlexStocks
Copy link
Collaborator

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Security
Remove the private key from the codebase to enhance security

It is highly insecure to store private keys within the codebase, especially in plaintext
format. This can lead to security vulnerabilities as unauthorized users could potentially
access sensitive information. Consider using environment variables or a secure vault
solution to manage private keys.

codis/key.pem [1-28]

------BEGIN PRIVATE KEY-----
-MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClOgprziC/+nQe
-...
------END PRIVATE KEY-----
+# Private key securely stored outside of the codebase
 
Suggestion importance[1-10]: 10

Why: Storing private keys in the codebase is a significant security risk. Using environment variables or secure vault solutions is a best practice to prevent unauthorized access to sensitive information.

10
Possible bug
Add checks for TLS certificate and key file paths before loading them

Consider adding a check for the --tls-cert and --tls-key flags before attempting to load
the TLS key pair. This ensures that the necessary parameters are provided, avoiding
runtime panics due to missing arguments.

codis/cmd/fe/main.go [205]

-cert, err := tls.LoadX509KeyPair(utils.ArgumentMust(d, "--tls-cert"), utils.ArgumentMust(d, "--tls-key"))
+certFile, keyFile := utils.ArgumentMust(d, "--tls-cert"), utils.ArgumentMust(d, "--tls-key")
+if certFile == "" || keyFile == "" {
+    log.Panic("TLS certificate or key file path must be provided")
+}
+cert, err := tls.LoadX509KeyPair(certFile, keyFile)
 
Suggestion importance[1-10]: 9

Why: This suggestion addresses a potential runtime panic by ensuring that the necessary TLS certificate and key file paths are provided before attempting to load them. This is a crucial check that enhances the robustness of the code.

9
Ensure safe token comparison by checking for nil after decoding

Add error handling for the Decode function to ensure that the token comparison only occurs
if the decoding is successful. This prevents potential nil pointer dereferences.

codis/pkg/models/store.go [133-138]

-t, err := Decode(b); 
+t, err := Decode(b)
 if err != nil {
     return err
 }
-if t.Token == token {
+if t != nil && t.Token == token {
     return s.Release()
 }
 
Suggestion importance[1-10]: 8

Why: This suggestion prevents potential nil pointer dereferences by ensuring that the token comparison only occurs if the decoding is successful. This is an important fix to avoid runtime errors.

8
Enhancement
Improve error handling for IP replacement to manage failures more effectively

Add error handling for utils.ReplaceUnspecifiedIP to handle potential failures gracefully
instead of only logging the error. This ensures that the function's failure is properly
managed.

codis/pkg/proxy/proxy.go [145-147]

 x, err := utils.ReplaceUnspecifiedIP(proto, l.Addr().String(), config.HostProxy)
 if err != nil {
+    log.Errorf("Failed to replace unspecified IP: %s", err)
     return err
 }
 
Suggestion importance[1-10]: 7

Why: The suggestion improves error handling by logging the error and returning it, which helps in debugging and ensures that failures are managed properly. However, it is a minor enhancement rather than a critical fix.

7
Wrap errors in the Decode function to enhance error information

Add error wrapping in the Decode function to provide more context about the error, which
can be useful for debugging.

codis/pkg/topom/topom.go [24-25]

 if err := jsonDecode(s, b); err != nil {
-    return nil,err
+    return nil, fmt.Errorf("json decode error: %w", err)
 }
 
Suggestion importance[1-10]: 6

Why: The suggestion enhances error information by wrapping errors with additional context. This is useful for debugging but is a minor improvement.

6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.0.0 ✏️ Feature New feature or request 🧹 Updates This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants