| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.domain; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collection; |
| 24 | import java.util.Collections; |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.TreeSet; |
| 28 | |
| 29 | import com.google.common.annotations.VisibleForTesting; |
| 30 | import com.google.common.base.Function; |
| 31 | import com.google.common.base.Predicate; |
| 32 | import com.google.common.collect.Collections2; |
| 33 | |
| 34 | /** |
| 35 | * An Access Control List (ACL) describes the access control settings for a bucket or object in S3. |
| 36 | * |
| 37 | * ACL settings comprise a set of {@link Grant}s, each of which specifies a {@link Permission} that |
| 38 | * has been granted to a specific {@link Grantee}. If an payload tries to access or modify an item |
| 39 | * in S3, the operation will be denied unless the item has ACL settings that explicitly permit that |
| 40 | * payload to perform that action. |
| 41 | * |
| 42 | * |
| 43 | * @author James Murty |
| 44 | * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html"/> |
| 45 | */ |
| 46 | public class AccessControlList { |
| 47 | |
| 48 | private CanonicalUser owner; |
| 49 | private final List<Grant> grants = new ArrayList<Grant>(); |
| 50 | |
| 51 | public void setOwner(CanonicalUser owner) { |
| 52 | this.owner = owner; |
| 53 | } |
| 54 | |
| 55 | public CanonicalUser getOwner() { |
| 56 | return owner; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return an unmodifiable set of grants represented by this ACL. |
| 61 | */ |
| 62 | public List<Grant> getGrants() { |
| 63 | return Collections.unmodifiableList(grants); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return an unmodifiable set of grantees who have been assigned permissions in this ACL. |
| 68 | */ |
| 69 | public Set<Grantee> getGrantees() { |
| 70 | Set<Grantee> grantees = new TreeSet<Grantee>(); |
| 71 | for (Grant grant : getGrants()) { |
| 72 | grantees.add(grant.getGrantee()); |
| 73 | } |
| 74 | return Collections.unmodifiableSet(grantees); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Add a permission for the given grantee. |
| 79 | * |
| 80 | * @param grantee |
| 81 | * @param permission |
| 82 | */ |
| 83 | public AccessControlList addPermission(Grantee grantee, String permission) { |
| 84 | Grant grant = new Grant(grantee, permission); |
| 85 | grants.add(grant); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Add a permission for the given group grantee. |
| 91 | * |
| 92 | * @param groupGranteeURI |
| 93 | * @param permission |
| 94 | */ |
| 95 | public AccessControlList addPermission(URI groupGranteeURI, String permission) { |
| 96 | return addPermission(new GroupGrantee(groupGranteeURI), permission); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Revoke a permission for the given grantee, if this specific permission was granted. |
| 101 | * |
| 102 | * Note that you must be very explicit about the permissions you revoke, you cannot revoke |
| 103 | * partial permissions and expect this class to determine the implied remaining permissions. For |
| 104 | * example, if you revoke the {@link Permission#READ} permission from a grantee with |
| 105 | * {@link Permission#FULL_CONTROL} access, <strong>the revocation will do nothing</strong> and |
| 106 | * the grantee will retain full access. To change the access settings for this grantee, you must |
| 107 | * first remove the {@link Permission#FULL_CONTROL} permission the add back the |
| 108 | * {@link Permission#READ} permission. |
| 109 | * |
| 110 | * @param grantee |
| 111 | * @param permission |
| 112 | */ |
| 113 | public AccessControlList revokePermission(Grantee grantee, String permission) { |
| 114 | Collection<Grant> grantsForGrantee = findGrantsForGrantee(grantee.getIdentifier()); |
| 115 | for (Grant grant : grantsForGrantee) { |
| 116 | if (grant.getPermission().equals(permission)) { |
| 117 | grants.remove(grant); |
| 118 | } |
| 119 | } |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Revoke a permission for the given group grantee, if this specific permission was granted. |
| 125 | * |
| 126 | * Note that you must be very explicit about the permissions you revoke, you cannot revoke |
| 127 | * partial permissions and expect this class to determine the implied remaining permissions. For |
| 128 | * example, if you revoke the {@link Permission#READ} permission from a grantee with |
| 129 | * {@link Permission#FULL_CONTROL} access, <strong>the revocation will do nothing</strong> and |
| 130 | * the grantee will retain full access. To change the access settings for this grantee, you must |
| 131 | * first remove the {@link Permission#FULL_CONTROL} permission the add back the |
| 132 | * {@link Permission#READ} permission. |
| 133 | * |
| 134 | * @param groupGranteeURI |
| 135 | * @param permission |
| 136 | */ |
| 137 | public AccessControlList revokePermission(URI groupGranteeURI, String permission) { |
| 138 | return revokePermission(new GroupGrantee(groupGranteeURI), permission); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Revoke all the permissions granted to the given grantee. |
| 143 | * |
| 144 | * @param grantee |
| 145 | */ |
| 146 | public AccessControlList revokeAllPermissions(Grantee grantee) { |
| 147 | Collection<Grant> grantsForGrantee = findGrantsForGrantee(grantee.getIdentifier()); |
| 148 | grants.removeAll(grantsForGrantee); |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param granteeId |
| 154 | * @return the permissions assigned to a grantee, as identified by the given ID. |
| 155 | */ |
| 156 | public Collection<String> getPermissions(String granteeId) { |
| 157 | Collection<Grant> grantsForGrantee = findGrantsForGrantee(granteeId); |
| 158 | return Collections2.transform(grantsForGrantee, new Function<Grant, String>() { |
| 159 | public String apply(Grant g) { |
| 160 | return g.getPermission(); |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param grantee |
| 167 | * @return the permissions assigned to a grantee. |
| 168 | */ |
| 169 | public Collection<String> getPermissions(Grantee grantee) { |
| 170 | return getPermissions(grantee.getIdentifier()); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param granteeURI |
| 175 | * @return the permissions assigned to a group grantee. |
| 176 | */ |
| 177 | public Collection<String> getPermissions(URI granteeURI) { |
| 178 | return getPermissions(granteeURI.toASCIIString()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param granteeId |
| 183 | * @param permission |
| 184 | * @return true if the grantee has the given permission. |
| 185 | */ |
| 186 | public boolean hasPermission(String granteeId, String permission) { |
| 187 | return getPermissions(granteeId).contains(permission); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param grantee |
| 192 | * @param permission |
| 193 | * @return true if the grantee has the given permission. |
| 194 | */ |
| 195 | public boolean hasPermission(Grantee grantee, String permission) { |
| 196 | return hasPermission(grantee.getIdentifier(), permission); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param granteeURI |
| 201 | * @param permission |
| 202 | * @return true if the grantee has the given permission. |
| 203 | */ |
| 204 | public boolean hasPermission(URI granteeURI, String permission) { |
| 205 | return getPermissions(granteeURI).contains(permission); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Find all the grants for a given grantee, identified by an ID which allows all Grantee types to |
| 210 | * be searched. |
| 211 | * |
| 212 | * @param granteeId |
| 213 | * identifier of a canonical user, email address user, or group. |
| 214 | */ |
| 215 | protected Collection<Grant> findGrantsForGrantee(final String granteeId) { |
| 216 | return Collections2.filter(grants, new Predicate<Grant>() { |
| 217 | public boolean apply(Grant g) { |
| 218 | return g.getGrantee().getIdentifier().equals(granteeId); |
| 219 | } |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Converts a canned access control policy into the equivalent access control list. |
| 225 | * |
| 226 | * @param cannedAP |
| 227 | * @param ownerId |
| 228 | */ |
| 229 | public static AccessControlList fromCannedAccessPolicy(CannedAccessPolicy cannedAP, String ownerId) { |
| 230 | AccessControlList acl = new AccessControlList(); |
| 231 | acl.setOwner(new CanonicalUser(ownerId)); |
| 232 | |
| 233 | // Canned access policies always allow full control to the owner. |
| 234 | acl.addPermission(new CanonicalUserGrantee(ownerId), Permission.FULL_CONTROL); |
| 235 | |
| 236 | if (CannedAccessPolicy.PRIVATE == cannedAP) { |
| 237 | // No more work to do. |
| 238 | } else if (CannedAccessPolicy.AUTHENTICATED_READ == cannedAP) { |
| 239 | acl.addPermission(GroupGranteeURI.AUTHENTICATED_USERS, Permission.READ); |
| 240 | } else if (CannedAccessPolicy.PUBLIC_READ == cannedAP) { |
| 241 | acl.addPermission(GroupGranteeURI.ALL_USERS, Permission.READ); |
| 242 | } else if (CannedAccessPolicy.PUBLIC_READ_WRITE == cannedAP) { |
| 243 | acl.addPermission(GroupGranteeURI.ALL_USERS, Permission.READ); |
| 244 | acl.addPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE); |
| 245 | } |
| 246 | return acl; |
| 247 | } |
| 248 | |
| 249 | // ///////////////////////////////////////////////////////////////////////////// |
| 250 | // Class and Enum declarations to represent Grants, Grantees and Permissions // |
| 251 | // ///////////////////////////////////////////////////////////////////////////// |
| 252 | |
| 253 | public static interface Permission { |
| 254 | public static final String READ = "READ"; |
| 255 | public static final String WRITE = "WRITE"; |
| 256 | public static final String READ_ACP = "READ_ACP"; |
| 257 | public static final String WRITE_ACP = "WRITE_ACP"; |
| 258 | public static final String FULL_CONTROL = "FULL_CONTROL"; |
| 259 | }; |
| 260 | |
| 261 | public static class Grant implements Comparable<Grant> { |
| 262 | |
| 263 | private Grantee grantee; |
| 264 | private final String permission; |
| 265 | |
| 266 | public Grant(Grantee grantee, String permission) { |
| 267 | this.grantee = grantee; |
| 268 | this.permission = permission; |
| 269 | } |
| 270 | |
| 271 | public Grantee getGrantee() { |
| 272 | return grantee; |
| 273 | } |
| 274 | |
| 275 | @VisibleForTesting |
| 276 | public void setGrantee(Grantee grantee) { |
| 277 | this.grantee = grantee; |
| 278 | } |
| 279 | |
| 280 | public String getPermission() { |
| 281 | return permission; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public String toString() { |
| 286 | final StringBuilder sb = new StringBuilder(); |
| 287 | sb.append("Grant"); |
| 288 | sb.append("{grantee=").append(grantee); |
| 289 | sb.append(", permission=").append(permission); |
| 290 | sb.append('}'); |
| 291 | return sb.toString(); |
| 292 | } |
| 293 | |
| 294 | public int compareTo(org.jclouds.s3.domain.AccessControlList.Grant o) { |
| 295 | if (this == o) { |
| 296 | return 0; |
| 297 | } else { |
| 298 | String myGranteeAndPermission = grantee.getIdentifier() + "\n" + permission; |
| 299 | String otherGranteeAndPermission = o.grantee.getIdentifier() + "\n" + o.permission; |
| 300 | return myGranteeAndPermission.compareTo(otherGranteeAndPermission); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public int hashCode() { |
| 306 | final int prime = 31; |
| 307 | int result = 1; |
| 308 | result = prime * result + ((grantee == null) ? 0 : grantee.hashCode()); |
| 309 | result = prime * result + ((permission == null) ? 0 : permission.hashCode()); |
| 310 | return result; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public boolean equals(Object obj) { |
| 315 | if (this == obj) |
| 316 | return true; |
| 317 | if (obj == null) |
| 318 | return false; |
| 319 | if (getClass() != obj.getClass()) |
| 320 | return false; |
| 321 | Grant other = (Grant) obj; |
| 322 | if (grantee == null) { |
| 323 | if (other.grantee != null) |
| 324 | return false; |
| 325 | } else if (!grantee.equals(other.grantee)) |
| 326 | return false; |
| 327 | if (permission == null) { |
| 328 | if (other.permission != null) |
| 329 | return false; |
| 330 | } else if (!permission.equals(other.permission)) |
| 331 | return false; |
| 332 | return true; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | public abstract static class Grantee implements Comparable<Grantee> { |
| 337 | private final String identifier; |
| 338 | |
| 339 | protected Grantee(String identifier) { |
| 340 | this.identifier = identifier; |
| 341 | } |
| 342 | |
| 343 | public String getIdentifier() { |
| 344 | return identifier; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public String toString() { |
| 349 | final StringBuilder sb = new StringBuilder(); |
| 350 | sb.append("Grantee"); |
| 351 | sb.append("{identifier='").append(identifier).append('\''); |
| 352 | sb.append('}'); |
| 353 | return sb.toString(); |
| 354 | } |
| 355 | |
| 356 | public int compareTo(org.jclouds.s3.domain.AccessControlList.Grantee o) { |
| 357 | return (this == o) ? 0 : getIdentifier().compareTo(o.getIdentifier()); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public int hashCode() { |
| 362 | final int prime = 31; |
| 363 | int result = 1; |
| 364 | result = prime * result + ((identifier == null) ? 0 : identifier.hashCode()); |
| 365 | return result; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public boolean equals(Object obj) { |
| 370 | if (this == obj) |
| 371 | return true; |
| 372 | if (obj == null) |
| 373 | return false; |
| 374 | if (getClass() != obj.getClass()) |
| 375 | return false; |
| 376 | Grantee other = (Grantee) obj; |
| 377 | if (identifier == null) { |
| 378 | if (other.identifier != null) |
| 379 | return false; |
| 380 | } else if (!identifier.equals(other.identifier)) |
| 381 | return false; |
| 382 | return true; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | public static class EmailAddressGrantee extends Grantee { |
| 387 | public EmailAddressGrantee(String emailAddress) { |
| 388 | super(emailAddress); |
| 389 | } |
| 390 | |
| 391 | public String getEmailAddress() { |
| 392 | return getIdentifier(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | public static class CanonicalUserGrantee extends Grantee { |
| 397 | private final String displayName; |
| 398 | |
| 399 | public CanonicalUserGrantee(String id, String displayName) { |
| 400 | super(id); |
| 401 | this.displayName = displayName; |
| 402 | } |
| 403 | |
| 404 | public CanonicalUserGrantee(String id) { |
| 405 | this(id, null); |
| 406 | } |
| 407 | |
| 408 | public String getDisplayName() { |
| 409 | return displayName; |
| 410 | } |
| 411 | |
| 412 | public String toString() { |
| 413 | final StringBuilder sb = new StringBuilder(); |
| 414 | sb.append("CanonicalUserGrantee"); |
| 415 | sb.append("{displayName='").append(displayName).append('\''); |
| 416 | sb.append(", identifier='").append(getIdentifier()).append('\''); |
| 417 | |
| 418 | sb.append('}'); |
| 419 | return sb.toString(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | public interface GroupGranteeURI { |
| 424 | public static final URI ALL_USERS = URI.create("http://acs.amazonaws.com/groups/global/AllUsers"); |
| 425 | public static final URI AUTHENTICATED_USERS = URI |
| 426 | .create("http://acs.amazonaws.com/groups/global/AuthenticatedUsers"); |
| 427 | public static final URI LOG_DELIVERY = URI.create("http://acs.amazonaws.com/groups/LogDelivery"); |
| 428 | } |
| 429 | |
| 430 | public static class GroupGrantee extends Grantee { |
| 431 | |
| 432 | public GroupGrantee(URI groupURI) { |
| 433 | super(groupURI.toASCIIString()); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public String toString() { |
| 439 | final StringBuilder sb = new StringBuilder(); |
| 440 | sb.append("AccessControlList"); |
| 441 | sb.append("{owner=").append(owner); |
| 442 | sb.append(", grants=").append(grants); |
| 443 | sb.append('}'); |
| 444 | return sb.toString(); |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public int hashCode() { |
| 449 | final int prime = 31; |
| 450 | int result = 1; |
| 451 | result = prime * result + ((grants == null) ? 0 : grants.hashCode()); |
| 452 | result = prime * result + ((owner == null) ? 0 : owner.hashCode()); |
| 453 | return result; |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public boolean equals(Object obj) { |
| 458 | if (this == obj) |
| 459 | return true; |
| 460 | if (obj == null) |
| 461 | return false; |
| 462 | if (getClass() != obj.getClass()) |
| 463 | return false; |
| 464 | AccessControlList other = (AccessControlList) obj; |
| 465 | if (grants == null) { |
| 466 | if (other.grants != null) |
| 467 | return false; |
| 468 | } else if (!grants.equals(other.grants)) |
| 469 | return false; |
| 470 | if (owner == null) { |
| 471 | if (other.owner != null) |
| 472 | return false; |
| 473 | } else if (!owner.equals(other.owner)) |
| 474 | return false; |
| 475 | return true; |
| 476 | } |
| 477 | } |