HADOOP-13499. Support session credentials for authenticating with Aliyun. Contributed by Genmao Yu.

This commit is contained in:
Mingfei 2016-08-23 17:10:00 +08:00
parent bd2d97adee
commit 6bb741b9f8
3 changed files with 131 additions and 2 deletions

View File

@ -31,8 +31,9 @@ private Constants() {
"fs.oss.credentials.provider"; "fs.oss.credentials.provider";
// OSS access verification // OSS access verification
public static final String ACCESS_KEY = "fs.oss.access.key"; public static final String ACCESS_KEY = "fs.oss.accessKeyId";
public static final String SECRET_KEY = "fs.oss.secret.key"; public static final String SECRET_KEY = "fs.oss.accessKeySecret";
public static final String SECURITY_TOKEN = "fs.oss.securityToken";
// Number of simultaneous connections to oss // Number of simultaneous connections to oss
public static final String MAXIMUM_CONNECTIONS_KEY = public static final String MAXIMUM_CONNECTIONS_KEY =

View File

@ -0,0 +1,64 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs.aliyun.oss;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentials;
import com.aliyun.oss.common.auth.InvalidCredentialsException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import java.net.URI;
import static org.apache.hadoop.fs.aliyun.oss.Constants.*;
/**
* Support session credentials for authenticating with ALiyun.
*/
public class TemporaryAliyunCredentialsProvider implements CredentialsProvider {
public static final String NAME
= "org.apache.hadoop.fs.aliyun.oss.TemporaryAliyunCredentialsProvider";
private final String accessKeyId;
private final String accessKeySecret;
private final String securityToken;
public TemporaryAliyunCredentialsProvider(URI uri, Configuration conf) {
this.accessKeyId = conf.get(ACCESS_KEY, null);
this.accessKeySecret = conf.get(SECRET_KEY, null);
this.securityToken = conf.get(SECURITY_TOKEN, null);
}
@Override
public void setCredentials(Credentials creds) {
}
@Override
public Credentials getCredentials() {
if (!StringUtils.isEmpty(accessKeyId)
&& !StringUtils.isEmpty(accessKeySecret)
&& !StringUtils.isEmpty(securityToken)) {
return new DefaultCredentials(accessKeyId, accessKeySecret,
securityToken);
}
throw new InvalidCredentialsException(
"AccessKeyId, AccessKeySecret or SecurityToken is unset");
}
}

View File

@ -0,0 +1,64 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs.aliyun.oss;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.InvalidCredentialsException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.aliyun.oss.contract.OSSContract;
import org.apache.hadoop.fs.contract.AbstractFSContract;
import org.apache.hadoop.fs.contract.AbstractFSContractTestBase;
import org.junit.Test;
import java.net.URI;
import static org.apache.hadoop.fs.aliyun.oss.Constants.ACCESS_KEY;
import static org.apache.hadoop.fs.aliyun.oss.Constants.SECRET_KEY;
import static org.apache.hadoop.fs.aliyun.oss.Constants.SECURITY_TOKEN;
/**
* Tests use of temporary credentials (for example, Aliyun STS & Aliyun OSS).
* This test extends a class that "does things to the root directory", and
* should only be used against transient filesystems where you don't care about
* the data.
*/
public class TestOSSTemporaryCredentials extends AbstractFSContractTestBase {
@Override
protected AbstractFSContract createContract(Configuration conf) {
return new OSSContract(conf);
}
@Test
public void testTemporaryCredentialValidation() throws Throwable {
Configuration conf = new Configuration();
conf.set(ACCESS_KEY, "accessKeyId");
conf.set(SECRET_KEY, "accessKeySecret");
conf.set(SECURITY_TOKEN, "");
URI uri = getFileSystem().getUri();
TemporaryAliyunCredentialsProvider provider
= new TemporaryAliyunCredentialsProvider(uri, conf);
try {
Credentials credentials = provider.getCredentials();
fail("Expected a CredentialInitializationException, got " + credentials);
} catch (InvalidCredentialsException expected) {
// expected
}
}
}