Skip to content

Commit

Permalink
feat: add support for insecure TLS connections (#34)
Browse files Browse the repository at this point in the history
- Add `crypto/tls` import in `jenkins.go`
- Add `Client` field to `Jenkins` struct
- Modify `NewJenkins` function to accept an `insecure` parameter and configure HTTP client accordingly
- Update `sendRequest` method to use the `Client` field from the `Jenkins` struct
- Update tests in `jenkins_test.go` to include the `insecure` parameter in `NewJenkins` calls
- Add `insecure` flag to CLI options in `main.go`
- Add `Insecure` field to `Plugin` struct
- Update `Plugin.Exec` method to pass `Insecure` field to `NewJenkins`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Oct 5, 2024
1 parent 24e8004 commit 1bb020b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
17 changes: 15 additions & 2 deletions jenkins.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
Expand All @@ -20,15 +21,27 @@ type (
Jenkins struct {
Auth *Auth
BaseURL string
Client *http.Client
}
)

// NewJenkins is initial Jenkins object
func NewJenkins(auth *Auth, url string) *Jenkins {
func NewJenkins(auth *Auth, url string, insecure bool) *Jenkins {
url = strings.TrimRight(url, "/")

client := http.DefaultClient
if insecure {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}

return &Jenkins{
Auth: auth,
BaseURL: url,
Client: client,
}
}

Expand All @@ -48,7 +61,7 @@ func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
if jenkins.Auth != nil {
req.SetBasicAuth(jenkins.Auth.Username, jenkins.Auth.Token)
}
return http.DefaultClient.Do(req)
return jenkins.Client.Do(req)
}

func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (err error) {
Expand Down
6 changes: 3 additions & 3 deletions jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestParseJobPath(t *testing.T) {
Username: "appleboy",
Token: "1234",
}
jenkins := NewJenkins(auth, "http://example.com")
jenkins := NewJenkins(auth, "http://example.com", false)

assert.Equal(t, "/job/foo", jenkins.parseJobPath("/foo/"))
assert.Equal(t, "/job/foo", jenkins.parseJobPath("foo/"))
Expand All @@ -25,7 +25,7 @@ func TestUnSupportProtocol(t *testing.T) {
Username: "foo",
Token: "bar",
}
jenkins := NewJenkins(auth, "example.com")
jenkins := NewJenkins(auth, "example.com", false)

err := jenkins.trigger("drone-jenkins", nil)
assert.NotNil(t, err)
Expand All @@ -36,7 +36,7 @@ func TestTriggerBuild(t *testing.T) {
Username: "foo",
Token: "bar",
}
jenkins := NewJenkins(auth, "http://example.com")
jenkins := NewJenkins(auth, "http://example.com", false)

err := jenkins.trigger("drone-jenkins", url.Values{"token": []string{"bar"}})
assert.Nil(t, err)
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func main() {
Usage: "jenkins job",
EnvVar: "PLUGIN_JOB,JENKINS_JOB,INPUT_JOB",
},
cli.BoolFlag{
Name: "insecure",
Usage: "allow insecure server connections when using SSL",
EnvVar: "PLUGIN_INSECURE,JENKINS_INSECURE,INPUT_INSECURE",
},
}

// Override a template
Expand Down Expand Up @@ -100,6 +105,7 @@ func run(c *cli.Context) error {
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
Insecure: c.Bool("insecure"),
}

return plugin.Exec()
Expand Down
3 changes: 2 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type (
Username string
Token string
Job []string
Insecure bool
}
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (p Plugin) Exec() error {
Token: p.Token,
}

jenkins := NewJenkins(auth, p.BaseURL)
jenkins := NewJenkins(auth, p.BaseURL, p.Insecure)

for _, v := range jobs {
if err := jenkins.trigger(v, nil); err != nil {
Expand Down

0 comments on commit 1bb020b

Please sign in to comment.