HAP-NodeJS icon indicating copy to clipboard operation
HAP-NodeJS copied to clipboard

Test permissions on "persist" storage location before publish

Open n0rt0nthec4t opened this issue 2 months ago • 0 comments

Current Situation

I have seen situations where the permissions of the AccessoryInfo.xxx.json and IdentityCache.xxx.json files have changed or mismatch with the hap-nodejs running process. Personally noticed this after converting docker project to run as non-root. The effect of this is upon publishing the accessory, fails due to this

Proposed Change

Incorporate a test for access permissions to the persist storage location and/or files associated with the accessory upon publish and warn if cannot be written to or access. I incorporate such a test in my project

Example code I have used

        // Check permissions for HAP-nodejs to access required storage for this device
        var storagePath = path.normalize(process.cwd() + "/" + HAP.HAPStorage.storage().options.dir);
        var fileAccessIssues = [];
        fs.readdirSync(storagePath).filter((file) => file.includes(this.HomeKitAccessory.username.replace(/:/g, "").toUpperCase())).forEach((file) => {
            try {
                fs.accessSync(storagePath + "/" + file, fs.constants.R_OK | fs.constants.W_OK);
            } catch (error) {
               // Access permission error to file
               fileAccessIssues.push(storagePath + "/" + file);
            }
        });

        if (fileAccessIssues.length == 0) {
            // Publish accessory on local network and push onto export array for HAP-NodeJS "accessory factory"
            this.HomeKitAccessory.publish({username: this.HomeKitAccessory.username, pincode: this.HomeKitAccessory.pincode, category: this.HomeKitAccessory.category, advertiser: this.mDNSAdvertiser});
            this.#outputLogging("Advertising '%s' as '%s' to local network. HomeKit pairing code is '%s'", this.deviceData.description, this.HomeKitAccessory.displayName, this.HomeKitAccessory.pincode);
        }

        if (fileAccessIssues.length != 0) {
            // Detected file permission/access issues with HAP-NodeJS file storage, so we'll be unable to publish this accessory on the local network for HomeKit
            this.#outputLogging("Permission/access issues to required storage used by HAP-NodeJS. This will prevent accessory from operating correctly in HomeKit until corrected");
            fileAccessIssues.forEach((file) => {
                this.#outputLogging("  += '%s'", file);
            });
        }

Additional Context

No response

n0rt0nthec4t avatar Apr 23 '24 00:04 n0rt0nthec4t