Skip to content
This repository
Browse code

Added auth basic support to handle private jenkins projects

  • Loading branch information...
commit fa976323e5b6f84d73b455ee26943e9b28b2c932 1 parent 61a92c2
William Durand authored June 18, 2012
2  build.gradle
@@ -33,7 +33,7 @@ task syncWithRepo(dependsOn: 'classes', type: JavaExec) {
33 33
     main = 'com.entagen.jenkins.Main'
34 34
     classpath = sourceSets.main.runtimeClasspath
35 35
     // pass through specified system properties to the call to main
36  
-    ['help', 'jenkinsUrl', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'nestedView', 'printConfig', 'dryRun'].each {
  36
+    ['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'nestedView', 'printConfig', 'dryRun'].each {
37 37
         if (System.getProperty(it)) systemProperty it, System.getProperty(it)
38 38
     }
39 39
 
20  src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
@@ -7,15 +7,31 @@ import static groovyx.net.http.ContentType.*
7 7
 import org.apache.http.conn.HttpHostConnectException
8 8
 import org.apache.http.client.HttpResponseException
9 9
 import org.apache.http.HttpStatus
  10
+import org.apache.http.HttpRequestInterceptor
  11
+import org.apache.http.protocol.HttpContext
  12
+import org.apache.http.HttpRequest
10 13
 
11 14
 class JenkinsApi {
12 15
     String jenkinsServerUrl
13 16
     RESTClient restClient
14 17
 
15  
-    public void setJenkinsServerUrl(String jenkinsServerUrl) {
16  
-        if (!jenkinsServerUrl.endsWith("/")) jenkinsServerUrl += "/"
  18
+    public JenkinsApi(String jenkinsServerUrl, String jenkinsServerUser, String jenkinsServerPassword) {
  19
+        if (!jenkinsServerUrl.endsWith("/")) {
  20
+            jenkinsServerUrl += "/"
  21
+        }
17 22
         this.jenkinsServerUrl = jenkinsServerUrl
18 23
         this.restClient = new RESTClient(jenkinsServerUrl)
  24
+
  25
+        if (null != jenkinsServerUser && null != jenkinsServerPassword) {
  26
+            println "use basic authentication"
  27
+
  28
+            this.restClient.client.addRequestInterceptor(new HttpRequestInterceptor() {
  29
+                void process(HttpRequest httpRequest, HttpContext httpContext) {
  30
+                    def auth = jenkinsServerUser + ':' + jenkinsServerPassword
  31
+                    httpRequest.addHeader('Authorization', 'Basic ' + auth.bytes.encodeBase64().toString())
  32
+                }
  33
+            })
  34
+        }
19 35
     }
20 36
 
21 37
     List<String> getJobNames(String prefix = null) {
4  src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy
@@ -11,6 +11,10 @@ import org.apache.http.HttpStatus
11 11
 
12 12
 class JenkinsApiReadOnly extends JenkinsApi {
13 13
 
  14
+    public JenkinsApiReadOnly(String jenkinsServerUrl, String jenkinsServerUser, String jenkinsServerPassword) {
  15
+        super(jenkinsServerUrl, jenkinsServerUser, jenkinsServerPassword)
  16
+    }
  17
+
14 18
     @Override
15 19
     protected Integer post(String path, postBody = [:], params = [:], ContentType contentType = ContentType.URLENC) {
16 20
         println "READ ONLY! skipping POST to $path with params: ${params}, postBody:\n$postBody"
6  src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
@@ -6,6 +6,8 @@ class JenkinsJobManager {
6 6
     String gitUrl
7 7
     String nestedView
8 8
     String jenkinsUrl
  9
+    String jenkinsUser
  10
+    String jenkinsPassword
9 11
     Boolean dryRun = false
10 12
 
11 13
     JenkinsApi jenkinsApi
@@ -128,9 +130,9 @@ class JenkinsJobManager {
128 130
             assert jenkinsUrl != null
129 131
             if (dryRun) {
130 132
                 println "DRY RUN! Not executing any POST commands to Jenkins, only GET commands"
131  
-                this.jenkinsApi = new JenkinsApiReadOnly(jenkinsServerUrl: jenkinsUrl)
  133
+                this.jenkinsApi = new JenkinsApiReadOnly(jenkinsUrl, jenkinsUser, jenkinsPassword)
132 134
             } else {
133  
-                this.jenkinsApi = new JenkinsApi(jenkinsServerUrl: jenkinsUrl)
  135
+                this.jenkinsApi = new JenkinsApi(jenkinsUrl, jenkinsUser, jenkinsPassword)
134 136
             }
135 137
         }
136 138
 
4  src/main/groovy/com/entagen/jenkins/Main.groovy
@@ -13,7 +13,9 @@ class Main {
13 13
             t: [longOpt: 'template-branch', required: true, args: 1, argName:  'templateBranchName', description: "Template Branch Name - gradle flag -DtemplateBranchName=<branchName>"],
14 14
             n: [longOpt: 'nested-view', required: false, args: 1, argName: 'nestedView', description: "Nested Parent View Name - gradle flag -DnestedView=<nestedView> - optional - must have Jenkins Nested View Plugin installed"],
15 15
             c: [longOpt: 'print-config', required: false, args: 0, argName: 'printConfig', description:  "Check configuration - print out settings then exit - gradle flag -DprintConfig=true"],
16  
-            d: [longOpt: 'dry-run', required: false, args: 0, argName: 'dryRun', description:  "Dry run, don't actually modify, create, or delete any jobs, just print out what would happen - gradle flag: -DdryRun=true"]
  16
+            d: [longOpt: 'dry-run', required: false, args: 0, argName: 'dryRun', description:  "Dry run, don't actually modify, create, or delete any jobs, just print out what would happen - gradle flag: -DdryRun=true"],
  17
+            usr: [longOpt: 'jenkins-user',  required: false, args: 1, argName: 'jenkinsUser', description: "Jenkins username - gradle flag -DjenkinsUser=<jenkinsUser>"],
  18
+            pwd: [longOpt: 'jenkins-password',  required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword=<jenkinsPassword>"]
17 19
     ]
18 20
 
19 21
     public static void main(String[] args) {

1 note on commit fa97632

Jeff Beck

Great add on.

Please sign in to comment.
Something went wrong with that request. Please try again.