From 364dcafa8a3a953610e152ec03df63ac5d42f4fe Mon Sep 17 00:00:00 2001 From: Joan Viladrosa Date: Tue, 12 Dec 2023 19:10:33 +0100 Subject: [PATCH 1/5] change to local config --- src/main/java/io/moderne/connect/commands/Jenkins.java | 10 +++++----- src/test/jenkins/config-agent.xml | 2 +- .../jenkins/config-freestyle-gradle-no-cleanup.xml | 6 +++--- src/test/jenkins/config-freestyle-gradle-validate.xml | 2 +- src/test/jenkins/config-freestyle-gradle.xml | 6 +++--- src/test/jenkins/config-freestyle-maven-validate.xml | 2 +- src/test/jenkins/config-freestyle-maven.xml | 6 +++--- src/test/jenkins/config-no-cleanup.xml | 2 +- src/test/jenkins/config.xml | 2 +- src/test/jenkins/rewrite-java-migration-config.xml | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/moderne/connect/commands/Jenkins.java b/src/main/java/io/moderne/connect/commands/Jenkins.java index bf9b57a..48874b4 100644 --- a/src/main/java/io/moderne/connect/commands/Jenkins.java +++ b/src/main/java/io/moderne/connect/commands/Jenkins.java @@ -620,9 +620,9 @@ private String createConfigArtifactsCommand() { if (downloadCLI || !StringUtils.isBlank(downloadCLIUrl)) { command += isWindowsPlatform ? ".\\" : "./"; } - command += String.format("%s config artifacts edit %s--user=%s --password=%s %s", + command += String.format("%s config artifacts edit %s--user=%s --password=%s %s --local .", isWindowsPlatform ? "mod.exe" : "mod", - skipSSL ? "--skipSSL " : "", + skipSSL ? "--skip-ssl " : "", isWindowsPlatform ? "$env:ARTIFACTS_PUBLISH_CRED_USR" : "${ARTIFACTS_PUBLISH_CRED_USR}", isWindowsPlatform ? "$env:ARTIFACTS_PUBLISH_CRED_PWD" : "${ARTIFACTS_PUBLISH_CRED_PWD}", publishUrl @@ -640,7 +640,7 @@ private String createConfigTenantCommand() { if (downloadCLI || !StringUtils.isBlank(downloadCLIUrl)) { command += isWindowsPlatform ? ".\\" : "./"; } - command += String.format("%s config moderne edit --token=%s %s", + command += String.format("%s config moderne edit --token=%s %s --local .", isWindowsPlatform ? "mod.exe" : "mod", isWindowsPlatform ? "$env:MODERNE_TOKEN" : "${MODERNE_TOKEN}", tenant.moderneUrl @@ -659,9 +659,9 @@ private String createConfigMavenSettingsCommand() { command += isWindowsPlatform ? ".\\" : "./"; } - return command + String.format("%s config maven settings edit %s", + return command + String.format("%s config maven settings edit %s --local .", isWindowsPlatform ? "mod.exe" : "mod", - isWindowsPlatform ? "\"\\$env:MODERNE_MVN_SETTINGS_XML\"" : "\"\\${MODERNE_MVN_SETTINGS_XML}\""); + isWindowsPlatform ? "$env:MODERNE_MVN_SETTINGS_XML" : "${MODERNE_MVN_SETTINGS_XML}"); } private String createBuildCommand(String activeStyle, String additionalBuildArgs) { diff --git a/src/test/jenkins/config-agent.xml b/src/test/jenkins/config-agent.xml index 1051b72..c0203a1 100644 --- a/src/test/jenkins/config-agent.xml +++ b/src/test/jenkins/config-agent.xml @@ -44,7 +44,7 @@ false - mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . diff --git a/src/test/jenkins/config-freestyle-gradle-no-cleanup.xml b/src/test/jenkins/config-freestyle-gradle-no-cleanup.xml index cc8f582..90ce96d 100644 --- a/src/test/jenkins/config-freestyle-gradle-no-cleanup.xml +++ b/src/test/jenkins/config-freestyle-gradle-no-cleanup.xml @@ -47,15 +47,15 @@ - ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io + ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io --local . - ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . - ./mod config maven settings edit "\${MODERNE_MVN_SETTINGS_XML}" + ./mod config maven settings edit ${MODERNE_MVN_SETTINGS_XML} --local . diff --git a/src/test/jenkins/config-freestyle-gradle-validate.xml b/src/test/jenkins/config-freestyle-gradle-validate.xml index a87aa70..32ada25 100644 --- a/src/test/jenkins/config-freestyle-gradle-validate.xml +++ b/src/test/jenkins/config-freestyle-gradle-validate.xml @@ -70,7 +70,7 @@ - ./mod config maven settings edit "\${MODERNE_MVN_SETTINGS_XML}" + ./mod config maven settings edit ${MODERNE_MVN_SETTINGS_XML} --local . diff --git a/src/test/jenkins/config-freestyle-gradle.xml b/src/test/jenkins/config-freestyle-gradle.xml index 7400c43..e5d3b1b 100644 --- a/src/test/jenkins/config-freestyle-gradle.xml +++ b/src/test/jenkins/config-freestyle-gradle.xml @@ -47,15 +47,15 @@ - ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io + ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io --local . - ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . - ./mod config maven settings edit "\${MODERNE_MVN_SETTINGS_XML}" + ./mod config maven settings edit ${MODERNE_MVN_SETTINGS_XML} --local . diff --git a/src/test/jenkins/config-freestyle-maven-validate.xml b/src/test/jenkins/config-freestyle-maven-validate.xml index 6cc240b..7e9f268 100644 --- a/src/test/jenkins/config-freestyle-maven-validate.xml +++ b/src/test/jenkins/config-freestyle-maven-validate.xml @@ -70,7 +70,7 @@ - ./mod config maven settings edit "\${MODERNE_MVN_SETTINGS_XML}" + ./mod config maven settings edit ${MODERNE_MVN_SETTINGS_XML} --local . diff --git a/src/test/jenkins/config-freestyle-maven.xml b/src/test/jenkins/config-freestyle-maven.xml index e2a9aec..ade573d 100644 --- a/src/test/jenkins/config-freestyle-maven.xml +++ b/src/test/jenkins/config-freestyle-maven.xml @@ -47,15 +47,15 @@ - ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io + ./mod config moderne edit --token=${MODERNE_TOKEN} https://app.moderne.io --local . - ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + ./mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . - ./mod config maven settings edit "\${MODERNE_MVN_SETTINGS_XML}" + ./mod config maven settings edit ${MODERNE_MVN_SETTINGS_XML} --local . diff --git a/src/test/jenkins/config-no-cleanup.xml b/src/test/jenkins/config-no-cleanup.xml index 317716a..956acbd 100644 --- a/src/test/jenkins/config-no-cleanup.xml +++ b/src/test/jenkins/config-no-cleanup.xml @@ -43,7 +43,7 @@ false - mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . diff --git a/src/test/jenkins/config.xml b/src/test/jenkins/config.xml index 594eb37..c32eabc 100644 --- a/src/test/jenkins/config.xml +++ b/src/test/jenkins/config.xml @@ -43,7 +43,7 @@ false - mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . diff --git a/src/test/jenkins/rewrite-java-migration-config.xml b/src/test/jenkins/rewrite-java-migration-config.xml index 3ae912c..ce967fb 100644 --- a/src/test/jenkins/rewrite-java-migration-config.xml +++ b/src/test/jenkins/rewrite-java-migration-config.xml @@ -43,7 +43,7 @@ false - mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest + mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . From f48f93ca82a605ba14455841724845ad7636d846 Mon Sep 17 00:00:00 2001 From: Joan Viladrosa Date: Tue, 12 Dec 2023 19:16:16 +0100 Subject: [PATCH 2/5] fixed --skip-ssl on gitlab too --- src/main/java/io/moderne/connect/commands/GitLab.java | 2 +- src/test/java/io/moderne/connect/commands/GitlabTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/moderne/connect/commands/GitLab.java b/src/main/java/io/moderne/connect/commands/GitLab.java index 597cb26..e99a072 100644 --- a/src/main/java/io/moderne/connect/commands/GitLab.java +++ b/src/main/java/io/moderne/connect/commands/GitLab.java @@ -472,7 +472,7 @@ private String createConfigArtifactsCommand() { return ""; // for unit tests, will always be non-null in production } String args = String.format("config artifacts edit %s--user=%s --password=%s %s", - skipSSL ? "--skipSSL " : "", + skipSSL ? "--skip-ssl " : "", variable(publishUserSecretName), variable(publishPwdSecretName), publishUrl diff --git a/src/test/java/io/moderne/connect/commands/GitlabTest.java b/src/test/java/io/moderne/connect/commands/GitlabTest.java index 87a2b20..875ec54 100644 --- a/src/test/java/io/moderne/connect/commands/GitlabTest.java +++ b/src/test/java/io/moderne/connect/commands/GitlabTest.java @@ -167,7 +167,7 @@ void skipSsl() { gitlab.publishUserSecretName = "PUBLISH_USER"; gitlab.skipSSL = true; assertBuildSteps( - "./mod config artifacts edit --skipSSL --user=$PUBLISH_USER --password=$PUBLISH_SECRET https://my.artifactory/moderne-ingest", + "./mod config artifacts edit --skip-ssl --user=$PUBLISH_USER --password=$PUBLISH_SECRET https://my.artifactory/moderne-ingest", "./mod build $REPO_PATH --no-download --active-styles some-style --additional-build-args \"--magic\"", "./mod publish $REPO_PATH" ); From 01491177e50fc919e78cd609f96c5eed19c942d7 Mon Sep 17 00:00:00 2001 From: Joan Viladrosa Date: Tue, 12 Dec 2023 23:17:31 +0100 Subject: [PATCH 3/5] add option to configure extra UsernamePasswordMultiBinding and StringBinding credentials --- .../io/moderne/connect/commands/Jenkins.java | 16 +++ .../moderne/connect/commands/JenkinsTest.java | 24 ++++ src/test/jenkins/config-credentials.xml | 108 ++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 src/test/jenkins/config-credentials.xml diff --git a/src/main/java/io/moderne/connect/commands/Jenkins.java b/src/main/java/io/moderne/connect/commands/Jenkins.java index 48874b4..786fc29 100644 --- a/src/main/java/io/moderne/connect/commands/Jenkins.java +++ b/src/main/java/io/moderne/connect/commands/Jenkins.java @@ -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 extraCredentials; + static final String JENKINS_CRUMB_HEADER = "Jenkins-Crumb"; private static final String PLATFORM_WINDOWS = "windows"; @@ -860,6 +866,16 @@ 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")); } + + for (Map.Entry 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 plugins) { diff --git a/src/test/java/io/moderne/connect/commands/JenkinsTest.java b/src/test/java/io/moderne/connect/commands/JenkinsTest.java index 535a3df..67edea8 100644 --- a/src/test/java/io/moderne/connect/commands/JenkinsTest.java +++ b/src/test/java/io/moderne/connect/commands/JenkinsTest.java @@ -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 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); + } } diff --git a/src/test/jenkins/config-credentials.xml b/src/test/jenkins/config-credentials.xml new file mode 100644 index 0000000..d5303da --- /dev/null +++ b/src/test/jenkins/config-credentials.xml @@ -0,0 +1,108 @@ + + + + false + + + + -1 + 3 + -1 + 3 + + + + + + 2 + + + https://github.com/openrewrite/rewrite-spring.git + myGitCreds + + + + + */main + + + false + + + + + true + false + false + false + + + H H * * * + + + false + + + mod config artifacts edit --user=${ARTIFACTS_PUBLISH_CRED_USR} --password=${ARTIFACTS_PUBLISH_CRED_PWD} https://artifactory.moderne.ninja/artifactory/moderne-ingest --local . + + + + mod build . --no-download + + + + + mod publish . + + + + + + + .moderne/build/**/build.log + false + false + false + true + true + false + + + + false + false + true + true + true + true + true + false + false + + false + + + + + + + artifactCreds + ARTIFACTS_PUBLISH_CRED_USR + ARTIFACTS_PUBLISH_CRED_PWD + + + extraCredentials1 + TOKEN_VARIABLE + + + extraCredentials2 + USER_VARIABLE + PASSWORD_VARIABLE + + + + + + + + \ No newline at end of file From 68eba045096a9ec907f6394e3d8dd957a3e5d256 Mon Sep 17 00:00:00 2001 From: Joan Viladrosa Date: Wed, 13 Dec 2023 06:15:50 +0100 Subject: [PATCH 4/5] default empty map --- src/main/java/io/moderne/connect/commands/Jenkins.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/moderne/connect/commands/Jenkins.java b/src/main/java/io/moderne/connect/commands/Jenkins.java index 786fc29..0084909 100644 --- a/src/main/java/io/moderne/connect/commands/Jenkins.java +++ b/src/main/java/io/moderne/connect/commands/Jenkins.java @@ -282,7 +282,7 @@ static class Tenant { description = "Extra credentials to bind in the form of " + "CRENDENTIALS_ID=VARIABLE for StringBinding or " + "CREDENTIALS_ID=USERNAME_VARIABLE:PASSWORD_VARIABLE for UsernamePasswordMultiBinding") - Map extraCredentials; + Map extraCredentials = new HashMap<>(); static final String JENKINS_CRUMB_HEADER = "Jenkins-Crumb"; From b176486b276483708307a472a41ca0adbefce63c Mon Sep 17 00:00:00 2001 From: Joan Viladrosa Date: Wed, 13 Dec 2023 06:18:20 +0100 Subject: [PATCH 5/5] check null --- .../io/moderne/connect/commands/Jenkins.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/moderne/connect/commands/Jenkins.java b/src/main/java/io/moderne/connect/commands/Jenkins.java index 0084909..96952e0 100644 --- a/src/main/java/io/moderne/connect/commands/Jenkins.java +++ b/src/main/java/io/moderne/connect/commands/Jenkins.java @@ -282,7 +282,7 @@ static class Tenant { description = "Extra credentials to bind in the form of " + "CRENDENTIALS_ID=VARIABLE for StringBinding or " + "CREDENTIALS_ID=USERNAME_VARIABLE:PASSWORD_VARIABLE for UsernamePasswordMultiBinding") - Map extraCredentials = new HashMap<>(); + Map extraCredentials; static final String JENKINS_CRUMB_HEADER = "Jenkins-Crumb"; @@ -867,13 +867,15 @@ private void addCommonCredentialBindings(StringBuilder bindings) { bindings.append(Templates.FREESTYLE_CREDENTIALS_BINDING_USER_DEFINITION.format(downloadCLICreds, "CLI_DOWNLOAD_CRED_USR", "CLI_DOWNLOAD_CRED_PWD")); } - for (Map.Entry 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])); + if (extraCredentials != null) { + for (Map.Entry 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])); + } } } }