-
Notifications
You must be signed in to change notification settings - Fork 34
Example of overriding Secret with custom implementation #17
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function define(runtime, observer) { | ||
const main = runtime.module(); | ||
main.variable(observer()).define(["Secret"], function(Secret){return( | ||
Secret("MY_SECRET_KEY") | ||
)}); | ||
return main; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
# Observable Example: Secret | ||||||
|
||||||
See it live: https://observablehq.github.io/examples/secret | ||||||
|
||||||
Observable lets you configure [Secrets](https://observablehq.com/@observablehq/secrets). Sensitive variables can be stored outside your code and returned by calling `Secret("MY_SECRET_KEY")`, which might return a password like `"$w0rdf1sh"`. If you publish the notebook, the Secret will throw an error instead of returning a value, so that the Secret is not publicly exposed. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Calling `Secret` will also throw an error if you download the notebook — but sometimes, you might be downloading it to run in your own secure setting where you’d like to set the Secret a different way. This example shows how to use your own implementation of the Secret function to provide things like environment variables or API keys without rewriting your code. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
In this _insecure_ example, index.html gets Secret values from a hardcoded Map. This should never be used in code that would be seen openly on the client side; if you published this index.html publicly on the Web, anyone could read your hardcoded Secrets. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js | ||||||
const secrets = new Map([["MY_SECRET_KEY", "$w0rdf1sh"]]); | ||||||
const Secret = () => key => secrets.get(key); | ||||||
const runtime = new Runtime(Object.assign(new Library, {Secret})); | ||||||
``` | ||||||
|
||||||
Note that our `Secret` is a function that returns the function that gets called by the cell; the [Runtime documentation](https://github.com/observablehq/runtime) says: | ||||||
|
||||||
> If you wish for the value of a builtin to be a function, the builtin must be defined either as a promise that resolves to a function or as a function that returns a function. Builtins may also be defined as generators for dynamic values… |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>Embedding a notebook that uses a Secret</title> | ||
<link rel="stylesheet" type="text/css" href="./inspector.css"> | ||
<style> | ||
|
||
body { | ||
max-width: 960px; | ||
margin: 1em auto; | ||
font-family: sans-serif; | ||
} | ||
|
||
</style> | ||
<body> | ||
|
||
<h1><a href="https://github.com/observablehq/examples">Observable Example</a>: <a href="https://github.com/observablehq/examples/tree/main/secret">Secret</a></h1> | ||
|
||
<p>This example demonstrates overriding <a href="https://observablehq.com/@observablehq/secrets">Secrets</a> with your own implementation.</p> | ||
|
||
<script type="module"> | ||
|
||
import define from "./index.js"; | ||
import {Runtime, Library, Inspector} from "./runtime.js"; | ||
|
||
// This is an *insecure* example of how to use your own implementation of the | ||
// built-in Secret function by getting values from a hardcoded Map. Note that | ||
// our `Secret` is a function that returns the function that gets called by the | ||
// cell; the runtime documentation https://github.com/observablehq/runtime says: | ||
|
||
// > If you wish for the value of a builtin to be a function, the builtin must | ||
// > be defined either as a promise that resolves to a function or as a function | ||
// > that returns a function. Builtins may also be defined as generators for | ||
// > dynamic values… | ||
|
||
const secrets = new Map([["MY_SECRET_KEY", "$w0rdf1sh"]]); | ||
const Secret = () => key => secrets.get(key); | ||
|
||
// When we initialize the runtime, we pass it a standard Library object, with | ||
// `Secret` overridden with our custom implementation. Now, when a cell calls | ||
// Secret("MY_SECRET_KEY"), it will return "$w0rdf1sh". | ||
|
||
const runtime = new Runtime(Object.assign(new Library, {Secret})); | ||
|
||
const main = runtime.module(define, Inspector.into(document.body)); | ||
|
||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {default} from "./[email protected]"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "9cdbbe083c4d48f4", | ||
"main": "[email protected]", | ||
"version": "9.0.0", | ||
"homepage": "https://observablehq.com/d/9cdbbe083c4d48f4", | ||
"author": { | ||
"name": "tophtest", | ||
"url": "https://observablehq.com/@tophtest" | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put this under @observablehq? |
||
}, | ||
"type": "module", | ||
"peerDependencies": { | ||
"@observablehq/runtime": "4" | ||
} | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s good to capitalize the
Secret
function, but I’d prefer to keep the term “secrets” lowercase.