-
-
Notifications
You must be signed in to change notification settings - Fork 72
feat: add support for docker over ssh tunnel #549
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
Open
raghavyuva
wants to merge
4
commits into
master
Choose a base branch
from
feat/tunneled_docker_ops
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b14bc9
feat: add support for docker over ssh tunnel
raghavyuva 0a212fd
feat: add support for multi server routes and services (#551)
raghavyuva d34a58f
feat: view for multi server management (#550)
raghavyuva 87221f2
fix: missing i18n keys, and ui issues, server endpoints in router
raghavyuva 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,98 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/raghavyuva/nixopus-api/internal/features/logger" | ||
| "github.com/raghavyuva/nixopus-api/internal/features/ssh" | ||
| ) | ||
|
|
||
| // CreateSSHTunnel creates a local Unix socket and forwards all connections | ||
| // through the provided SSH client to the remote Docker daemon socket | ||
| // at /var/run/docker.sock. It returns an SSHTunnel with a cleanup function | ||
| // that closes the listener and removes the temporary socket file. | ||
| func CreateSSHTunnel(sshClient *ssh.SSH, logger logger.Logger) (*SSHTunnel, error) { | ||
| tempDir := os.TempDir() | ||
| localSocket := filepath.Join(tempDir, fmt.Sprintf("docker-ssh-%d.sock", time.Now().UnixNano())) | ||
|
|
||
| os.Remove(localSocket) | ||
|
|
||
| listener, err := net.Listen("unix", localSocket) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create local socket: %w", err) | ||
| } | ||
|
|
||
| tunnel := &SSHTunnel{ | ||
| localSocket: localSocket, | ||
| sshClient: sshClient, | ||
| listener: listener, | ||
| cleanup: func() error { | ||
| listener.Close() | ||
| os.Remove(localSocket) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| go tunnel.handleConnections(logger) | ||
|
|
||
| return tunnel, nil | ||
| } | ||
raghavyuva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // handleConnections manages the SSH tunnel connections | ||
| func (t *SSHTunnel) handleConnections(lgr logger.Logger) { | ||
| for { | ||
| localConn, err := t.listener.Accept() | ||
| if err != nil { | ||
| lgr.Log(logger.Error, "SSH tunnel listener error", err.Error()) | ||
| return | ||
| } | ||
|
|
||
| go t.forwardConnection(localConn, lgr) | ||
| } | ||
| } | ||
|
|
||
| // forwardConnection forwards a local connection through the SSH tunnel to the remote Docker socket | ||
| func (t *SSHTunnel) forwardConnection(localConn net.Conn, lgr logger.Logger) { | ||
| defer localConn.Close() | ||
|
|
||
| sshConn, err := t.sshClient.Connect() | ||
| if err != nil { | ||
| lgr.Log(logger.Error, "Failed to establish SSH connection", err.Error()) | ||
| return | ||
| } | ||
| defer sshConn.Close() | ||
raghavyuva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| remoteConn, err := sshConn.Dial("unix", "/var/run/docker.sock") | ||
| if err != nil { | ||
| lgr.Log(logger.Error, "Failed to connect to remote Docker socket", err.Error()) | ||
| return | ||
| } | ||
| defer remoteConn.Close() | ||
|
|
||
| done := make(chan struct{}, 2) | ||
|
|
||
| go func() { | ||
| io.Copy(remoteConn, localConn) | ||
| done <- struct{}{} | ||
| }() | ||
|
|
||
| go func() { | ||
| io.Copy(localConn, remoteConn) | ||
| done <- struct{}{} | ||
| }() | ||
|
|
||
| <-done | ||
| } | ||
raghavyuva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Close cleans up the DockerService and any SSH tunnels | ||
| func (s *DockerService) Close() error { | ||
| if s.sshTunnel != nil { | ||
| return s.sshTunnel.cleanup() | ||
| } | ||
| return nil | ||
| } | ||
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,88 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/go-fuego/fuego" | ||
| "github.com/google/uuid" | ||
| "github.com/raghavyuva/nixopus-api/internal/features/logger" | ||
| "github.com/raghavyuva/nixopus-api/internal/features/servers/types" | ||
| "github.com/raghavyuva/nixopus-api/internal/utils" | ||
|
|
||
| shared_types "github.com/raghavyuva/nixopus-api/internal/types" | ||
| ) | ||
|
|
||
| func (c *ServersController) CreateServer(f fuego.ContextWithBody[types.CreateServerRequest]) (*shared_types.Response, error) { | ||
| serverRequest, err := f.Body() | ||
|
|
||
| if err != nil { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| w, r := f.Response(), f.Request() | ||
| if !c.parseAndValidate(w, r, &serverRequest) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: nil, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| user := utils.GetUser(w, r) | ||
|
|
||
| if user == nil { | ||
| return nil, fuego.HTTPError{ | ||
| Err: nil, | ||
| Status: http.StatusUnauthorized, | ||
| } | ||
| } | ||
|
|
||
| organization := utils.GetOrganizationID(r) | ||
|
|
||
| if organization == uuid.Nil { | ||
| return nil, fuego.HTTPError{ | ||
| Err: nil, | ||
| Status: http.StatusUnauthorized, | ||
| } | ||
| } | ||
|
|
||
| created, err := c.service.CreateServer(serverRequest, user.ID.String(), organization.String()) | ||
|
|
||
| if err != nil { | ||
| c.logger.Log(logger.Error, err.Error(), "") | ||
|
|
||
| if isInvalidServerError(err) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| if err == types.ErrServerAlreadyExists || err == types.ErrServerHostAlreadyExists { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusConflict, | ||
| } | ||
| } | ||
|
|
||
| if isPermissionError(err) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusForbidden, | ||
| } | ||
| } | ||
|
|
||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusInternalServerError, | ||
| } | ||
| } | ||
|
|
||
| return &shared_types.Response{ | ||
| Status: "success", | ||
| Message: "Server created successfully", | ||
| Data: created, | ||
| }, nil | ||
| } |
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,78 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/go-fuego/fuego" | ||
| "github.com/raghavyuva/nixopus-api/internal/features/logger" | ||
| "github.com/raghavyuva/nixopus-api/internal/features/servers/types" | ||
| "github.com/raghavyuva/nixopus-api/internal/utils" | ||
|
|
||
| shared_types "github.com/raghavyuva/nixopus-api/internal/types" | ||
| ) | ||
|
|
||
| func (c *ServersController) DeleteServer(f fuego.ContextWithBody[types.DeleteServerRequest]) (*shared_types.Response, error) { | ||
| serverRequest, err := f.Body() | ||
|
|
||
| if err != nil { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| w, r := f.Response(), f.Request() | ||
| if !c.parseAndValidate(w, r, &serverRequest) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: nil, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| user := utils.GetUser(w, r) | ||
|
|
||
| if user == nil { | ||
| return nil, fuego.HTTPError{ | ||
| Err: nil, | ||
| Status: http.StatusUnauthorized, | ||
| } | ||
| } | ||
|
|
||
| err = c.service.DeleteServer(serverRequest, user.ID.String()) | ||
|
|
||
| if err != nil { | ||
| c.logger.Log(logger.Error, err.Error(), "") | ||
|
|
||
| if isInvalidServerError(err) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusBadRequest, | ||
| } | ||
| } | ||
|
|
||
| if err == types.ErrServerNotFound { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusNotFound, | ||
| } | ||
| } | ||
|
|
||
| if isPermissionError(err) { | ||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusForbidden, | ||
| } | ||
| } | ||
|
|
||
| return nil, fuego.HTTPError{ | ||
| Err: err, | ||
| Status: http.StatusInternalServerError, | ||
| } | ||
| } | ||
|
|
||
| return &shared_types.Response{ | ||
| Status: "success", | ||
| Message: "Server deleted successfully", | ||
| Data: nil, | ||
| }, nil | ||
| } |
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.