Skip to content
Open
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
13 changes: 13 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Goal
<!-- What does this PR accomplish? 1 sentence. -->

## Changes
-

## Testing
<!-- How did you verify it? -->

## Checklist
- [ ] Title is a clear sentence (≤ 70 chars)
- [ ] Commits are signed (`git log --show-signature`)
- [ ] `submissions/labN.md` updated
115 changes: 115 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: CI

on:
push:
branches:
- main
paths:
- "app/**"
- ".github/workflows/**"

pull_request:
branches:
- main
paths:
- "app/**"
- ".github/workflows/**"

permissions:
contents: read

jobs:
vet:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
go-version: ["1.23", "1.24"]

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: ${{ matrix.go-version }}
cache: true
cache-dependency-path: app/go.mod

- name: Go Vet
working-directory: app
run: go vet ./...

test:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
go-version: ["1.23", "1.24"]

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: ${{ matrix.go-version }}
cache: true
cache-dependency-path: app/go.mod

- name: Go Test
working-directory: app
run: go test -race -count=1 ./...

lint:
runs-on: ubuntu-24.04

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: "1.24"
cache: true
cache-dependency-path: app/go.mod

- name: GolangCI-Lint
uses: golangci/golangci-lint-action@25e2cdc5eb1d7a04fdc45ff538f1a00e960ae128 # v8.0.0
with:
version: v2.5.0
working-directory: app

# Lab 9 (bonus): reachability-based vulnerability gate.
govulncheck:
runs-on: ubuntu-24.04

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Setup Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: "1.26"
cache: true
cache-dependency-path: app/go.mod

- name: Install govulncheck
run: go install golang.org/x/vuln/cmd/govulncheck@v1.1.4

- name: Run govulncheck
working-directory: app
run: govulncheck ./...

ci-ok:
if: always()
needs: [vet, test, lint, govulncheck]
runs-on: ubuntu-24.04
steps:
- name: Verify all checks passed
run: |
test "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "false"
22 changes: 22 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.26-alpine AS builder

WORKDIR /build

COPY go.mod ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags='-s -w' -o /out/quicknotes . && \
CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags='-s -w' -o /out/healthcheck ./healthcheck/ && \
mkdir -p /out/data

FROM gcr.io/distroless/static:nonroot

COPY --from=builder /out/quicknotes /quicknotes
COPY --from=builder /out/healthcheck /healthcheck
COPY --from=builder /build/seed.json /seed.json
COPY --chown=65532:65532 --from=builder /out/data /data

USER nonroot
EXPOSE 8080
ENTRYPOINT ["/quicknotes"]
13 changes: 13 additions & 0 deletions app/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"net/http"
"os"
)

func main() {
resp, err := http.Get("http://localhost:8080/health")
if err != nil || resp.StatusCode != http.StatusOK {
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
server := NewServer(store)
srv := &http.Server{
Addr: addr,
Handler: server.Routes(),
Handler: server.Handler(),
ReadHeaderTimeout: 5 * time.Second,
}

Expand Down
18 changes: 18 additions & 0 deletions app/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "net/http"

func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("X-Content-Type-Options", "nosniff")
h.Set("X-Frame-Options", "DENY")
h.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'")
h.Set("Referrer-Policy", "no-referrer")
next.ServeHTTP(w, r)
})
}

func (s *Server) Handler() http.Handler {
return securityHeaders(s.Routes())
}
42 changes: 42 additions & 0 deletions app/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"
)

// wantSecurityHeaders is the exact set the securityHeaders middleware must apply to
// every response. If the middleware is removed from Server.Handler, none of these are
// present and this test fails.
var wantSecurityHeaders = map[string]string{
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'",
"Referrer-Policy": "no-referrer",
}

func TestSecurityHeaders_PresentOnAllRoutes(t *testing.T) {
srv := newTestServer(t)
handler := srv.Handler()

// Exercise a real route, an error route, and an unregistered path to prove the
// middleware wraps the whole router and not just the happy path.
routes := []struct{ method, target string }{
{http.MethodGet, "/health"},
{http.MethodGet, "/notes"},
{http.MethodGet, "/notes/999"}, // 404 from a handler
{http.MethodGet, "/does-not-exist"}, // 404 from the mux itself
}

for _, rt := range routes {
req := httptest.NewRequest(rt.method, rt.target, nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
for name, want := range wantSecurityHeaders {
if got := rec.Header().Get(name); got != want {
t.Errorf("%s %s: header %q = %q, want %q", rt.method, rt.target, name, got, want)
}
}
}
}
40 changes: 40 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
services:
volume-init:
image: busybox:1.36-musl
volumes:
- quicknotes-data:/data
command: ["sh", "-c", "chown 65532:65532 /data"]
restart: "no"

quicknotes:
build:
context: ./app
image: quicknotes:lab6
depends_on:
volume-init:
condition: service_completed_successfully
ports:
- "8080:8080"
environment:
ADDR: ":8080"
DATA_PATH: "/data/notes.json"
SEED_PATH: "/seed.json"
volumes:
- quicknotes-data:/data
healthcheck:
test: ["CMD", "/healthcheck"]
interval: 10s
timeout: 3s
retries: 3
start_period: 5s
restart: unless-stopped
read_only: true
tmpfs:
- /tmp:size=16m,mode=1777
cap_drop:
- ALL
security_opt:
- no-new-privileges:true

volumes:
quicknotes-data:
Loading