Skip to content
Permalink
Browse files

Merge pull request #3150 from jglick/User-JENKINS-47429

[JENKINS-47429] User.getLegacyConfigFilesFor no longer seems to be necessary
  • Loading branch information
oleg-nenashev committed Nov 28, 2017
2 parents 5f8f426 + 3853b38 commit 53a320afd86844ad82c2d7fa7b64e4143e776c55
Showing with 9 additions and 46 deletions.
  1. +9 −46 core/src/main/java/hudson/model/User.java
@@ -436,6 +436,10 @@ public synchronized void doSubmitDescription( StaplerRequest req, StaplerRespons
* {@code create} is false.
*/
private static @Nullable User getOrCreate(@Nonnull String id, @Nonnull String fullName, boolean create) {
return getOrCreate(id, fullName, create, getUnsanitizedLegacyConfigFileFor(id));
}

private static @Nullable User getOrCreate(@Nonnull String id, @Nonnull String fullName, boolean create, File unsanitizedLegacyConfigFile) {
String idkey = idStrategy().keyFor(id);

byNameLock.readLock().lock();
@@ -446,49 +450,17 @@ public synchronized void doSubmitDescription( StaplerRequest req, StaplerRespons
byNameLock.readLock().unlock();
}
final File configFile = getConfigFileFor(id);
if (u == null && !configFile.isFile() && !configFile.getParentFile().isDirectory()) {
// check for legacy users and migrate if safe to do so.
File[] legacy = getLegacyConfigFilesFor(id);
if (legacy != null && legacy.length > 0) {
for (File legacyUserDir : legacy) {
final XmlFile legacyXml = new XmlFile(XSTREAM, new File(legacyUserDir, "config.xml"));
try {
Object o = legacyXml.read();
if (o instanceof User) {
if (idStrategy().equals(id, legacyUserDir.getName()) && !idStrategy().filenameOf(legacyUserDir.getName())
.equals(legacyUserDir.getName())) {
if (legacyUserDir.renameTo(configFile.getParentFile())) {
LOGGER.log(Level.INFO, "Migrated user record from {0} to {1}", new Object[] {legacyUserDir, configFile.getParentFile()});
} else {
LOGGER.log(Level.WARNING, "Failed to migrate user record from {0} to {1}",
new Object[]{legacyUserDir, configFile.getParentFile()});
}
break;
}
} else {
LOGGER.log(Level.FINE, "Unexpected object loaded from {0}: {1}",
new Object[]{ legacyUserDir, o });
}
} catch (IOException e) {
LOGGER.log(Level.FINE, String.format("Exception trying to load user from %s: %s",
new Object[]{ legacyUserDir, e.getMessage() }), e);
}
}
}
}

File unsanitizedLegacyConfigFile = getUnsanitizedLegacyConfigFileFor(id);
if (unsanitizedLegacyConfigFile.exists() && !unsanitizedLegacyConfigFile.equals(configFile)) {
File ancestor = unsanitizedLegacyConfigFile.getParentFile();
if (!configFile.exists()) {
try {
Files.createDirectory(configFile.getParentFile().toPath());
Files.move(unsanitizedLegacyConfigFile.toPath(), configFile.toPath());
LOGGER.log(Level.INFO, "Migrated unsafe user record from {0} to {1}", new Object[] {unsanitizedLegacyConfigFile, configFile});
LOGGER.log(Level.INFO, "Migrated user record from {0} to {1}", new Object[] {unsanitizedLegacyConfigFile, configFile});
} catch (IOException | InvalidPathException e) {
LOGGER.log(
Level.WARNING,
String.format("Failed to migrate user record from %s to %s, see SECURITY-499 for more information", idStrategy().legacyFilenameOf(id), idStrategy().filenameOf(id)),
String.format("Failed to migrate user record from %s to %s", unsanitizedLegacyConfigFile, configFile),
e);
}
}
@@ -764,16 +736,6 @@ private static final File getConfigFileFor(String id) {
return new File(getRootDir(), idStrategy().filenameOf(id) +"/config.xml");
}

private static final File[] getLegacyConfigFilesFor(final String id) {
return getRootDir().listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory() && new File(pathname, "config.xml").isFile() && idStrategy().equals(
pathname.getName(), id);
}
});
}

private static File getUnsanitizedLegacyConfigFileFor(String id) {
return new File(getRootDir(), idStrategy().legacyFilenameOf(id) + "/config.xml");
}
@@ -1094,9 +1056,10 @@ public static void scanAll() {
File[] subdirs = getRootDir().listFiles((FileFilter) DirectoryFileFilter.INSTANCE);
if (subdirs != null) {
for (File subdir : subdirs) {
if (new File(subdir, "config.xml").exists()) {
File configFile = new File(subdir, "config.xml");
if (configFile.exists()) {
String name = strategy.idFromFilename(subdir.getName());
getOrCreate(name, /* <init> calls load(), probably clobbering this anyway */name, true);
getOrCreate(name, /* <init> calls load(), probably clobbering this anyway */name, true, configFile);
}
}
}

0 comments on commit 53a320a

Please sign in to comment.
You can’t perform that action at this time.