Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test permissions on "persist" storage location before publish #1028

Open
n0rt0nthec4t opened this issue Apr 23, 2024 · 0 comments
Open

Test permissions on "persist" storage location before publish #1028

n0rt0nthec4t opened this issue Apr 23, 2024 · 0 comments

Comments

@n0rt0nthec4t
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant