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

[POC] Use WebAssembly build of package-spec for package validation #128076

Closed

Conversation

joshdover
Copy link
Contributor

@joshdover joshdover commented Mar 18, 2022

Summary

Related to #115032
Uses a WebAssembly build from elastic/package-spec#285

This imports a WebAssembly (wasm) build of the package-spec validation code and applies it to all packages installed via upload. It will fail to install these packages if they do not pass the package-spec validation.

For instance, this fails on the endpoint package today with this error (due to elastic/package-spec#23):

Failed installing package [endpoint] due to error: [found 2 validation errors:
   1. item [index_template] is not allowed in folder [endpoint-1.5.0.zip/elasticsearch]
   2. item [transform] is not allowed in folder [endpoint-1.5.0.zip/elasticsearch]
]

Maybe for now we make this a dev-only log warning to start?

Checklist

Delete any items that are not applicable to this PR.

Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release.

When forming the risk matrix, consider some of the following examples and how they may potentially impact the change:

Risk Probability Severity Mitigation/Notes
Multiple Spaces—unexpected behavior in non-default Kibana Space. Low High Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces.
Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. High Low Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure.
Code should gracefully handle cases when feature X or plugin Y are disabled. Medium High Unit tests will verify that any feature flag or plugin combination still results in our service operational.
See more potential risk examples

For maintainers

@joshdover joshdover added release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting Team:Fleet Team label for Observability Data Collection Fleet team labels Mar 18, 2022
@joshdover
Copy link
Contributor Author

@elastic/kibana-security I noticed that we have no other usages of WASM in Kibana today. Are there any known reasons we can't or shouldn't pursue this option?

@kibana-ci
Copy link
Collaborator

kibana-ci commented Mar 18, 2022

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] Docker CI Group / endpoint "before all" hook in "endpoint"
  • [job] [logs] Docker CI Group / endpoint "before all" hook in "endpoint"
  • [job] [logs] Jest Tests #8 / install registry should install from bundled package if one exists

Metrics [docs]

Unknown metric groups

ESLint disabled in files

id before after diff
fleet 8 9 +1

Total ESLint disabled count

id before after diff
fleet 55 56 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@legrego
Copy link
Member

legrego commented Mar 18, 2022

@elastic/kibana-security I noticed that we have no other usages of WASM in Kibana today. Are there any known reasons we can't or shouldn't pursue this option?

Thanks for asking! I'll let others on the team respond do your direct usage, which appears to be server-side. I don't know enough to have an opinion one way or another here.

For client-side WASM, we would likely need a CSP that included either script-src: unsafe-eval or script-src: wasm-unsafe-eval. We are actively working to remove unsafe-eval, and I'm not keen on adding wasm-unsafe-eval. You can probably ignore all of this since your usage is server-side, but I wanted to mention it to start to spread awareness.

@joshdover joshdover changed the title [POC] Use wasm build of package-spec for package validation [POC] Use WebAssembly build of package-spec for package validation Mar 18, 2022
Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

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

I agree with Larry's earlier assessment. If we use WASM on the server-side and compile only the module we ship, then I don't have any significant security-related concerns. I just left few performance related questions and it'd be great if we can avoid polluting the global scope with Go wasm helper.

size: number,
buffer: Uint8Array
): Promise<void> {
const validator = await WebAssembly.instantiate(wasmBuffer, go.importObject);
Copy link
Member

Choose a reason for hiding this comment

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

nit: if it's called multiple times then it'd probably make sense to compile module just once and reuse it for instantiation (with new WebAssembly.Module or WebAssembly.compile).

// @ts-expect-error
const go = new Go();

const wasmBuffer = fs.readFileSync(path.join(__dirname, 'validator.wasm'));
Copy link
Member

Choose a reason for hiding this comment

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

question: Just curious, I see this module is about 12mb, I assume it's not optimized yet (with more aggressive build optimization flags, wasm-opt and friends)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep we still need to explore making this smaller and distribute it via a proper npm module. This is currently being built with go-wasm, but I think it would make sense to look at tinygo.

// Provides global.Go runtime
import './wasm_exec';
// @ts-expect-error
const go = new Go();
Copy link
Member

Choose a reason for hiding this comment

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

question: I'm not familiar with wasm_exec.js\tinygo, is it possible to use its importObject without polluting the global scope with Go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without editing the wasm_exec.js from Go (this is not from tinygo), I don't see a way. I think it would be a small edit though and I agree we shouldn't merge this in its current state (polluting the global scope).

@legrego legrego closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:Fleet Team label for Observability Data Collection Fleet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants