Mark config entry stores explicitly in exports (#1545)

The default (XML) store of SCM-Manager does not distinguish between config and config entry stores in regards to
storage locations. Nonetheless, we want to make a difference in export files, so that other store providers can handle
these stores differently. To do so, this change adds an attribute to the top level xml element of config entry stores
to mark them. In exports, these store files can now be exported in a different folder. To mark existing stores, this
introduces an update step.
This commit is contained in:
René Pfeuffer
2021-02-23 09:37:59 +01:00
committed by GitHub
parent 83a9c90130
commit ee02ba096f
18 changed files with 555 additions and 47 deletions

View File

@@ -63,17 +63,17 @@ public class TarArchiveRepositoryStoreImporter {
private void importStoreByType(Repository repository, TarArchiveInputStream tais, String[] entryPathParts) {
String storeType = entryPathParts[1];
if (storeType.equals(StoreType.DATA.getValue())) {
if (isDataStore(storeType)) {
repositoryStoreImporter
.doImport(repository)
.importStore(new StoreEntryMetaData(StoreType.DATA, entryPathParts[2]))
.importEntry(entryPathParts[3], tais);
} else if (storeType.equals(StoreType.CONFIG.getValue())){
} else if (isConfigStore(storeType)){
repositoryStoreImporter
.doImport(repository)
.importStore(new StoreEntryMetaData(StoreType.CONFIG, ""))
.importEntry(entryPathParts[2], tais);
} else if(storeType.equals(StoreType.BLOB.getValue())) {
} else if(isBlobStore(storeType)) {
repositoryStoreImporter
.doImport(repository)
.importStore(new StoreEntryMetaData(StoreType.BLOB, entryPathParts[2]))
@@ -94,16 +94,28 @@ public class TarArchiveRepositoryStoreImporter {
//This prevents array out of bound exceptions
if (entryPathParts.length > 1) {
String storeType = entryPathParts[1];
if (storeType.equals(StoreType.DATA.getValue()) || storeType.equals(StoreType.BLOB.getValue())) {
if (isDataStore(storeType) || isBlobStore(storeType)) {
return entryPathParts.length == 4;
}
if (storeType.equals(StoreType.CONFIG.getValue())) {
if (isConfigStore(storeType)) {
return entryPathParts.length == 3;
}
}
return false;
}
private boolean isBlobStore(String storeType) {
return storeType.equals(StoreType.BLOB.getValue());
}
private boolean isDataStore(String storeType) {
return storeType.equals(StoreType.DATA.getValue());
}
private boolean isConfigStore(String storeType) {
return storeType.equals(StoreType.CONFIG.getValue()) || storeType.equals(StoreType.CONFIG_ENTRY.getValue());
}
static class NoneClosingTarArchiveInputStream extends TarArchiveInputStream {
public NoneClosingTarArchiveInputStream(InputStream is) {

View File

@@ -0,0 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.update.store;
import sonia.scm.SCMContextProvider;
import sonia.scm.migration.UpdateStep;
import sonia.scm.plugin.Extension;
import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
@Extension
public class DifferentiateBetweenConfigAndConfigEntryForGlobalStoreUpdateStep extends DifferentiateBetweenConfigAndConfigEntryUpdateStep implements UpdateStep {
private final SCMContextProvider contextProvider;
@Inject
public DifferentiateBetweenConfigAndConfigEntryForGlobalStoreUpdateStep(SCMContextProvider contextProvider) {
this.contextProvider = contextProvider;
}
@Override
public void doUpdate() throws Exception {
Path configPath = new File(contextProvider.getBaseDirectory(), "config").toPath();
updateAllInDirectory(configPath);
}
}

View File

@@ -0,0 +1,49 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.update.store;
import sonia.scm.migration.RepositoryUpdateContext;
import sonia.scm.migration.RepositoryUpdateStep;
import sonia.scm.plugin.Extension;
import sonia.scm.repository.RepositoryLocationResolver;
import javax.inject.Inject;
import java.nio.file.Path;
@Extension
public class DifferentiateBetweenConfigAndConfigEntryForRepositoryStoresUpdateStep extends DifferentiateBetweenConfigAndConfigEntryUpdateStep implements RepositoryUpdateStep {
private final RepositoryLocationResolver locationResolver;
@Inject
public DifferentiateBetweenConfigAndConfigEntryForRepositoryStoresUpdateStep(RepositoryLocationResolver locationResolver) {
this.locationResolver = locationResolver;
}
@Override
public void doUpdate(RepositoryUpdateContext repositoryUpdateContext) throws Exception {
updateAllInDirectory(locationResolver.forClass(Path.class).getLocation(repositoryUpdateContext.getRepositoryId()));
}
}

View File

@@ -0,0 +1,131 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.update.store;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import sonia.scm.migration.UpdateException;
import sonia.scm.store.CopyOnWrite;
import sonia.scm.version.Version;
import sonia.scm.xml.XmlStreams;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
abstract class DifferentiateBetweenConfigAndConfigEntryUpdateStep {
private static final Logger LOG = LoggerFactory.getLogger(DifferentiateBetweenConfigAndConfigEntryUpdateStep.class);
public Version getTargetVersion() {
return Version.parse("2.0.0");
}
public String getAffectedDataType() {
return "sonia.scm.dao.xml";
}
void updateAllInDirectory(Path configDirectory) throws IOException {
try (Stream<Path> list = Files.list(configDirectory)) {
list
.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".xml"))
.filter(this::isConfigEntryFile)
.forEach(this::updateSingleFile);
}
}
private boolean isConfigEntryFile(Path potentialFile) {
LOG.trace("Testing whether file is config entry file without mark: {}", potentialFile);
XMLStreamReader reader = null;
try {
reader = XmlStreams.createReader(potentialFile);
reader.nextTag();
if ("configuration".equals(reader.getLocalName())) {
reader.nextTag();
if ("entry".equals(reader.getLocalName())) {
return true;
}
}
} catch (XMLStreamException | IOException e) {
throw new UpdateException("Error reading file " + potentialFile, e);
} finally {
XmlStreams.close(reader);
}
return false;
}
private void updateSingleFile(Path configFile) {
LOG.info("Updating config entry file: {}", configFile);
Document configEntryDocument = readAsXmlDocument(configFile);
configEntryDocument.getDocumentElement().setAttribute("type", "config-entry");
CopyOnWrite.withTemporaryFile(
temporaryFile -> writeXmlDocument(configEntryDocument, temporaryFile), configFile
);
}
private void writeXmlDocument(Document configEntryDocument, Path temporaryFile) throws TransformerException {
try {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(configEntryDocument);
StreamResult streamResult = new StreamResult(Files.newOutputStream(temporaryFile));
transformer.transform(domSource, streamResult);
} catch (IOException e) {
throw new UpdateException("Could not write modified config entry file", e);
}
}
private Document readAsXmlDocument(Path configFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(Files.newInputStream(configFile));
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new UpdateException("Could not read config entry file", e);
}
}
}