public static void getBucketAcl(String bucket_name) { System.out.println("Retrieving ACL for bucket: " + bucket_name); final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { AccessControlList acl = s3.getBucketAcl(bucket_name); List<Grant> grants = acl.getGrantsAsList(); for (Grant grant : grants) { System.out.format(" %s: %s\n", grant.getGrantee().getIdentifier(), grant.getPermission().toString()); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
public static void getObjectAcl(String bucket_name, String object_key) { System.out.println("Retrieving ACL for object: " + object_key); System.out.println(" in bucket: " + bucket_name); final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { AccessControlList acl = s3.getObjectAcl(bucket_name, object_key); List<Grant> grants = acl.getGrantsAsList(); for (Grant grant : grants) { System.out.format(" %s: %s\n", grant.getGrantee().getIdentifier(), grant.getPermission().toString()); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
@Test public void testPut() { ModelBucket bucket = getService(ModelBucket.class); InputStream stream = new ByteArrayInputStream("file content".getBytes()); ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); PutObjectResult expected = new PutObjectResult(); when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected); assertEquals(expected, bucket.put("path", stream, 12L)); PutObjectRequest request = requestCaptor.getValue(); assertEquals("model-bucket", request.getBucketName()); assertEquals("path", request.getKey()); assertEquals(stream, request.getInputStream()); assertEquals(12L, request.getMetadata().getContentLength()); List<Grant> grants = request.getAccessControlList().getGrantsAsList(); assertEquals(1, grants.size()); assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee()); assertEquals(Permission.Read, grants.get(0).getPermission()); }
@Override public boolean hasFullControlPermission(final String bucketName) throws AmazonClientException, AmazonServiceException, AmazonS3Exception { LOGGER.info("Checking full controll permission on bucket.."); boolean hasFullControl = false; final AccessControlList acl = getBucketAccessControlList(bucketName); final List<Grant> grantList = acl.getGrantsAsList(); for (final Grant grant : grantList) { if(Permission.FullControl.equals(grant.getPermission())){ hasFullControl = true; LOGGER.info("Permissions validated, hasFullControl: {}", hasFullControl); break; } } return hasFullControl; }
@Override public boolean checkBucketPermission(final String bucketName, final Permission permission) throws AmazonClientException, AmazonServiceException, AmazonS3Exception { LOGGER.info("Checking bucket permission.."); boolean hasPermission = false; final AccessControlList acl = getBucketAccessControlList(bucketName); final List<Grant> grantList = acl.getGrantsAsList(); for (final Grant grant : grantList) { if(permission.equals(grant.getPermission())){ hasPermission = true; LOGGER.info("Permissions validated,hasPermission: {}",hasPermission); break; } } return hasPermission; }
@Override public boolean checkObjectPermission(final String bucketName, final String key, final Permission permission) throws AmazonClientException, AmazonServiceException, AmazonS3Exception { LOGGER.info("Checking object permission.."); boolean hasPermission = false; final AccessControlList objectAcl = s3client.getObjectAcl(bucketName, key); final List<Grant> grantList = objectAcl.getGrantsAsList(); for (final Grant grant : grantList) { if(permission.equals(grant.getPermission())){ hasPermission = true; LOGGER.info("Permissions validated,hasPermission: {}",hasPermission); break; } } return hasPermission; }
/** * Test method for {@link com.github.abhinavmishra14.aws.s3.service.AwsS3IamService#getBucketPermissions(java.lang.String)}. * * @throws Exception the exception */ @Test public void testGetBucketPermissions() throws Exception{ //Create bucket for test awsS3IamService.createBucket(AWS_S3_BUCKET); List<Grant> bucketAcl = awsS3IamService.getBucketPermissions(AWS_S3_BUCKET); assertEquals(true, Permission.FullControl.equals(bucketAcl.get(0).getPermission())); }
@Override public boolean isPublicEntity(String bucketName, String keyName) { LOG.info("Gets the AccessControlList (ACL) for the specified object " + keyName + " in the specified bucket " + bucketName); final String GROUPS_USERS = "http://acs.amazonaws.com/groups/global/AllUsers"; try { AccessControlList accessControlList = amazonS3Client.getObjectAcl(bucketName, keyName); for (Iterator<Grant> iterator = accessControlList.getGrants().iterator(); iterator.hasNext();) { Grant grant = iterator.next(); if (grant.getPermission().equals(Permission.Read) && grant.getGrantee().getIdentifier().equals(GROUPS_USERS)) { return true; } } } catch (AmazonServiceException ase) { LOG.warn(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.warn(ace.getMessage(), ace); } return false; }
/** * Sets the access control headers for the request given. */ private static void addAclHeaders(Request<? extends AmazonWebServiceRequest> request, AccessControlList acl) { List<Grant> grants = acl.getGrantsAsList(); Map<Permission, Collection<Grantee>> grantsByPermission = new HashMap<Permission, Collection<Grantee>>(); for ( Grant grant : grants ) { if ( !grantsByPermission.containsKey(grant.getPermission()) ) { grantsByPermission.put(grant.getPermission(), new LinkedList<Grantee>()); } grantsByPermission.get(grant.getPermission()).add(grant.getGrantee()); } for ( Permission permission : Permission.values() ) { if ( grantsByPermission.containsKey(permission) ) { Collection<Grantee> grantees = grantsByPermission.get(permission); boolean seenOne = false; StringBuilder granteeString = new StringBuilder(); for ( Grantee grantee : grantees ) { if ( !seenOne ) seenOne = true; else granteeString.append(", "); granteeString.append(grantee.getTypeIdentifier()).append("=").append("\"") .append(grantee.getIdentifier()).append("\""); } request.addHeader(permission.getHeaderName(), granteeString.toString()); } } }
/** * Converts the specified AccessControlList object to an XML fragment that * can be sent to Amazon S3. * * @param acl * The AccessControlList to convert to XML. * * @return an XML representation of the Access Control List object, suitable * to send in a request to Amazon S3. */ public byte[] convertToXmlByteArray(AccessControlList acl) throws SdkClientException { Owner owner = acl.getOwner(); if (owner == null) { throw new SdkClientException("Invalid AccessControlList: missing an S3Owner"); } XmlWriter xml = new XmlWriter(); xml.start("AccessControlPolicy", "xmlns", Constants.XML_NAMESPACE); xml.start("Owner"); if (owner.getId() != null) { xml.start("ID").value(owner.getId()).end(); } if (owner.getDisplayName() != null) { xml.start("DisplayName").value(owner.getDisplayName()).end(); } xml.end(); xml.start("AccessControlList"); for (Grant grant : acl.getGrantsAsList()) { xml.start("Grant"); convertToXml(grant.getGrantee(), xml); xml.start("Permission").value(grant.getPermission().toString()).end(); xml.end(); } xml.end(); xml.end(); return xml.getBytes(); }
@Test public void testPutWithoutContentLenght() { ModelBucket bucket = getService(ModelBucket.class); InputStream stream = new ByteArrayInputStream("file content".getBytes()); ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); PutObjectResult expected = new PutObjectResult(); when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected); assertEquals(expected, bucket.put("path", stream, null)); PutObjectRequest request = requestCaptor.getValue(); Scanner scanner = new Scanner(request.getInputStream()); assertEquals("model-bucket", request.getBucketName()); assertEquals("path", request.getKey()); assertEquals("file content", scanner.useDelimiter("\\A").next()); assertEquals(12L, request.getMetadata().getContentLength()); List<Grant> grants = request.getAccessControlList().getGrantsAsList(); assertEquals(1, grants.size()); assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee()); assertEquals(Permission.Read, grants.get(0).getPermission()); scanner.close(); }
@Override public void prepareCommit() throws Exception { logger.info("prepareCommit"); if (!validS3Sink) { // check if bucket exist if (!s3Client.doesBucketExist(bucketName)) { System.out.println("bucket does not exist."); logger.info("Bucket does not Exist"); s3Client.createBucket(bucketName); } logger.info("Bucket Exist"); /* * BucketVersioningConfiguration configuration = new * BucketVersioningConfiguration( bucketVersionConfig); * SetBucketVersioningConfigurationRequest request = new * SetBucketVersioningConfigurationRequest( bucketName, configuration); * s3Client.setBucketVersioningConfiguration(request); */ AccessControlList acl = s3Client.getBucketAcl(bucketName); List<Permission> permissions = new ArrayList<Permission>(); for (Grant grant : acl.getGrants()) { permissions.add(grant.getPermission()); } if (permissions.contains(Permission.FullControl) || permissions.contains(Permission.Write)) { validS3Sink = true; } } else { validS3Sink = true; } logger.info("validS3Sink = " + validS3Sink); System.out.println("validS3Sink = " + validS3Sink); }
@Override public List<Grant> getBucketPermissions(final String bucketName) throws AmazonClientException, AmazonServiceException, AmazonS3Exception { LOGGER.info("getBucketPermissions invoked, bucketName: {}", bucketName); return getBucketAccessControlList(bucketName).getGrantsAsList(); }
/** * Gets the bucket permissions.<br/> * Returns the list of Grant objects in this access control list (ACL).<br/> * The Grant object has Permission object which tell what kind of permissions are available.<br/> * and it has Grantee object which tell who are grantees.<br/> * Following are permissions available: <i>FullControl, Read, Write, ReadAcp, WriteAcp </i><br/> * If access to the given bucket is not valid then 'AccessDenied' error will be raised. * * @param bucketName the bucket name * @return the bucket permissions * @throws AmazonClientException the amazon client exception * @throws AmazonServiceException the amazon service exception * @throws AmazonS3Exception the amazon s3 exception * @see com.amazonaws.services.s3.model.Grant * @see com.amazonaws.services.s3.model.Permission * @see com.amazonaws.services.s3.model.Grantee * @see <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#permissions">Permissions</a> */ List<Grant> getBucketPermissions(final String bucketName) throws AmazonClientException, AmazonServiceException, AmazonS3Exception;