Skip to content

Commit

Permalink
added revoked certificate list view (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimg authored Mar 16, 2024
1 parent 56290e7 commit dbb5d14
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
Expand All @@ -41,6 +43,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
7 changes: 3 additions & 4 deletions internal/ports/models/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m BaseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case messages.CRLResponseMsg:
m.state = listView
m.title = titles[listView]
m.list = NewListModel(msg.RevocationList)
m.list = NewListModel(msg.RevocationList, m.width, m.height)
}

// state specific actions
Expand All @@ -155,6 +155,7 @@ func (m BaseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.help.Width = msg.Width
m.list.list.SetSize(msg.Width, msg.Height)
default:
listModel, listCmd := m.list.Update(msg)
m.list = listModel.(ListModel)
Expand Down Expand Up @@ -206,9 +207,7 @@ func (m BaseModel) View() string {
case listView:
title := m.styles.Title.Render(m.title)
listInfo := m.list.View()
helpMenu := m.help.View(&listKeys)
height := strings.Count(listInfo, "\n") + strings.Count(title, "\n")
return lipgloss.JoinVertical(lipgloss.Top, title, listInfo) + lipgloss.Place(m.width, m.height-height-1, lipgloss.Left, lipgloss.Bottom, helpMenu)
return lipgloss.JoinVertical(lipgloss.Top, title, listInfo)
case browseView:
title := m.styles.Title.Render(m.title)
listInfo := m.browse.View()
Expand Down
54 changes: 50 additions & 4 deletions internal/ports/models/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package models
import (
"crypto/x509"
"fmt"
"strconv"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/pimg/certguard/internal/ports/models/styles"
)

// keyMap defines a set of keybindings. To work for help it must satisfy
// key.Map. It could also very easily be a map[string]key.Binding.
type listKeyMap struct {
list.KeyMap
Back key.Binding
Quit key.Binding
}
Expand Down Expand Up @@ -41,36 +45,78 @@ var listKeys = listKeyMap{
),
}

type item struct {
serialnumber, revocationReason, revocationDate string
}

func (i item) Title() string { return i.serialnumber }
func (i item) Description() string { return i.revocationDate }
func (i item) FilterValue() string { return i.serialnumber }

type ListModel struct {
keys listKeyMap
styles *styles.Styles
list list.Model
crl *x509.RevocationList
}

func NewListModel(crl *x509.RevocationList) ListModel {
func NewListModel(crl *x509.RevocationList, width, height int) ListModel {
items := revokedCertificatesToItems(crl.RevokedCertificateEntries)

defaultDelegate := list.NewDefaultDelegate()
c := styles.DefaultStyles().ListComponentTitle
defaultDelegate.Styles.SelectedTitle = defaultDelegate.Styles.SelectedTitle.Foreground(c).BorderLeftForeground(c)
defaultDelegate.Styles.SelectedDesc = defaultDelegate.Styles.SelectedTitle.Copy()

revokedList := list.New(items, defaultDelegate, width, height-12)
revokedList.Title = "Revoked Certificates"
revokedList.KeyMap.Quit = key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit"))
revokedList.KeyMap.ClearFilter = key.NewBinding(key.WithKeys("ctrl+q"), key.WithHelp("ctrl-q", "clear"))
revokedList.KeyMap.CancelWhileFiltering = key.NewBinding(key.WithKeys("ctrl+q"), key.WithHelp("ctrl-q", "clear"))
revokedList.AdditionalShortHelpKeys = listKeys.ShortHelp

revokedList.Styles.Title = revokedList.Styles.Title.Background(c)
return ListModel{
keys: listKeys,
styles: styles.DefaultStyles(),
list: revokedList,
crl: crl,
}
}

func revokedCertificatesToItems(entries []x509.RevocationListEntry) []list.Item {
items := make([]list.Item, 0, len(entries))
for _, entry := range entries {
items = append(items, item{
serialnumber: entry.SerialNumber.String(),
revocationReason: strconv.Itoa(entry.ReasonCode),
revocationDate: entry.RevocationTime.String(),
})
}

return items
}

func (l ListModel) Init() tea.Cmd {
return nil
}

func (l ListModel) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
return l, nil
func (l ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
l.list, cmd = l.list.Update(msg)
return l, cmd
}

func (l ListModel) View() string {
issuer := fmt.Sprintf("CRL Issuer : %s", l.crl.Issuer)
updatedAt := fmt.Sprintf("Updated At : %s", l.crl.ThisUpdate)
nextUpdate := fmt.Sprintf("Next Update : %s", l.crl.NextUpdate)
revokedCertCount := fmt.Sprintf("Revoked Certificates: %d", len(l.crl.RevokedCertificateEntries))
revokedList := l.list.View()

crlInfo := l.styles.Text.Render(
fmt.Sprintf("%s\n%s\n%s\n%s", issuer, updatedAt, nextUpdate, revokedCertCount),
)
return crlInfo

return lipgloss.JoinVertical(lipgloss.Top, crlInfo, revokedList)
}
26 changes: 14 additions & 12 deletions internal/ports/models/styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
)

type Styles struct {
InputField lipgloss.Style
Title lipgloss.Style
Background lipgloss.Style
ErrorMessages lipgloss.Style
Text lipgloss.Style
FilePickerFile lipgloss.Style
FilePickerCurrent lipgloss.Style
InputField lipgloss.Style
Title lipgloss.Style
Background lipgloss.Style
ErrorMessages lipgloss.Style
Text lipgloss.Style
FilePickerFile lipgloss.Style
FilePickerCurrent lipgloss.Style
ListComponentTitle lipgloss.Color
}

func DefaultStyles() *Styles {
Expand All @@ -22,10 +23,11 @@ func DefaultStyles() *Styles {
PaddingTop(2).
PaddingBottom(2).
PaddingLeft(2),
Background: lipgloss.NewStyle().Background(lipgloss.Color("#282828")),
ErrorMessages: lipgloss.NewStyle().Background(lipgloss.Color("#FB4934")).BorderForeground(lipgloss.Color("#FB4934")).BorderStyle(lipgloss.NormalBorder()).Width(80).Padding(1),
Text: lipgloss.NewStyle().Foreground(lipgloss.Color("#B8BB26")).Padding(1).Width(80),
FilePickerFile: lipgloss.NewStyle().Foreground(lipgloss.Color("#83A598")),
FilePickerCurrent: lipgloss.NewStyle().Foreground(lipgloss.Color("#B8BB26")),
Background: lipgloss.NewStyle().Background(lipgloss.Color("#282828")),
ErrorMessages: lipgloss.NewStyle().Background(lipgloss.Color("#FB4934")).BorderForeground(lipgloss.Color("#FB4934")).BorderStyle(lipgloss.NormalBorder()).Width(80).Padding(1),
Text: lipgloss.NewStyle().Foreground(lipgloss.Color("#B8BB26")).Padding(1).Width(80),
FilePickerFile: lipgloss.NewStyle().Foreground(lipgloss.Color("#83A598")),
FilePickerCurrent: lipgloss.NewStyle().Foreground(lipgloss.Color("#B8BB26")),
ListComponentTitle: "#83A598",
}
}

0 comments on commit dbb5d14

Please sign in to comment.