S3Credentials { public void initialize(URI uri, Configuration conf) throws IOException { if (uri.getHost() == null) { throw new IllegalArgumentException("Invalid hostname in URI " + uri); } String userInfo = uri.getUserInfo(); if (userInfo != null) { int index = userInfo.indexOf(':'); if (index != -1) { accessKey = userInfo.substring(0, index); secretAccessKey = userInfo.substring(index + 1); } else { accessKey = userInfo; } } String scheme = uri.getScheme(); String accessKeyProperty = String.format("fs.%s.awsAccessKeyId", scheme); String secretAccessKeyProperty = String.format("fs.%s.awsSecretAccessKey", scheme); if (accessKey == null) { accessKey = conf.getTrimmed(accessKeyProperty); } if (secretAccessKey == null) { final char[] pass = conf.getPassword(secretAccessKeyProperty); if (pass != null) { secretAccessKey = (new String(pass)).trim(); } } if (accessKey == null && secretAccessKey == null) { throw new IllegalArgumentException("AWS " + "Access Key ID and Secret Access " + "Key must be specified as the " + "username or password " + "(respectively) of a " + scheme + " URL, or by setting the " + accessKeyProperty + " or " + secretAccessKeyProperty + " properties (respectively)."); } else if (accessKey == null) { throw new IllegalArgumentException("AWS " + "Access Key ID must be specified " + "as the username of a " + scheme + " URL, or by setting the " + accessKeyProperty + " property."); } else if (secretAccessKey == null) { throw new IllegalArgumentException("AWS " + "Secret Access Key must be " + "specified as the password of a " + scheme + " URL, or by setting the " + secretAccessKeyProperty + " property."); } }  void initialize(URI uri, Configuration conf); String getAccessKey(); String getSecretAccessKey();  }
@Test public void testInvalidHostnameWithUnderscores() throws Exception { S3Credentials s3Credentials = new S3Credentials(); try { s3Credentials.initialize(new URI("s3: fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { assertEquals("Invalid hostname in URI s3: } }
@Test(expected=IllegalArgumentException.class) public void noSecretShouldThrow() throws Exception { S3Credentials s3Credentials = new S3Credentials(); Configuration conf = new Configuration(); conf.set("fs.s3.awsAccessKeyId", EXAMPLE_ID); s3Credentials.initialize(new URI("s3: }
@Test(expected=IllegalArgumentException.class) public void noAccessIdShouldThrow() throws Exception { S3Credentials s3Credentials = new S3Credentials(); Configuration conf = new Configuration(); conf.set("fs.s3.awsSecretAccessKey", EXAMPLE_KEY); s3Credentials.initialize(new URI("s3: }