Skip to content

Commit

Permalink
changed storage to SQLite and introduces the browse stored CRL view
Browse files Browse the repository at this point in the history
  • Loading branch information
pimg committed Jul 11, 2024
1 parent c828fba commit 8deaecc
Show file tree
Hide file tree
Showing 36 changed files with 1,401 additions and 138 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ build:
@PHONY: gif
gif: build
vhs cassette.tape

@PHONY: sqlc
sqlc:
sqlc generate -f internal/adapter/db/sqlc.yaml
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
A Terminal User Interface (TUI) for inspecting Certificate Revocation Lists (CRL's)

With CertGuard it is currently possible to:
- download new CRL files to the local cache directory
- browse locally downloaded CRL files
- download & save new CRL files to the local storage
- import locally downloaded CRL files to the local storage
- browse stored CRL's
- list entries in a CRL file
- inspect entries in a CRL file

![demo](demo.gif)

## File locations
CertGuard uses two file locations:
- `~/.cache/certguard` for the file cache where CRL files are stored
- `~/.cache/certguard` location of the database/storage file
- `~/.cache/certguard/import` import directory for importing CRLs from file
- `~/.local/share/certguard` for the `debug.log` file

## States
Expand All @@ -22,11 +25,18 @@ Different screens are built using different states. Below is a statemachine depi

![states](states.svg)

## Storage
All information on CRL's and revoked certificates are stored on a local SQLite database.
The Database schema used for Certguard only stores public information:
![database schema](db_schema.svg)

## Development
A MAKE file has been included for convenience:
- `make run` builds and run the `certguard` application in `debug` mode
- `make test` runs all unit tests
- `make lint` runs the linter
- `make build` builds the binary file `cg`
- `make sqlc` generates the Go source files from SQL files using sqlc
- `make gif` generates the gif based on the cassette.tape using vhs

Since a TUI application cannot log to `stdout` a `debug.log` file is used for debug logging. It is located at: `~/.local/share/certguard/debug.log`
48 changes: 42 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cmd

import (
"context"
"errors"
"fmt"
"log"
"os"
"path/filepath"

tea "github.com/charmbracelet/bubbletea"
"github.com/pimg/certguard/internal/adapter"
"github.com/pimg/certguard/internal/adapter/db"
"github.com/pimg/certguard/internal/ports/models"
"github.com/pimg/certguard/pkg/domain/crl"
"github.com/spf13/cobra"
)

Expand All @@ -18,9 +21,9 @@ func init() {

var rootCmd = &cobra.Command{
Version: "v0.0.1",
Use: "crl",
Long: "Crl Inspector (crl) can download and inspect x.509 Certificate Revocation Lists",
Example: "crl",
Use: "certguard",
Long: "Certguard can download, store and inspect x.509 Certificate Revocation Lists",
Example: "certguard",
RunE: runInteractiveCertGuard,
}

Expand Down Expand Up @@ -48,11 +51,35 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
defer f.Close()
}

cacheDir, err := adapter.NewFileCache()
cacheDir, err := determineCacheDir()
if err != nil {
return err
}
log.Printf("file cache initialized at: %s", cacheDir)

dbConnection, err := db.NewDBConnection(cacheDir)
if err != nil {
return err
}

libsqlStorage := db.NewLibSqlStorage(dbConnection)
defer func() {
err := libsqlStorage.CloseDB()
if err != nil {
log.Printf("could not close database: %v", err)
}
}()

err = libsqlStorage.InitDB(context.Background())
if err != nil {
return err
}

_, err = crl.NewStorage(libsqlStorage, cacheDir) // TODO consider better setup for this
if err != nil {
return err
}

log.Printf("cache initialized at: %s", cacheDir)

if _, err := tea.NewProgram(models.NewBaseModel(), tea.WithAltScreen()).Run(); err != nil {
return err
Expand All @@ -63,3 +90,12 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
func Execute() error {
return rootCmd.Execute()
}

func determineCacheDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", errors.New("could not create file path to User home dir, Cache will not be enabled")
}

return filepath.Join(homeDir, ".cache", "certguard"), nil
}
36 changes: 36 additions & 0 deletions db_schema.plantuml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@startuml

!theme plain
top to bottom direction
skinparam linetype ortho

class certificate_revocation_list {
name: text
signature: blob
this_update: date
next_update: date
url: text
raw: blob
id: integer
}
class gorp_migrations {
applied_at: datetime
id: varchar(255)
}
class revoked_certificate {
serialnumber: text
revocation_date: date
reason: text
revocation_list: integer
id: integer
}
class sqlite_master {
type: text
name: text
tbl_name: text
rootpage: int
sql: text
}

revoked_certificate -[#595959,plain]-^ certificate_revocation_list : "revocation_list:id"
@enduml
1 change: 1 addition & 0 deletions db_schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module github.com/pimg/certguard

go 1.21
go 1.22

require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/lipgloss v0.11.1
github.com/rubenv/sql-migrate v1.6.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/tursodatabase/go-libsql v0.0.0-20240429120401-651096bbee0b
)

require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.3 // indirect
Expand All @@ -20,7 +23,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -34,6 +39,7 @@ require (
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
Loading

0 comments on commit 8deaecc

Please sign in to comment.