-
Notifications
You must be signed in to change notification settings - Fork 11
feature: add redis resource access for PAM #95
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
7444282
Add redis res proxy
fangpenlin e1c4eae
Remove gen code
fangpenlin 30c2114
Handle cmd
fangpenlin b04c054
Add redis proxy
fangpenlin e364ee9
Handle auth
fangpenlin 492cb78
Forward cmd
fangpenlin be773a5
Add todo
fangpenlin 1494346
Use a different lib
fangpenlin 2736369
Use our own conn
fangpenlin 26ec61b
Read resp
fangpenlin 1a2b310
Refine the code
fangpenlin b3c3cc9
Add todo
fangpenlin 20732c2
Log entries
fangpenlin 9cb6533
Fix log uploading for redis
fangpenlin 30dc694
Clean code a bit
fangpenlin ea03f33
Clean up
fangpenlin 1dbba3a
Handle TLS
fangpenlin e8c7efe
Try to read the msg in a goroutine
fangpenlin d79eaad
Implement resp3 push msg
fangpenlin f6716c3
Fix pubsub hanging issue
fangpenlin 1e24f3c
Fix resp2 msg as well
fangpenlin 3095037
Log push msg as well
fangpenlin 5992dbb
Implement monitor mode
fangpenlin c1823f4
Use diff type of audit log for monitor
fangpenlin cafa9ac
Use our own fork for resp3
fangpenlin 1b9fcb8
Fix review feedbacks
fangpenlin 625f176
Address review feedbacks
fangpenlin c9ce0aa
Add project id for k8s as well
fangpenlin b4aa62d
Remove unused database for redis
fangpenlin 8807c28
Use the right ver of resp3
fangpenlin a329071
Just let ide yell at me
fangpenlin a252b19
Make auth cmd optional for redis
fangpenlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package redis | ||
|
|
||
| import ( | ||
| "net" | ||
|
|
||
| "github.com/smallnest/resp3" | ||
| ) | ||
|
|
||
| type RedisConn struct { | ||
| conn net.Conn | ||
| reader *resp3.Reader | ||
| writer *resp3.Writer | ||
| } | ||
|
|
||
| func NewRedisConn(conn net.Conn) *RedisConn { | ||
| return &RedisConn{ | ||
| conn: conn, | ||
| reader: resp3.NewReader(conn), | ||
| writer: resp3.NewWriter(conn), | ||
| } | ||
| } | ||
|
|
||
| func (c *RedisConn) Close() error { | ||
| defer func() { _ = c.conn.Close() }() | ||
| if err := c.writer.Flush(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *RedisConn) Reader() *resp3.Reader { | ||
| return c.reader | ||
| } | ||
|
|
||
| func (c *RedisConn) Writer() *resp3.Writer { | ||
| return c.writer | ||
| } | ||
|
|
||
| func (c *RedisConn) WriteValue(value *resp3.Value, flush bool) error { | ||
| _, err := c.writer.WriteString(value.ToRESP3String()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !flush { | ||
| return nil | ||
| } | ||
| return c.writer.Flush() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package redis | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net" | ||
|
|
||
| "github.com/Infisical/infisical-merge/packages/pam/session" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/smallnest/resp3" | ||
| ) | ||
|
|
||
| // RedisProxyConfig holds configuration for the Redis proxy | ||
| type RedisProxyConfig struct { | ||
| TargetAddr string | ||
| InjectUsername string | ||
| InjectPassword string | ||
| EnableTLS bool | ||
| TLSConfig *tls.Config | ||
| SessionID string | ||
| SessionLogger session.SessionLogger | ||
| } | ||
|
|
||
| // RedisProxy handles proxying Redis connections | ||
| type RedisProxy struct { | ||
| config RedisProxyConfig | ||
| relayHandler *RelayHandler | ||
| } | ||
|
|
||
| // NewRedisProxy creates a new Redis proxy instance | ||
| func NewRedisProxy(config RedisProxyConfig) *RedisProxy { | ||
| return &RedisProxy{config: config} | ||
| } | ||
|
|
||
| // HandleConnection handles a single client connection | ||
| func (p *RedisProxy) HandleConnection(ctx context.Context, clientConn net.Conn) error { | ||
| defer clientConn.Close() | ||
|
|
||
| sessionID := p.config.SessionID | ||
|
|
||
| // Ensure session logger cleanup | ||
| defer func() { | ||
| if err := p.config.SessionLogger.Close(); err != nil { | ||
| log.Error().Err(err).Str("sessionID", sessionID).Msg("Failed to close session logger") | ||
| } | ||
| }() | ||
|
|
||
| log.Info(). | ||
| Str("sessionID", sessionID). | ||
| Msg("New Redis connection for PAM session") | ||
|
|
||
| var selfToServerConn net.Conn | ||
| if !p.config.EnableTLS { | ||
| c, err := net.Dial("tcp", p.config.TargetAddr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| selfToServerConn = c | ||
| } else { | ||
| c, err := tls.Dial("tcp", p.config.TargetAddr, p.config.TLSConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| selfToServerConn = c | ||
| } | ||
|
|
||
| selfToClientRedisConn := NewRedisConn(selfToServerConn) | ||
| defer func(selfToClientRedisConn *RedisConn) { _ = selfToClientRedisConn.Close() }(selfToClientRedisConn) | ||
|
|
||
| // Only authenticate if credentials are provided | ||
| if p.config.InjectUsername != "" && p.config.InjectPassword != "" { | ||
| if err := selfToClientRedisConn.Writer().WriteCommand("AUTH", p.config.InjectUsername, p.config.InjectPassword); err != nil { | ||
| return err | ||
| } | ||
| if err := selfToClientRedisConn.Writer().Flush(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| respValue, _, err := selfToClientRedisConn.Reader().ReadValue() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if respValue.Str != "OK" { | ||
| errorMsg := "unknown" | ||
| if respValue.Type == resp3.TypeSimpleError || respValue.Type == resp3.TypeBlobError { | ||
| errorMsg = respValue.Err | ||
| } | ||
| log.Error().Str("errorMsg", errorMsg).Msg("Failed to authenticate with the target redis server") | ||
| return fmt.Errorf("failed to authenticate with the target redis server") | ||
| } | ||
| } | ||
|
|
||
| clientToSelfConn := NewRedisConn(clientConn) | ||
| defer clientToSelfConn.Close() | ||
|
|
||
| p.relayHandler = NewRelayHandler(clientToSelfConn, selfToClientRedisConn, p.config.SessionLogger) | ||
| return p.relayHandler.Handle(ctx) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.