From be393af3c0f54908145f25383b68cadcb8ed6bc8 Mon Sep 17 00:00:00 2001 From: Florian Begusch Date: Wed, 9 Dec 2020 13:35:10 +0000 Subject: [PATCH] remoteBackup: fix invalid config file breaks Artifactory instance --- storage/remoteBackup/remoteBackup.groovy | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/storage/remoteBackup/remoteBackup.groovy b/storage/remoteBackup/remoteBackup.groovy index cfa8003e..ffcfe818 100644 --- a/storage/remoteBackup/remoteBackup.groovy +++ b/storage/remoteBackup/remoteBackup.groovy @@ -51,7 +51,13 @@ storage { afterCreate { item -> def etcdir = ctx.artifactoryHome.etcDir def cfgfile = new File(etcdir, REMOTE_BACKUP) - def cfg = new JsonSlurper().parse(cfgfile) + + // Ensure invalid json config does not prevent Artifactory + // from serving and receiving artifacts. + if (!cfgfile.exists()) return + cfg = parseJson(cfgfile) + if (!cfg) return + if (item.repoKey in cfg && !item.isFolder()) { asSystem { def dest = cfg[item.repoKey] @@ -66,10 +72,24 @@ storage { } } +def parseJson(cfgfile) { + try { + return new JsonSlurper().parse(cfgfile) + } catch (Exception e) { + log.warn("Invalid json, error was: $e") + } +} + def runBackup(repos) { def etcdir = ctx.artifactoryHome.etcDir def cfgfile = new File(etcdir, REMOTE_BACKUP) - def cfg = new JsonSlurper().parse(cfgfile) + + // Ensure invalid json config does not prevent Artifactory + // from serving and receiving artifacts. + if (!cfgfile.exists()) return + cfg = parseJson(cfgfile) + if (!cfg) return + if (repos) { def cfgtmp = [:] for (repo in repos) {