Skip to content

Commit

Permalink
feat(connector): add connector as a ShellHub service
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto committed Oct 20, 2023
1 parent 7087045 commit c8243f2
Show file tree
Hide file tree
Showing 19 changed files with 590 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ SHELLHUB_ASNYQ_GROUP_GRACE_PERIOD=1
# The maximum number of tasks that can be aggregated together. If that number is reached, Asynq server will aggregate
# the tasks immediately.
SHELLHUB_ASYNQ_GROUP_MAX_SIZE=500

# Connector is a special kind of agent that becomes containers into ShellHub Agent.
SHELLHUB_CONNECTOR=false
13 changes: 8 additions & 5 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ name: QA

on:
push:
branches: [ master ]
branches: [master]

pull_request:
branches: [ master ]
branches: [master]
types: [assigned, opened, synchronize, reopened, edited, ready_for_review]


jobs:
validate:
name: validate
Expand All @@ -17,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
project: [ api, agent, pkg, ssh, ui, cli ]
project: [api, agent, pkg, ssh, ui, cli, connector]
include:
- project: api
extra_args: ""
Expand All @@ -37,6 +36,9 @@ jobs:
- project: cli
extra_args: ""
lint_args: ""
- project: connector
extra_args: ""
lint_args: ""

runs-on: ubuntu-latest

Expand All @@ -57,6 +59,7 @@ jobs:
- 'pkg/**'
- 'ssh/**'
- 'cli/**'
- 'connector/**'
- name: Set up Go 1.x [Go]
if: matrix.project != 'ui' && steps.filter.outputs.go == 'true' && github.event.pull_request.draft == false
Expand Down Expand Up @@ -132,7 +135,7 @@ jobs:
if: matrix.project == 'ui' && steps.filter.outputs.ui == 'true' && github.event.pull_request.draft == false
working-directory: ${{ matrix.project }}
run: npm run test

- name: Save Code Linting Report JSON [UI]
if: matrix.project == 'ui' && steps.filter.outputs.ui == 'true' && github.event.pull_request.draft == false
working-directory: ${{ matrix.project }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ agent/shellhub.key
cli/cli
go.work
go.work.sum
connector/.keys
connector/connector
6 changes: 6 additions & 0 deletions agent/connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connector
import (
"context"
"fmt"
"os"
"sync"
"time"

Expand All @@ -13,6 +14,8 @@ import (
log "github.com/sirupsen/logrus"
)

const AgentPlatformConnector = "connector"

var _ Connector = new(DockerConnector)

// DockerConnector is a struct that represents a connector that uses Docker as the container runtime.
Expand Down Expand Up @@ -133,6 +136,9 @@ func (d *DockerConnector) Listen(ctx context.Context) error {

// initContainerAgent initializes the agent for a container.
func initContainerAgent(ctx context.Context, container Container) {
agent.AgentVersion = os.Getenv("SHELLHUB_VERSION")
agent.AgentPlatform = AgentPlatformConnector

cfg := &agent.Config{
ServerAddress: container.ServerAddress,
TenantID: container.Tenant,
Expand Down
47 changes: 0 additions & 47 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"runtime"
"time"

"github.com/Masterminds/semver"
"github.com/kelseyhightower/envconfig"
"github.com/shellhub-io/shellhub/agent/connector"
"github.com/shellhub-io/shellhub/pkg/agent"
"github.com/shellhub-io/shellhub/pkg/agent/pkg/selfupdater"
"github.com/shellhub-io/shellhub/pkg/envs"
Expand Down Expand Up @@ -265,51 +263,6 @@ It is initialized by the agent when a new SFTP session is created.`,
},
})

rootCmd.AddCommand(&cobra.Command{ // nolint: exhaustruct
Use: "connector",
Short: "Starts the Connector",
Long: "Starts the Connector, a special kind of Agent that turns your docker containers into ShellHub devices.",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := envs.ParseWithPrefix[agent.ConfigConnector]("shellhub")
if err != nil {
envconfig.Usage("shellhub", &cfg) // nolint:errcheck
log.Fatal(err)
}

cfg.PrivateKeys = path.Dir(cfg.PrivateKeys)

log.WithFields(log.Fields{
"version": AgentVersion,
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
"private_keys": cfg.PrivateKeys,
}).Info("Starting ShellHub Connector")

connector, err := connector.NewDockerConnector(cfg.ServerAddress, cfg.TenantID, cfg.PrivateKeys)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"version": AgentVersion,
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
}).Fatal("Failed to create connector")
}

if err := connector.Listen(cmd.Context()); err != nil {
log.WithError(err).WithFields(log.Fields{
"version": AgentVersion,
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
}).Fatal("Failed to listen for connections")
}

log.WithFields(log.Fields{
"version": AgentVersion,
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
}).Info("Connector stopped")
},
})

rootCmd.Version = AgentVersion

rootCmd.SetVersionTemplate(fmt.Sprintf("{{ .Name }} version: {{ .Version }}\ngo: %s\n",
Expand Down
2 changes: 2 additions & 0 deletions bin/docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ COMPOSE_FILE="docker-compose.yml"
[ "$SHELLHUB_AUTO_SSL" = "true" ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.autossl.yml"
[ "$SHELLHUB_ENV" = "development" ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.dev.yml"
[ "$SHELLHUB_ENTERPRISE" = "true" ] && [ "$SHELLHUB_ENV" != "development" ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.enterprise.yml"
[ "$SHELLHUB_CONNECTOR" = "true" ] && [ "$SHELLHUB_ENV" = "development" ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.connector.dev.yml"
[ "$SHELLHUB_CONNECTOR" = "true" ] && [ "$SHELLHUB_ENV" != "development" ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.connector.yml"
[ -f docker-compose.override.yml ] && COMPOSE_FILE="${COMPOSE_FILE}:docker-compose.override.yml"

[ -f "$EXTRA_COMPOSE_FILE" ] && COMPOSE_FILE="${COMPOSE_FILE}:${EXTRA_COMPOSE_FILE}"
Expand Down
Empty file added connector/.keys/.gitkeep
Empty file.
70 changes: 70 additions & 0 deletions connector/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# base stage
FROM golang:1.20.4-alpine3.16 AS base

ARG GOPROXY

RUN apk add --update git ca-certificates build-base bash util-linux perl xz

# We are using libxcrypt to support yescrypt password hashing method
# Since libxcrypt package is not available in Alpine, so we need to build libxcrypt from source code
RUN wget -q https://github.com/besser82/libxcrypt/releases/download/v4.4.27/libxcrypt-4.4.27.tar.xz && \
tar xvf libxcrypt-4.4.27.tar.xz && cd libxcrypt-4.4.27 && \
./configure --prefix /usr && make -j$(nproc) && make install && \
cd .. && rm -rf libxcrypt-4.4.27*

RUN ln -sf /bin/bash /bin/sh

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

COPY ./go.mod ./

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/connector

COPY ./connector/go.mod ./connector/go.sum ./

RUN go mod download

# builder stage
FROM base AS builder

ARG SHELLHUB_VERSION=latest
ARG GOPROXY

COPY ./pkg $GOPATH/src/github.com/shellhub-io/shellhub/pkg
COPY ./connector .

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

RUN go mod download

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/connector

RUN go build -tags docker

# development stage
FROM base AS development

ARG GOPROXY
ENV GOPROXY ${GOPROXY}

RUN apk add --update openssl openssh-client
RUN go install github.com/markbates/[email protected] && \
go install github.com/golangci/golangci-lint/cmd/[email protected]

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

RUN go mod download

COPY ./connector/entrypoint-dev.sh /entrypoint.sh

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/connector

ENTRYPOINT ["/entrypoint.sh"]

# production stage
FROM alpine:3.18.4 AS production

WORKDIR /app
COPY --from=builder /connector /app/

ENTRYPOINT ./connector
3 changes: 3 additions & 0 deletions connector/entrypoint-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

refresh run
55 changes: 55 additions & 0 deletions connector/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module github.com/shellhub-io/shellhub/connector

go 1.20

require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/shellhub-io/shellhub v0.13.0-rc.3.0.20231016135144-7087045b3877
github.com/shellhub-io/shellhub/agent v0.0.0-20231016135144-7087045b3877
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
)

require (
github.com/GehirnInc/crypt v0.0.0-20230320061759-8cc1b52080c5 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gliderlabs/ssh v0.3.5 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/go-resty/resty/v2 v2.10.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/labstack/echo/v4 v4.11.2 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/sftp v1.13.6 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
Loading

0 comments on commit c8243f2

Please sign in to comment.