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
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: quicknotes-ci

on:
push:
branches: [main]
paths:
- "app/**"
- ".github/workflows/ci.yml"
- "submissions/**"
pull_request:
branches: [main]
paths:
- "app/**"
- ".github/workflows/ci.yml"
- "submissions/**"

permissions:
contents: read

jobs:
vet:
name: vet (${{ matrix.go-version }})
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
go-version: ["1.23", "1.24"]
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
with:
go-version: ${{ matrix.go-version }}
cache: true
cache-dependency-path: app/go.mod
- run: go vet ./...

test:
name: test (${{ matrix.go-version }})
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
go-version: ["1.23", "1.24"]
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
with:
go-version: ${{ matrix.go-version }}
cache: true
cache-dependency-path: app/go.mod
- run: go test -race -count=1 ./...

lint:
name: lint
runs-on: ubuntu-24.04
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
with:
go-version: "1.24"
cache: true
cache-dependency-path: app/go.mod
- uses: golangci/golangci-lint-action@db582008a42febd596419635a5abc9d9815daa9c # v9.2.1
with:
version: v2.5.0
working-directory: app

ci-ok:
name: ci-ok
if: always()
needs: [vet, test, lint]
runs-on: ubuntu-24.04
steps:
- run: |
test "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "false"
46 changes: 46 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Release image

on:
push:
tags:
- "v*"

permissions:
contents: read
packages: write

env:
IMAGE_NAME: ghcr.io/buiniyyarik/devops-intro/quicknotes

jobs:
build-and-push:
name: Build and push QuickNotes image
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435

- name: Login to GitHub Container Registry
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract tag
id: meta
shell: bash
run: echo "version=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"

- name: Build and push
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
with:
context: ./app
push: true
tags: |
${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
${{ env.IMAGE_NAME }}:latest
58 changes: 58 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.hostname = "quicknotes-vm"

config.vm.network "forwarded_port",
guest: 8080,
host: 18080,
host_ip: "127.0.0.1"

config.vm.synced_folder "./app", "/opt/quicknotes/app", type: "virtualbox"

config.vm.provider "virtualbox" do |vb|
vb.name = "quicknotes-lab5"
vb.cpus = 2
vb.memory = 1024
end

config.vm.provision "shell", inline: <<-SHELL
set -eux

GO_VERSION="1.24.5"
GO_TARBALL="go${GO_VERSION}.linux-amd64.tar.gz"

apt-get update
apt-get install -y curl ca-certificates build-essential

if ! /usr/local/go/bin/go version 2>/dev/null | grep -q "go${GO_VERSION}"; then
rm -rf /usr/local/go
curl -fsSLo "/tmp/${GO_TARBALL}" "https://go.dev/dl/${GO_TARBALL}"
tar -C /usr/local -xzf "/tmp/${GO_TARBALL}"
ln -sf /usr/local/go/bin/go /usr/local/bin/go
ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt
fi

cd /opt/quicknotes/app
/usr/local/go/bin/go build -o /usr/local/bin/quicknotes .

cat >/etc/systemd/system/quicknotes.service <<'EOF'
[Unit]
Description=QuickNotes service
After=network.target

[Service]
WorkingDirectory=/opt/quicknotes/app
Environment=ADDR=:8080
ExecStart=/usr/local/bin/quicknotes
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable quicknotes
systemctl restart quicknotes
SHELL
end
Binary file added ansible/files/quicknotes
Binary file not shown.
2 changes: 2 additions & 0 deletions ansible/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[quicknotes]
lab5-vm ansible_host=172.22.0.1 ansible_port=2223 ansible_user=vagrant ansible_ssh_private_key_file=~/.ssh/vagrant-lab7/private_key ansible_python_interpreter=/usr/bin/python3 ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
74 changes: 74 additions & 0 deletions ansible/playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: Deploy QuickNotes
hosts: quicknotes
become: true
gather_facts: false

vars:
quicknotes_user: quicknotes
quicknotes_data_dir: /var/lib/quicknotes
listen_addr: ":8080"
data_path: /var/lib/quicknotes/notes.json
seed_path: /var/lib/quicknotes/seed.json
restart_sec: 2

tasks:
- name: Ensure quicknotes system user exists
ansible.builtin.user:
name: "{{ quicknotes_user }}"
system: true
shell: /usr/sbin/nologin
create_home: false

- name: Ensure data directory exists
ansible.builtin.file:
path: "{{ quicknotes_data_dir }}"
state: directory
owner: "{{ quicknotes_user }}"
group: "{{ quicknotes_user }}"
mode: "0750"

- name: Copy seed file
ansible.builtin.copy:
src: ../app/seed.json
dest: "{{ seed_path }}"
owner: "{{ quicknotes_user }}"
group: "{{ quicknotes_user }}"
mode: "0640"

- name: Copy QuickNotes binary
ansible.builtin.copy:
src: files/quicknotes
dest: /usr/local/bin/quicknotes
owner: root
group: root
mode: "0755"
notify: restart quicknotes

- name: Render systemd unit
ansible.builtin.template:
src: templates/quicknotes.service.j2
dest: /etc/systemd/system/quicknotes.service
owner: root
group: root
mode: "0644"
notify:
- reload systemd
- restart quicknotes

- name: Enable and start QuickNotes service
ansible.builtin.systemd:
name: quicknotes
enabled: true
state: started
daemon_reload: true

handlers:
- name: reload systemd
ansible.builtin.systemd:
daemon_reload: true

- name: restart quicknotes
ansible.builtin.systemd:
name: quicknotes
state: restarted
18 changes: 18 additions & 0 deletions ansible/templates/quicknotes.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=QuickNotes service
After=network-online.target
Wants=network-online.target

[Service]
User={{ quicknotes_user }}
Group={{ quicknotes_user }}
WorkingDirectory={{ quicknotes_data_dir }}
Environment=ADDR={{ listen_addr }}
Environment=DATA_PATH={{ data_path }}
Environment=SEED_PATH={{ seed_path }}
ExecStart=/usr/local/bin/quicknotes
Restart=on-failure
RestartSec={{ restart_sec }}

[Install]
WantedBy=multi-user.target
26 changes: 26 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# syntax=docker/dockerfile:1

FROM golang:1.24-alpine AS builder

WORKDIR /src

COPY go.mod ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" -o /out/quicknotes .

FROM gcr.io/distroless/static-debian12:nonroot

WORKDIR /

COPY --from=builder /out/quicknotes /quicknotes
COPY --from=builder /src/seed.json /seed.json

EXPOSE 8080

USER nonroot:nonroot

ENTRYPOINT ["/quicknotes"]
4 changes: 2 additions & 2 deletions app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func NewServer(store *Store) *Server {
return &Server{store: store, requestsByCode: by}
}

func (s *Server) Routes() *http.ServeMux {
func (s *Server) Routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /health", s.wrap(s.handleHealth))
mux.HandleFunc("GET /metrics", s.wrap(s.handleMetrics))
mux.HandleFunc("GET /notes", s.wrap(s.handleListNotes))
mux.HandleFunc("POST /notes", s.wrap(s.handleCreateNote))
mux.HandleFunc("GET /notes/{id}", s.wrap(s.handleGetNote))
mux.HandleFunc("DELETE /notes/{id}", s.wrap(s.handleDeleteNote))
return mux
return securityHeaders(mux)
}

type statusWriter struct {
Expand Down
34 changes: 34 additions & 0 deletions app/handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"bytes"
"encoding/json"
"net/http"
Expand Down Expand Up @@ -131,3 +132,36 @@ func TestMetrics_ExposesPrometheusFormat(t *testing.T) {
}
}

func TestSecurityHeadersPresent(t *testing.T) {
path := t.TempDir() + "/notes.json"
if err := os.WriteFile(path, []byte("[]"), 0o644); err != nil {
t.Fatalf("write test store: %v", err)
}

store, err := NewStore(path)
if err != nil {
t.Fatalf("new store: %v", err)
}

server := NewServer(store)

req := httptest.NewRequest(http.MethodGet, "/health", nil)
rr := httptest.NewRecorder()

server.Routes().ServeHTTP(rr, req)

tests := map[string]string{
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "default-src 'none'",
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
"Cache-Control": "no-store",
}

for header, want := range tests {
if got := rr.Header().Get(header); got != want {
t.Fatalf("%s = %q, want %q", header, got, want)
}
}
}
Loading