forked from prospero238/liquibase-runner
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/JENKINS-33401-credentials-migration' into develop
- Loading branch information
Showing
7 changed files
with
416 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/org/jenkinsci/plugins/liquibase/evaluator/CredentialsMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.jenkinsci.plugins.liquibase.evaluator; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Describable; | ||
import hudson.model.Item; | ||
import hudson.model.ModelObject; | ||
import hudson.model.Project; | ||
import hudson.model.listeners.ItemListener; | ||
import hudson.util.DescribableList; | ||
import jenkins.model.Jenkins; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import com.cloudbees.plugins.credentials.Credentials; | ||
import com.cloudbees.plugins.credentials.CredentialsScope; | ||
import com.cloudbees.plugins.credentials.SystemCredentialsProvider; | ||
import com.cloudbees.plugins.credentials.domains.Domain; | ||
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; | ||
import com.google.common.base.Strings; | ||
|
||
/** | ||
* Migrates projects that had been using old username/password fields to use credentials. | ||
*/ | ||
public class CredentialsMigrator { | ||
|
||
private static final Logger LOG = Logger.getLogger(CredentialsMigrator.class.getName()); | ||
|
||
@Extension | ||
static final public class ItemListenerImpl extends ItemListener { | ||
@Override | ||
public void onLoaded() { | ||
CredentialsMigrator.migrateLegacyCredentials(); | ||
} | ||
} | ||
static void migrateLegacyCredentials() { | ||
LOG.finest("credentials migrator invoked"); | ||
|
||
Jenkins instance = Jenkins.getInstance(); | ||
if (instance != null) { | ||
List<Project> projects = instance.getItems(Project.class); | ||
for (Project project : projects) { | ||
DescribableList buildersList = project.getBuildersList(); | ||
Describable describable = buildersList.get(AbstractLiquibaseBuilder.class); | ||
if (describable != null) { | ||
AbstractLiquibaseBuilder liquibaseBuilder = (AbstractLiquibaseBuilder) describable; | ||
migrateCredentials(project, liquibaseBuilder); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected static void migrateCredentials(Item project, AbstractLiquibaseBuilder liquibaseBuilder) { | ||
if (liquibaseBuilder.hasLegacyCredentials()) { | ||
if (LOG.isLoggable(Level.FINER)) { | ||
LOG.finer("found builder needing credentials migration. project:" + | ||
project.getDisplayName()); | ||
} | ||
try { | ||
String plainTextPassword = Strings.nullToEmpty(liquibaseBuilder.getPassword()); | ||
String credentialsId = createCredentials(liquibaseBuilder.getUsername(), | ||
plainTextPassword, | ||
project); | ||
liquibaseBuilder.setCredentialsId(credentialsId); | ||
liquibaseBuilder.clearLegacyCredentials(); | ||
project.save(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static String createCredentials(String username, String password, ModelObject project) throws IOException { | ||
if (LOG.isLoggable(Level.FINE)) { | ||
LOG.fine("creating new credential for project:" + project.getDisplayName()); | ||
} | ||
|
||
String credentialsId = UUID.randomUUID().toString(); | ||
String description = "migrated from " + project.getDisplayName(); | ||
Credentials credentialsToCreate = new UsernamePasswordCredentialsImpl( | ||
CredentialsScope.GLOBAL, | ||
credentialsId, | ||
description, | ||
username, | ||
password | ||
); | ||
|
||
SystemCredentialsProvider credentialsProvider = SystemCredentialsProvider.getInstance(); | ||
Map<Domain, List<Credentials>> credentialsMap = credentialsProvider.getDomainCredentialsMap(); | ||
|
||
Domain global = Domain.global(); | ||
if (credentialsMap.get(global) == null) { | ||
credentialsMap.put(global, Collections.<Credentials>emptyList()); | ||
} | ||
credentialsMap.get(global).add(credentialsToCreate); | ||
credentialsProvider.setDomainCredentialsMap(credentialsMap); | ||
credentialsProvider.save(); | ||
|
||
return credentialsId; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/resources/scripted-upgrade-test/oldauth_job_config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project> | ||
<actions/> | ||
<description></description> | ||
<keepDependencies>false</keepDependencies> | ||
<properties/> | ||
<scm class="hudson.scm.NullSCM"/> | ||
<canRoam>true</canRoam> | ||
<disabled>false</disabled> | ||
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> | ||
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> | ||
<triggers/> | ||
<concurrentBuild>false</concurrentBuild> | ||
<builders> | ||
<org.jenkinsci.plugins.liquibase.evaluator.ChangesetEvaluator plugin="liquibase-runner@1.1.0"> | ||
<databaseEngine>H2</databaseEngine> | ||
<changeLogFile>@CHANGELOG_FILEPATH@</changeLogFile> | ||
<url>jdbc:h2:file:@H2_FILE@</url> | ||
<defaultSchemaName></defaultSchemaName> | ||
<contexts></contexts> | ||
<liquibasePropertiesPath></liquibasePropertiesPath> | ||
<classpath></classpath> | ||
<driverClassname></driverClassname> | ||
<labels></labels> | ||
<changeLogParameters></changeLogParameters> | ||
<basePath>@BASE_PATH@</basePath> | ||
<useIncludedDriver>true</useIncludedDriver> | ||
<username>sa</username> | ||
<password></password> | ||
<testRollbacks>true</testRollbacks> | ||
<dropAll>true</dropAll> | ||
<tagOnSuccessfulBuild>false</tagOnSuccessfulBuild> | ||
</org.jenkinsci.plugins.liquibase.evaluator.ChangesetEvaluator> | ||
</builders> | ||
<publishers/> | ||
<buildWrappers/> | ||
</project> |
Oops, something went wrong.