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

feat(sdk): add collections for nanotdf #1695

Merged
merged 19 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions examples/cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cmd
import (
"bytes"
"errors"
"fmt"
"github.com/spf13/cobra"
"io"
"os"

"github.com/spf13/cobra"
"path/filepath"
)

func init() {
Expand All @@ -31,6 +32,29 @@ func decrypt(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
// Collection
if stat, err := os.Stat(tdfFile); err == nil && stat.IsDir() {
entries, err := os.ReadDir(tdfFile)
if err != nil {
return err
}
for _, entry := range entries {
if !entry.IsDir() {
f, err := os.Open(filepath.Join(tdfFile, entry.Name()))
if err != nil {
return err
}
_, err = client.ReadNanoTDF(os.Stdout, f)
fmt.Println()
if err != nil {
return err
}
}
}
client.Close()
return nil
}

file, err := os.Open(tdfFile)
if err != nil {
return err
Expand Down
56 changes: 42 additions & 14 deletions examples/cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/opentdf/platform/lib/ocrypto"
Expand All @@ -20,6 +22,7 @@ var (
noKIDInNano bool
outputName string
dataAttributes []string
collection int
)

func init() {
Expand All @@ -35,6 +38,7 @@ func init() {
encryptCmd.Flags().BoolVar(&noKIDInKAO, "no-kid-in-kao", false, "[deprecated] Disable storing key identifiers in TDF KAOs")
encryptCmd.Flags().BoolVar(&noKIDInNano, "no-kid-in-nano", true, "Disable storing key identifiers in nanoTDF KAS ResourceLocator")
encryptCmd.Flags().StringVarP(&outputName, "output", "o", "sensitive.txt.tdf", "name or path of output file; - for stdout")
encryptCmd.Flags().IntVarP(&collection, "collection", "c", 0, "number of nano's to create for collection. If collection >0 (default) then output will be <iteration>_<output>")

ExamplesCmd.AddCommand(&encryptCmd)
}
Expand All @@ -50,7 +54,6 @@ func encrypt(cmd *cobra.Command, args []string) error {
opts := []sdk.Option{
sdk.WithInsecurePlaintextConn(),
sdk.WithClientCredentials("opentdf-sdk", "secret", nil),
sdk.WithTokenEndpoint("http://localhost:8888/auth/realms/opentdf/protocol/openid-connect/token"),
}

if noKIDInKAO {
Expand All @@ -68,17 +71,32 @@ func encrypt(cmd *cobra.Command, args []string) error {
}

out := os.Stdout
if outputName != "-" {
out, err = os.Create(outputName)
if err != nil {
return err
}
if outputName == "-" && collection > 0 {
return fmt.Errorf("cannot use stdout for collection")
}
defer func() {
if outputName != "-" {
out.Close()

var writer []io.Writer
if outputName == "-" {
writer = append(writer, out)
} else {
dir, file := filepath.Split(outputName)
for i := 0; i < collection; i++ {
out, err = os.Create(filepath.Join(dir, fmt.Sprintf("%d_%s", i, file)))
if err != nil {
return err
}
writer = append(writer, out)
defer out.Close()
}
if collection == 0 {
out, err = os.Create(outputName)
writer = append(writer, out)
defer out.Close()
if err != nil {
return err
}
}
}()
}

if !nanoFormat {
opts := []sdk.TDFOption{sdk.WithDataAttributes(dataAttributes...)}
Expand Down Expand Up @@ -108,17 +126,27 @@ func encrypt(cmd *cobra.Command, args []string) error {
}
nanoTDFConfig.SetAttributes(dataAttributes)
nanoTDFConfig.EnableECDSAPolicyBinding()
if collection > 0 {
nanoTDFConfig.EnableCollection()
}
err = nanoTDFConfig.SetKasURL(fmt.Sprintf("http://%s/kas", platformEndpoint))
if err != nil {
return err
}
for i, writer := range writer {
input := plainText
if collection > 0 {
input = fmt.Sprintf("%d: %s", i, plainText)
}
in = strings.NewReader(input)
_, err = client.CreateNanoTDF(writer, in, *nanoTDFConfig)
if err != nil {
return err

_, err = client.CreateNanoTDF(out, in, *nanoTDFConfig)
if err != nil {
return err
}
}

if outputName != "-" {
if outputName != "-" && collection == 0 {
err = cat(cmd, outputName)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion examples/cmd/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"google.golang.org/grpc/resolver"
"log"
"os"
"strings"
Expand All @@ -28,7 +29,8 @@ func init() {
}

func newSDK() (*sdk.SDK, error) {
opts := []sdk.Option{sdk.WithInsecurePlaintextConn()}
resolver.SetDefaultScheme("passthrough")
opts := []sdk.Option{sdk.WithStoreCollectionHeaders(), sdk.WithInsecurePlaintextConn()}
if clientCredentials != "" {
i := strings.Index(clientCredentials, ":")
if i < 0 {
Expand Down
37 changes: 21 additions & 16 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ require (
github.com/opentdf/platform/lib/ocrypto v0.1.6
github.com/opentdf/platform/protocol/go v0.2.20
github.com/opentdf/platform/sdk v0.3.19
github.com/spf13/cobra v1.8.0
github.com/spf13/cobra v1.8.1
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
)

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.33.0-20240221180331-f05a6f4403ce.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.1-20240508200655-46a4cf4ba109.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/containerd v1.7.21 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gowebpki/jcs v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.5 // indirect
Expand All @@ -31,17 +35,18 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/testcontainers/testcontainers-go v0.32.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/grpc v1.62.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading