Skip to content

Commit fc34f46

Browse files
NigelGreenwayNigel Greenwayboltlessengineer
authored
feat: add ability to pass certs (#519)
feat: add ability to pass certs A key feature that is missing from what I can tell is the ability to pass a cert/key. This change request is to add a cert/key via configuration based on the host. If it matches that host, then the file path for the certs/keys will be added to the request. Changes: - Add `--cert` and `--key` flag with file path to docs - Add a configuration setup so the keys are only sent with the corresponding request - Add test case for applying certifications Co-authored-by: Nigel Greenway <github@futurepixels.co.uk> Co-authored-by: Seongmin Lee <boltlessengineer@gmail.com>
1 parent 59b93c8 commit fc34f46

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

doc/rest-nvim.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ rest.Opts.Clients.Curl *rest.Opts.Clients.Curl*
185185
rest.Opts.Clients.Curl.Opts *rest.Opts.Clients.Curl.Opts*
186186

187187
Fields: ~
188-
{set_compressed?} (boolean) Add `--compressed` argument when `Accept-Encoding` header includes `gzip`
189-
(Default: `false`)
188+
{set_compressed?} (boolean) Add `--compressed` argument when `Accept-Encoding` header includes `gzip`
189+
(Default: `false`)
190+
{certificates?} (Certificates[]) Add `--cert` or `--key` to the `curl` command when required with
191+
specific domains
192+
(default: nil)
190193

191194

192195
RestStatisticsStyle *RestStatisticsStyle*

lua/rest-nvim/client/curl/cli.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ function builder.extras(req)
184184
vim.list_extend(args, { "--compressed" })
185185
end
186186
end
187+
--
188+
for domain, _ in pairs(config.clients.curl.opts.certificates) do
189+
local target = req.url
190+
191+
-- TODO(boltless): this is temporary solution. use same logic from cookie_jar instead
192+
local s, _ = string.find(target, domain, 1, true)
193+
194+
if s ~= nil then
195+
vim.list_extend(args, { "--cert", config.clients.curl.opts.certificates[domain].set_certificate_crt })
196+
vim.list_extend(args, { "--key", config.clients.curl.opts.certificates[domain].set_certificate_key })
197+
break
198+
end
199+
end
200+
187201
return args
188202
end
189203

lua/rest-nvim/config/default.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ local default_config = {
4949
---@type boolean Add `--compressed` argument when `Accept-Encoding` header includes
5050
---`gzip`
5151
set_compressed = false,
52+
---@type table<string, Certificate> Table containing certificates for each domains
53+
certificates = {},
5254
},
5355
},
5456
},

lua/rest-nvim/config/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ local config
6969
--- Add `--compressed` argument when `Accept-Encoding` header includes `gzip`
7070
--- (Default: `false`)
7171
---@field set_compressed? boolean
72+
--- Add `--cert`, `--key` or `--pem` to the `curl` command when required with specific domains
73+
--- (default: `nil`)
74+
---@field certificates? table<string, Certificate>
75+
76+
---@class Certificate
77+
---@field set_certificate_crt string
78+
---@field set_certificate_key string
7279

7380
---@class RestStatisticsStyle
7481
--- Identifier used used in curl's `--write-out` option

spec/client/curl/cli_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ vim.g.rest_nvim = vim.tbl_deep_extend("force", {
77
curl = {
88
opts = {
99
set_compressed = true,
10+
certificates = {},
1011
},
1112
},
1213
},
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---@diagnostic disable: invisible
2+
---@module 'luassert'
3+
4+
require("spec.minimal_init")
5+
vim.g.rest_nvim = vim.tbl_deep_extend("force", {
6+
clients = {
7+
curl = {
8+
opts = {
9+
certificates = {
10+
["localhost"] = {
11+
set_certificate_crt = "./my.cert",
12+
set_certificate_key = "./my.key",
13+
},
14+
},
15+
},
16+
},
17+
},
18+
}, vim.g.rest_nvim)
19+
20+
local Context = require("rest-nvim.context").Context
21+
local curl = require("rest-nvim.client.curl.cli")
22+
local builder = curl.builder
23+
24+
local STAT_FORMAT = builder.STAT_ARGS[2]
25+
26+
require("rest-nvim.client.curl.cli").config = vim.g.rest_nvim
27+
28+
describe("Curl cli builder", function()
29+
it("with opts.certificates", function()
30+
local args = builder.build({
31+
context = Context:new(),
32+
method = "POST",
33+
url = "http://localhost:8000",
34+
headers = {},
35+
cookies = {},
36+
handlers = {},
37+
})
38+
assert.same({
39+
"http://localhost:8000",
40+
"--cert",
41+
"./my.cert",
42+
"--key",
43+
"./my.key",
44+
"-X",
45+
"POST",
46+
"-w",
47+
STAT_FORMAT,
48+
}, args)
49+
end)
50+
end)

0 commit comments

Comments
 (0)