-
Notifications
You must be signed in to change notification settings - Fork 24
fix(sdk): more efficient encryption in experiment TDF Writer #2904
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9dffeb4
perf
imdominicreed 4130e20
no mem
imdominicreed 24ba5c6
map write fix
imdominicreed 8117eeb
fixes
imdominicreed 3d55448
bug fixes
imdominicreed 5861346
benchmark
imdominicreed d633c44
lint fixes
imdominicreed 7cb700c
lint fixes
imdominicreed 0cb2032
fomat file
imdominicreed 274be83
lint fixes
imdominicreed 157ab53
fix go mod
imdominicreed 2fb31ce
test fixes
imdominicreed 8c7b2e0
lint fixes
imdominicreed 04fa7a2
reset examples
imdominicreed 61ece0b
fix sdk
imdominicreed 38e13f7
benchmark
imdominicreed c67bcf8
benchmark experiemtnal
imdominicreed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //nolint:forbidigo // We use Println here extensively because we are printing markdown. | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "connectrpc.com/connect" | ||
| "github.com/opentdf/platform/lib/ocrypto" | ||
| kasp "github.com/opentdf/platform/protocol/go/kas" | ||
| "github.com/opentdf/platform/protocol/go/kas/kasconnect" | ||
| "github.com/opentdf/platform/protocol/go/policy" | ||
|
|
||
| "github.com/opentdf/platform/sdk/experimental/tdf" | ||
| "github.com/opentdf/platform/sdk/httputil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| payloadSize int | ||
| segmentChunk int | ||
| testAttr = "https://example.com/attr/attr1/value/value1" | ||
| ) | ||
|
|
||
| func init() { | ||
| benchmarkCmd := &cobra.Command{ | ||
| Use: "benchmark-experimental-writer", | ||
| Short: "Benchmark experimental TDF writer speed", | ||
| Long: `Benchmark the experimental TDF writer with configurable payload size.`, | ||
| RunE: runExperimentalWriterBenchmark, | ||
| } | ||
| //nolint: mnd // no magic number, this is just default value for payload size | ||
| benchmarkCmd.Flags().IntVar(&payloadSize, "payload-size", 1024*1024, "Payload size in bytes") // Default 1MB | ||
| //nolint: mnd // same as above | ||
| benchmarkCmd.Flags().IntVar(&segmentChunk, "segment-chunks", 16*1024, "segment chunks ize") // Default 16 segments | ||
| ExamplesCmd.AddCommand(benchmarkCmd) | ||
| } | ||
|
|
||
| func runExperimentalWriterBenchmark(_ *cobra.Command, _ []string) error { | ||
| payload := make([]byte, payloadSize) | ||
| _, err := rand.Read(payload) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to generate random payload: %w", err) | ||
| } | ||
|
|
||
| http := httputil.SafeHTTPClient() | ||
| fmt.Println("endpoint:", platformEndpoint) | ||
| serviceClient := kasconnect.NewAccessServiceClient(http, platformEndpoint) | ||
| resp, err := serviceClient.PublicKey(context.Background(), connect.NewRequest(&kasp.PublicKeyRequest{Algorithm: string(ocrypto.RSA2048Key)})) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get public key from KAS: %w", err) | ||
| } | ||
| var attrs []*policy.Value | ||
|
|
||
| simpleyKey := &policy.SimpleKasKey{ | ||
| KasUri: platformEndpoint, | ||
| KasId: "id", | ||
| PublicKey: &policy.SimpleKasPublicKey{ | ||
| Kid: resp.Msg.GetKid(), | ||
| Pem: resp.Msg.GetPublicKey(), | ||
| Algorithm: policy.Algorithm_ALGORITHM_RSA_2048, | ||
| }, | ||
| } | ||
|
|
||
| attrs = append(attrs, &policy.Value{Fqn: testAttr, KasKeys: []*policy.SimpleKasKey{simpleyKey}, Attribute: &policy.Attribute{Namespace: &policy.Namespace{Name: "example.com"}, Fqn: testAttr}}) | ||
| writer, err := tdf.NewWriter(context.Background(), tdf.WithDefaultKASForWriter(simpleyKey), tdf.WithInitialAttributes(attrs), tdf.WithSegmentIntegrityAlgorithm(tdf.HS256)) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create writer: %w", err) | ||
| } | ||
| i := 0 | ||
| wg := sync.WaitGroup{} | ||
| segs := len(payload) / segmentChunk | ||
| wg.Add(segs) | ||
| start := time.Now() | ||
| for i < segs { | ||
| segment := i | ||
| go func() { | ||
| start := i * segmentChunk | ||
| end := min(start+segmentChunk, len(payload)) | ||
| _, err = writer.WriteSegment(context.Background(), segment, payload[start:end]) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| panic(err) | ||
| } | ||
| wg.Done() | ||
| }() | ||
| i++ | ||
| } | ||
| wg.Wait() | ||
|
|
||
| end := time.Now() | ||
| result, err := writer.Finalize(context.Background()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to finalize writer: %w", err) | ||
| } | ||
| totalTime := end.Sub(start) | ||
|
|
||
| fmt.Printf("# Benchmark Experimental TDF Writer Results:\n") | ||
| fmt.Printf("| Metric | Value |\n") | ||
| fmt.Printf("|--------------------|--------------|\n") | ||
| fmt.Printf("| Payload Size (B) | %d |\n", payloadSize) | ||
| fmt.Printf("| Output Size (B) | %d |\n", len(result.Data)) | ||
| fmt.Printf("| Total Time | %s |\n", totalTime) | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.