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 CREATE_OBJECTS_IN_BUCKET Create new KiiObjects in the bucket.
    QUERY_OBJECTS_IN_BUCKET QUERY_OBJECTS_IN_BUCKET Query KiiObjects in the bucket.
    READ_OBJECTS_IN_BUCKET READ_OBJECTS_IN_BUCKET Read KiiObjects in the bucket.
    DROP_BUCKET_WITH_ALL_CONTENT DROP_BUCKET_WITH_ALL_CONTENT Drop the bucket along with all KiiObjects inside.

    * These symbols are defined in the BucketAction enumeration and can be specified like KiiACL.BucketAction.CREATE_OBJECTS_IN_BUCKET.

    Note: The "READ_OBJECTS_IN_BUCKET" 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 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 QUERY_OBJECTS_IN_BUCKET and CREATE_OBJECTS_IN_BUCKET actions are permitted to KiiAnyAuthenticatedUser.

  • KiiBucket userBucket = Kii.user().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    
    // Set ACL entries to the bucket ACL.
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.QUERY_OBJECTS_IN_BUCKET, true));
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.CREATE_OBJECTS_IN_BUCKET, true));
    
    try {
      // Save the ACL to the server.
      bucketACL.save();
    } catch (ACLOperationException e) {
      // Handle the error.
      List<KiiACLEntry> failed = e.getFailedACLEntries();
    }
  • KiiBucket userBucket = Kii.user().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    
    // Set ACL entries to the bucket ACL.
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.QUERY_OBJECTS_IN_BUCKET, true));
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.CREATE_OBJECTS_IN_BUCKET, true));
    
    // Save the ACL to the server.
    bucketACL.save(new KiiACLCallBack() {
      @Override
      public void onSaveCompleted(int token, KiiACL acl, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });
  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 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.

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. In such a situation, you can get a list of ACL entries that were failed to register by executing the getFailedACLEntries() method of the ACLOperationException.

Deleting a bucket ACL entry

To delete an ACL entry, create a KiiACLEntry instance with its third argument 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.

  • KiiBucket userBucket = Kii.user().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    
    // Delete the ACL entries from the bucket ACL.
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.QUERY_OBJECTS_IN_BUCKET, false));
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.CREATE_OBJECTS_IN_BUCKET, false));
    
    try {
      // Save the ACL to the server.
      bucketACL.save();
    } catch (ACLOperationException e) {
      // Handle the error.
    }
  • KiiBucket userBucket = Kii.user().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    
    // Delete the ACL entries from the bucket ACL.
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.QUERY_OBJECTS_IN_BUCKET, false));
    bucketACL.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
                KiiACL.BucketAction.CREATE_OBJECTS_IN_BUCKET, false));
    
    // Save the ACL to the server.
    bucketACL.save(new KiiACLCallBack() {
      @Override
      public void onSaveCompleted(int token, KiiACL acl, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

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 QUERY_OBJECTS_IN_BUCKET and CREATE_OBJECTS_IN_BUCKET actions from bucketACL 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 a Set.

  • KiiBucket userBucket = KiiUser.getCurrentUser().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    try {
      // Get the ACL entries from the server.
      Set<KiiACLEntry> list = bucketACL.listACLEntries();
      for (KiiACLEntry entry : list) {
        KiiACL.Action action = entry.getAction();
        KiiSubject subject = entry.getSubject();
        // Check the ACL entry.
      }
    } catch (ACLOperationException e) {
      // Handle the error.
    }
  • KiiBucket userBucket = KiiUser.getCurrentUser().bucket("my_private");
    KiiACL bucketACL = userBucket.acl();
    
    // Get the ACL entries from the server.
    bucketACL.listACLEntries(new KiiACLCallBack() {
      public void onListACLEntriesCompleted(int token, Set<KiiACLEntry> list, KiiACL acl, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        for (KiiACLEntry entry : list) {
          KiiACL.Action action = entry.getAction();
          KiiSubject subject = entry.getSubject();
          // Check the ACL entry.
        }
      }
    });
  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 a Set of the KiiACLEntry instances.
  3. Parse through each ACL entry in the Set.

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 "QUERY_OBJECTS_IN_BUCKET" action but failed to query KiiObjects in the bucket

    The QUERY_OBJECTS_IN_BUCKET 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 READ_OBJECTS_IN_BUCKET action to the subject together with the QUERY_OBJECTS_IN_BUCKET 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 READ_EXISTING_OBJECT action on the KiiObjects in the bucket. See KiiObject Access Control 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.