-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from uselagoon/ssh-portal
Implement SSH portal service
- Loading branch information
Showing
19 changed files
with
820 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,9 +1,25 @@ | ||
builds: | ||
- dir: cmd/service-api | ||
- id: service-api | ||
dir: cmd/service-api | ||
binary: service-api | ||
ldflags: | ||
- > | ||
-s -w -X main.date={{.Date}} -X "main.goVersion={{.Env.GOVERSION}}" | ||
-X main.shortCommit={{.ShortCommit}} -X main.version={{.Version}} | ||
env: | ||
- CGO_ENABLED=0 | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
- id: ssh-portal | ||
dir: cmd/ssh-portal | ||
binary: ssh-portal | ||
ldflags: | ||
- > | ||
-s -w -X main.date={{.Date}} -X "main.goVersion={{.Env.GOVERSION}}" | ||
-X main.shortCommit={{.ShortCommit}} -X main.version={{.Version}} | ||
env: | ||
- CGO_ENABLED=0 | ||
goarch: | ||
- amd64 | ||
- arm64 |
This file contains 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 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 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 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,35 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/alecthomas/kong" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// CLI represents the command-line interface. | ||
type CLI struct { | ||
Debug bool `kong:"env='DEBUG',help='Enable debug logging'"` | ||
Serve ServeCmd `kong:"cmd,default=1,help='(default) Serve service-api requests'"` | ||
Version VersionCmd `kong:"cmd,help='Print version information'"` | ||
} | ||
|
||
func main() { | ||
// parse CLI config | ||
cli := CLI{} | ||
kctx := kong.Parse(&cli, | ||
kong.UsageOnError(), | ||
) | ||
// init logger | ||
var log *zap.Logger | ||
var err error | ||
if cli.Debug { | ||
log, err = zap.NewDevelopment(zap.AddStacktrace(zap.ErrorLevel)) | ||
} else { | ||
log, err = zap.NewProduction() | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer log.Sync() //nolint:errcheck | ||
// execute CLI | ||
kctx.FatalIfErrorf(kctx.Run(log)) | ||
} |
This file contains 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,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/uselagoon/ssh-portal/internal/k8s" | ||
"github.com/uselagoon/ssh-portal/internal/metrics" | ||
"github.com/uselagoon/ssh-portal/internal/signalctx" | ||
"github.com/uselagoon/ssh-portal/internal/sshportal" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// ServeCmd represents the serve command. | ||
type ServeCmd struct { | ||
NATSServer string `kong:"required,env='NATS_URL',help='NATS server URL (nats://... or tls://...)'"` | ||
SSHServerPort uint `kong:"default='2222',env='SSH_SERVER_PORT',help='Port the SSH server will listen on for SSH client connections'"` | ||
} | ||
|
||
// Run the serve command to service API requests. | ||
func (cmd *ServeCmd) Run(log *zap.Logger) error { | ||
// instrumentation requires a separate context because deferred Shutdown() | ||
// will exit immediately if the context is already done. | ||
ictx := context.Background() | ||
// init metrics | ||
m := metrics.NewServer(log) | ||
defer m.Shutdown(ictx) //nolint:errcheck | ||
// get main process context | ||
ctx, cancel := signalctx.GetContext() | ||
defer cancel() | ||
// get nats server connection | ||
nc, err := nats.Connect(cmd.NATSServer) | ||
if err != nil { | ||
return fmt.Errorf("couldn't connect to NATS server: %v", err) | ||
} | ||
// start listening on TCP port | ||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", cmd.SSHServerPort)) | ||
if err != nil { | ||
return fmt.Errorf("couldn't listen on port %d: %v", cmd.SSHServerPort, err) | ||
} | ||
// get kubernetes client | ||
c, err := k8s.NewClient() | ||
if err != nil { | ||
return fmt.Errorf("couldn't create k8s client: %v", err) | ||
} | ||
// start serving SSH connection requests | ||
return sshportal.Serve(ctx, log, nc, l, c) | ||
} |
This file contains 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,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
var ( | ||
date string | ||
goVersion string | ||
shortCommit string | ||
version string | ||
) | ||
|
||
// VersionCmd represents the version command. | ||
type VersionCmd struct{} | ||
|
||
// Run the version command to print version information. | ||
func (cmd *VersionCmd) Run() error { | ||
fmt.Printf("Lagoon service-api %v (%v) compiled with %v on %v\n", version, | ||
shortCommit, goVersion, date) | ||
return nil | ||
} |
This file contains 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
Oops, something went wrong.