Skip to content

Commit 4e678e1

Browse files
committed
update EcsRamRoleProvider
1 parent fd64c27 commit 4e678e1

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/test/java/com/aliyun/oss/common/provider/EcsRamRoleCredentialsProviderTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import com.aliyun.oss.common.auth.*;
2525
import com.aliyun.oss.common.provider.mock.EcsRamRoleCredentialsFetcherMock;
2626
import com.aliyun.oss.common.provider.mock.EcsRamRoleCredentialsFetcherMock.ResponseCategory;
27+
import com.aliyun.oss.common.utils.DateUtil;
2728
import junit.framework.Assert;
2829
import org.junit.Test;
2930

3031
import java.io.ByteArrayInputStream;
32+
import java.util.Date;
3133

3234
public class EcsRamRoleCredentialsProviderTest extends TestBase {
3335

@@ -257,4 +259,69 @@ public void testGetCredentialsFromAuthInOss() {
257259
Assert.fail(e.getMessage());
258260
}
259261
}
262+
263+
@Test
264+
public void testExpireTime() {
265+
266+
String NORMAL_METADATA_1 = "{" + "\"AccessKeyId\" : \"STS.EgnR2nX****FAf9uuqjHS8Ddt\","
267+
+ "\"AccessKeySecret\" : \"CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV\","
268+
+ "\"SecurityToken\" : \"CAISigJ1q6Ft5B2yfSjIpKTbGYjatahPg6CtQ0CIkXUkZsd/14HPljz2IHBE****AOEetfs2lW1T6P0TlrRtTtpfTEmBbI569s1WqQW+Z5fT5JHo4LZfhoGoRzB9keMGTIyADd/iRfbxJ92PCTmd5AIRrJ****K9JS/HVbSClZ9gaPkOQwC8dkAoLdxKJwxk2qR4XDmrQp****PxhXfKB0dFoxd1jXgFiZ6y2cqB8BHT/jaYo603392ofsj1NJE1ZMglD4nlhbxMG/CfgHIK2X9j77xriaFIwzDDs+yGDkNZixf8aLqEqIM/dV4hPfdjSvMf8qOtj5t1sffJnoHtzBJAIexOT****FVtcH5xchqAAXp1d/dYv+2L+dJDW+2pm1vACD/UlRk93prPkyuU3zH2wnvXBxEi26QnoQSCA+T1yE2wo41V2mS+LSGYN/PC+2Ml1q+JX5DzKgfGrUPt7kU4FeXJDzGh2YaXRGpO7yERKgAc/NukkDNqthMaHntyTeix08DYBuTT6gd3V8XmN8vF\","
269+
+ "\"Code\" : \"Success\","
270+
+ "\"Expiration\" :" ;
271+
272+
String NORMAL_METADATA_2 = "{" + "\"AccessKeyId\" : \"STS.AgnR2nX****FAf9uuqjHS8Ddt\","
273+
+ "\"AccessKeySecret\" : \"AJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV\","
274+
+ "\"SecurityToken\" : \"AAISigJ1q6Ft5B2yfSjIpKTbGYjatahPg6CtQ0CIkXUkZsd/14HPljz2IHBE****AOEetfs2lW1T6P0TlrRtTtpfTEmBbI569s1WqQW+Z5fT5JHo4LZfhoGoRzB9keMGTIyADd/iRfbxJ92PCTmd5AIRrJ****K9JS/HVbSClZ9gaPkOQwC8dkAoLdxKJwxk2qR4XDmrQp****PxhXfKB0dFoxd1jXgFiZ6y2cqB8BHT/jaYo603392ofsj1NJE1ZMglD4nlhbxMG/CfgHIK2X9j77xriaFIwzDDs+yGDkNZixf8aLqEqIM/dV4hPfdjSvMf8qOtj5t1sffJnoHtzBJAIexOT****FVtcH5xchqAAXp1d/dYv+2L+dJDW+2pm1vACD/UlRk93prPkyuU3zH2wnvXBxEi26QnoQSCA+T1yE2wo41V2mS+LSGYN/PC+2Ml1q+JX5DzKgfGrUPt7kU4FeXJDzGh2YaXRGpO7yERKgAc/NukkDNqthMaHntyTeix08DYBuTT6gd3V8XmN8vF\","
275+
+ "\"Code\" : \"Success\","
276+
+ "\"Expiration\" :" ;
277+
278+
try {
279+
280+
EcsRamRoleCredentialsFetcherMock credentialsFetcher = new EcsRamRoleCredentialsFetcherMock(
281+
TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.FromExternal);
282+
283+
String meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date()) + "\"}";
284+
credentialsFetcher.setExternalData(meta);
285+
286+
EcsRamRoleCredentialsProvider credentialsProvider = new EcsRamRoleCredentialsProvider(
287+
TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
288+
289+
BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
290+
Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt");
291+
Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV");
292+
Assert.assertTrue(credentials.useSecurityToken());
293+
Assert.assertTrue(credentials.willSoonExpire());
294+
295+
meta = NORMAL_METADATA_2 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date()) + "\"}";
296+
credentialsFetcher.setExternalData(meta);
297+
298+
credentials = (BasicCredentials) credentialsProvider.getCredentials();
299+
Assert.assertEquals(credentials.getAccessKeyId(), "STS.AgnR2nX****FAf9uuqjHS8Ddt");
300+
Assert.assertEquals(credentials.getSecretAccessKey(), "AJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV");
301+
Assert.assertTrue(credentials.useSecurityToken());
302+
Assert.assertTrue(credentials.willSoonExpire());
303+
304+
long currTime = new Date().getTime() + (3600 * 6 * 15/100 - 100) * 1000;
305+
meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date(currTime)) + "\"}";
306+
credentialsFetcher.setExternalData(meta);
307+
credentials = (BasicCredentials) credentialsProvider.getCredentials();
308+
Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt");
309+
Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV");
310+
Assert.assertTrue(credentials.useSecurityToken());
311+
Assert.assertTrue(credentials.willSoonExpire());
312+
313+
currTime = new Date().getTime() + (3600 * 6 * 15/100 + 100) * 1000;
314+
meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date(currTime)) + "\"}";
315+
credentialsFetcher.setExternalData(meta);
316+
credentials = (BasicCredentials) credentialsProvider.getCredentials();
317+
Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt");
318+
Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV");
319+
Assert.assertTrue(credentials.useSecurityToken());
320+
Assert.assertFalse(credentials.willSoonExpire());
321+
322+
} catch (Exception e) {
323+
e.printStackTrace();
324+
Assert.fail(e.getMessage());
325+
}
326+
}
260327
}

src/test/java/com/aliyun/oss/common/provider/mock/EcsRamRoleCredentialsFetcherMock.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ public EcsRamRoleCredentialsFetcherMock(String ossAuthServerHost) {
7979
+ "\"SecurityToken\" : \"CAISigJ1q6Ft5B2yfSjIpKTbGYjatahPg6CtQ0CIkXUkZsd/14HPljz2IHBE****AOEetfs2lW1T6P0TlrRtTtpfTEmBbI569s1WqQW+Z5fT5JHo4LZfhoGoRzB9keMGTIyADd/iRfbxJ92PCTmd5AIRrJ****K9JS/HVbSClZ9gaPkOQwC8dkAoLdxKJwxk2qR4XDmrQp****PxhXfKB0dFoxd1jXgFiZ6y2cqB8BHT/jaYo603392ofsj1NJE1ZMglD4nlhbxMG/CfgHIK2X9j77xriaFIwzDDs+yGDkNZixf8aLqEqIM/dV4hPfdjSvMf8qOtj5t1sffJnoHtzBJAIexOT****FVtcH5xchqAAXp1d/dYv+2L+dJDW+2pm1vACD/UlRk93prPkyuU3zH2wnvXBxEi26QnoQSCA+T1yE2wo41V2mS+LSGYN/PC+2Ml1q+JX5DzKgfGrUPt7kU4FeXJDzGh2YaXRGpO7yERKgAc/NukkDNqthMaHntyTeix08DYBuTT6gd3V8XmN8vF\","
8080
+ "\"Code\" : \"Success\"" + "}";
8181

82+
private String extrnalData = "";
83+
8284
public enum ResponseCategory {
8385
Normal, NormalWithoutExpiration, NormalWithoutToken, Expired, FormatInvalid, ServerHalt, Exceptional,
84-
ExceptionalWithoutStatus, ExceptionalFailStatus, ExceptionalWithoutAK, ExceptionalWithoutSK
86+
ExceptionalWithoutStatus, ExceptionalFailStatus, ExceptionalWithoutAK, ExceptionalWithoutSK,
87+
FromExternal
8588
};
8689

8790
@Override
@@ -136,6 +139,10 @@ public HttpResponse send(HttpRequest request) throws IOException {
136139
response.setStatus(200);
137140
response.setHttpContent(FORMAT_INVALID_WITHOUT_SK_METADATA.getBytes(), "UTF-8", FormatType.JSON);
138141
break;
142+
case FromExternal:
143+
response.setStatus(200);
144+
response.setHttpContent(extrnalData.getBytes(), "UTF-8", FormatType.JSON);
145+
break;
139146
default:
140147
break;
141148
}
@@ -148,5 +155,9 @@ public EcsRamRoleCredentialsFetcherMock withResponseCategory(ResponseCategory re
148155
return this;
149156
}
150157

158+
public void setExternalData(String extrnalData) {
159+
this.extrnalData = extrnalData;
160+
}
161+
151162
private ResponseCategory responseCategory;
152163
}

0 commit comments

Comments
 (0)