Skip to content

Commit

Permalink
Attestor json schema (#443)
Browse files Browse the repository at this point in the history
Adding json schemas from go-witness to the docs
---------

Signed-off-by: chaosinthecrd <tom@tmlabs.co.uk>
Signed-off-by: John Kjell <john@testifysec.com>
Co-authored-by: John Kjell <john@testifysec.com>
  • Loading branch information
ChaosInTheCRD and jkjell committed May 13, 2024
1 parent d866f90 commit 8e1f2fc
Show file tree
Hide file tree
Showing 41 changed files with 7,937 additions and 36 deletions.
65 changes: 62 additions & 3 deletions cmd/attestors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package cmd

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"

Expand All @@ -27,21 +29,48 @@ import (

func AttestorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "attestors",
Use: "attestors",
Short: "Get information about available attestors",
Long: "Get information about all the available attestors in Witness",
}

cmd.AddCommand(SchemaCmd())
cmd.AddCommand(ListCmd())

return cmd
}

func ListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all available attestors",
Long: "Lists all the available attestors in Witness with supporting information",
SilenceErrors: true,
SilenceUsage: true,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runAttestors(cmd.Context())
return runList(cmd.Context())
},
}
return cmd
}

func SchemaCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "schema",
Short: "Show the JSON schema of a specific attestor",
Long: "Print the JSON schema of the predicate that the specified attestor generates",
SilenceErrors: true,
SilenceUsage: true,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runSchema(cmd.Context(), args)
},
}
return cmd
}

func runAttestors(ctx context.Context) error {
func runList(ctx context.Context) error {
items := [][]string{}
entries := attestation.RegistrationEntries()
for _, entry := range entries {
Expand Down Expand Up @@ -73,3 +102,33 @@ func runAttestors(ctx context.Context) error {

return nil
}

func runSchema(ctx context.Context, args []string) error {
if len(args) == 0 {
return fmt.Errorf("You must specify an attestor to view the schema of. Use 'witness attestors' for a list of available attestors.")
} else if len(args) > 1 {
return fmt.Errorf("You can only get one attestor schema at a time.")
}

attestor, err := attestation.GetAttestor(args[0])
if err != nil {
return fmt.Errorf("Error getting attestor: %w", err)
}

schema := attestor.Schema()
schemaJson, err := schema.MarshalJSON()
if err != nil {
return fmt.Errorf("Error marshalling JSON schema: %w", err)
}

var indented bytes.Buffer
err = json.Indent(&indented, schemaJson, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON schema:", err)
os.Exit(1)
}

fmt.Print(indented.String())

return nil
}
67 changes: 67 additions & 0 deletions docgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/in-toto/witness/cmd"
"github.com/spf13/cobra/doc"

_ "github.com/in-toto/go-witness"
"github.com/in-toto/go-witness/attestation"
)

var directory string
Expand All @@ -32,6 +38,7 @@ func init() {
}

func main() {
log.Println("Generating CLI Reference documentation")
mdContent := "# Witness CLI Reference\n\nThis is the reference for the Witness command line tool, generated by [Cobra](https://cobra.dev/).\n\n"
// Generate markdown content for all commands
for _, command := range cmd.New().Commands() {
Expand All @@ -55,4 +62,64 @@ func main() {
fmt.Println("Error writing to file:", err)
os.Exit(1)
}

log.Println("Documentation generated successfully")

entries := attestation.RegistrationEntries()
for _, entry := range entries {
att := entry.Factory()
schema := att.Schema()
schemaJson, err := schema.MarshalJSON()
if err != nil {
fmt.Println("Error marshalling JSON schema:", err)
os.Exit(1)
}

var indented bytes.Buffer
err = json.Indent(&indented, schemaJson, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON schema:", err)
os.Exit(1)
}

schemaContent := "## Schema" + "\n```json\n" + indented.String() + "```\n"
err = os.WriteFile(fmt.Sprintf("%s/attestors/%s.json", directory, att.Name()), []byte(indented.String()+"\n "), 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
os.Exit(1)
}
log.Printf("Schema for %s written to %s/attestors/%s.json\n", att.Name(), directory, att.Name())
f, err := os.ReadFile(fmt.Sprintf("%s/attestors/%s.md", directory, att.Name()))
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}

// Find the index of "## Schema" string
index := strings.Index(string(f), "## Schema")
if index == -1 {
f = append(f, schemaContent...)

err = os.WriteFile(fmt.Sprintf("%s/attestors/%s.md", directory, att.Name()), f, 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
os.Exit(1)
}
continue
}

// Truncate the content to remove everything after "## Schema"
f = f[:index]

f = append(f, schemaContent...)

err = os.WriteFile(fmt.Sprintf("%s/attestors/%s.md", directory, att.Name()), f, 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
os.Exit(1)
}

log.Printf("Schema for %s written to %s/attestors/%s.md\n", att.Name(), directory, att.Name())

}
}
4 changes: 4 additions & 0 deletions docgen/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ set -e
tmpdir=$(mktemp -d)
tmpdir2=$(mktemp -d)
cp docs/commands.md "$tmpdir2/"
mkdir "$tmpdir2/attestors"
mkdir "$tmpdir/attestors"
cp docs/attestors/* "$tmpdir2/attestors/"
cp docs/attestors/*.md "$tmpdir/attestors/"
go run ./docgen --dir "$tmpdir"
echo "###########################################"
echo "If diffs are found, run: make docgen"
Expand Down
16 changes: 0 additions & 16 deletions docs/attestors/aws-iid.md

This file was deleted.

97 changes: 97 additions & 0 deletions docs/attestors/aws.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/Attestor",
"$defs": {
"Attestor": {
"properties": {
"devpayProductCodes": {
"items": {
"type": "string"
},
"type": "array"
},
"marketplaceProductCodes": {
"items": {
"type": "string"
},
"type": "array"
},
"availabilityZone": {
"type": "string"
},
"privateIp": {
"type": "string"
},
"version": {
"type": "string"
},
"region": {
"type": "string"
},
"instanceId": {
"type": "string"
},
"billingProducts": {
"items": {
"type": "string"
},
"type": "array"
},
"instanceType": {
"type": "string"
},
"accountId": {
"type": "string"
},
"pendingTime": {
"type": "string",
"format": "date-time"
},
"imageId": {
"type": "string"
},
"kernelId": {
"type": "string"
},
"ramdiskId": {
"type": "string"
},
"architecture": {
"type": "string"
},
"rawiid": {
"type": "string"
},
"rawsig": {
"type": "string"
},
"publickey": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"devpayProductCodes",
"marketplaceProductCodes",
"availabilityZone",
"privateIp",
"version",
"region",
"instanceId",
"billingProducts",
"instanceType",
"accountId",
"pendingTime",
"imageId",
"kernelId",
"ramdiskId",
"architecture",
"rawiid",
"rawsig",
"publickey"
]
}
}
}

0 comments on commit 8e1f2fc

Please sign in to comment.