Skip to content

Commit e253b3e

Browse files
authored
Fix cannot connect jnlp agent with http proxy (#420)
* Fix cannot connect jnlp agent with http proxy * Add proxy credentials * Add comments * Fix the proxy protocoal * Try to fix the unit test errors * Fix the const export issues
1 parent 9e0ba17 commit e253b3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+283
-220
lines changed

app/cmd/center_download_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var _ = Describe("center download command", func() {
6060
})
6161

6262
It("download the lts Jenkins", func() {
63-
request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war-stable/latest/jenkins.war", nil)
63+
request, _ := http.NewRequest(http.MethodGet, "http://mirrors.jenkins.io/war-stable/latest/jenkins.war", nil)
6464
response := &http.Response{
6565
StatusCode: 200,
6666
Request: request,
@@ -82,7 +82,7 @@ var _ = Describe("center download command", func() {
8282
})
8383

8484
It("download the weekly Jenkins", func() {
85-
request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war/latest/jenkins.war", nil)
85+
request, _ := http.NewRequest(http.MethodGet, "http://mirrors.jenkins.io/war/latest/jenkins.war", nil)
8686
response := &http.Response{
8787
StatusCode: 200,
8888
Request: request,

app/cmd/center_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = Describe("center command", func() {
4444
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
4545
Expect(err).To(BeNil())
4646

47-
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/api/json", nil)
47+
requestCrumb, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/api/json", nil)
4848
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
4949
responseCrumb := &http.Response{
5050
StatusCode: 200,
@@ -57,7 +57,7 @@ var _ = Describe("center command", func() {
5757
roundTripper.EXPECT().
5858
RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
5959

60-
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil)
60+
request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil)
6161
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
6262
response := &http.Response{
6363
StatusCode: 200,

app/cmd/center_upgrade_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var _ = Describe("center upgrade command", func() {
4343
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
4444
Expect(err).To(BeNil())
4545

46-
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
46+
requestCrumb, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
4747
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
4848
responseCrumb := &http.Response{
4949
StatusCode: 200,
@@ -56,7 +56,7 @@ var _ = Describe("center upgrade command", func() {
5656
roundTripper.EXPECT().
5757
RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
5858

59-
request, _ := http.NewRequest("POST", "http://localhost:8080/jenkins/updateCenter/upgrade", nil)
59+
request, _ := http.NewRequest(http.MethodPost, "http://localhost:8080/jenkins/updateCenter/upgrade", nil)
6060
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
6161
request.Header.Add("CrumbRequestField", "Crumb")
6262
response := &http.Response{

app/cmd/center_watch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var _ = Describe("center watch command", func() {
4646
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
4747
Expect(err).To(BeNil())
4848

49-
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/api/json", nil)
49+
requestCrumb, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/api/json", nil)
5050
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
5151
responseCrumb := &http.Response{
5252
StatusCode: 200,

app/cmd/computer_launch.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import (
44
"fmt"
55
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
66
. "github.com/jenkins-zh/jenkins-cli/app/config"
7+
"github.com/jenkins-zh/jenkins-cli/app/i18n"
78
"github.com/jenkins-zh/jenkins-cli/client"
89
"github.com/jenkins-zh/jenkins-cli/util"
10+
"github.com/spf13/cobra"
911
"go.uber.org/zap"
1012
"io/ioutil"
13+
"net/url"
1114
"os"
12-
13-
"github.com/jenkins-zh/jenkins-cli/app/i18n"
14-
15-
"github.com/spf13/cobra"
15+
"strings"
1616
)
1717

1818
// ComputerLaunchOption option for config list command
@@ -28,14 +28,21 @@ type ComputerLaunchOption struct {
2828
Output string
2929
}
3030

31+
const (
32+
// AgentJNLP is the agent type of jnlp
33+
AgentJNLP = "jnlp"
34+
)
35+
3136
var computerLaunchOption ComputerLaunchOption
3237

3338
func init() {
3439
computerCmd.AddCommand(computerLaunchCmd)
35-
computerLaunchCmd.Flags().StringVarP(&computerLaunchOption.Type, "type", "", "",
40+
computerLaunchCmd.Flags().StringVarP(&computerLaunchOption.Type, "type", "", AgentJNLP,
3641
i18n.T("The type of agent, include jnlp"))
3742
computerLaunchCmd.Flags().BoolVarP(&computerLaunchOption.ShowProgress, "show-progress", "", true,
3843
i18n.T("Show the progress of downloading agent.jar"))
44+
45+
healthCheckRegister.Register(getCmdPath(computerLaunchCmd), &computerLaunchOption)
3946
}
4047

4148
var computerLaunchCmd = &cobra.Command{
@@ -50,17 +57,24 @@ jcli agent launch agent-name --type jnlp`,
5057
computerLaunchOption.ComputerClient, computerLaunchOption.CurrentJenkins =
5158
GetComputerClient(computerLaunchOption.CommonOption)
5259

53-
if computerLaunchOption.Type != "jnlp" {
60+
if computerLaunchOption.Type != AgentJNLP {
5461
return
5562
}
5663

5764
var f *os.File
5865
if f, err = ioutil.TempFile("/tmp", "agent.jar"); err == nil {
5966
computerLaunchOption.Output = f.Name()
67+
agentURL := fmt.Sprintf("%s/jnlpJars/agent.jar", computerLaunchOption.ComputerClient.URL)
68+
logger.Debug("start to download agent.jar", zap.String("url", agentURL))
69+
logger.Debug("proxy setting", zap.String("sever", computerLaunchOption.CurrentJenkins.Proxy),
70+
zap.String("auth", computerLaunchOption.CurrentJenkins.ProxyAuth))
71+
6072
downloader := util.HTTPDownloader{
6173
RoundTripper: computerLaunchOption.RoundTripper,
6274
TargetFilePath: computerLaunchOption.Output,
63-
URL: fmt.Sprintf("%s/jnlpJars/agent.jar", computerLaunchOption.ComputerClient.URL),
75+
URL: agentURL,
76+
Proxy: computerLaunchOption.CurrentJenkins.Proxy,
77+
ProxyAuth: computerLaunchOption.CurrentJenkins.ProxyAuth,
6478
ShowProgress: computerLaunchOption.ShowProgress,
6579
}
6680
err = downloader.DownloadFile()
@@ -69,11 +83,15 @@ jcli agent launch agent-name --type jnlp`,
6983
},
7084
RunE: func(_ *cobra.Command, args []string) (err error) {
7185
name := args[0]
86+
logger.Info("prepare to start agent", zap.String("name", name), zap.String("type", computerLaunchOption.Type))
87+
7288
switch computerLaunchOption.Type {
7389
case "":
7490
err = computerLaunchOption.Launch(name)
75-
case "jnlp":
91+
case AgentJNLP:
7692
err = computerLaunchOption.LaunchJnlp(name)
93+
default:
94+
err = fmt.Errorf("unsupported agent type %s", computerLaunchOption.Type)
7795
}
7896
return
7997
},
@@ -89,6 +107,8 @@ func (o *ComputerLaunchOption) Launch(name string) (err error) {
89107
func (o *ComputerLaunchOption) LaunchJnlp(name string) (err error) {
90108
var secret string
91109
if secret, err = o.ComputerClient.GetSecret(name); err == nil {
110+
logger.Info("get agent secret", zap.String("value", secret))
111+
92112
var binary string
93113
binary, err = util.LookPath("java", centerStartOption.LookPathContext)
94114
if err == nil {
@@ -97,10 +117,29 @@ func (o *ComputerLaunchOption) LaunchJnlp(name string) (err error) {
97117
"-jnlpUrl", fmt.Sprintf("%s/computer/%s/slave-agent.jnlp", o.ComputerClient.URL, name),
98118
"-secret", secret, "-workDir", "/tmp"}
99119

100-
logger.Debug("start a jnlp agent", zap.Any("command", agentArgs))
120+
if o.CurrentJenkins.ProxyAuth != "" {
121+
proxyURL, _ := url.Parse(o.CurrentJenkins.Proxy)
122+
agentArgs = append(agentArgs, "-proxyCredentials", o.CurrentJenkins.ProxyAuth)
123+
124+
proxyAuth := strings.SplitN(o.CurrentJenkins.ProxyAuth, ":", 2)
125+
if len(proxyAuth) == 2 {
126+
env = append(env, fmt.Sprintf("http_proxy=http://%s:%s@%s", url.QueryEscape(proxyAuth[0]), url.QueryEscape(proxyAuth[1]), proxyURL.Host))
127+
}
128+
}
129+
130+
logger.Debug("start a jnlp agent", zap.Any("command", strings.Join(agentArgs, " ")))
101131

102132
err = util.Exec(binary, agentArgs, env, o.SystemCallExec)
103133
}
104134
}
105135
return
106136
}
137+
138+
// Check do the health check of casc cmd
139+
func (o *ComputerLaunchOption) Check() (err error) {
140+
opt := PluginOptions{
141+
CommonOption: common.CommonOption{RoundTripper: o.RoundTripper},
142+
}
143+
_, err = opt.FindPlugin("pipeline-restful-api")
144+
return
145+
}

app/cmd/computer_launch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var _ = Describe("computer launch command", func() {
7373
computerLaunchOption.SystemCallExec = util.FakeSystemCallExecSuccess
7474
computerLaunchOption.LookPathContext = util.FakeLookPath
7575

76-
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/jnlpJars/agent.jar", nil)
76+
request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/jnlpJars/agent.jar", nil)
7777
response := &http.Response{
7878
StatusCode: 200,
7979
Request: request,

app/cmd/cwp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestGetLatest(t *testing.T) {
9797
}
9898

9999
func prepareDownloadFileRequest(url, content string, roundTripper *mhttp.MockRoundTripper) {
100-
request, _ := http.NewRequest("GET", url, nil)
100+
request, _ := http.NewRequest(http.MethodGet, url, nil)
101101
response := &http.Response{
102102
StatusCode: 200,
103103
Request: request,
@@ -108,7 +108,7 @@ func prepareDownloadFileRequest(url, content string, roundTripper *mhttp.MockRou
108108
}
109109

110110
func prepareMavenMetadataRequest(roundTripper *mhttp.MockRoundTripper) {
111-
request, _ := http.NewRequest("GET", "http://localhost/maven-metadata.xml", nil)
111+
request, _ := http.NewRequest(http.MethodGet, "http://localhost/maven-metadata.xml", nil)
112112
response := &http.Response{
113113
StatusCode: 200,
114114
Request: request,

app/cmd/job_artifact_download_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var _ = Describe("job artifact download command", func() {
6666

6767
client.PrepareGetArtifacts(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", jobName, buildID)
6868

69-
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/job/pipeline/1/artifact/a.log", nil)
69+
request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/job/pipeline/1/artifact/a.log", nil)
7070
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
7171
response := &http.Response{
7272
StatusCode: 200,

app/cmd/job_build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ You need to give the parameters if your pipeline has them. Learn more about it f
6666
paramDefs = append(paramDefs, client.ParameterDefinition{
6767
Name: entryArray[0],
6868
Value: entryArray[1],
69-
Type: "StringParameterDefinition",
69+
Type: client.StringParameterDefinition,
7070
})
7171
}
7272
}

app/cmd/job_build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var _ = Describe("job build command", func() {
5353
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
5454
Expect(err).To(BeNil())
5555

56-
request, _ := http.NewRequest("POST", fmt.Sprintf("http://localhost:8080/jenkins/job/%s/build", jobName), nil)
56+
request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/jenkins/job/%s/build", jobName), nil)
5757
request.Header.Add("CrumbRequestField", "Crumb")
5858
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
5959
response := &http.Response{
@@ -65,7 +65,7 @@ var _ = Describe("job build command", func() {
6565
roundTripper.EXPECT().
6666
RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
6767

68-
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
68+
requestCrumb, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
6969
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
7070
responseCrumb := &http.Response{
7171
StatusCode: 200,
@@ -156,7 +156,7 @@ var _ = Describe("job build command", func() {
156156
// token = "111e3a2f0231198855dceaff96f20540a9"
157157
// )
158158
//
159-
// request, _ := http.NewRequest("GET", fmt.Sprintf("%s/job/%s/api/json",
159+
// request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/job/%s/api/json",
160160
// url, jobName), nil)
161161
// request.SetBasicAuth(user, token)
162162
// response := &http.Response{

0 commit comments

Comments
 (0)