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

[core-crypto] initial core-crypto with sha256 hmac/hash functions #13232

Merged
merged 9 commits into from
Jan 21, 2021

Conversation

chradek
Copy link
Contributor

@chradek chradek commented Jan 15, 2021

Resolves #10204

Description

This PR adds a core-crypto package that our SDK can use to access shared crypto logic.

Motivations

DRY

We have multiple packages that need to compute SHA256 hashes and HMACs at runtime. Thus far, each package has rolled their own support for this as needed. This means if we want to fix a bug or add functionality (e.g. react-native support), we currently need to update each package's implementation. A developer may not even be aware of all the packages that need updating.

Support multiple environments

I had considered using a common folder for this functionality instead of a package, but then the burden is put on each consuming package to properly map node.js to browser/react-native versions of files. Using a separate package instead let's us define these mappings in one place.

Future plans

This PR only supports SHA256 hash/hmac algorithms in node.js and browsers. After this is merged, I'll explore adding react-native support as well.

Copy link
Member

@xirzec xirzec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's finally happening! 🎈🎈🎈🎉🎊🎉🎈🎈🎈

Left a few small thoughts, but I'm very excited for this!


## Key concepts

TODO: ???
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now a list of supported protocols might be sufficient here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

@@ -0,0 +1,118 @@
import path from "path";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use the common rollup config that @witemple-msft set up inside of devtool? Look at Text Analytics for an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the browser/nodejs builds to use the common rollup from devtool. I have a separate rollup config for creating the webworker script that runs in our tests.

return subtleCrypto;
}

if (!self.crypto || !self.crypto.subtle) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good using self here, we should maybe add a webworker test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Webworker tests added!

* @hidden
* @internal
*/
export function bufferToBase64(buffer: ArrayBuffer): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to provide these helpers publicly too? I'm pretty sure there are some credential types out there that need base64 support

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, perhaps consider using this buffer library: https://www.npmjs.com/package/buffer

I am using it in attestation tests:
https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/attestation/attestation/test/utils/Buffer.browser.ts

return Buffer.from(payload, "base64").toString(encoding);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating exposing these helpers. Would we need these outside of crypto? e.g. do we need like a core-encoder package?

I thought it was interesting that every usage of the sha256 hmac function today passed the key as a base64 string already...does this feel acceptable to continue doing this here or should I accept something like a byte array/buffer?

Also, perhaps consider using this buffer

I did consider this. I actually wanted to stay away from Buffer since it can be a fairly large package and increases browser bundle sizes. Also I'd expect the built in atob/btoa functions in browsers to perform better than a JS Buffer implementation. Now, I think we depend on it in core-amqp and core-http but wasn't sure if we were going to continue that with corev2. If we are then it won't be a big deal to include it in this package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Chris. Perhaps we can discuss in our next weekly meeting whether having a core-utils/core-encoding is a good idea. I am personally onboard with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid exporting these helpers until we know we need them to avoid adding to the public API prematurely. I do think a core-encoding package may be a better place for these to live long-term.

"packageName": "@azure/core-crypto",
"projectFolder": "sdk/core/core-crypto",
"versionPolicyName": "core"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor

@sadasant sadasant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is beautiful. Thank you!

Copy link
Member

@deyaaeldeen deyaaeldeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yayy!

@check-enforcer
Copy link

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run js - [service] - ci

@chradek chradek merged commit 3f04d2f into Azure:master Jan 21, 2021
ljian3377 pushed a commit to ljian3377/azure-sdk-for-js that referenced this pull request Jan 22, 2021
…ure#13232)

* [core-crypto] initial core-crypto with sha256 hmac/hash functions

* [core-crypto] add entry in ci.yml

* fix api review quotes

* add webworker tests

* [core-crypto] add supported protocols to README key concepts section

* [core-crypto] use rollup config from dev-tool for node and browser builds

* [core-crypto] update typedoc dep

* update pnpm-lock.yaml

* [core-crypto] fix README validation
maorleger added a commit that referenced this pull request Feb 19, 2021
## What

- Adds a rule to prevent usage of `window` and prefer `self` instead

## Why

- This came up recently in a few cases: #11067 and in this discussion: #13232 (comment)
- window is not always the global object, but self's reference changes depending on the context and will point to the right global object
- Adding a linter rule makes this easy to remember instead of relying on diligence

Resolves #13472
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.

Create a core-crypto package and expose sha256 HMAC from it
4 participants