summaryrefslogtreecommitdiff
path: root/src/validators
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-08-07 11:35:26 +0200
committerChristian Poessinger <christian@poessinger.com>2019-08-07 11:58:21 +0200
commit097a088725eb632bec3e09a2e563fc96139d86ba (patch)
treebd1cbc2b780bc6c55dfe2552c2b162e2bcadf50a /src/validators
parent89da1559e145d8e86750a634e5268df60e63b4b3 (diff)
downloadvyos-1x-097a088725eb632bec3e09a2e563fc96139d86ba.tar.gz
vyos-1x-097a088725eb632bec3e09a2e563fc96139d86ba.zip
Validator: add file-exists as replacement to Vyatta check_file_in_config_dir
Verify if a file exists or not on the system. Can be called by: <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> The --directory option is used to ensure a given file path lies under this (mandatory) directory. A directory can be mandatory when the optional argument -e, --error is used. This will return '1' instead of '0'.
Diffstat (limited to 'src/validators')
-rwxr-xr-xsrc/validators/file-exists62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/validators/file-exists b/src/validators/file-exists
new file mode 100755
index 000000000..e179805ed
--- /dev/null
+++ b/src/validators/file-exists
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2019 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Description:
+# Check if a given file exists on the system. Used for files that
+# are referenced from the CLI and need to be preserved during an image upgrade.
+# Warn the user if these aren't under /config
+
+import os
+import sys
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument("-d", "--directory", type=str, help="File must be present in this directory.")
+parser.add_argument("-e", "--error", action="store_true", help="Tread warnings as errors - change exit code to '1'")
+parser.add_argument("file", type=str, help="Path of file to validate")
+
+args = parser.parse_args()
+
+msg_prefix = "WARNING: "
+if args.error:
+ msg_prefix = "ERROR: "
+
+#
+# Always check if the given file exists
+#
+if not os.path.exists(args.file):
+ print(msg_prefix + "File '{}' not found".format(args.file))
+ if args.error:
+ sys.exit(1)
+ else:
+ sys.exit(0)
+
+#
+# Optional check if the file is under a certain directory path
+#
+if args.directory:
+ # remove directory path from path to verify
+ rel_filename = args.file.replace(args.directory, '').lstrip('/')
+
+ if not os.path.exists(args.directory + '/' + rel_filename):
+ print(msg_prefix + "'{}' lies outside of '{}' directory.\n" \
+ "It will not get preserved during image upgrade!".format(args.file, args.directory))
+ if args.error:
+ sys.exit(1)
+ else:
+ sys.exit(0)
+
+sys.exit(0)