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

Add example code for ACL #194

Closed
DavidVujic opened this issue Jul 27, 2019 · 6 comments · Fixed by #346
Closed

Add example code for ACL #194

DavidVujic opened this issue Jul 27, 2019 · 6 comments · Fixed by #346

Comments

@DavidVujic
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
There should be a working runnable code example for using the ACL feature of the library. Example code exist in the README, but that one is not longer valid (the API was probably different at the time of writing). The current example will be removed from the README shortly.

Describe the solution you'd like
A JavaScript file with ACL example code, in the examples folder. The code should use the Promise based ZooKeeper client. This file should be able to run using node examples/acl.js.

Additional context
This is the example code (that is not currently working) from the README:

var ZooKeeper = require("zookeeper");

zk = new ZooKeeper({
    connect: "localhost:2181",
    timeout: 2000,
});

var key = "/acl-test";

zk.connect(function (err, client) {
    if (err) throw err;
    console.log("zoolocker: Connected to Zookeeper, id=%s", zk.client_id);

    client.add_auth("digest", "username:password", function (rc, error) {
        console.log("ADD_AUTH", rc, error);

        client.a_create(key, "", {
            version: -1
        }, function (rc, error, path) {
            console.log("CREATE", rc, error);

            client.a_set_acl(key, -1, [ZooKeeper.ZOO_CREATOR_ALL_ACL, ZooKeeper.ZOO_OPEN_ACL_UNSAFE, {
                perms: ZooKeeper.ZOO_PERM_WRITE,
                scheme: "world",
                auth: "anyone",
            }], function (rc, error) {
                console.log("SET_ACL", rc, error);

                client.a_get_acl(key, function (rc, error, acl, stat) {
                    console.log("GET_ACL", rc, error, acl);
                });
            });
        });
    });
});
@nitin-pandita
Copy link

I would like to work on it

@maharjanraj
Copy link
Contributor

@DavidVujic is set_acl working for you? After setting acl I can no longer access the node. Something wrong with set_acl?

@DavidVujic
Copy link
Collaborator Author

DavidVujic commented Jul 4, 2024

@maharjanraj It looks like the doc-strings for the set_acl function is incorrect. The ACL object to pass in should be a list of objects.

I will update the docs (and add a runnable example). Thank you for reporting!

Working example:

client.on('connect', async () => {
        const path = "/acl-testing";
        const data = "";
        const flags = constants.ZOO_EPHEMERAL;

        await client.create(path, data, flags);

        const [aclBefore, statBefore] = await client.get_acl(path);

        updatedAcl = [{
            perm: constants.ZOO_PERM_READ | constants.ZOO_PERM_WRITE,
            scheme: "world",
            auth: "anyone",
        }];

        await client.set_acl(path, 0, updatedAcl);

        const [aclAfter, statAfter] = await client.get_acl(path);

        console.log("before:", aclBefore);
        console.log("after:", aclAfter);
    });

The output:

before: [ { perms: 31, scheme: 'world', auth: 'anyone' } ]
after: [ { perms: 3, scheme: 'world', auth: 'anyone' } ]

@DavidVujic
Copy link
Collaborator Author

There's a new version out: 6.2.1. This version has the correct typing for the ACL. Have a look at the added runnable example for setting the ACL on a ZooKeeper node. You will find it in the examples folder.

@maharjanraj
Copy link
Contributor

@DavidVujic thanks for quickly answering and helping out. As you said, using perm instead of perms worked. Are you going to change perm to perms?

@DavidVujic
Copy link
Collaborator Author

@DavidVujic thanks for quickly answering and helping out. As you said, using perm instead of perms worked. Are you going to change perm to perms?

Great! Happy to hear that it worked.

As for now, I'll keep perm. A more correct naming would probably be perms, and I will probably accept both in an upcoming version to avoid breaking changes for users.

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

Successfully merging a pull request may close this issue.

3 participants