Skip to content

Commit

Permalink
add getBearerToken
Browse files Browse the repository at this point in the history
  • Loading branch information
atptro authored and JacksonTian committed Feb 21, 2020
1 parent 1b8aeb1 commit 26bb99d
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>0.0.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/credentials/AccessKeyCredential.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public String getType() {
return AuthConstant.ACCESS_KEY;
}

@Override
public String getBearerToken() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface AlibabaCloudCredentials {
public String getSecurityToken();

public String getType();

public String getBearerToken();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public String getSecurityToken() {

@Override
public String getType() {
return null;
return "bearer";
}

@Override
public String getBearerToken() {
return bearerToken;
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/aliyun/credentials/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.aliyun.credentials;

import com.aliyun.credentials.exception.CredentialException;
import com.aliyun.credentials.models.Config;
import com.aliyun.credentials.provider.*;
import com.aliyun.credentials.utils.AuthConstant;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;

public class Client {

private AlibabaCloudCredentials cloudCredential;

public Client(Config config) throws ParseException, CredentialException, IOException {
this.cloudCredential = getCredential(config);
}

public AlibabaCloudCredentials getCredential(Config config) throws IOException, CredentialException, ParseException {
switch (config.type) {
case AuthConstant.ACCESS_KEY:
return new AccessKeyCredential(config.accessKeyId, config.accessKeySecret);
case AuthConstant.STS:
return new StsCredential(config.accessKeyId, config.accessKeySecret, config.securityToken);
case AuthConstant.BEARER:
return new BearerTokenCredential(config.bearerToken);
default:
return this.getProvider(config).getCredentials();
}
}

private AlibabaCloudCredentialsProvider getProvider(Config config) throws CredentialException, MalformedURLException {
try {
switch (config.type) {
case AuthConstant.ECS_RAM_ROLE:
return new EcsRamRoleCredentialProvider(config);
case AuthConstant.RAM_ROLE_ARN:
return new RamRoleArnCredentialProvider(config);
case AuthConstant.RSA_KEY_PAIR:
return new RsaKeyPairCredentialProvider(config);
default:
}
} catch (Exception e) {
e.printStackTrace();
}
return new DefaultCredentialsProvider();
}

public String getAccessKeyId() {
return this.cloudCredential.getAccessKeyId();
}

public String getAccessKeySecret() {
return this.cloudCredential.getAccessKeySecret();
}

public String getSecurityToken() {
return this.cloudCredential.getSecurityToken();
}

public String getType() {
return this.cloudCredential.getType();
}

public String getBearerToken() {
return this.cloudCredential.getBearerToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public String getType() {
return AuthConstant.ECS_RAM_ROLE;
}

@Override
public String getBearerToken() {
return null;
}

public long getExpiration() {
return expiration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public String getType() {
return AuthConstant.RAM_ROLE_ARN;
}

@Override
public String getBearerToken() {
return null;
}

public long getExpiration() {
refreshCredential();
return expiration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public String getType() {
return AuthConstant.RSA_KEY_PAIR;
}

@Override
public String getBearerToken() {
return null;
}

public long getExpiration() {
return expiration;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/credentials/StsCredential.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public void setSecurityToken(String securityToken) {
public String getType() {
return AuthConstant.STS;
}

@Override
public String getBearerToken() {
return null;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/aliyun/credentials/models/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.aliyun.credentials.models;

import com.aliyun.tea.NameInMap;
import com.aliyun.tea.TeaModel;

public class Config extends TeaModel {
@NameInMap("type")
public String type = "default";
@NameInMap("access_key_id")
public String accessKeyId;
@NameInMap("access_key_secret")
public String accessKeySecret;
@NameInMap("role_arn")
public String roleArn;
@NameInMap("role_session_name")
public String roleSessionName;
@NameInMap("private_key_file")
public String privateKeyFile;
@NameInMap("public_key_id")
public String publicKeyId;
@NameInMap("role_name")
public String roleName;
@NameInMap("bearer_token")
public String bearerToken;
@NameInMap("security_token")
public String securityToken;
@NameInMap("host")
public String host;
@NameInMap("rad_time_out")
public int timeout;
@NameInMap("connect_timeout")
public int connectTimeout;
@NameInMap("proxy")
public String proxy;

public static Config build(java.util.Map<String, ?> map) throws Exception {
Config self = new Config();
return TeaModel.build(map, self);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.aliyun.credentials.Configuration;
import com.aliyun.credentials.exception.CredentialException;
import com.aliyun.credentials.http.CompatibleUrlConnClient;
import com.aliyun.credentials.models.Config;
import com.aliyun.credentials.utils.StringUtils;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -32,6 +33,15 @@ public EcsRamRoleCredentialProvider(Configuration config) throws MalformedURLExc
this.fetcher = new ECSMetadataServiceCredentialsFetcher(config.getRoleName(), config.getConnectTimeout(), config.getReadTimeout());
}

public EcsRamRoleCredentialProvider(Config config) throws MalformedURLException, CredentialException {
if (StringUtils.isEmpty(config.roleName)) {
CompatibleUrlConnClient client = new CompatibleUrlConnClient();
String roleName = new ECSMetadataServiceCredentialsFetcher("").fetchRoleName(client);
config.roleName = roleName;
}
this.fetcher = new ECSMetadataServiceCredentialsFetcher(config.roleName, config.connectTimeout, config.timeout);
}

@Override
public AlibabaCloudCredentials getCredentials() throws CredentialException, ParseException {
CompatibleUrlConnClient client = new CompatibleUrlConnClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.aliyun.credentials.http.HttpRequest;
import com.aliyun.credentials.http.HttpResponse;
import com.aliyun.credentials.http.MethodType;
import com.aliyun.credentials.models.Config;
import com.aliyun.credentials.utils.ParameterHelper;
import com.google.gson.Gson;

Expand Down Expand Up @@ -46,6 +47,12 @@ public RamRoleArnCredentialProvider(Configuration config) {
this.readTimeout = config.getReadTimeout();
}

public RamRoleArnCredentialProvider(Config config) {
this(config.accessKeyId, config.accessKeySecret, config.roleArn);
this.connectTimeout = config.connectTimeout;
this.readTimeout = config.timeout;
}

public RamRoleArnCredentialProvider(String accessKeyId, String accessKeySecret, String roleArn) {
this.roleArn = roleArn;
this.accessKeyId = accessKeyId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.aliyun.credentials.http.HttpRequest;
import com.aliyun.credentials.http.HttpResponse;
import com.aliyun.credentials.http.MethodType;
import com.aliyun.credentials.models.Config;
import com.aliyun.credentials.utils.ParameterHelper;
import com.google.gson.Gson;

Expand Down Expand Up @@ -36,6 +37,12 @@ public RsaKeyPairCredentialProvider(Configuration config) {
this.readTimeout = config.getReadTimeout();
}

public RsaKeyPairCredentialProvider(Config config) {
this(config.publicKeyId, config.privateKeyFile);
this.connectTimeout = config.connectTimeout;
this.readTimeout = config.timeout;
}


public RsaKeyPairCredentialProvider(String publicKeyId, String privateKey) {
this.publicKeyId = publicKeyId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public class AuthConstant {
public static final String ECS_RAM_ROLE = "ecs_ram_role";
public static final String RAM_ROLE_ARN = "ram_role_arn";
public static final String RSA_KEY_PAIR = "rsa_key_pair";
public static final String BEARER = "bearer";

}
29 changes: 23 additions & 6 deletions src/test/java/AccessKeyCredentialTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import com.aliyun.credentials.AccessKeyCredential;
import com.aliyun.credentials.*;
import com.aliyun.credentials.utils.AuthConstant;
import org.junit.Assert;
import org.junit.Test;

public class AccessKeyCredentialTest {
@Test
public void accessKeyCredentialTest(){
public void accessKeyCredentialTest() {
AccessKeyCredential credential;
try {
new AccessKeyCredential(null, "test");
Assert.fail();
}catch (IllegalArgumentException e){
Assert.assertEquals("Access key ID cannot be null.",e.getMessage());
} catch (IllegalArgumentException e) {
Assert.assertEquals("Access key ID cannot be null.", e.getMessage());
}

try {
new AccessKeyCredential("test", null);
Assert.fail();
}catch (IllegalArgumentException e){
Assert.assertEquals("Access key secret cannot be null.",e.getMessage());
} catch (IllegalArgumentException e) {
Assert.assertEquals("Access key secret cannot be null.", e.getMessage());
}

credential = new AccessKeyCredential("test", "test");
Expand All @@ -29,4 +29,21 @@ public void accessKeyCredentialTest(){

}

@Test
public void getBearerTokenTest() {
AlibabaCloudCredentials credentials = new AccessKeyCredential("", "");
Assert.assertNull(credentials.getBearerToken());

credentials = new EcsRamRoleCredential();
Assert.assertNull(credentials.getBearerToken());

credentials = new RamRoleArnCredential(null, null, null, 0, null);
Assert.assertNull(credentials.getBearerToken());

credentials = new StsCredential();
Assert.assertNull(credentials.getBearerToken());

credentials = new RsaKeyPairCredential("", "", 0, null);
Assert.assertNull(credentials.getBearerToken());
}
}
2 changes: 1 addition & 1 deletion src/test/java/BearerTokenCredentialsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void setBearerTokenTest() {
String newToken = "new Token";
credentials.setBearerToken(newToken);
Assert.assertEquals(newToken, credentials.getBearerToken());
Assert.assertNull(credentials.getType());
Assert.assertEquals("bearer", credentials.getType());
Assert.assertNull(credentials.getSecurityToken());
}
}
Loading

0 comments on commit 26bb99d

Please sign in to comment.