Skip to content

Commit

Permalink
fixes i dont even rmb
Browse files Browse the repository at this point in the history
  • Loading branch information
argonaut0 committed Sep 16, 2023
1 parent c329bff commit 9c40189
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git
WORKDIR /app
COPY ./* ./
COPY go.mod go.sum ./
# Fetch dependencies.
RUN go get -d -v
RUN go mod download
COPY ./* ./
# Build the binary.
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app
RUN go get -v && GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app
############################
# STEP 2 build image
############################
Expand Down
42 changes: 42 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"

_ "modernc.org/sqlite"
Expand All @@ -15,6 +17,18 @@ type InstanceRecord struct {
Challenge string `json:"challenge"`
TeamID string `json:"team"`
UUID string `json:"uuid"`
Url string `json:"url"`
}

func (r *InstanceRecord) MarshalJSON() ([]byte, error) {
type Alias InstanceRecord
return json.Marshal(&struct {
*Alias
Expiry int64 `json:"stamp"`
}{
Alias: (*Alias)(r),
Expiry: r.Expiry.Unix(),
})
}

func (in *Instancer) InitDB(file string) error {
Expand Down Expand Up @@ -88,6 +102,33 @@ func (in *Instancer) DeleteInstanceRecord(id int64) error {
return nil
}

func (in *Instancer) ReadInstanceRecord(id int64) (InstanceRecord, error) {
if in.db == nil {
return InstanceRecord{}, errors.New("db not initialized")
}
rows, err := in.db.Query("SELECT id, challenge, team, expiry, uuid FROM instances WHERE id = ?")
if err != nil {
return InstanceRecord{}, err
}
defer rows.Close()
records := make([]InstanceRecord, 0)
for rows.Next() {
record := InstanceRecord{}
var t int64
err = rows.Scan(&record.Id, &record.Challenge, &record.TeamID, &t, &record.UUID)
if err != nil {
return InstanceRecord{}, err
}
record.Expiry = time.Unix(t, 0)
records = append(records, record)
}
if len(records) != 1 {
return InstanceRecord{}, fmt.Errorf("unique record not found with id %v", id)
}
err = rows.Err()
return records[0], err
}

func (in *Instancer) ReadInstanceRecords() ([]InstanceRecord, error) {
if in.db == nil {
return nil, errors.New("db not initialized")
Expand Down Expand Up @@ -136,6 +177,7 @@ func (in *Instancer) ReadInstanceRecordsTeam(teamID string) ([]InstanceRecord, e
return records, err
}
record.Expiry = time.Unix(t, 0)
record.Url = fmt.Sprintf("http://%v.%v.ctf.maplebacon.org", record.UUID, record.Challenge)
records = append(records, record)
}
err = rows.Err()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ require (
)

require (
github.com/lithammer/shortuuid/v3 v3.0.7
github.com/rs/zerolog v1.30.0
modernc.org/sqlite v1.25.0
)

require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/mod v0.10.0 // indirect
Expand Down Expand Up @@ -44,7 +44,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.0
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.4.0
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand All @@ -35,7 +37,6 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand All @@ -57,8 +58,6 @@ github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUU
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJVuvyJQQ8=
github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand Down
15 changes: 15 additions & 0 deletions instancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -37,6 +38,20 @@ func InitInstancer() (*Instancer, error) {
in.echo.Logger = NewEchoLog(in.log)
log := in.log.With().Str("component", "instanced-init").Logger()

reqLog := log.With().Str("component", "echo-req").Logger()
in.echo.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
reqLog.Info().
Str("URI", v.URI).
Int("status", v.Status).
Msg("request")

return nil
},
}))

in.registerEndpoints()

var err error
Expand Down
25 changes: 22 additions & 3 deletions webserver.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -36,6 +36,17 @@ func (in *Instancer) handleInstanceCreate(c echo.Context) error {
chalName := c.QueryParam("chal")
teamID := c.QueryParam("team")

recs, err := in.ReadInstanceRecordsTeam(teamID)
if err != nil {
c.Logger().Errorf("request failed: %v", err)
return c.JSON(http.StatusInternalServerError, "challenge deploy failed: contact admin")
}
for _, r := range recs {
if r.Challenge == chalName {
return c.JSON(http.StatusTooManyRequests, "instance already exists for this challenge")
}
}

rec, err := in.CreateInstance(chalName, teamID)
if _, ok := err.(*ChallengeNotFoundError); ok {
return c.JSON(http.StatusNotFound, "challenge not supported")
Expand All @@ -47,7 +58,7 @@ func (in *Instancer) handleInstanceCreate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, "challenge deploy failed: contact admin")
}
c.Logger().Info("processed request to provision new instance")
return c.JSON(http.StatusAccepted, InstancesResponse{"created", chalName, rec.Id, "TODO"})
return c.JSON(http.StatusAccepted, InstancesResponse{"created", chalName, rec.Id, fmt.Sprintf("http://%v.%v.ctf.maplebacon.org", rec.UUID, chalName)})
}

func (in *Instancer) handleInstanceDelete(c echo.Context) error {
Expand All @@ -58,7 +69,15 @@ func (in *Instancer) handleInstanceDelete(c echo.Context) error {
return c.JSON(http.StatusBadRequest, "invalid id")
}

err = in.DestroyInstance(InstanceRecord{instanceID, time.Now(), chalName, "", ""})
// todo: bugged, pls fix
rec, err := in.ReadInstanceRecord(instanceID)

if err != nil {
c.Logger().Errorf("request failed: %v", err)
return c.JSON(http.StatusNotFound, "instance id not found")
}

err = in.DestroyInstance(rec)

if _, ok := err.(*ChallengeNotFoundError); ok {
return c.JSON(http.StatusNotFound, "challenge not supported")
Expand Down

0 comments on commit 9c40189

Please sign in to comment.