Skip to content

Commit bbe2227

Browse files
committed
HttpRequestStepCredentialsTest: cpsScriptCredentialTestHttpRequest(): refactor to optionally juggle withReentrability and to debug-trace withLocalCertLookup [JENKINS-70101]
Import and adapt code evicted from jenkinsci/credentials-plugin#391 Signed-off-by: Jim Klimov <jimklimov+jenkinsci@gmail.com>
1 parent 5e2b2df commit bbe2227

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

src/test/java/jenkins/plugins/http_request/HttpRequestStepCredentialsTest.java

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ private String getLogAsStringPlaintext(WorkflowRun f) throws java.io.IOException
211211
return baos.toString();
212212
}
213213

214+
/** Returns a String with prepared part of the pipeline script with imports used by some other snippet generators */
215+
private String cpsScriptCredentialTestImports() {
216+
return "import com.cloudbees.plugins.credentials.CredentialsMatchers;\n" +
217+
"import com.cloudbees.plugins.credentials.CredentialsProvider;\n" +
218+
"import com.cloudbees.plugins.credentials.common.StandardCertificateCredentials;\n" +
219+
"import com.cloudbees.plugins.credentials.common.StandardCredentials;\n" +
220+
"import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;\n" +
221+
"import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;\n" +
222+
"import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;\n" +
223+
"import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl.KeyStoreSource;\n" +
224+
"import hudson.security.ACL;\n" +
225+
"import java.security.KeyStore;\n" +
226+
"\n";
227+
}
228+
214229
/** Returns a String with prepared part of the pipeline script with a request
215230
* (to non-existent site) using a credential named by "id" parameter.<br/>
216231
*
@@ -223,12 +238,39 @@ private String getLogAsStringPlaintext(WorkflowRun f) throws java.io.IOException
223238
*
224239
* @param id Credential ID, saved earlier into the store
225240
* @param runnerTag Currently not used
241+
* @param withReentrability If true, generate a second request with same credential,
242+
* to make sure it is not garbled etc. by first use.
243+
* @param withLocalCertLookup If true, add lookup and logging of keystore data
244+
* (into the pipeline build console, optionally also system streams).
245+
* Note: test cases {@code withLocalCertLookup} need to
246+
* generate {@link #cpsScriptCredentialTestImports} into
247+
* their pipelines first.
226248
* @return String with prepared part of pipeline script
227249
*/
228-
private String cpsScriptCredentialTestHttpRequest(String id, String runnerTag) {
250+
private String cpsScriptCredentialTestHttpRequest(String id, String runnerTag, Boolean withReentrability, Boolean withLocalCertLookup) {
229251
return "def authentication='" + id + "';\n"
230252
+ "\n"
231253
+ "def msg\n"
254+
+ (withLocalCertLookup ? (
255+
"if (true) { // scoping\n"
256+
+ " msg = \"Finding credential...\"\n"
257+
+ " echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
258+
+ " StandardCredentials credential = CredentialsMatchers.firstOrNull(\n"
259+
+ " CredentialsProvider.lookupCredentials(\n"
260+
+ " StandardCredentials.class,\n"
261+
+ " Jenkins.instance, null, null),\n"
262+
+ " CredentialsMatchers.withId(authentication));\n"
263+
+ " msg = \"Getting keystore...\"\n"
264+
+ " echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
265+
+ " KeyStore keyStore = credential.getKeyStore();\n"
266+
+ " msg = \"Getting keystore source...\"\n"
267+
+ " echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
268+
+ " KeyStoreSource kss = ((CertificateCredentialsImpl) credential).getKeyStoreSource();\n"
269+
+ " msg = \"Getting keystore source bytes...\"\n"
270+
+ " echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
271+
+ " byte[] kssb = kss.getKeyStoreBytes();\n"
272+
+ "}\n" )
273+
: "" )
232274
+ "\n"
233275
+ "msg = \"Querying HTTPS with credential...\"\n"
234276
+ "echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
@@ -239,21 +281,36 @@ private String cpsScriptCredentialTestHttpRequest(String id, String runnerTag) {
239281
+ " contentType : 'APPLICATION_FORM',\n"
240282
+ " validResponseCodes: '100:599',\n"
241283
+ " quiet: false)\n"
242-
+ "println('First HTTP Request Plugin Status: '+ response.getStatus())\n"
243-
+ "println('First HTTP Request Plugin Response: '+ response.getContent())\n"
284+
+ "println('" + (withReentrability ? "First " : "") + "HTTP Request Plugin Status: '+ response.getStatus())\n"
285+
+ "println('" + (withReentrability ? "First " : "") + "First HTTP Request Plugin Response: '+ response.getContent())\n"
244286
+ "\n"
245-
+ "msg = \"Querying HTTPS with credential again (reentrability)...\"\n"
246-
+ "echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
247-
+ "response = httpRequest(url: 'https://github.xcom/api/v3',\n"
248-
+ " httpMode: 'GET',\n"
249-
+ " authentication: authentication,\n"
250-
+ " consoleLogResponseBody: true,\n"
251-
+ " contentType : 'APPLICATION_FORM',\n"
252-
+ " validResponseCodes: '100:599',\n"
253-
+ " quiet: false)\n"
254-
+ "println('Second HTTP Request Plugin Status: '+ response.getStatus())\n"
255-
+ "println('Second HTTP Request Plugin Response: '+ response.getContent())\n"
256-
+ "\n";
287+
+ (withReentrability ? (
288+
"msg = \"Querying HTTPS with credential again (reentrability)...\"\n"
289+
+ "echo msg;" + (verbosePipelines ? " System.out.println(msg); System.err.println(msg)" : "" ) + ";\n"
290+
+ "response = httpRequest(url: 'https://github.xcom/api/v3',\n"
291+
+ " httpMode: 'GET',\n"
292+
+ " authentication: authentication,\n"
293+
+ " consoleLogResponseBody: true,\n"
294+
+ " contentType : 'APPLICATION_FORM',\n"
295+
+ " validResponseCodes: '100:599',\n"
296+
+ " quiet: false)\n"
297+
+ "println('Second HTTP Request Plugin Status: '+ response.getStatus())\n"
298+
+ "println('Second HTTP Request Plugin Response: '+ response.getContent())\n"
299+
+ "\n" )
300+
: "" );
301+
}
302+
303+
/** Wrapper for {@link #cpsScriptCredentialTestHttpRequest(String, String, Boolean, Boolean)}
304+
* to MAYBE trace {@code withLocalCertLookup=verbosePipelines} by default */
305+
private String cpsScriptCredentialTestHttpRequest(String id, String runnerTag, Boolean withReentrability) {
306+
return cpsScriptCredentialTestHttpRequest(id, runnerTag, withReentrability, verbosePipelines);
307+
}
308+
309+
/** Wrapper for {@link #cpsScriptCredentialTestHttpRequest(String, String, Boolean, Boolean)}
310+
* to MAYBE trace {@code withLocalCertLookup=verbosePipelines}
311+
* and enable {@code withReentrability=true} by default */
312+
private String cpsScriptCredentialTestHttpRequest(String id, String runnerTag) {
313+
return cpsScriptCredentialTestHttpRequest(id, runnerTag, true, verbosePipelines);
257314
}
258315

259316
/////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)