Customizing a Bucket's ACL

You can change the access control applied to a bucket by setting the bucket's ACL.

When a KiiObject inside a bucket is accessed, the KiiObject's ACL will be also checked.

Bucket ACL entries

A bucket ACL entry is composed of an action and a subject:

  • Action

    This item defines "what" the target user/group/thing can execute.

    Action Representation in code * What the target user/group/thing can execute
    CREATE_OBJECTS_IN_BUCKET KiiACLBucketActionCreateObjects Create new KiiObjects in the bucket.
    QUERY_OBJECTS_IN_BUCKET KiiACLBucketActionQueryObjects Query KiiObjects in the bucket.
    READ_OBJECTS_IN_BUCKET KiiACLBucketActionReadObjects Read KiiObjects in the bucket.
    DROP_BUCKET_WITH_ALL_CONTENT KiiACLBucketActionDropBucket Drop the bucket along with all KiiObjects inside.

    * These symbols are defined in the KiiACLAction enumeration and can be specified like KiiACLAction.KiiACLBucketActionCreateObjects.

    Note: The "KiiACLBucketActionReadObjects" action allows the subject to unconditionally read all KiiObjects in the bucket. For more information, see ACL Customization Examples.

  • Subject

    This item defines "who" can execute.

    Subject Who can execute the designated action?
    KiiUser instance The specified user.
    KiiGroup instance The members of the specified group.
    KiiThing instance The specified thing.
    KiiAnyAuthenticatedUser Any authenticated users.
    KiiAnonymousUser Anonymous users.

    See Subject for the definition of the "Any authenticated users" and "Anonymous users".

Managing a bucket's ACL

You can add and delete an ACL entry in a bucket's ACL. You can also get a list of ACL entries.

Adding a bucket ACL entry

Authorized subjects can add ACL entries to the ACL of buckets not in the application scope to add permissions (e.g., allowing anonymous users to create KiiObjects in the bucket). Only the app administrator can modify the ACL of buckets in the application scope (See Admin Features and the hint below).

You can also set an ACL entry to a non-existent bucket. In this case, the bucket will be auto-generated with the specified ACL entry.

Here is an example of adding two ACL entries to the ACL of a bucket in a user scope. In this example, the KiiACLBucketActionQueryObjects and BucketActionCreateObjects actions are permitted to KiiAnyAuthenticatedUser.

  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    // Create ACL entries.
    var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionQueryObjects);
    var entry2 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionCreateObjects);
    
    var acl = bucket.acl();
    
    // Set the ACL entries to the bucket ACL.
    acl.putACLEntry(entry1);
    acl.putACLEntry(entry2);
    
    // Save the ACL to the server.
    acl.save({
      success: function(theACL) {
        // Do something.
      },
      failure: function(theACL, errorString) {
        // Handle the error.
      }
    });
  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    // Create ACL entries.
    var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionQueryObjects);
    var entry2 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionCreateObjects);
    
    var acl = bucket.acl();
    
    // Set the ACL entries to the bucket ACL.
    acl.putACLEntry(entry1);
    acl.putACLEntry(entry2);
    
    // Save the ACL to the server.
    acl.save().then(
      function(theACL) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theACL = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  1. Create a KiiACL instance of the bucket by calling the acl() method.
  2. Create KiiACLEntry instances and pass them as the argument of the putACLEntry() method. Here, KiiAnyAuthenticatedUser instances are specified as the ACL entries' subjects so as to permit the actions to any authenticated users. If you set KiiAnonymousUser instances as the subjects instead, you can permit the same actions to anonymous users. If you set specific KiiUser instances, the actions will be permitted to those user instances.
  3. Call the save() method of the KiiACL instance to send the request for updating the ACL to Kii Cloud.

See KiiACLEntry for more information about how you can specify other subjects like users and groups.

Deleting a bucket ACL entry

To delete an ACL entry, create a KiiACLEntry instance with its grant set to false and save it. The ACL entry will be deleted from the server.

The following example deletes the ACL entries created in the previous sample code.

  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    // Create ACL entries.
    var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionQueryObjects);
    entry1.setGrant(false);
    var entry2 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionCreateObjects);
    entry2.setGrant(false);
    
    var acl = bucket.acl();
    
    // Delete the ACL entries from the bucket ACL.
    acl.putACLEntry(entry1);
    acl.putACLEntry(entry2);
    
    // Save the ACL to the server.
    acl.save({
      success: function(theACL) {
        // Do something.
      },
      failure: function(theACL, errorString) {
        // Handle the error.
      }
    });
  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    // Create ACL entries.
    var entry1 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionQueryObjects);
    entry1.setGrant(false);
    var entry2 = KiiACLEntry.entryWithSubject(new KiiAnyAuthenticatedUser(), KiiACLAction.KiiACLBucketActionCreateObjects);
    entry2.setGrant(false);
    
    var acl = bucket.acl();
    
    // Delete the ACL entries from the bucket ACL.
    acl.putACLEntry(entry1);
    acl.putACLEntry(entry2);
    
    // Save the ACL to the server.
    acl.save().then(
      function(theACL) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theACL = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );

Note that the removeACLEntry() method of the KiiACL just deletes an ACL entry from the local modification list. The method does not delete an ACL entry set on the server. In the above sample code, we first register requests to remove the KiiACLBucketActionQueryObjects and KiiACLBucketActionCreateObjects actions from acl on the client and then execute the save() method to reflect the requests on the server. If you execute the removeACLEntry() method, it would remove these requests from the ACL modification list, but not from the ACL on the server.

Getting a bucket's ACL

You can get the ACL set on a bucket. Even if you have not set any ACL entries in a bucket's ACL, you can get its default ACL.

Here is the sample code for getting a list of ACL entries as an array.

  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    var acl = bucket.acl();
    
    // Get the ACL entries from the server.
    acl.listACLEntries({
      success: function(theACL, theEntries) {
        for(var i = 0; i < theEntries.length; i++) {
          var action = theEntries[i].getAction();
          var subject = theEntries[i].getSubject();
          // Check the ACL entry.
        }
      },
      failure: function(theACL, errorString) {
        // Handle the error.
      }
    });
  • var user = KiiUser.getCurrentUser();
    var bucket = user.bucketWithName("my_private");
    
    var acl = bucket.acl();
    
    // Get the ACL entries from the server.
    acl.listACLEntries().then(
      function(params) {
        var theACL = params[0];
        var theEntries = params[1];
        for(var i = 0; i < theEntries.length; i++) {
          var action = theEntries[i].getAction();
          var subject = theEntries[i].getSubject();
          // Check the ACL entry.
        }
      }
    ).catch(
      function(error) {
        var theACL = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  1. Create a KiiACL instance of the bucket by executing the acl() method.
  2. Call the listACLEntries() method to get the bucket's ACL as an array of the KiiACLEntry instances.
  3. Parse through each ACL entry in the array.

action will have one of the KiiACLAction, and subject will have the subject of the action. See the lists in Bucket ACL entries for more information.

Trying to set an existing ACL entry will give you an error. By checking existing ACL entries beforehand as shown in this sample code, you can find which ACL entries should be set.

Troubleshooting

  • I got an error when I run my application multiple times

    The ACL entry should be registered just once (e.g., in the initialization) and should not be registered again in the next run.

    If you try to save an existing ACL entry, you will get an error. If your application runs the same ACL entry registration flow every time, therefore, you will get an error because the application attempts to re-register an already existing ACL entry.

    When you are adding multiple ACL entries, the entry registration is handled one by one. If an error occurs before all entries are registered, some entries will remain unregistered. To recover from such a situation, you will first need to get a list of the existing ACL entries, remove the duplicating ACL entries from the ACL modification list, and then register remaining entries. See Getting a bucket's ACL to learn how to get the list of the existing ACL entries.

  • The subject is permitted the "KiiACLBucketActionQueryObjects" action but failed to query KiiObjects in the bucket

    The KiiACLBucketActionQueryObjects action only allows the subject to execute the query. To read the result (i.e., the KiiObjects queried), the subject also needs to have the privilege to read the KiiObjects.

    The easiest way to grant the privilege to read the KiiObjects is to permit the KiiACLBucketActionReadObjects action to the subject together with the KiiACLBucketActionQueryObjects action. This will allow the subject to read all KiiObjects in the bucket, so they will be able to read the query result.

    Alternatively, you can permit the KiiACLObjectActionRead action on the KiiObjects in the bucket. See Setting a KiiObject's ACL for more information about the KiiObject ACL.

    You can find the related discussion in ACL Customization Examples.

  • I cannot delete an ACL entry

    You cannot delete default ACL entries applied to scope owners and bucket creators. See Cannot delete default ACL entries of scope owners and creators for more details.

  • I cannot change the ACL of an application-scope bucket

    Changing the ACL of an application-scope bucket requires the app administrator privilege. Follow the procedure described in Admin Features to get an app administrator token and change the ACL with the steps described in Customizing a Bucket's ACL.

    For more information about who can change a bucket's ACL, see Bucket Access Control.