Skip to content

Commit

Permalink
resolve enclave ids to an enclave name for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewarren committed Nov 1, 2019
1 parent c0f8d46 commit b70b09a
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 36 deletions.
108 changes: 108 additions & 0 deletions enclaves.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/GitbookIO/diskache"
"github.com/fatih/color"
"github.com/jakewarren/trustar-golang"
"github.com/rodaine/table"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

// command to list available enclaves
// https://docs.trustar.co/api/v13/enclaves/get_enclaves.html
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists enclaves",
Run: func(cmd *cobra.Command, args []string) {
// get Enclaves
enclaves, err := c.GetEnclaves()
if err != nil {
fmt.Println(err)
}

// TODO: add option to output as json?

prettyPrintEnclaves(enclaves)
},
}

func prettyPrintEnclaves(enclaves []trustar.Enclave) {
headerFmt := color.New(color.FgCyan, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("id", "name", "type", "read", "update", "create")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, e := range enclaves {
tbl.AddRow(e.ID, e.Name, e.Type, e.Read, e.Update, e.Create)
}
tbl.Print()
}

func setupEnclaveCache() error {
var err error

// open up a disk cache
dir := filepath.Join(os.TempDir(), "trustar-enclave-cache")

log.Debug().Str("dir", dir).Msg("setting up enclave")
opts := diskache.Opts{
Directory: fmt.Sprintf("%s", dir),
}

enclaveCache, err = diskache.New(&opts)
if err != nil {
return err
}

stats := enclaveCache.Stats()
if stats.Items == 0 {
return fillEnclaveCache()
}

return nil
}

func fillEnclaveCache() error {
log.Debug().Msg("filling up enclave")

// get all available enclaves from Trustar
enclaves, err := c.GetEnclaves()
if err != nil {
return err
}

// fill the cache associating the enclave id to the enclave name
for _, e := range enclaves {
err = enclaveCache.Set(e.ID, []byte(e.Name))
if err != nil {
return err
}
}

return nil
}

func lookupEnclave(enclaveID string) (enclaveName string) {
onlyOnce.Do(func() {
err := setupEnclaveCache()
if err != nil {
log.Error().Err(err).Msg("error setting up cache")
}
})

name, exists := enclaveCache.Get(enclaveID)

if exists {
enclaveName = string(name)
} else {
// if we can't resolve the id to an enclave name, just return the id
enclaveName = enclaveID
}

return enclaveName
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/jakewarren/trustar
go 1.13

require (
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1
github.com/GitbookIO/syncgroup v0.0.0-20181003125046-3e73b2e6a972 // indirect
github.com/briandowns/spinner v1.7.0
github.com/fatih/color v1.7.0
github.com/jakewarren/trustar-golang v0.0.0-20191029192456-68eb70eb9bf6
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1 h1:1ui53h0HCYjyrlza+yY+sQsulJdHdYL/xdVWIH3UsyE=
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1/go.mod h1:TTHndD25/UJVOyBl/vOq2g5RIg4bidGlmtzb+4Zr+Nw=
github.com/GitbookIO/syncgroup v0.0.0-20181003125046-3e73b2e6a972 h1:t9LCqP1L6hvq0uQAzFp20CPpHzlZGLoNFG2bm4I/4F4=
github.com/GitbookIO/syncgroup v0.0.0-20181003125046-3e73b2e6a972/go.mod h1:2Cn3sd0iZa+NmA9dsv8p/fQMx0TK+jzQUbJSjULomrc=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
12 changes: 10 additions & 2 deletions indicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var indicatorFindCorrelatedReportsCmd = &cobra.Command{

headerFmt := color.New(color.FgCyan, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("id", "title", "created", "updated")
tbl := table.New("id", "title", "created", "updated", "enclave")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

formatTime := func(e int64) string {
Expand All @@ -103,7 +103,15 @@ var indicatorFindCorrelatedReportsCmd = &cobra.Command{
}

for _, r := range correlatedReports.Items {
tbl.AddRow(r.ID, r.Title, formatTime(r.Created), formatTime(r.Updated))
// AFAIK each report will only have one enclave attached to it
// but the API specifies it is an array so proccessing the entire thing just to be safe
associatedEnclaves := make([]string, 0)

for _, e := range r.EnclaveIds {
associatedEnclaves = append(associatedEnclaves, lookupEnclave(e.(string)))
}

tbl.AddRow(r.ID, r.Title, formatTime(r.Created), formatTime(r.Updated), strings.Join(associatedEnclaves, ","))
}

if len(correlatedReports.Items) > 0 {
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"io/ioutil"
"os"
"sync"
"time"

"github.com/GitbookIO/diskache"
"github.com/briandowns/spinner"
"github.com/jakewarren/trustar-golang"
"github.com/rs/zerolog"
Expand All @@ -17,6 +19,10 @@ import (
var (
c *trustar.Client

// disk cache to hold enclave information
enclaveCache *diskache.Diskache
onlyOnce sync.Once

// global config struct to hold all the flags
config = struct {
whitelistDelete struct {
Expand Down Expand Up @@ -88,6 +94,9 @@ func main() {
log.Fatal().Err(err).Msg("error creating client")
}

// TODO: add a verbose parameter to specify the logging level
zerolog.SetGlobalLevel(zerolog.DebugLevel)

// TODO: add option for user to log to file
c.SetLog(ioutil.Discard)

Expand Down
32 changes: 0 additions & 32 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,10 @@ import (
"runtime"
"text/tabwriter"

"github.com/fatih/color"
"github.com/jakewarren/trustar-golang"
"github.com/rodaine/table"
"github.com/spf13/cobra"
)

// command to list available enclaves
// https://docs.trustar.co/api/v13/enclaves/get_enclaves.html
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists enclaves",
Run: func(cmd *cobra.Command, args []string) {
// get Enclaves
enclaves, err := c.GetEnclaves()
if err != nil {
fmt.Println(err)
}

// TODO: add option to output as json?

prettyPrint(enclaves)
},
}

// command to print token
var tokenCmd = &cobra.Command{
Use: "token",
Expand Down Expand Up @@ -81,18 +61,6 @@ var quotaCmd = &cobra.Command{
},
}

func prettyPrint(enclaves []trustar.Enclave) {
headerFmt := color.New(color.FgCyan, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("id", "name", "type", "read", "update", "create")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, e := range enclaves {
tbl.AddRow(e.ID, e.Name, e.Type, e.Read, e.Update, e.Create)
}
tbl.Print()
}

// TODO: document how to use this in the README
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Expand Down
14 changes: 12 additions & 2 deletions reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"os"
"strings"

"github.com/fatih/color"
"github.com/jakewarren/trustar-golang"
Expand All @@ -29,7 +30,7 @@ var reportSearchCmd = &cobra.Command{

headerFmt := color.New(color.FgCyan, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("id", "title", "created", "updated")
tbl := table.New("id", "title", "created", "updated", "enclave")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

formatTime := func(e int64) string {
Expand All @@ -56,7 +57,16 @@ var reportSearchCmd = &cobra.Command{
}

for _, r := range reports.Reports {
tbl.AddRow(r.ID, r.Title, formatTime(r.Created), formatTime(r.Updated))

// AFAIK each report will only have one enclave attached to it
// but the API specifies it is an array so proccessing the entire thing just to be safe
associatedEnclaves := make([]string, 0)

for _, e := range r.EnclaveIds {
associatedEnclaves = append(associatedEnclaves, lookupEnclave(e))
}

tbl.AddRow(r.ID, r.Title, formatTime(r.Created), formatTime(r.Updated), strings.Join(associatedEnclaves, ","))
numOfReports++
}

Expand Down

0 comments on commit b70b09a

Please # to comment.