From c67c2b214cd69934255db73e4359e964a1685de2 Mon Sep 17 00:00:00 2001 From: Will Date: Wed, 22 Jan 2020 13:49:32 +0000 Subject: [PATCH 1/3] #109: added * naive implementation of SSID/PSK settings to user-data * basic tests to try to cover this --- flash | 8 ++++++++ test/cloud-init.bats | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/flash b/flash index e926056..5726833 100755 --- a/flash +++ b/flash @@ -714,6 +714,14 @@ fi echo "Set hostname=${SD_HOSTNAME}" sed_i -e "s/.*hostname:.*\$/hostname: ${SD_HOSTNAME}/" "${boot}/user-data" fi + if [ -n "${WIFI_SSID}" ]; then + echo " Set ssid=${WIFI_SSID}" + sed_i -e "s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data" + fi + if [ -n "${WIFI_PASSWORD}" ]; then + echo " Set psk=${WIFI_PASSWORD}" + sed_i -e "s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data" + fi if [ ! -f "${boot}/meta-data" ]; then echo "Creating empty meta-data" diff --git a/test/cloud-init.bats b/test/cloud-init.bats index f231e2f..c4693c2 100644 --- a/test/cloud-init.bats +++ b/test/cloud-init.bats @@ -62,6 +62,26 @@ teardown() { assert [ ! -s /tmp/boot/meta-data ] } +@test "cloud-init: flash --ssid sets ssid" { + run ./flash -f -d $img --ssid NEWSSID cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + assert_output_contains 'ssid="NEWSSID"' +} + +@test "cloud-init: flash --password sets psk" { + run ./flash -f -d $img --password NEWPSK cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + assert_output_contains 'psk="NEWPSK"' +} + @test "cloud-init: flash --config does not replace user-data" { run ./flash -f -d $img --config test/resources/good.yml cloud-init.img assert_success From 64f6789bbf1d589c9a91ca585f58e17079aaf317 Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 26 Jan 2020 12:11:28 +0000 Subject: [PATCH 2/3] Mofified implementation for #109 * should negate for commented lines Tests * user-data test files - wifi commented and uncommented * new tests fo mixing params for user-data & wifi creds * new refute test - commented ssid/psk lines should be unchanged --- flash | 4 +- test/cloud-init.bats | 40 +++++++++++ test/resources/wifi-commented-user-data.yml | 76 +++++++++++++++++++++ test/resources/wifi-user-data.yml | 76 +++++++++++++++++++++ 4 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 test/resources/wifi-commented-user-data.yml create mode 100644 test/resources/wifi-user-data.yml diff --git a/flash b/flash index 5726833..746b8e8 100755 --- a/flash +++ b/flash @@ -716,11 +716,11 @@ fi fi if [ -n "${WIFI_SSID}" ]; then echo " Set ssid=${WIFI_SSID}" - sed_i -e "s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data" + sed_i -e "^[^#]s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data" fi if [ -n "${WIFI_PASSWORD}" ]; then echo " Set psk=${WIFI_PASSWORD}" - sed_i -e "s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data" + sed_i -e "^[^#]s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data" fi if [ ! -f "${boot}/meta-data" ]; then diff --git a/test/cloud-init.bats b/test/cloud-init.bats index c4693c2..da391ac 100644 --- a/test/cloud-init.bats +++ b/test/cloud-init.bats @@ -82,6 +82,46 @@ teardown() { assert_output_contains 'psk="NEWPSK"' } +@test "cloud-init: flash --ssid still sets ssid when user-data also specified" { + run ./flash -f -d $img -u test/resources/wifi-user-data.yml --ssid NEWSSID cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + assert_output_contains 'ssid="NEWSSID"' +} + +@test "cloud-init: flash --password still sets psk when user-data also specified" { + run ./flash -f -d $img -u test/resources/wifi-user-data.yml --password NEWPSK cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + assert_output_contains 'psk="NEWPSK"' +} + +@test "cloud-init: flash --ssid does NOT set ssid if COMMENTED" { + run ./flash -f -d $img -u test/resources/commented-wifi-user-data.yml --ssid NEWSSID cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + refute_output_contains 'ssid="NEWSSID"' +} + +@test "cloud-init: flash --password does NOT set psk if COMMENTED" { + run ./flash -f -d $img -u test/resources/commented-wifi-user-data.yml --password NEWPSK cloud-init.img + assert_success + assert_output_contains Finished. + + mount_sd_boot $img /tmp/boot + run cat /tmp/boot/user-data + refute_output_contains 'psk="NEWPSK"' +} + @test "cloud-init: flash --config does not replace user-data" { run ./flash -f -d $img --config test/resources/good.yml cloud-init.img assert_success diff --git a/test/resources/wifi-commented-user-data.yml b/test/resources/wifi-commented-user-data.yml new file mode 100644 index 0000000..569e90b --- /dev/null +++ b/test/resources/wifi-commented-user-data.yml @@ -0,0 +1,76 @@ +#cloud-config +# vim: syntax=yaml +# + +# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9 +# When dealing with cloud-init, it is SUPER important to know the version +# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had +# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html + +# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well +hostname: pi3 +manage_etc_hosts: true + +# You could modify this for your own user information +users: + - name: pirate + gecos: "Hypriot Pirate" + sudo: ALL=(ALL) NOPASSWD:ALL + shell: /bin/bash + groups: users,docker,video + plain_text_passwd: hypriot + lock_passwd: false + ssh_pwauth: true + chpasswd: { expire: false } + +# # Set the locale of the system +# locale: "en_US.UTF-8" + +# # Set the timezone +# # Value of 'timezone' must exist in /usr/share/zoneinfo +# timezone: "America/Los_Angeles" + +# # Update apt packages on first boot +# package_update: true +# package_upgrade: true +# package_reboot_if_required: true +package_upgrade: false + +# # Install any additional apt packages you need here +# packages: +# - ntp + +# # WiFi connect to HotSpot +# To make wifi work with RPi3 and RPi0 +# you also have to set "enable_uart=0" in config.txt +# See no-uart-config.txt for an example. +# +# # - use `wpa_passphrase SSID PASSWORD` to encrypt the psk +# write_files: +# - content: | +# allow-hotplug wlan0 +# iface wlan0 inet dhcp +# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf +# iface default inet dhcp +# path: /etc/network/interfaces.d/wlan0 +# - content: | +# country=YourContryCode +# ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev +# update_config=1 +# network={ +# ssid="YourSSID" +# psk="YourSecretPreSharedKey" +# proto=RSN +# key_mgmt=WPA-PSK +# pairwise=CCMP +# auth_alg=OPEN +# } +# path: /etc/wpa_supplicant/wpa_supplicant.conf + +# These commands will be ran once on first boot only +runcmd: + # Pickup the hostname changes + - 'systemctl restart avahi-daemon' + + # Activate WiFi interface + - 'ifup wlan0' diff --git a/test/resources/wifi-user-data.yml b/test/resources/wifi-user-data.yml new file mode 100644 index 0000000..cc7a857 --- /dev/null +++ b/test/resources/wifi-user-data.yml @@ -0,0 +1,76 @@ +#cloud-config +# vim: syntax=yaml +# + +# The current version of cloud-init in the Hypriot rpi-64 is 0.7.9 +# When dealing with cloud-init, it is SUPER important to know the version +# I have wasted many hours creating servers to find out the module I was trying to use wasn't in the cloud-init version I had +# Documentation: http://cloudinit.readthedocs.io/en/0.7.9/index.html + +# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well +hostname: pi3 +manage_etc_hosts: true + +# You could modify this for your own user information +users: + - name: pirate + gecos: "Hypriot Pirate" + sudo: ALL=(ALL) NOPASSWD:ALL + shell: /bin/bash + groups: users,docker,video + plain_text_passwd: hypriot + lock_passwd: false + ssh_pwauth: true + chpasswd: { expire: false } + +# # Set the locale of the system +# locale: "en_US.UTF-8" + +# # Set the timezone +# # Value of 'timezone' must exist in /usr/share/zoneinfo +# timezone: "America/Los_Angeles" + +# # Update apt packages on first boot +# package_update: true +# package_upgrade: true +# package_reboot_if_required: true +package_upgrade: false + +# # Install any additional apt packages you need here +# packages: +# - ntp + +# # WiFi connect to HotSpot +# To make wifi work with RPi3 and RPi0 +# you also have to set "enable_uart=0" in config.txt +# See no-uart-config.txt for an example. +# +# # - use `wpa_passphrase SSID PASSWORD` to encrypt the psk +write_files: + - content: | + allow-hotplug wlan0 + iface wlan0 inet dhcp + wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf + iface default inet dhcp + path: /etc/network/interfaces.d/wlan0 + - content: | + country=YourContryCode + ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev + update_config=1 + network={ + ssid="YourSSID" + psk="YourSecretPreSharedKey" + proto=RSN + key_mgmt=WPA-PSK + pairwise=CCMP + auth_alg=OPEN + } + path: /etc/wpa_supplicant/wpa_supplicant.conf + +# These commands will be ran once on first boot only +runcmd: + # Pickup the hostname changes + - 'systemctl restart avahi-daemon' + + # Activate WiFi interface + - 'ifup wlan0' From 0e21d078fb23ed59daf90e66a04785c0efc4dd11 Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 26 Jan 2020 12:28:42 +0000 Subject: [PATCH 3/3] #109 * fixes to pass tests Now ignores wifi params for user-data if the wifi section is commented. That means basic implementation works if also using a user-data file where the wifi section is present and not commented. --- flash | 4 ++-- test/cloud-init.bats | 16 ++++++++-------- test/resources/wifi-commented-user-data.yml | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/flash b/flash index 746b8e8..fe4ae29 100755 --- a/flash +++ b/flash @@ -716,11 +716,11 @@ fi fi if [ -n "${WIFI_SSID}" ]; then echo " Set ssid=${WIFI_SSID}" - sed_i -e "^[^#]s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data" + sed_i -e "/^#/!s/.*ssid=.*\$/ ssid=\"${WIFI_SSID}\"/" "${boot}/user-data" fi if [ -n "${WIFI_PASSWORD}" ]; then echo " Set psk=${WIFI_PASSWORD}" - sed_i -e "^[^#]s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data" + sed_i -e "/^#/!s/.*psk=.*\$/ psk=\"${WIFI_PASSWORD}\"/" "${boot}/user-data" fi if [ ! -f "${boot}/meta-data" ]; then diff --git a/test/cloud-init.bats b/test/cloud-init.bats index da391ac..7788fb4 100644 --- a/test/cloud-init.bats +++ b/test/cloud-init.bats @@ -62,24 +62,24 @@ teardown() { assert [ ! -s /tmp/boot/meta-data ] } -@test "cloud-init: flash --ssid sets ssid" { +@test "cloud-init: flash --ssid does NOT set ssid as it's COMMENTED in default image" { run ./flash -f -d $img --ssid NEWSSID cloud-init.img assert_success assert_output_contains Finished. mount_sd_boot $img /tmp/boot run cat /tmp/boot/user-data - assert_output_contains 'ssid="NEWSSID"' + refute_output_contains 'ssid="NEWSSID"' } -@test "cloud-init: flash --password sets psk" { +@test "cloud-init: flash --password does NOT set psk as it's COMMENTED in default image" { run ./flash -f -d $img --password NEWPSK cloud-init.img assert_success assert_output_contains Finished. mount_sd_boot $img /tmp/boot run cat /tmp/boot/user-data - assert_output_contains 'psk="NEWPSK"' + refute_output_contains 'psk="NEWPSK"' } @test "cloud-init: flash --ssid still sets ssid when user-data also specified" { @@ -102,8 +102,8 @@ teardown() { assert_output_contains 'psk="NEWPSK"' } -@test "cloud-init: flash --ssid does NOT set ssid if COMMENTED" { - run ./flash -f -d $img -u test/resources/commented-wifi-user-data.yml --ssid NEWSSID cloud-init.img +@test "cloud-init: flash --ssid does NOT set ssid if COMMENTED when user-data also specified" { + run ./flash -f -d $img -u test/resources/wifi-commented-user-data.yml --ssid NEWSSID cloud-init.img assert_success assert_output_contains Finished. @@ -112,8 +112,8 @@ teardown() { refute_output_contains 'ssid="NEWSSID"' } -@test "cloud-init: flash --password does NOT set psk if COMMENTED" { - run ./flash -f -d $img -u test/resources/commented-wifi-user-data.yml --password NEWPSK cloud-init.img +@test "cloud-init: flash --password does NOT set psk if COMMENTED when user-data also specified" { + run ./flash -f -d $img -u test/resources/wifi-commented-user-data.yml --password NEWPSK cloud-init.img assert_success assert_output_contains Finished. diff --git a/test/resources/wifi-commented-user-data.yml b/test/resources/wifi-commented-user-data.yml index 569e90b..f3c5089 100644 --- a/test/resources/wifi-commented-user-data.yml +++ b/test/resources/wifi-commented-user-data.yml @@ -57,7 +57,7 @@ package_upgrade: false # country=YourContryCode # ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev # update_config=1 -# network={ + # network={ # ssid="YourSSID" # psk="YourSecretPreSharedKey" # proto=RSN