Skip to content

Commit 1e485d3

Browse files
committed
feat(CustomisationGenerator): Update paths for Pi Connect configuration and deployment key
- Introduced constants for Pi Connect configuration path and deployment key filename. - Updated script generation to use the new paths for creating directories and files related to Pi Connect. - Adjusted cloud-init user data generation to reflect the new directory structure for the deployment key.
1 parent f847c8e commit 1e485d3

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/customization_generator.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace rpi_imager {
1313

14+
constexpr auto PI_CONNECT_CONFIG_PATH = ".config/com.raspberrypi.connect";
15+
constexpr auto PI_CONNECT_DEPLOY_KEY_FILENAME = "deploy.key";
16+
1417
QString CustomisationGenerator::shellQuote(const QString& value) {
1518
QString t = value;
1619
t.replace("'", "'\"'\"'");
@@ -177,16 +180,18 @@ QByteArray CustomisationGenerator::generateSystemdScript(const QVariantMap& s, c
177180
const bool piConnectEnabled = s.value("piConnectEnabled").toBool();
178181
QString piConnectTokenTrimmed = piConnectToken.trimmed();
179182
if (piConnectEnabled && !piConnectTokenTrimmed.isEmpty()) {
180-
// Determine home directory for the effective user
183+
const QString configDir = QStringLiteral("$TARGET_HOME/") + PI_CONNECT_CONFIG_PATH;
184+
const QString deployKeyPath = configDir + QStringLiteral("/") + PI_CONNECT_DEPLOY_KEY_FILENAME;
185+
181186
line(QStringLiteral("TARGET_USER=\"") + effectiveUser + QStringLiteral("\""), script);
182187
line(QStringLiteral("TARGET_HOME=$(getent passwd \"$TARGET_USER\" | cut -d: -f6)"), script);
183188
line(QStringLiteral("if [ -z \"$TARGET_HOME\" ] || [ ! -d \"$TARGET_HOME\" ]; then TARGET_HOME=\"/home/") + effectiveUser + QStringLiteral("\"; fi"), script);
184-
line(QStringLiteral("install -o \"$TARGET_USER\" -m 700 -d \"$TARGET_HOME/com.raspberrypi.connect\""), script);
185-
line(QStringLiteral("cat > \"$TARGET_HOME/com.raspberrypi.connect/deploy.key\" <<'EOF'"), script);
189+
line(QStringLiteral("install -o \"$TARGET_USER\" -m 700 -d \"") + configDir + QStringLiteral("\""), script);
190+
line(QStringLiteral("cat > \"") + deployKeyPath + QStringLiteral("\" <<'EOF'"), script);
186191
line(piConnectTokenTrimmed, script);
187192
line(QStringLiteral("EOF"), script);
188-
line(QStringLiteral("chown \"$TARGET_USER:$TARGET_USER\" \"$TARGET_HOME/com.raspberrypi.connect/deploy.key\""), script);
189-
line(QStringLiteral("chmod 600 \"$TARGET_HOME/com.raspberrypi.connect/deploy.key\""), script);
193+
line(QStringLiteral("chown \"$TARGET_USER:$TARGET_USER\" \"") + deployKeyPath + QStringLiteral("\""), script);
194+
line(QStringLiteral("chmod 600 \"") + deployKeyPath + QStringLiteral("\""), script);
190195

191196
// Enable systemd user service rpi-connect-signin.service for the target user
192197
line(QStringLiteral("install -o \"$TARGET_USER\" -m 700 -d \"$TARGET_HOME/.config/systemd/user/default.target.wants\""), script);
@@ -381,7 +386,8 @@ QByteArray CustomisationGenerator::generateCloudInitUserData(const QVariantMap&
381386
if (piConnectEnabled && !cleanToken.isEmpty()) {
382387
// Use the same effective user decision as above
383388
const QString effectiveUser = userName.isEmpty() && sshEnabled ? currentUser : (userName.isEmpty() ? QStringLiteral("pi") : userName);
384-
const QString targetPath = QStringLiteral("/home/") + effectiveUser + QStringLiteral("/com.raspberrypi.connect/auth.key");
389+
const QString configDir = QStringLiteral("/home/") + effectiveUser + QStringLiteral("/") + PI_CONNECT_CONFIG_PATH;
390+
const QString targetPath = configDir + QStringLiteral("/") + PI_CONNECT_DEPLOY_KEY_FILENAME;
385391
push(QStringLiteral("write_files:"), cloud);
386392
push(QStringLiteral(" - path: ") + targetPath, cloud);
387393
push(QStringLiteral(" permissions: '0600'"), cloud);
@@ -392,7 +398,7 @@ QByteArray CustomisationGenerator::generateCloudInitUserData(const QVariantMap&
392398
// Ensure directory exists with correct owner
393399
push(QString(), cloud);
394400
push(QStringLiteral("runcmd:"), cloud);
395-
push(QStringLiteral(" - [ sh, -c, \"install -o ") + effectiveUser + QStringLiteral(" -m 700 -d /home/") + effectiveUser + QStringLiteral("/com.raspberrypi.connect\" ]"), cloud);
401+
push(QStringLiteral(" - [ sh, -c, \"install -o ") + effectiveUser + QStringLiteral(" -m 700 -d ") + configDir + QStringLiteral("\" ]"), cloud);
396402
} else if (needsRuncmd) {
397403
// Start runcmd section if not already started
398404
push(QStringLiteral("runcmd:"), cloud);

src/test/customization_generator_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,13 +597,13 @@ TEST_CASE("CustomisationGenerator generates cloud-init user-data with Pi Connect
597597
QString yaml = QString::fromUtf8(userdata);
598598

599599
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("write_files:"));
600-
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("- path: /home/testuser/com.raspberrypi.connect/auth.key"));
600+
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("- path: /home/testuser/.config/com.raspberrypi.connect/deploy.key"));
601601
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("permissions: '0600'"));
602602
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("owner: testuser:testuser"));
603603
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("content: |"));
604604
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("test-token-abcd-1234"));
605605
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("runcmd:"));
606-
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("install -o testuser -m 700 -d /home/testuser/com.raspberrypi.connect"));
606+
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("install -o testuser -m 700 -d /home/testuser/.config/com.raspberrypi.connect"));
607607
}
608608

609609
TEST_CASE("CustomisationGenerator generates cloud-init network-config with WiFi", "[cloudinit][network]") {

0 commit comments

Comments
 (0)