Skip to content

[Crypto] Implement support for high-level crypto APIs#387

Merged
yaron2 merged 10 commits into
dapr:mainfrom
ItalyPaleAle:crypto-encv1
May 26, 2023
Merged

[Crypto] Implement support for high-level crypto APIs#387
yaron2 merged 10 commits into
dapr:mainfrom
ItalyPaleAle:crypto-encv1

Conversation

@ItalyPaleAle

@ItalyPaleAle ItalyPaleAle commented Apr 5, 2023

Copy link
Copy Markdown
Contributor

This implements support for the high-level crypto APIs implemented here: dapr/dapr#6197

These allow encrypting and decrypting files of arbitrary length (up to 256TB :) ), while working on a stream of data.

The gRPC APIs are quite complex as they require data to be chunked, but the SDK takes care of all that complexity and offers a simple API with Encrypt and Decrypt. Both accept a readable stream as input (io.Reader) and return a readable stream (io.Reader) that receives the processed data.

Example usage. This reads a file and encrypts it, storing the result in another file:

// Input file, clear-text
rf, err := os.Open("input")
if err != nil {
	panic(err)
}
defer rf.Close()

// Output file, encrypted
wf, err := os.Create("output.enc")
if err != nil {
	panic(err)
}
defer wf.Close()

// Encrypt the data using Dapr
out, err := sdkClient.Encrypt(context.Background(), rf, dapr.EncryptOptions{
	// These are the 3 required parameters
	ComponentName: "mycryptocomponent",
	KeyName:        "mykey",
	Algorithm:     "RSA",
})
if err != nil {
	panic(err)
}

// Read the stream and copy it to the out file
n, err := io.Copy(wf, out)
if err != nil {
	panic(err)
}
fmt.Println("Written", n, "bytes")

Decrypting works very similarly:

out, err := sdkClient.Decrypt(context.Background(), rf, dapr.EncryptOptions{
	// Only required option is the component name
	ComponentName: "mycryptocomponent",
})

Signed-off-by: ItalyPaleAle <[email protected]>
Signed-off-by: ItalyPaleAle <[email protected]>
Signed-off-by: ItalyPaleAle <[email protected]>
@codecov

codecov Bot commented Apr 5, 2023

Copy link
Copy Markdown

Codecov Report

Merging #387 (54d4130) into main (b14253b) will increase coverage by 0.88%.
The diff coverage is 83.75%.

@@            Coverage Diff             @@
##             main     #387      +/-   ##
==========================================
+ Coverage   69.08%   69.97%   +0.88%     
==========================================
  Files          31       33       +2     
  Lines        2491     2651     +160     
==========================================
+ Hits         1721     1855     +134     
- Misses        676      695      +19     
- Partials       94      101       +7     
Impacted Files Coverage Δ
client/client.go 69.74% <ø> (ø)
client/crypto.go 83.11% <83.11%> (ø)
client/bufpool.go 100.00% <100.00%> (ø)

Signed-off-by: ItalyPaleAle <[email protected]>
Signed-off-by: ItalyPaleAle <[email protected]>
Comment thread Makefile
Signed-off-by: ItalyPaleAle <[email protected]>
Signed-off-by: ItalyPaleAle <[email protected]>
@yaron2

yaron2 commented May 25, 2023

Copy link
Copy Markdown
Member

@daixiang0 can you please review this?

Comment thread client/bufpool.go Outdated
Comment thread client/crypto.go
Comment thread dapr/proto/runtime/v1/dapr_additional.go
@yaron2
yaron2 merged commit 92c23df into dapr:main May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants