Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Extra credentials #61

Merged
merged 6 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/io/moderne/connect/commands/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ static class Tenant {
String moderneToken;
}

@CommandLine.Option(names = "--credentials",
description = "Extra credentials to bind in the form of " +
"CRENDENTIALS_ID=VARIABLE for StringBinding or " +
"CREDENTIALS_ID=USERNAME_VARIABLE:PASSWORD_VARIABLE for UsernamePasswordMultiBinding")
Map<String, String> extraCredentials;

static final String JENKINS_CRUMB_HEADER = "Jenkins-Crumb";

private static final String PLATFORM_WINDOWS = "windows";
Expand Down Expand Up @@ -860,6 +866,18 @@ private void addCommonCredentialBindings(StringBuilder bindings) {
bindings.append("\n");
bindings.append(Templates.FREESTYLE_CREDENTIALS_BINDING_USER_DEFINITION.format(downloadCLICreds, "CLI_DOWNLOAD_CRED_USR", "CLI_DOWNLOAD_CRED_PWD"));
}

if (extraCredentials != null) {
for (Map.Entry<String, String> entry : extraCredentials.entrySet()) {
String credentialsId = entry.getKey();
String[] variables = entry.getValue().split(":");
if (variables.length == 1) {
bindings.append(Templates.FREESTYLE_CREDENTIALS_BINDING_TOKEN_DEFINITION.format(credentialsId, variables[0]));
} else if (variables.length == 2) {
bindings.append(Templates.FREESTYLE_CREDENTIALS_BINDING_USER_DEFINITION.format(credentialsId, variables[0], variables[1]));
}
}
}
}

private String createFreestyleConfigFiles(Map<String, String> plugins) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/moderne/connect/commands/JenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,28 @@ void submitFreestyleJobsNoCleanup() throws Exception {
}

}

@Test
void submitJobExtraCredentials() throws Exception {
int result = cmd.execute("jenkins",
"--fromCsv", new File("src/test/csv/repos.csv").getAbsolutePath(),
"--controllerUrl", jenkinsHost,
"--jenkinsUser", JENKINS_TESTING_USER,
"--apiToken", apiToken,
"--publishCredsId", ARTIFACT_CREDS,
"--gitCredsId", GIT_CREDS,
"--publishUrl", ARTIFACTORY_URL,
"--workspaceCleanup",
"--credentials", "extraCredentials1=TOKEN_VARIABLE",
"--credentials", "extraCredentials2=USER_VARIABLE:PASSWORD_VARIABLE");
assertEquals(0, result);

await().untilAsserted(() -> assertTrue(Unirest.get(jenkinsHost + "/job/moderne-ingest/job/openrewrite_rewrite-spring_main/api/json")
.asString().isSuccess()));

HttpResponse<String> response = Unirest.get(jenkinsHost + "/job/moderne-ingest/job/openrewrite_rewrite-spring_main/config.xml").asString();
assertTrue(response.isSuccess(), "Failed to get job config.xml: " + response.getStatusText());
String expectedJob = new String(Files.readAllBytes(new File("src/test/jenkins/config-credentials.xml").toPath()));
assertThat(response.getBody()).isEqualToIgnoringWhitespace(expectedJob);
}
}
108 changes: 108 additions & 0 deletions src/test/jenkins/config-credentials.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.1" encoding="UTF-8"?><project>
<actions/>
<description/>
<keepDependencies>false</keepDependencies>
<properties>
<jenkins.model.BuildDiscarderProperty>
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>3</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>3</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>

</properties>
<scm class="hudson.plugins.git.GitSCM" plugin="git@5.1.0">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/openrewrite/rewrite-spring.git</url>
<credentialsId>myGitCreds</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/main</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="empty-list"/>
<extensions/>
</scm>

<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers>
<hudson.triggers.TimerTrigger>
<spec>H H * * *</spec>
</hudson.triggers.TimerTrigger>
</triggers>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local .</command>
<configuredLocalRules/>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>mod build . --no-download</command>
<configuredLocalRules/>
</hudson.tasks.Shell>

<hudson.tasks.Shell>
<command>mod publish .</command>
<configuredLocalRules/>
</hudson.tasks.Shell>

</builders>
<publishers>
<hudson.tasks.ArtifactArchiver>
<artifacts>.moderne/build/**/build.log</artifacts>
<allowEmptyArchive>false</allowEmptyArchive>
<onlyIfSuccessful>false</onlyIfSuccessful>
<fingerprint>false</fingerprint>
<defaultExcludes>true</defaultExcludes>
<caseSensitive>true</caseSensitive>
<followSymlinks>false</followSymlinks>
</hudson.tasks.ArtifactArchiver>
<hudson.plugins.ws__cleanup.WsCleanup plugin="ws-cleanup@0.45">
<patterns class="empty-list"/>
<deleteDirs>false</deleteDirs>
<skipWhenFailed>false</skipWhenFailed>
<cleanWhenSuccess>true</cleanWhenSuccess>
<cleanWhenUnstable>true</cleanWhenUnstable>
<cleanWhenFailure>true</cleanWhenFailure>
<cleanWhenNotBuilt>true</cleanWhenNotBuilt>
<cleanWhenAborted>true</cleanWhenAborted>
<notFailBuild>false</notFailBuild>
<cleanupMatrixParent>false</cleanupMatrixParent>
<externalDelete/>
<disableDeferredWipeout>false</disableDeferredWipeout>
</hudson.plugins.ws__cleanup.WsCleanup>
</publishers>
<buildWrappers>
<org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper plugin="credentials-binding@631.v861c06d062b_4">
<bindings>
<org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
<credentialsId>artifactCreds</credentialsId>
<usernameVariable>ARTIFACTS_PUBLISH_CRED_USR</usernameVariable>
<passwordVariable>ARTIFACTS_PUBLISH_CRED_PWD</passwordVariable>
</org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
<org.jenkinsci.plugins.credentialsbinding.impl.StringBinding>
<credentialsId>extraCredentials1</credentialsId>
<variable>TOKEN_VARIABLE</variable>
</org.jenkinsci.plugins.credentialsbinding.impl.StringBinding>
<org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
<credentialsId>extraCredentials2</credentialsId>
<usernameVariable>USER_VARIABLE</usernameVariable>
<passwordVariable>PASSWORD_VARIABLE</passwordVariable>
</org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>

</bindings>
</org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper>


</buildWrappers>
</project>