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

feat: add ability to pass certs #519

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,11 @@ rest.Opts.Clients.Curl *rest.Opts.Clients.Curl*
rest.Opts.Clients.Curl.Opts *rest.Opts.Clients.Curl.Opts*

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


RestStatisticsStyle *RestStatisticsStyle*
Expand Down
14 changes: 14 additions & 0 deletions lua/rest-nvim/client/curl/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ function builder.extras(req)
vim.list_extend(args, { "--compressed" })
end
end
--
for domain, _ in pairs(config.clients.curl.opts.certificates) do
local target = req.url

-- TODO(boltless): this is temporary solution. use same logic from cookie_jar instead
local s, _ = string.find(target, domain, 1, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have domain matching functionality in cookie_jar.lua so we can use same logic for both, but for now, I think this is fine. I might add a TODO comment here though.

local function match_cookie(url, cookie)
local req_domain, req_path = utils.parse_url(url)
if not req_domain then
return false
end
local domain_matches = ("." .. req_domain):match(vim.pesc(cookie.domain) .. "$")
local path_matches = req_path:sub(1, #cookie.path) == cookie.path
if domain_matches and path_matches then
logger.debug(
("cookie %s with domain %s and path %s matched to url: %s"):format(
cookie.name,
cookie.domain,
cookie.path,
url
)
)
else
logger.debug(
("cookie %s with domain %s and path %s NOT matched to url: %s"):format(
cookie.name,
cookie.domain,
cookie.path,
url
)
)
end
return domain_matches and path_matches
end


if s ~= nil then
vim.list_extend(args, { "--cert", config.clients.curl.opts.certificates[domain].set_certificate_crt })
vim.list_extend(args, { "--key", config.clients.curl.opts.certificates[domain].set_certificate_key })
break
end
end

return args
end

Expand Down
2 changes: 2 additions & 0 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ local default_config = {
---@type boolean Add `--compressed` argument when `Accept-Encoding` header includes
---`gzip`
set_compressed = false,
---@type table<string, Certificate> Table containing certificates for each domains
certificates = {},
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ local config
--- Add `--compressed` argument when `Accept-Encoding` header includes `gzip`
--- (Default: `false`)
---@field set_compressed? boolean
--- Add `--cert`, `--key` or `--pem` to the `curl` command when required with specific domains
--- (default: `nil`)
---@field certificates? table<string, Certificate>

---@class Certificate
---@field set_certificate_crt string
---@field set_certificate_key string

---@class RestStatisticsStyle
--- Identifier used used in curl's `--write-out` option
Expand Down
1 change: 1 addition & 0 deletions spec/client/curl/cli_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ vim.g.rest_nvim = vim.tbl_deep_extend("force", {
curl = {
opts = {
set_compressed = true,
certificates = {},
},
},
},
Expand Down
50 changes: 50 additions & 0 deletions spec/client/curl/cli_with_certs_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---@diagnostic disable: invisible
---@module 'luassert'

require("spec.minimal_init")
vim.g.rest_nvim = vim.tbl_deep_extend("force", {
clients = {
curl = {
opts = {
certificates = {
["localhost"] = {
set_certificate_crt = "./my.cert",
set_certificate_key = "./my.key",
},
},
},
},
},
}, vim.g.rest_nvim)

local Context = require("rest-nvim.context").Context
local curl = require("rest-nvim.client.curl.cli")
local builder = curl.builder

local STAT_FORMAT = builder.STAT_ARGS[2]

require("rest-nvim.client.curl.cli").config = vim.g.rest_nvim

describe("Curl cli builder", function()
it("with opts.certificates", function()
local args = builder.build({
context = Context:new(),
method = "POST",
url = "http://localhost:8000",
headers = {},
cookies = {},
handlers = {},
})
assert.same({
"http://localhost:8000",
"--cert",
"./my.cert",
"--key",
"./my.key",
"-X",
"POST",
"-w",
STAT_FORMAT,
}, args)
end)
end)
Loading