Skip to content

Commit

Permalink
Merge pull request #463 from jfrog/remotebackup-config-check
Browse files Browse the repository at this point in the history
Added validations for remoteBackupConfig.json file prior to processing
  • Loading branch information
bhanurp authored Jun 21, 2024
2 parents 592d96b + 8753b11 commit 72c926b
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions storage/remoteBackup/remoteBackup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import groovy.json.JsonException
import groovy.transform.Field
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
Expand Down Expand Up @@ -51,7 +52,17 @@ storage {
afterCreate { item ->
def etcdir = ctx.artifactoryHome.etcDir
def cfgfile = new File(etcdir, REMOTE_BACKUP)
def cfg = new JsonSlurper().parse(cfgfile)
def configExists = isRemoteBackupConfigExists(cfgfile.toString())
if (!configExists) {
return [complete="remoteBackup.json doesn't exists in plugins directory", total=0]
}
def cfg = getRemoteBackupConfigJSON(cfgfile.toString())
if (cfg == null) {
return [complete="Failed to read remoteBackup.json", total=0]
}
if (cfg == null) {
return [complete="Failed to read remoteBackup.json", total=0]
}
if (item.repoKey in cfg && !item.isFolder()) {
asSystem {
def dest = cfg[item.repoKey]
Expand All @@ -69,7 +80,14 @@ storage {
def runBackup(repos) {
def etcdir = ctx.artifactoryHome.etcDir
def cfgfile = new File(etcdir, REMOTE_BACKUP)
def cfg = new JsonSlurper().parse(cfgfile)
def configExists = isRemoteBackupConfigExists(cfgfile.toString())
if (!configExists) {
return [complete="remoteBackup.json doesn't exists in plugins directory", total=0]
}
def cfg = getRemoteBackupConfigJSON(cfgfile.toString())
if (cfg == null) {
return [complete="Failed to parse remoteBackup.json", total=0]
}
if (repos) {
def cfgtmp = [:]
for (repo in repos) {
Expand Down Expand Up @@ -106,3 +124,23 @@ def runBackup(repos) {
}
return [complete, total]
}

def isRemoteBackupConfigExists(String configFilePath) {
def configFile = new File(configFilePath)
if (!configFile.exists()) {
log.warn("File $configFilePath does not exist")
return false
}
return true
}

def getRemoteBackupConfigJSON(String configFilePath) {
try {
def configFile = new File(configFilePath)
def cfg = new JsonSlurper().parse(configFile)
return cfg
} catch (JsonException e) {
log.warn("File $configFilePath is not a valid JSON file: {}", e.message)
return null
}
}

0 comments on commit 72c926b

Please sign in to comment.