Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Jun 3, 2024
2 parents 97398ed + edf0c74 commit 83eb7a5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-rock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
docker run -d -p 3000:3000 --name gocert gocert:latest
- name: Load config
run: |
docker exec gocert /bin/pebble mkdir /etc/config
docker exec gocert /usr/bin/pebble mkdir /etc/config
docker cp key.pem gocert:/etc/config/key.pem
docker cp cert.pem gocert:/etc/config/cert.pem
docker cp config.yaml gocert:/etc/config/config.yaml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:
name: frontend-static-files
path: ui/out
- name: golangci-lint
uses: golangci/golangci-lint-action@v5
uses: golangci/golangci-lint-action@v6
with:
version: v1.54
2 changes: 1 addition & 1 deletion .github/workflows/publish-rock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3.1.0
uses: docker/login-action@v3.2.0
with:
registry: ghcr.io
username: ${{ github.actor }}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.1

require (
github.com/mattn/go-sqlite3 v1.14.22
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_golang v1.19.1
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -15,6 +15,6 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
Expand All @@ -23,8 +23,8 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
22 changes: 11 additions & 11 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ type PrometheusMetrics struct {
RequestsDuration prometheus.HistogramVec
}

// NewMetricsSubsystem returns the metrics endpoint HTTP handler and the Prometheus metrics for the server and middleware.
// NewMetricsSubsystem returns the metrics endpoint HTTP handler and the Prometheus metrics collectors for the server and middleware.
func NewMetricsSubsystem(db *certdb.CertificateRequestsRepository) *PrometheusMetrics {
csrs, err := db.RetrieveAll()
if err != nil {
log.Println(errors.Join(errors.New("error generating metrics repository: "), err))
}
metricsBackend := newPrometheusMetrics()
metricsBackend.generateMetrics(csrs)
metricsBackend.Handler = promhttp.HandlerFor(metricsBackend.registry, promhttp.HandlerOpts{})
ticker := time.NewTicker(120 * time.Second)
go func() {
for range ticker.C {
metricsBackend.generateMetrics(csrs)
for ; ; <-ticker.C {
csrs, err := db.RetrieveAll()
if err != nil {
log.Println(errors.Join(errors.New("error generating metrics repository: "), err))
panic(1)
}
metricsBackend.GenerateMetrics(csrs)
}
}()
metricsBackend.Handler = promhttp.HandlerFor(metricsBackend.registry, promhttp.HandlerOpts{})
return metricsBackend
}

Expand Down Expand Up @@ -83,9 +83,9 @@ func newPrometheusMetrics() *PrometheusMetrics {
return m
}

// generateMetrics receives the live list of csrs to calculate the most recent values for the metrics
// GenerateMetrics receives the live list of csrs to calculate the most recent values for the metrics
// defined for prometheus
func (pm *PrometheusMetrics) generateMetrics(csrs []certdb.CertificateRequest) {
func (pm *PrometheusMetrics) GenerateMetrics(csrs []certdb.CertificateRequest) {
var csrCount float64 = float64(len(csrs))
var outstandingCSRCount float64
var certCount float64
Expand Down
29 changes: 19 additions & 10 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"math/big"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand All @@ -23,7 +23,7 @@ import (
func TestPrometheusHandler(t *testing.T) {
db, err := certdb.NewCertificateRequestsRepository(":memory:", "CertificateReq")
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
m := metrics.NewMetricsSubsystem(db)

Expand All @@ -46,7 +46,7 @@ func TestPrometheusHandler(t *testing.T) {
}
err = db.Close()
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
}

Expand Down Expand Up @@ -79,28 +79,37 @@ func generateCertPair(daysRemaining int) (string, string) {
return csr, cert
}

func initializeTestDB(db *certdb.CertificateRequestsRepository) {
func initializeTestDB(t *testing.T, db *certdb.CertificateRequestsRepository) {
for i, v := range []int{5, 10, 32} {
csr, cert := generateCertPair(v)
_, err := db.Create(csr)
if err != nil {
log.Fatalf("couldn't create test csr:%s", err)
t.Fatalf("couldn't create test csr:%s", err)
}
_, err = db.Update(fmt.Sprint(i+1), cert)
if err != nil {
log.Fatalf("couldn't create test cert:%s", err)
t.Fatalf("couldn't create test cert:%s", err)
}
}
}

// TestMetrics tests some of the metrics that we currently collect.
func TestMetrics(t *testing.T) {
db, err := certdb.NewCertificateRequestsRepository(":memory:", "CertificateReq")
f, err := os.CreateTemp("./","*.db")
fmt.Print(f.Name())
if err != nil {
t.Fatal("couldn't create temp db file: "+ err.Error())
}
defer f.Close()
defer os.Remove(f.Name())
db, err := certdb.NewCertificateRequestsRepository(f.Name(), "CertificateReq")
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
initializeTestDB(db)
initializeTestDB(t, db)
m := metrics.NewMetricsSubsystem(db)
csrs, _ := db.RetrieveAll()
m.GenerateMetrics(csrs)

request, _ := http.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder()
Expand Down Expand Up @@ -150,6 +159,6 @@ func TestMetrics(t *testing.T) {

err = db.Close()
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
}
5 changes: 3 additions & 2 deletions rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: gocert
base: bare
build-base: ubuntu@22.04
build-base: ubuntu@24.04
version: '0.0.2'
summary: A certificate management tool
description: |
Expand Down Expand Up @@ -30,4 +30,5 @@ parts:
craftctl default
stage-packages:
- ca-certificates_data
- libc6_libs
- libc6_libs
- base-files_lib

0 comments on commit 83eb7a5

Please sign in to comment.