Skip to content

Commit

Permalink
push builds to a new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
aditsachde committed Sep 14, 2024
1 parent 177b12e commit 0f7c34b
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,30 @@ jobs:
with:
name: "bin"
path: "bin/"

- name: Push to builds branch
run: |
# Capture the short SHA hash of the current commit
SHORT_SHA=$(git rev-parse --short HEAD)
# Set Git user name and email for the commits made by this workflow
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
# Create a new orphan branch named 'builds' with no prior history
git checkout --orphan builds
# Remove all existing files in the working directory
git rm -rf .
# Reset to a clean state with no files in the working directory
git reset --hard
# Add the contents of the 'bin' directory to the staging area
git add bin/
# Commit the build with a message including the short SHA hash
git commit -m "Build binaries - SHA: $SHORT_SHA"
# Force push the 'builds' branch to the remote repository
git push origin +builds
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ go.work
.DS_Store
rfc.pdf

/alphalog
/alphalog

frontend/
stuff_test.go
devcontainer.json
dependabot.yml
Binary file added bin/main.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions cmd/fastly-monitor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"log"
"os"

"github.com/fastly/compute-sdk-go/fsthttp"
"itko.dev/internal/ctmonitor"
)

func main() {
log.Println("FASTLY_SERVICE_VERSION:", os.Getenv("FASTLY_SERVICE_VERSION"))
fsthttp.ServeFunc(ctmonitor.FastlyServe)
}
8 changes: 8 additions & 0 deletions fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
manifest_version = 3
name = "ct2025-monitoring"
language = "go"
service_id = "X7QzVa4t0wXcAvoOMoXzA5"

[scripts]
env_vars = ["GOARCH=wasm", "GOOS=wasip1"]
build = "go build -o bin/main.wasm ./cmd/fastly-monitor"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fastly/compute-sdk-go v1.3.2
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fastly/compute-sdk-go v1.3.2 h1:9y49qzks/dH8QIPUuawfW8eKJRAOT1XY5hO9XP8xoCw=
github.com/fastly/compute-sdk-go v1.3.2/go.mod h1:AdOwbVc56tusuDLtufaO57NIzfoox4bgvRc460B/T8o=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand Down
133 changes: 133 additions & 0 deletions internal/ctmonitor/fastly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ctmonitor

import (
"context"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/url"

"github.com/fastly/compute-sdk-go/configstore"
"github.com/fastly/compute-sdk-go/fsthttp"
)

// The log needs two configs, the backend service name and the mask size.
// These can be fetched from edge config but for now we will hard code them.
const configStoreName = "hostmap"
const maskSize = 5

func FastlyServe(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" || r.Method == "DELETE" {
w.WriteHeader(fsthttp.StatusMethodNotAllowed)
fmt.Fprintf(w, "This method is not allowed\n")
return
}

config, err := configstore.Open(configStoreName)
if err != nil {
w.WriteHeader(fsthttp.StatusInternalServerError)
log.Println("Error opening config store: %v\n", err)
return
}

backend, err := config.Get(r.Host)
if err != nil {
w.WriteHeader(fsthttp.StatusNotFound)
fmt.Fprintln(w, "Not found")
return
}

s := &FastlyStorage{backend}
f := newFetch(s, maskSize)

if r.URL.Path == "/ct/v1/get-sth-consistency" {
FastlyWrapper(f.get_sth_consistency)(ctx, w, r)
} else if r.URL.Path == "/ct/v1/get-proof-by-hash" {
FastlyWrapper(f.get_proof_by_hash)(ctx, w, r)
} else if r.URL.Path == "/ct/v1/get-entries" {
FastlyWrapper(f.get_entries)(ctx, w, r)
} else if r.URL.Path == "/ct/v1/get-entry-and-proof" {
FastlyWrapper(f.get_entry_and_proof)(ctx, w, r)
} else if r.URL.Path == "/" {
w.WriteHeader(fsthttp.StatusOK)
fmt.Fprintln(w, "OK\nhttps://github.com/aditsachde/itko?tab=readme-ov-file#public-instance")
} else {
path := r.URL.Path
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}

data, notfound, err := s.Get(ctx, r.URL.Path)

if notfound {
w.WriteHeader(fsthttp.StatusNotFound)
fmt.Fprintln(w, "Not found")
return
}
if err != nil {
w.WriteHeader(fsthttp.StatusInternalServerError)
fmt.Fprintln(w, "Error fetching data")
return
}

w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(data)
}
}

func FastlyWrapper(wrapped func(ctx context.Context, reqBody io.ReadCloser, query url.Values) (resp []byte, code int, err error)) func(c context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
return func(c context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
log.Println("Path:", r.URL.Path, "Method:", r.Method, "Query:", r.URL.Query())

query := r.URL.Query()
resp, code, err := wrapped(c, r.Body, query)

if err != nil {
if code == fsthttp.StatusServiceUnavailable {
w.Header().Set("Retry-After", fmt.Sprintf("%d", 30+rand.Intn(60)))
fsthttp.Error(w, "pool full", code)
} else {
fsthttp.Error(w, err.Error(), code)
}
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
if _, err = w.Write(resp); err != nil {
log.Printf("Error writing response: %v", err)
}
}
}

type FastlyStorage struct {
backend string
}

func (f *FastlyStorage) Get(ctx context.Context, key string) (data []byte, notfounderr bool, err error) {
url := fmt.Sprintf("https://%s/%s", f.backend, key)

req, err := fsthttp.NewRequest("GET", url, nil)
if err != nil {
return nil, false, err
}
resp, err := req.Send(ctx, f.backend)
if err != nil {
return nil, false, err
}
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return nil, true, errors.New(fsthttp.StatusText(resp.StatusCode))
} else {
return nil, false, errors.New(fsthttp.StatusText(resp.StatusCode))
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, false, err
}
return body, false, nil
}
Binary file added pkg/ct2025-monitoring.tar.gz
Binary file not shown.

0 comments on commit 0f7c34b

Please sign in to comment.