Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attestor json schema #443

Merged
merged 27 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3d887a4
testing out json schema
ChaosInTheCRD Mar 28, 2024
447d914
lets see
ChaosInTheCRD Mar 28, 2024
9fcecae
trying this
ChaosInTheCRD Mar 28, 2024
8b49aa2
moving to mdx
ChaosInTheCRD Mar 28, 2024
1ba8ffd
saving changes to client
ChaosInTheCRD Apr 12, 2024
9970630
switching error logs to be error returns (should ensure exit 1 is exit
ChaosInTheCRD Apr 30, 2024
6388fc9
Merge branch 'main' into improve-verify-errors
ChaosInTheCRD May 3, 2024
be848b5
changing failure statement to match changes in go-witness
ChaosInTheCRD May 8, 2024
ec353c2
fixing go mod
ChaosInTheCRD May 8, 2024
e69916d
Merge branch 'improve-verify-errors' of github.com:ChaosInTheCRD/witn…
ChaosInTheCRD May 8, 2024
5e3ed0f
adding check for a source
ChaosInTheCRD May 8, 2024
2f1e173
adding changes to appropriately inspect evidence
ChaosInTheCRD May 8, 2024
f18e8dc
adding changes for json schemas
ChaosInTheCRD May 9, 2024
e4f20d9
Merge branch 'main' of github.com:in-toto/witness into attestor-json-…
ChaosInTheCRD May 9, 2024
b8484c5
Merge branch 'feat/verification-attestation' of github.com:ChaosInThe…
ChaosInTheCRD May 9, 2024
5807ce8
changing schemas
ChaosInTheCRD May 9, 2024
55917ef
adding schema for link, policyverify, slsa
ChaosInTheCRD May 9, 2024
ce45f1f
weird I thought I did this yesterday
ChaosInTheCRD May 10, 2024
d29c928
making final changes
ChaosInTheCRD May 10, 2024
dca71ac
removed replcae
ChaosInTheCRD May 10, 2024
73e1828
Merge branch 'main' into attestor-json-schema
jkjell May 10, 2024
ec0e91a
updating go mod to pin go-witness to recent merge
ChaosInTheCRD May 13, 2024
858b8f2
updating go mod
ChaosInTheCRD May 13, 2024
f3b8a63
Merge branch 'attestor-json-schema' of github.com:ChaosInTheCRD/witne…
ChaosInTheCRD May 13, 2024
35b3f14
updating verify.sh for docgen
ChaosInTheCRD May 13, 2024
96754f5
missed err handle
ChaosInTheCRD May 13, 2024
b419b8e
fixing linter
ChaosInTheCRD May 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
]
}
}
}