I'm writing a KRL module for an API. The API requires an access key, and that needs to be provided by the ruleset that calls my module. My module includes my access key that is used by the in-module test rules.
The ruleset that uses my module provides the access key like this:
use module a421x99 alias SuperModule with access_key = "01234567";
1 - How do I write my module so that the access key doesn't leak into the generated Javascript?
2 - Suppose the calling ruleset doesn't provide an access_key. How do I protect my own access key that I put in the module for testing?
First of all, you ought to be including API keys using a
keyblock in themeta, like this:That's better than storing or passing keys in plain strings.
Second, your module needs a
configure usingline in themeta(I'm assuming you already have one). Passing an empty hash as the default value will prevent your hard-coded key in the module from being used by a ruleset calling the module.Finally, in the global block do something like this:
This tells KRL to use either the
s3keysthat was passed in by the calling ruleset or else thes3key from the module's ownmetablock if your module is being used by itself. Even if someone uses your module, they will never get yourkeys:s3()because of the default value you set in theconfigure usingline.Once you have
usekeys, you canpick()out the pieces you need:Sam's Twilio module is a great place to refer for examples.