Allow insecure connections to Gitlab (#234)

This MR makes it possible to ignore bad x509 certificates when connecting to Gitlab, by creating a custom HTTP connection. The option is exposed via the setup function. This is a PATCH release.
This commit is contained in:
Harrison (Harry) Cramer
2024-04-02 21:53:25 -04:00
committed by GitHub
parent 670f08849f
commit 4f1fe4ae25
5 changed files with 45 additions and 15 deletions

View File

@@ -92,6 +92,8 @@ gitlab_url=https://my-personal-gitlab-instance.com/
The plugin will look for the `.gitlab.nvim` file in the root of the current project by default. However, you may provide a custom path to the configuration file via the `config_path` option. This must be an absolute path to the directory that holds your `.gitlab.nvim` file. The plugin will look for the `.gitlab.nvim` file in the root of the current project by default. However, you may provide a custom path to the configuration file via the `config_path` option. This must be an absolute path to the directory that holds your `.gitlab.nvim` file.
For more settings, please see `:h gitlab.nvim.connecting-to-gitlab`
## Configuring the Plugin ## Configuring the Plugin
Here is the default setup function. All of these values are optional, and if you call this function with no values the defaults will be used: Here is the default setup function. All of these values are optional, and if you call this function with no values the defaults will be used:
@@ -108,6 +110,9 @@ require("gitlab").setup({
imply_local = false, -- If true, will attempt to use --imply_local option when calling |:DiffviewOpen| imply_local = false, -- If true, will attempt to use --imply_local option when calling |:DiffviewOpen|
}, },
}, },
connection_settings = {
insecure = false, -- Like curl's --insecure option, ignore bad x509 certificates on connection
},
help = "g?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc) help = "g?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc)
popup = { -- The popup for comment creation, editing, and replying popup = { -- The popup for comment creation, editing, and replying
exit = "<Esc>", exit = "<Esc>",

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -18,6 +19,10 @@ type DebugSettings struct {
GoResponse bool `json:"go_response"` GoResponse bool `json:"go_response"`
} }
type ConnectionOptions struct {
Insecure bool `json:"insecure"`
}
type ProjectInfo struct { type ProjectInfo struct {
ProjectId string ProjectId string
MergeId int MergeId int
@@ -40,8 +45,8 @@ type Client struct {
/* initGitlabClient parses and validates the project settings and initializes the Gitlab client. */ /* initGitlabClient parses and validates the project settings and initializes the Gitlab client. */
func initGitlabClient() (error, *Client) { func initGitlabClient() (error, *Client) {
if len(os.Args) < 6 { if len(os.Args) < 7 {
return errors.New("Must provide gitlab url, port, auth token, debug settings, and log path"), nil return errors.New("Must provide gitlab url, port, auth token, debug settings, log path, and connection settings"), nil
} }
gitlabInstance := os.Args[1] gitlabInstance := os.Args[1]
@@ -62,6 +67,14 @@ func initGitlabClient() (error, *Client) {
return fmt.Errorf("Could not parse debug settings: %w, %s", err, debugSettings), nil return fmt.Errorf("Could not parse debug settings: %w, %s", err, debugSettings), nil
} }
/* Parse connection options */
connectionSettings := os.Args[6]
var connectionObject ConnectionOptions
err = json.Unmarshal([]byte(connectionSettings), &connectionObject)
if err != nil {
return fmt.Errorf("Could not parse connection settings: %w, %s", err, connectionSettings), nil
}
var apiCustUrl = fmt.Sprintf(gitlabInstance + "/api/v4") var apiCustUrl = fmt.Sprintf(gitlabInstance + "/api/v4")
gitlabOptions := []gitlab.ClientOptionFunc{ gitlabOptions := []gitlab.ClientOptionFunc{
@@ -76,6 +89,16 @@ func initGitlabClient() (error, *Client) {
gitlabOptions = append(gitlabOptions, gitlab.WithResponseLogHook(responseLogger)) gitlabOptions = append(gitlabOptions, gitlab.WithResponseLogHook(responseLogger))
} }
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: connectionObject.Insecure,
},
}
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = tr
gitlabOptions = append(gitlabOptions, gitlab.WithHTTPClient(retryClient.HTTPClient))
client, err := gitlab.NewClient(authToken, gitlabOptions...) client, err := gitlab.NewClient(authToken, gitlabOptions...)
if err != nil { if err != nil {

View File

@@ -124,6 +124,8 @@ project by default. However, you may provide a custom path to the configuration
file via the `config_path` option. This must be an absolute path to the file via the `config_path` option. This must be an absolute path to the
directory that holds your `.gitlab.nvim` file. directory that holds your `.gitlab.nvim` file.
The `connection_settings` block in the `state.lua` file will be used to
configure your connection to Gitlab.
CONFIGURING THE PLUGIN *gitlab.nvim.configuring-the-plugin* CONFIGURING THE PLUGIN *gitlab.nvim.configuring-the-plugin*

View File

@@ -13,19 +13,16 @@ M.start = function(callback)
local port = state.settings.port or empty_port local port = state.settings.port or empty_port
local parsed_port = nil local parsed_port = nil
local callback_called = false local callback_called = false
local command = state.settings.bin local command = string.format(
.. " " "%s %s %s %s '%s' %s '%s'",
.. state.settings.gitlab_url state.settings.bin,
.. " " state.settings.gitlab_url,
.. port port,
.. " " state.settings.auth_token,
.. state.settings.auth_token vim.json.encode(state.settings.debug),
.. " " state.settings.log_path,
.. "'" vim.json.encode(state.settings.connection_settings)
.. vim.json.encode(state.settings.debug) )
.. "'"
.. " "
.. state.settings.log_path
local job_id = vim.fn.jobstart(command, { local job_id = vim.fn.jobstart(command, {
on_stdout = function(_, data) on_stdout = function(_, data)

View File

@@ -20,6 +20,9 @@ M.settings = {
imply_local = false, imply_local = false,
}, },
}, },
connection_settings = {
insecure = true,
},
attachment_dir = "", attachment_dir = "",
help = "g?", help = "g?",
popup = { popup = {