Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,14 @@ APP_CONFIG_CACHE_TTL=2m
# CORS
CORS_ALLOWED_ORIGINS=http://localhost:3000,https://smctf.example.com

# Stack (Container Provisioner)
STACKS_ENABLED=true
STACKS_MAX_SCOPE=team
STACKS_MAX_PER=3
STACKS_PROVISIONER_BASE_URL=http://localhost:8081
STACKS_PROVISIONER_USE_GRPC=false
STACKS_PROVISIONER_GRPC_ADDR=localhost:9090
STACKS_PROVISIONER_API_KEY=change-me
STACKS_PROVISIONER_TIMEOUT=5s
STACKS_CREATE_WINDOW=1m
STACKS_CREATE_MAX=1
# VM (Container Orchestrator)
VMS_ENABLED=true
VMS_MAX_SCOPE=team
VMS_MAX_PER=3
VMS_ORCHESTRATOR_BASE_URL=http://localhost:8081
VMS_ORCHESTRATOR_TIMEOUT=5s
VMS_CREATE_WINDOW=1m
VMS_CREATE_MAX=1

# Logging
LOG_DIR=logs
Expand Down
21 changes: 6 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
SHELL := /bin/bash

GO ?= go
BUF ?= buf
BUF_VERSION ?= v1.66.1
BUF_MODULE ?= buf.build/smctf/container-provisioner

.PHONY: all fmt vet lint buf-install buf-lint buf-generate test build
.PHONY: all fmt vet lint test build

all: buf-lint buf-generate test build
all: lint test build

fmt:
$(GO) fmt ./...

vet:
$(GO) vet ./...

lint: buf-lint vet

buf-install:
$(GO) install github.com/bufbuild/buf/cmd/buf@$(BUF_VERSION)

buf-lint:
$(BUF) lint $(BUF_MODULE)

buf-generate:
$(BUF) generate $(BUF_MODULE) --template buf.gen.yaml
lint: vet

test:
$(GO) test ./...

build:
$(GO) build ./cmd/server

run:
$(GO) run ./cmd/server
56 changes: 15 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
Docs
</a>
| <strong>Backend</strong> |
<a href="https://github.com/nullforu/container-provisioner-k8s">
Container Provisioner
<a href="https://github.com/swualabs/sandboxd-o">
Container Orchestrator
</a>
|
<a href="https://github.com/nullforu/smctfe">
Expand Down Expand Up @@ -71,8 +71,8 @@ See [SMCTF Docs](https://ctf.null4u.cloud/smctf/) for more details. This README
- Frontend has been moved to a separate repository ([nullforu/smctfe](https://github.com/nullforu/smctfe))
- Challenge file upload/download support via AWS S3 Presigned URL
- Ref Issue: [#20](https://github.com/nullforu/smctf/issues/20), PR: [#21](https://github.com/nullforu/smctf/pull/21)
- Per challenge individual Stack(instance/VM) provisioning support via Kubernetes
- Ref PR: [#25](https://github.com/nullforu/smctf/pull/25), See [container-provisioner-k8s](https://github.com/nullforu/container-provisioner-k8s) and [docs](https://ctf.null4u.cloud/container-provisioner/) for more details.
- Per challenge individual VM(instance/VM) provisioning support via Kubernetes
- Ref PR: [#25](https://github.com/nullforu/smctf/pull/25), See [container-orchestrator-k8s](https://github.com/nullforu/container-orchestrator-k8s) and [docs](https://ctf.null4u.cloud/container-orchestrator/) for more details.
- ... and more! (See [docs](https://github.com/nullforu/smctf-docs) for more details) -->

### Planned/Upcoming features:
Expand All @@ -82,10 +82,10 @@ Also, the following features are planned to be implemented. see [issues](https:/
- (WIP) Systematized admin dashboard and behavior log/monitoring system integration
- ... and more features to be added.

## Tech Stacks
## Tech VMs

- Backend: [Go](https://go.dev/), [Gin](https://github.com/gin-gonic/gin), [Bun ORM](https://bun.uptrace.dev/)
- Container Provisioner: [Go (nullforu/container-provisioner-k8s)](https://github.com/nullforu/container-provisioner-k8s)
- Container Orchestrator: [Go (nullforu/container-orchestrator-k8s)](https://github.com/nullforu/container-orchestrator-k8s)
- Frontend: React [(nullforu/smctfe)](https://github.com/nullforu/smctfe)
- Database, Cache: [PostgreSQL](https://www.postgresql.org/)(instead of MySQL/MariaDB), [Redis](https://redis.io/)
- Testing: [Testcontainers for Go](https://github.com/testcontainers/testcontainers-go)
Expand Down Expand Up @@ -168,17 +168,14 @@ SUBMIT_MAX=10
TIMELINE_CACHE_TTL=60s
LEADERBOARD_CACHE_TTL=60s

# Stack (Container Provisioner)
STACKS_ENABLED=true
STACKS_MAX_SCOPE=team
STACKS_MAX_PER=3
STACKS_PROVISIONER_BASE_URL=http://localhost:8081
STACKS_PROVISIONER_USE_GRPC=false
STACKS_PROVISIONER_GRPC_ADDR=localhost:9090
STACKS_PROVISIONER_API_KEY=change-me
STACKS_PROVISIONER_TIMEOUT=5s
STACKS_CREATE_WINDOW=1m
STACKS_CREATE_MAX=1
# VM (Container Orchestrator)
VMS_ENABLED=true
VMS_MAX_SCOPE=team
VMS_MAX_PER=3
VMS_ORCHESTRATOR_BASE_URL=http://localhost:8081
VMS_ORCHESTRATOR_TIMEOUT=5s
VMS_CREATE_WINDOW=1m
VMS_CREATE_MAX=1

# Logging
LOG_DIR=logs
Expand All @@ -205,29 +202,6 @@ S3_PRESIGN_TTL=15m

</details>

## Buf / BSR (container-provisioner proto)

This repo consumes the container-provisioner proto via Buf Schema Registry (BSR).

Setup:

```bash
make buf-install
buf registry login
```

Generate code:

```bash
make buf-generate
```

Module reference is set via `BUF_MODULE` (Makefile). You can override via:

```bash
make buf-generate BUF_MODULE=buf.build/<org>/container-provisioner
```

> [!IMPORTANT]
>
> Make sure to change `JWT_SECRET` to a secure random string in production!
Expand Down Expand Up @@ -271,7 +245,7 @@ go build -o smctf ./cmd/server
"app": { "type": "string" },
"legacy": { "type": "boolean" },
"error": {},
"stack": { "type": "string" },
"stack_trace": { "type": "string" },
"http": {
"type": "object",
"additionalProperties": true,
Expand Down
8 changes: 0 additions & 8 deletions buf.gen.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions buf.lock

This file was deleted.

31 changes: 5 additions & 26 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"smctf/internal/realtime"
"smctf/internal/repo"
"smctf/internal/service"
"smctf/internal/stack"
"smctf/internal/storage"
"smctf/internal/vm"
)

func main() {
Expand Down Expand Up @@ -84,7 +84,7 @@ func main() {
submissionRepo := repo.NewSubmissionRepo(database)
scoreRepo := repo.NewScoreboardRepo(database)
appConfigRepo := repo.NewAppConfigRepo(database)
stackRepo := repo.NewStackRepo(database)
vmRepo := repo.NewVMRepo(database)

var fileStore storage.ChallengeFileStore
if cfg.S3.Enabled {
Expand All @@ -104,29 +104,8 @@ func main() {
ctfSvc := service.NewCTFService(cfg, challengeRepo, submissionRepo, redisClient, fileStore)
appConfigSvc := service.NewAppConfigService(appConfigRepo, redisClient, cfg.Cache.AppConfigTTL)

var stackClient stack.API
var stackClientCloser func() error
if cfg.Stack.ProvisionerUseGRPC {
client, err := stack.NewGRPCClient(cfg.Stack.ProvisionerGRPCAddr, cfg.Stack.ProvisionerAPIKey, cfg.Stack.ProvisionerTimeout)
if err != nil {
logger.Error("grpc stack client init error", slog.Any("error", err))
os.Exit(1)
}

stackClient = client
stackClientCloser = client.Close
} else {
stackClient = stack.NewClient(cfg.Stack.ProvisionerBaseURL, cfg.Stack.ProvisionerAPIKey, cfg.Stack.ProvisionerTimeout)
}
if stackClientCloser != nil {
defer func() {
if err := stackClientCloser(); err != nil {
logger.Warn("stack client close error", slog.Any("error", err))
}
}()
}

stackSvc := service.NewStackService(cfg.Stack, stackRepo, challengeRepo, submissionRepo, stackClient, redisClient)
vmClient := vm.NewClient(cfg.VM.OrchestratorBaseURL, cfg.VM.OrchestratorTimeout)
vmSvc := service.NewVMService(cfg.VM, vmRepo, challengeRepo, submissionRepo, vmClient, redisClient)

bootstrap.BootstrapAdmin(ctx, cfg, database, userRepo, teamRepo, divisionRepo, logger)

Expand All @@ -143,7 +122,7 @@ func main() {
leaderboardBus := realtime.NewScoreboardBus(redisClient, cfg, scoreSvc, divisionSvc, logger, sseHub)
leaderboardBus.Start(ctx)

router := httpserver.NewRouter(cfg, authSvc, ctfSvc, appConfigSvc, userSvc, scoreSvc, divisionSvc, teamSvc, stackSvc, redisClient, logger, sseHub)
router := httpserver.NewRouter(cfg, authSvc, ctfSvc, appConfigSvc, userSvc, scoreSvc, divisionSvc, teamSvc, vmSvc, redisClient, logger, sseHub)
srv := &nethttp.Server{
Addr: cfg.HTTPAddr,
Handler: router,
Expand Down
3 changes: 1 addition & 2 deletions codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ ignore:
- "migrations/**"
- "**/*.sql"
- "internal/storage/s3.go" # S3 storage is production only code. instead, we test mock storage.
- "internal/stack/client.go" # Container Provisioner HTTP client is production only code. instead, we test mock client.
- "internal/stack/grpc_client.go" # Container Provisioner gRPC client is production only code. instead, we test mock client.
- "internal/vm/client.go" # Container Orchestrator HTTP client is production only code. instead, we test mock client.
- "internal/http/handlers/types.go" # only type definitions and constructors.
Loading
Loading