Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validations for remoteBackupConfig.json file prior to processing #463

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading