Support fetch external resources over HTTP#1007
Support fetch external resources over HTTP#1007tlimoncelli merged 2 commits intoDNSControl:masterfrom
Conversation
|
I'm unsure if I should add unit tests for the fetch functionality, since I didn't find individual tests for basic functions like |
tlimoncelli
left a comment
There was a problem hiding this comment.
A. It is fabulous that you can add such a big feature in so few lines of code. Good job!
I have 2 concerns:
- There have been no updates to fknsrs.biz/p/ottoext in years. Is it abandoned or just very stable?
- This will encourage people to make dnsconfig.js configs that break when a third-party dependency is broken or down. I think that the docs should include a warning against complexity.
B. Please add docs (docs/_functions/global/FETCH.md)
C. Unit tests would be great:
I'm unsure if I should add unit tests for the fetch functionality, since I didn't find individual tests for basic functions like
A(). Please let me know if a unit test for this is necessary.
My knowledge of Javascript is limited. If you can help me make unit tests, I would appreciate it!
| currentDirectory = filepath.Dir(file) | ||
|
|
||
| vm := otto.New() | ||
| l := loop.New(vm) |
There was a problem hiding this comment.
Please add comments explaining these timers.
9d592d8 to
8b30a48
Compare
The project seems abandoned, but from the testing the functionalities are stable. Nevertheless I can take over and maintain it. For example, I just forked that project and added the functionality to specify request headers, and updated this PR to point to my fork. Please let me know if it's acceptable to you.
I've added docs for the two functions I added, FETCH and PANIC.
A warning is included in the docs for FETCH function.
On a second thought, the ottoext project already includes its own unit tests, so I doubt if adding separate unit tests for FETCH is necessary. If needed, I can reuse the unit tests from ottoext. |
|
Yes, using your fork is good. I don't see the new docs. Did you "git add" them? (or maybe it's a problem on my side?) |
|
Oh wait, I forgot to push the doc changes to remote. Should be fixed now. |
|
Speaking of security, I just realized that a switch is needed to explicitly enable this functionality, along with some additional warnings. Or, in the case where DnsControl runs in a CI, based on data from a git repo that accepts and tests PRs, I can easily turn the CI into a DDoS machine with some |
|
Good point. It would be good to enable this only with a flag. |
add PANIC() and error-handled FETCH()
|
A flag |
tlimoncelli
left a comment
There was a problem hiding this comment.
LGTM. Ready for merge?
|
Sure, good to go. |
|
Thanks! P.S. I'd love to hear about how people use this feature! |
|
Sorry for being late to the party: A usecase I can think of is querying the DKIM public key from my mailserver setup (using mailcow) from the API and add the records to the DNS zone. However this requires authenticating on the API via POST parameters, but when I see it correct the current |
|
@patschi Technically POSTing parameters is supported, but for now you'll have to handcraft the request body. For example, both snippers below POST "a=12345" and "b=54321". This is the easier way of form-urlencoded, useful to pass a pair of API key/secret along: FETCH('http://localhost/test.php', {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
body: 'a=12345&b=54321',
}).then(function(r) {
return r.text();
}).then(function(t) {
console.log(t);
});This is the more complicated way of creating a multipart form-data. data = '--------------------------2c3e2cab183d59b3' + "\n"
+ 'Content-Disposition: form-data; name="a"' + "\n"
+ '' + "\n"
+ '12345' + "\n"
+ '--------------------------2c3e2cab183d59b3' + "\n"
+ 'Content-Disposition: form-data; name="b"' + "\n"
+ '' + "\n"
+ '54321' + "\n"
+ '--------------------------2c3e2cab183d59b3--' + "\n";
FETCH('http://localhost/test.php', {
headers: {
"Content-Type": "multipart/form-data; boundary=------------------------2c3e2cab183d59b3",
},
method: "POST",
body: data,
}).then(function(r) {
return r.text();
}).then(function(t) {
console.log(t);
}); |
This PR adds Fetch API to dnscontrol, mentioned in issue #994, via this extension for otto https://github.com/deoxxa/ottoext.
In addition, a
PANIC(reason)function is added to abort dnscontrol execution with an exit code, in case an error happened.And a
FETCH()wrapper function is added to automatically callPANIC()if a network error occurred.Fixes #994
Example: successful request
dnscontrol.js:
Generated zone:
Example: failed request
dnscontrol.js:
Dnscontrol output: