summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
AgeCommit message (Collapse)Author
2021-12-15Adopt Black and isort (SC-700) (#1157)James Falcon
Applied Black and isort, fixed any linting issues, updated tox.ini and CI.
2021-12-13find_devs_with_openbsd: ensure we return the last entry (#1149)Gonéri Le Bouder
`sysctl -n hw.disknames` returns a trailing `\n`. We need to clean this up. In addition, the criteria matching system is a source of problem because: - we don't have a way to look up the label of the partition - we've got situation where an ISO image can be exposed through a virtio block device. So we just totally ignore the value of `criteria`. We end-up with a slightly longer loop of mount-retry. But this way we're sure we don't miss a configuration disk. Tested on Kubvirt with the help of Brady Pratt @jbpratt.
2021-12-09find_devs/openbsd: accept ISO on disk (#1132)Gonéri Le Bouder
When the metadata is an ISO image and is exposed through a disk, the device is called `/dev/sd?a` internally. For instance `/dev/sd1a`. It can then be mounted with `mount_cd9660 /dev/sd1a /mnt`. Metadata in the FAT32 format are exposed as `/dev/sd?i`. With this change, we try to mount `/dev/sd?a` in addition to `/dev/sd?i`. Closes: https://github.com/ContainerCraft/kmi/issues/12
2021-12-09Improve error log message when mount failed (#1140)Ksenija Stanojevic
2021-12-07Add miraclelinux support (#1128)Haruki TSURUMOTO
2021-12-06Add Strict Metaschema Validation (#1101)Brett Holman
Improve schema validation. This adds strict validation of config module definitions at testing time, with plumbing included for future runtime validation. This eliminates a class of bugs resulting from schemas that have definitions that are incorrect, but get interpreted by jsonschema as "additionalProperties" that are therefore ignored. - Add strict meta-schema for jsonschema unit test validation - Separate schema from module metadata structure - Improve type annotations for various functions and data types Cleanup: - Remove unused jsonschema "required" elements - Eliminate manual memoization in schema.py:get_schema(), reference module.__doc__ directly
2021-11-22testing: monkeypatch system_info call in unit tests (SC-533) (#1117)James Falcon
testing: monkeypatch system_info call in unit tests system_info can make calls that read or write from the filesystem, which should require special mocking. It is also decorated with 'lru_cache', which means test authors often don't realize they need to be mocking. Also, we don't actually want the results from the user's local machine, so monkeypatching it across all tests should be reasonable. Additionally, moved some of 'system_info` into a helper function to reduce the surface area of the monkeypatch, added tests for the new function (and fixed a bug as a result), and removed related mocks that should be no longer needed.
2021-10-19cc_ssh.py: fix private key group owner and permissions (#1070)Emanuele Giuseppe Esposito
When default host keys are created by sshd-keygen (/etc/ssh/ssh_host_*_key) in RHEL/CentOS/Fedora, openssh it performs the following: # create new keys if ! $KEYGEN -q -t $KEYTYPE -f $KEY -C '' -N '' >&/dev/null; then exit 1 fi # sanitize permissions /usr/bin/chgrp ssh_keys $KEY /usr/bin/chmod 640 $KEY /usr/bin/chmod 644 $KEY.pub Note that the group ssh_keys exists only in RHEL/CentOS/Fedora. Now that we disable sshd-keygen to allow only cloud-init to create them, we miss the "sanitize permissions" part, where we set the group owner as ssh_keys and the private key mode to 640. According to https://bugzilla.redhat.com/show_bug.cgi?id=2013644#c8, failing to set group ownership and permissions like openssh does makes the RHEL openscap tool generate an error. Signed-off-by: Emanuele Giuseppe Esposito eesposit@redhat.com RHBZ: 2013644
2021-10-07Allow comments in runcmd and report failed commands correctly (#1049)Brett Holman
Allow comments in runcmd and report failed commands correctly A `runcmd` script may fail to parse properly, but does not mark `runcmd` as failed when that occurs. Additionally `shellify()` fails to correctly parse scripts that contain a comment line. Rectify both issues and add unit tests to verify correct behavior. LP: #1853146
2021-09-13Improve ug_util.py (#1013)Shreenidhi Shedi
No functional changes.
2021-09-13Support openEuler OS (#1012)zhuzaifangxuele
openEuler Homepage: https://www.openeuler.org/en/
2021-09-01Add CloudLinux OS support (#1003)Alexandr Kravchenko
https://www.cloudlinux.com/
2021-08-09Stop copying ssh system keys and check folder permissions (#956)Emanuele Giuseppe Esposito
In /etc/ssh/sshd_config, it is possible to define a custom authorized_keys file that will contain the keys allowed to access the machine via the AuthorizedKeysFile option. Cloudinit is able to add user-specific keys to the existing ones, but we need to be careful on which of the authorized_keys files listed to pick. Chosing a file that is shared by all user will cause security issues, because the owner of that key can then access also other users. We therefore pick an authorized_keys file only if it satisfies the following conditions: 1. it is not a "global" file, ie it must be defined in AuthorizedKeysFile with %u, %h or be in /home/<user>. This avoids security issues. 2. it must comply with ssh permission requirements, otherwise the ssh agent won't use that file. If it doesn't meet either of those conditions, write to ~/.ssh/authorized_keys We also need to consider the case when the chosen authorized_keys file does not exist. In this case, the existing behavior of cloud-init is to create the new file. We therefore need to be sure that the file complies with ssh permissions too, by setting: - the actual file to permission 600, and owned by the user - the directories in the path that do not exist must be root owned and with permission 755.
2021-08-05Add support for EuroLinux 7 && EuroLinux 8 (#957)Aleksander Baranowski
2021-07-21Add VZLinux support (#951)eb3095
Virtuozzo Linux is a distro based off of CentOS 8, similar to Alma Linux and Rocky Linux.
2021-06-18Add support for VMware PhotonOS (#909)sshedi
Also added a new (currently experimental) systemd-networkd renderer, and includes a small refactor to cc_resolv_conf.py to support the resolved.conf used by systemd-resolved.
2021-06-14add DragonFlyBSD support (#904)Gonéri Le Bouder
- Mostly based on FreeBSD, the main exception is that `find_devs_with_on_freebsd` does not work. - Since we cannot get the CDROM or the partition labels, `find_devs_with_on_dragonflybsd()` has a more naive approach and returns all the block devices.
2021-05-25Add Rocky Linux support to cloud-init (#906)Louis Abel
Rocky Linux is a RHEL-compatible distribution so all changes that have been made should be trivial.
2021-05-07Add AlmaLinux OS support (#872)Andrew Lukoshko
AlmaLinux OS is RHEL-compatible so all the changes needed are trivial.
2021-03-19write passwords only to serial console, lock down cloud-init-output.log (#847)Daniel Watkins
Prior to this commit, when a user specified configuration which would generate random passwords for users, cloud-init would cause those passwords to be written to the serial console by emitting them on stderr. In the default configuration, any stdout or stderr emitted by cloud-init is also written to `/var/log/cloud-init-output.log`. This file is world-readable, meaning that those randomly-generated passwords were available to be read by any user with access to the system. This presents an obvious security issue. This commit responds to this issue in two ways: * We address the direct issue by moving from writing the passwords to sys.stderr to writing them directly to /dev/console (via util.multi_log); this means that the passwords will never end up in cloud-init-output.log * To avoid future issues like this, we also modify the logging code so that any files created in a log sink subprocess will only be owner/group readable and, if it exists, will be owned by the adm group. This results in `/var/log/cloud-init-output.log` no longer being world-readable, meaning that if there are other parts of the codebase that are emitting sensitive data intended for the serial console, that data is no longer available to all users of the system. LP: #1918303
2020-11-06replace usage of dmidecode with kenv on FreeBSD (#621)Mina Galić
FreeBSD lets us read out kernel parameters with kenv(1), a user-space utility that's shipped in "base" We can use it in place of dmidecode(8), thus removing the dependency on sysutils/dmidecode, and the restrictions to i386 and x86_64 architectures that this utility imposes on FreeBSD. Co-authored-by: Scott Moser <smoser@brickies.net>
2020-11-02cloudinit: move dmi functions out of util (#622)Scott Moser
This just separates the reading of dmi values into its own file. Some things of note: * left import of util in dmi.py only for 'is_container' It'd be good if is_container was not in util. * just the use of 'util.is_x86' to dmi.py * open() is used directly rather than load_file.
2020-11-02util: fix mounting of vfat on *BSD (#637)Mina Galić
Fix mounting of vfat filesystems by normalizing the different names for vfat to "msdos" which works across BSDs.
2020-10-23stages: don't reset permissions of cloud-init.log every boot (#624)Daniel Watkins
ensure_file needed modification to support doing this, so this commit also includes the following changes: test_util: add tests for util.ensure_file util: add preserve_mode parameter to ensure_file util: add (partial) type annotations to ensure_file LP: #1900837
2020-09-18Add vendor-data support to seedfrom parameter for NoCloud and OVF (#570)Johann Queuniet
2020-08-26util: remove debug statement (#556)Joshua Powers
2020-08-25tox: bump the pylint version to 2.6.0 in the default run (#544)Paride Legovini
Changes: tox: bump the pylint version to 2.6.0 in the default run Fix pylint 2.6.0 W0707 warnings (raise-missing-from)
2020-08-19Add Alpine Linux support. (#535)dermotbradley
Add new module cc_apk_configure for creating Alpine /etc/apk/repositories file. Modify cc_ca_certs, cc_ntp, cc_power_state_change, and cc_resolv_conf for Alpine. Add Alpine template files for Chrony and Busybox NTP support. Add Alpine template file for /etc/hosts.
2020-08-18Detect kernel version before swap file creation (#428)Eduardo Otubo
According to man page `man 8 swapon', "Preallocated swap files are supported on XFS since Linux 4.18". This patch checks for kernel version before attepting to create swapfile, using dd for XFS only on kernel versions <= 4.18 or btrfs. Add new func util.kernel_version which returns a tuple of ints (major, minor) Signed-off-by: Eduardo Otubo otubo@redhat.com
2020-08-07Recognize LABEL_FATBOOT labels (#513)James Falcon
Update DataSourceNoCloud and ds-identify to recognize LABEL_FATBOOT labels from blkid. Also updated associated tests. LP: #1841466
2020-08-06Handle additional identifier for SLES For HPC (#520)Robert Schweikert
Add "sle_hpc" to list of values which are variant 'suse'.
2020-07-06cloudinit: fix tip-pylint failures and bump pinned pylint version (#478)Daniel Watkins
Specifically: * disable E1102 in cloudinit/sources/helpers/openstack.py for reasons described in a comment, and * refactor `abs_join` to require at least one positional argument; this matches os.path.join's signature, and that mismatch is what was causing pylint to emit a warning * bump to pylint 2.4.2
2020-06-19util: add ensure_dir_exists parameter to write_file (#443)Daniel Watkins
This allows us to disable the `ensure_dir` call when it isn't appropriate.
2020-06-17util: rename write_file's copy_mode parameter to preserve_mode (#439)Daniel Watkins
When updating the docstring to include it, I realised that the current name is somewhat misleading; this makes it a little easier to understand, I think.
2020-06-08Move runparts to subp. (#420)Scott Moser
runparts (run a directory of scripts) seems to fit well in subp module. The request to move it there was raised in #416. Replace use of logexc with LOG.debug as logexc comes from util.
2020-06-08Move subp into its own module. (#416)Scott Moser
This was painful, but it finishes a TODO from cloudinit/subp.py. It moves the following from util to subp: ProcessExecutionError subp which target_path I moved subp_blob_in_tempfile into cc_chef, which is its only caller. That saved us from having to deal with it using write_file and temp_utils from subp (which does not import any cloudinit things now). It is arguable that 'target_path' could be moved to a 'path_utils' or something, but in order to use it from subp and also from utils, we had to get it out of utils.
2020-05-18make finding libc platform independent (#366)Mina Galić (deprecated: Igor Galić)
and slower. and since we're making it slower, let's cache it, in case boottime gets called more than once.
2020-04-24BSD: find_devs_with_ refactoring (#298)Gonéri Le Bouder
Refactoring of the `find_devs_with_*bsd()` methods: - centralize everything in `util.py` - add test coverage
2020-04-01util: remove unnecessary lru_cache import fallback (#299)Daniel Watkins
functools.lru_cache has been present since Python 3.2, so we no longer need to handle its absence. (Also sort util's imports while we're modifying them.)
2020-03-30cloudinit: refactor util.is_ipv4 to net.is_ipv4_address (#292)Daniel Watkins
This also simplifies the implementation to rely on the stdlib, instead of our own NIH checking.
2020-03-26add Openbsd support (#147)Gonéri Le Bouder
- tested on OpenBSD 6.6 - tested on OpenStack without config drive, and NoCloud with ISO config drive
2020-03-25set_passwords: avoid chpasswd on BSD (#268)Gonéri Le Bouder
Avoid chpasswd on all the BSD variants.
2020-03-25util: read_cc_from_cmdline handle urlencoded yaml content (#275)Ryan Harper
Add support for additional escaping of formatting characters in the YAML content between the 'cc:' and 'end_cc' tokens. On s390x legacy terminals the use of square brackets [] are not available limiting the ability to indicate lists of values in yaml content. Using #5B and #5D, [ and ] respectively enables s390x users to pass list yaml content into cloud-init via command line interface.
2020-03-23cc_apt_configure/util: combine search_for_mirror implementations (#271)Daniel Watkins
These two implementations had drifted away from one another very slightly. Reconcile them and then remove the one in cc_apt_configure.
2020-03-23bsd: boottime does not depend on the libc soname (#269)Gonéri Le Bouder
On NetBSD and OpenBSD, the libc soname and location are different. There is no reason to be to specific. Python is able to propely open the share library by itself.
2020-03-14util/netbsd: drop six usage (#252)Daniel Watkins
Drop remaining python six usage
2020-03-12Add Netbsd support (#62)Gonéri Le Bouder
Add support for the NetBSD Operating System. Features in this branch: * Add BSD distro parent class from which NetBSD and FreeBSD can specialize * Add *bsd util functions to cloudinit.net and cloudinit.net.bsd_utils * subclass cloudinit.distro.freebsd.Distro from bsd.Distro * Add new cloudinit.distro.netbsd and cloudinit.net.renderer for netbsd * Add lru_cached util.is_NetBSD functions * Add NetBSD detection for ConfigDrive and NoCloud datasources This branch has been tested with: - NoCloud and OpenStack (with and without config-drive) - NetBSD 8.1. and 9.0 - FreeBSD 11.2 and 12.1 - Python 3.7 only, because of the dependency oncrypt.METHOD_BLOWFISH. This version is available in NetBSD 7, 8 and 9 anyway
2020-03-10instance-data: add cloud-init merged_cfg and sys_info keys to json (#214)Chad Smith
Cloud-config userdata provided as jinja templates are now distro, platform and merged cloud config aware. The cloud-init query command will also surface this config data. Now users can selectively render portions of cloud-config based on: * distro name, version, release * python version * merged cloud config values * machine platform * kernel To support template handling of this config, add new top-level keys to /run/cloud-init/instance-data.json. The new 'merged_cfg' key represents merged cloud config from /etc/cloud/cloud.cfg and /etc/cloud/cloud.cfg.d/*. The new 'sys_info' key which captures distro and platform info from cloudinit.util.system_info. Cloud config userdata templates can render conditional content based on these additional environmental checks such as the following simple example: ``` ## template: jinja #cloud-config runcmd: {% if distro == 'opensuse' %} - sh /custom-setup-sles {% elif distro == 'centos' %} - sh /custom-setup-centos {% elif distro == 'debian' %} - sh /custom-setup-debian {% endif %} ``` To see all values: sudo cloud-init query --all Any keys added to the standardized v1 keys are guaranteed to not change or drop on future released of cloud-init. 'v1' keys will be retained for backward-compatibility even if a new standardized 'v2' set of keys are introduced The following standardized v1 keys are added: * distro, distro_release, distro_version, kernel_version, machine, python_version, system_platform, variant LP: #1865969
2020-02-18utils: use SystemRandom when generating random password. (#204)Dimitri John Ledkov
As noticed by Seth Arnold, non-deterministic SystemRandom should be used when creating security sensitive random strings.
2020-01-16util: rename get_architecture to get_dpkg_architecture (#173)Daniel Watkins
This makes it clearer that we should only use this in code paths that will definitely have dpkg available to them. - Rename get_architecture -> get_dpkg_architecture - Add docstring to get_dpkg_architecture