Skip to content

Commit

Permalink
chore(examples): Lets isvalid be useful (#1856)
Browse files Browse the repository at this point in the history
### Proposed Changes

* Replaces isvalid example with a utility for checking isvalid on a file


### Checklist

- [ ] I have added or updated unit tests
- [ ] I have added or updated integration tests (if appropriate)
- [ ] I have added or updated documentation

### Testing Instructions
  • Loading branch information
dmihalcik-virtru authored Jan 15, 2025
1 parent c3cda1b commit bc3808f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
87 changes: 56 additions & 31 deletions examples/cmd/isvalid.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,81 @@
package cmd

import (
"bytes"
"encoding/base64"
"fmt"
"io"
"os"

"github.com/opentdf/platform/sdk"
"github.com/spf13/cobra"
)

func init() {
encryptCmd := cobra.Command{
Use: "isvalid",
var isValidCmd = &cobra.Command{
Use: "isvalid [files...]",
Short: "Check validity of a TDF",
RunE: isValid,
RunE: func(cmd *cobra.Command, args []string) error {
return areValid(cmd, args)
},
}

ExamplesCmd.AddCommand(&encryptCmd)
ExamplesCmd.AddCommand(isValidCmd)
}

func isValid(cmd *cobra.Command, args []string) error {
goodNanoTdfStr := "TDFMABJsb2NhbGhvc3Q6ODA4MC9rYXOAAQIA2qvjMRfg7b27lT2kf9SwHRkDIg8ZXtfRoiIvdMUHq/gL5AUMfmv4Di8sKCyLkmUm/WITVj5hDeV/z4JmQ0JL7ZxqSmgZoK6TAHvkKhUly4zMEWMRXH8IktKhFKy1+fD+3qwDopqWAO5Nm2nYQqi75atEFckstulpNKg3N+Ul22OHr/ZuR127oPObBDYNRfktBdzoZbEQcPlr8q1B57q6y5SPZFjEzL9weK+uS5bUJWkF3nsHASo2bZw7IPhTZxoFVmCDjwvj6MbxNa7zG6aClHJ162zKxLLnD9TtIHuZ59R7LgiSieipXeExj+ky9OgIw5DfwyUuxsQLtKpMIAFPmLY9Hy2naUJxke0MT1EUBgastCq+YtFGslV9LJo/A8FtrRqludwtM0O+Z9FlAkZ1oNL7M7uOkLrh7eRrv+C1AAAX6FaBQoOtqnmyu6Jp+VzkxDddEeLRUyI="
badNanoTdfStr := "TDFMABfg7b27lT2kf9SwHRkDIg8ZXtfRoiIvdMUHq/gL5AUMfmv4Di8sKCyLkmUm/WITVj5hDeV/z4JmQ0JL7ZxqSmgZoK6TAHvkKhUly4zMEWMRXH8IktKhFKy1+fD+3qwDopqWAO5Nm2nYQqi75atEFckstulpNKg3N+Ul22OHr/ZuR127oPObBDYNRfktBdzoZbEQcPlr8q1B57q6y5SPZFjEzL9weK+uS5bUJWkF3nsHASo2bZw7IPhTZxoFVmCDjwvj6MbxNa7zG6aClHJ162zKxLLnD9TtIHuZ59R7LgiSieipXeExj+ky9OgIw5DfwyUuxsQLtKpMIAFPmLY9Hy2naUJxke0MT1EUBgastCq+YtFGslV9LJo/A8FtrRqludwtM0O+Z9FlAkZ1oNL7M7uOkLrh7eRrv+C1AAAX6FaBQoOtqnmyu6Jp+VzkxDddEeLRUyI="
type typeInfo struct {
Valid bool
Type string
Error error
}

func (t typeInfo) String() string {
if t.Valid {
return fmt.Sprintf("[✅ %s]", t.Type)
}
if t.Error != nil {
return fmt.Sprintf("[🚮🔥 %s %v]", t.Type, t.Error)
}
return fmt.Sprintf("[📛 %s]", t.Type)
}

goodStandardTdf := "UEsDBC0ACAAAAJ2TFTEAAAAAAAAAAAAAAAAJAAAAMC5wYXlsb2Fktu4m+vdwl0mtjhY3U5e7TG2o1s8ifK+RAhFNjRjGTLJ7V3w5UEsHCGiY7skkAAAAJAAAAFBLAwQtAAgAAACdkxUxAAAAAAAAAAAAAAAADwAAADAubWFuaWZlc3QuanNvbnsiZW5jcnlwdGlvbkluZm9ybWF0aW9uIjp7InR5cGUiOiJzcGxpdCIsInBvbGljeSI6ImV5SjFkV2xrSWpvaU1HTTFORGsyWlRZdE5EYzRaaTB4TVdWbUxXSXlOakV0WWpJMVl6UmhORE14TjJFM0lpd2lZbTlrZVNJNmV5SmtZWFJoUVhSMGNtbGlkWFJsY3lJNlczc2lZWFIwY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0cmVhbWFibGUiOnRydWV9LCJpbnRlZ3JpdHlJbmZvcm1hdGlvbiI6eyJyb290U2lnbmF0dXJlIjp7ImFsZyI6IkhTMjU2Iiwic2lnIjoiWkdWaFltRmtNRGhsTURCbU1UVm1ZekJtTVdFME0ySmhOamhrTmpBMVpUazFNVGRtWmpoa1pETmtNekk0Tldaa01XUXhOVFZsWXpjME1EVXhPRE13Tmc9PSJ9LCJzZWdtZW50SGFzaEFsZyI6IkdNQUMiLCJzZWdtZW50U2l6ZURlZmF1bHQiOjIwOTcxNTIsImVuY3J5cHRlZFNlZ21lbnRTaXplRGVmYXVsdCI6MjA5NzE4MCwic2VnbWVudHMiOlt7Imhhc2giOiJNakkzWTJGbU9URXdNakV4TkdRNFpERTRZelkwWTJJeU4ySTFOemRqTXprPSIsInNlZ21lbnRTaXplIjo4LCJlbmNyeXB0ZWRTZWdtZW50U2l6ZSI6MzZ9XX19LCJwYXlsb2FkIjp7InR5cGUiOiJyZWZlcmVuY2UiLCJ1cmwiOiIwLnBheWxvYWQiLCJwcm90b2NvbCI6InppcCIsIm1pbWVUeXBlIjoiYXBwbGljYXRpb24vb2N0ZXQtc3RyZWFtIiwiaXNFbmNyeXB0ZWQiOnRydWV9fVBLBwgwpFOlrwUAAK8FAABQSwECLQAtAAgAAACdkxUxaJjuySQAAAAkAAAACQAAAAAAAAAAAAAAAAAAAAAAMC5wYXlsb2FkUEsBAi0ALQAIAAAAnZMVMTCkU6WvBQAArwUAAA8AAAAAAAAAAAAAAAAAWwAAADAubWFuaWZlc3QuanNvblBLBQYAAAAAAgACAHQAAABHBgAAAAA="
func areValid(cmd *cobra.Command, files []string) error {
if len(files) == 0 {
// TK Add support for handling stdin
return cmd.Usage()
}
for _, file := range files {
in, err := os.Open(file)
if err != nil {
cmd.PrintErrf("Error opening file %s: %v\n", file, err)
return err
}
defer in.Close()

// Decode the base64 string
goodDecodedNanoTdf, _ := base64.StdEncoding.DecodeString(goodNanoTdfStr)
badDecodedNanoTdf, _ := base64.StdEncoding.DecodeString(badNanoTdfStr)
cmd.Printf("File: [%s], TypeInfo: %v\n", file, isValid(cmd, in))
}
return nil
}

goodDecodedStandardTdf, _ := base64.StdEncoding.DecodeString(goodStandardTdf)
func isValid(cmd *cobra.Command, in io.ReadSeeker) []typeInfo {
var typeInfos []typeInfo

inGoodStandard := bytes.NewReader(goodDecodedStandardTdf)
isValidTdf, _ := sdk.IsValidTdf(inGoodStandard)
cmd.Println("Valid TDF: ")
cmd.Println(isValidTdf)
isValidTdf, err := sdk.IsValidTdf(in)
typeInfos = append(typeInfos, typeInfo{Valid: isValidTdf, Error: err, Type: "TDF3"})

inGood := bytes.NewReader(goodDecodedNanoTdf)
inBad := bytes.NewReader(badDecodedNanoTdf)
isValidNanoTdfGood, _ := sdk.IsValidNanoTdf(inGood)
isValidNanoTdfBad, _ := sdk.IsValidNanoTdf(inBad)
if _, err := in.Seek(0, io.SeekStart); err != nil {
cmd.PrintErrf("Error seeking to start of file: %v\n", err)
return typeInfos
}

cmd.Println("Valid NanoTDF (Good): ")
cmd.Println(isValidNanoTdfGood)
isValidNano, err := sdk.IsValidNanoTdf(in)
typeInfos = append(typeInfos, typeInfo{Valid: isValidNano, Error: err, Type: "NanoTDF"})

cmd.Println("Valid NanoTDF (Bad): ")
cmd.Println(isValidNanoTdfBad)
if _, err := in.Seek(0, io.SeekStart); err != nil {
cmd.PrintErrf("Error seeking to start of file: %v\n", err)
return typeInfos
}

_, _ = inGood.Seek(0, io.SeekStart)
tdfType := sdk.GetTdfType(inGood)
cmd.Println("Type: ")
cmd.Println(tdfType.String())
tdfType := sdk.GetTdfType(in)
typeInfos = append(typeInfos, typeInfo{Valid: tdfType != sdk.Invalid, Error: nil, Type: tdfType.String()})

return nil
return typeInfos
}
2 changes: 1 addition & 1 deletion sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func IsValidTdf(reader io.ReadSeeker) (bool, error) {
}

if !result.Valid() {
return false, errors.New("manifest was not valid")
return false, fmt.Errorf("manifest was not valid: %v", result.Errors())
}

return true, nil
Expand Down

0 comments on commit bc3808f

Please sign in to comment.