diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/CredentialsProviderFactory.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/CredentialsProviderFactory.java deleted file mode 100644 index ed052c9eb6..0000000000 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/CredentialsProviderFactory.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.aliyuncs.auth; - -public class CredentialsProviderFactory { - public T createCredentialsProvider(T classInstance) { - return classInstance; - } -} diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java index cd6594b756..e89d45b0f1 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ProfileCredentialsProvider.java @@ -45,8 +45,7 @@ public AlibabaCloudCredentials getCredentials() throws ClientException { if (clientConfig == null) { throw new ClientException("Client is not open in the specified credentials file"); } - CredentialsProviderFactory credentialsProviderFactory = new CredentialsProviderFactory(); - return createCredential(clientConfig, credentialsProviderFactory); + return createCredential(clientConfig); } private Map> loadIni(Wini ini) { @@ -65,20 +64,19 @@ private Map> loadIni(Wini ini) { return client; } - private AlibabaCloudCredentials createCredential(Map clientConfig, - CredentialsProviderFactory factory) throws ClientException { + private AlibabaCloudCredentials createCredential(Map clientConfig) throws ClientException { String configType = clientConfig.get(AuthConstant.INI_TYPE); if (StringUtils.isEmpty(configType)) { throw new ClientException("The configured client type is empty"); } if (AuthConstant.INI_TYPE_ARN.equals(configType)) { - return getSTSAssumeRoleSessionCredentials(clientConfig, factory); + return getSTSAssumeRoleSessionCredentials(clientConfig); } if (AuthConstant.INI_TYPE_KEY_PAIR.equals(configType)) { - return getSTSGetSessionAccessKeyCredentials(clientConfig, factory); + return getSTSGetSessionAccessKeyCredentials(clientConfig); } if (AuthConstant.INI_TYPE_RAM.equals(configType)) { - return getInstanceProfileCredentials(clientConfig, factory); + return getInstanceProfileCredentials(clientConfig); } String accessKeyId = clientConfig.get(AuthConstant.INI_ACCESS_KEY_ID); String accessKeySecret = clientConfig.get(AuthConstant.INI_ACCESS_KEY_IDSECRET); @@ -88,8 +86,7 @@ private AlibabaCloudCredentials createCredential(Map clientConfi return new BasicCredentials(accessKeyId, accessKeySecret); } - private AlibabaCloudCredentials getSTSAssumeRoleSessionCredentials(Map clientConfig, - CredentialsProviderFactory factory) + private AlibabaCloudCredentials getSTSAssumeRoleSessionCredentials(Map clientConfig) throws ClientException { String accessKeyId = clientConfig.get(AuthConstant.INI_ACCESS_KEY_ID); String accessKeySecret = clientConfig.get(AuthConstant.INI_ACCESS_KEY_IDSECRET); @@ -103,14 +100,12 @@ private AlibabaCloudCredentials getSTSAssumeRoleSessionCredentials(Map clientConfig, - CredentialsProviderFactory factory) + private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials(Map clientConfig) throws ClientException { String publicKeyId = clientConfig.get(AuthConstant.INI_PUBLIC_KEY_ID); String privateKeyFile = clientConfig.get(AuthConstant.INI_PRIVATE_KEY_FILE); @@ -121,20 +116,16 @@ private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials(Map clientConfig, - CredentialsProviderFactory factory) - throws ClientException { + private AlibabaCloudCredentials getInstanceProfileCredentials(Map clientConfig) throws ClientException { String roleName = clientConfig.get(AuthConstant.INI_ROLE_NAME); if (StringUtils.isEmpty(roleName)) { throw new ClientException("The configured role_name is empty"); } - InstanceProfileCredentialsProvider provider = - factory.createCredentialsProvider(new InstanceProfileCredentialsProvider(roleName)); + InstanceProfileCredentialsProvider provider = new InstanceProfileCredentialsProvider(roleName); return provider.getCredentials(); } diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/CredentialsProviderFactoryTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/CredentialsProviderFactoryTest.java deleted file mode 100644 index 4b630bed3b..0000000000 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/CredentialsProviderFactoryTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.aliyuncs.auth; - -import org.junit.Assert; -import org.junit.Test; - -public class CredentialsProviderFactoryTest { - @Test - public void createCredentialsProviderTest(){ - CredentialsProviderFactory factory = new CredentialsProviderFactory(); - SystemPropertiesCredentialsProvider provider = new SystemPropertiesCredentialsProvider(); - Assert.assertTrue(factory.createCredentialsProvider(provider) == provider); - } -} diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java index 990c14aedd..46cfd710bc 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ProfileCredentialsProviderTest.java @@ -47,12 +47,11 @@ public void createCredentialTest() throws NoSuchMethodException, InvocationTarge ProfileCredentialsProvider provider = new ProfileCredentialsProvider(); Class providerClass = provider.getClass(); Method createCredential = providerClass.getDeclaredMethod( - "createCredential", Map.class, CredentialsProviderFactory.class); + "createCredential", Map.class); createCredential.setAccessible(true); - CredentialsProviderFactory factory = new CredentialsProviderFactory(); Map client = new HashMap(); try { - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured client type is empty", e.getCause().getLocalizedMessage()); @@ -61,7 +60,7 @@ public void createCredentialTest() throws NoSuchMethodException, InvocationTarge client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_RAM); try { - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured role_name is empty", @@ -71,7 +70,7 @@ public void createCredentialTest() throws NoSuchMethodException, InvocationTarge client.clear(); client.put(AuthConstant.INI_ACCESS_KEY_ID, AuthConstant.INI_TYPE_RAM); client.put(AuthConstant.INI_TYPE, "access_key"); - Assert.assertNull(createCredential.invoke(provider, client, factory)); + Assert.assertNull(createCredential.invoke(provider, client)); } @Test @@ -79,13 +78,12 @@ public void getSTSAssumeRoleSessionCredentialsTest() throws NoSuchMethodExceptio ProfileCredentialsProvider provider = new ProfileCredentialsProvider(); Class providerClass = provider.getClass(); Method createCredential = providerClass.getDeclaredMethod( - "createCredential", Map.class, CredentialsProviderFactory.class); + "createCredential", Map.class); createCredential.setAccessible(true); - CredentialsProviderFactory factory = new CredentialsProviderFactory(); Map client = new HashMap(); client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_ARN); try { - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured access_key_id or access_key_secret is empty", @@ -94,7 +92,7 @@ public void getSTSAssumeRoleSessionCredentialsTest() throws NoSuchMethodExceptio try { client.put(AuthConstant.INI_ACCESS_KEY_ID, AuthConstant.INI_TYPE_ARN); - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured access_key_id or access_key_secret is empty", @@ -102,7 +100,7 @@ public void getSTSAssumeRoleSessionCredentialsTest() throws NoSuchMethodExceptio } try { client.put(AuthConstant.INI_ACCESS_KEY_IDSECRET, AuthConstant.INI_TYPE_ARN); - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured role_session_name or role_arn is empty", @@ -110,7 +108,7 @@ public void getSTSAssumeRoleSessionCredentialsTest() throws NoSuchMethodExceptio } try { client.put(AuthConstant.INI_ROLE_SESSION_NAME, AuthConstant.INI_TYPE_ARN); - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); } catch (Exception e) { Assert.assertEquals("The configured role_session_name or role_arn is empty", e.getCause().getLocalizedMessage()); @@ -122,13 +120,12 @@ public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodExcep ProfileCredentialsProvider provider = new ProfileCredentialsProvider(); Class providerClass = provider.getClass(); Method createCredential = providerClass.getDeclaredMethod( - "createCredential", Map.class, CredentialsProviderFactory.class); + "createCredential", Map.class); createCredential.setAccessible(true); - CredentialsProviderFactory factory = new CredentialsProviderFactory(); Map client = new HashMap(); client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_KEY_PAIR); try { - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured private_key_file is empty", @@ -137,7 +134,7 @@ public void getSTSGetSessionAccessKeyCredentialsTest() throws NoSuchMethodExcep client.put(AuthConstant.INI_PRIVATE_KEY_FILE, "sads"); AuthUtils.setPrivateKey("test"); try { - createCredential.invoke(provider, client, factory); + createCredential.invoke(provider, client); Assert.fail(); } catch (Exception e) { Assert.assertEquals("The configured public_key_id or private_key_file content is empty", @@ -153,7 +150,7 @@ public void createCredentialsProviderTest() throws ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(); Class providerClass = profileCredentialsProvider.getClass(); Method createCredential = providerClass.getDeclaredMethod( - "createCredential", Map.class, CredentialsProviderFactory.class); + "createCredential", Map.class); createCredential.setAccessible(true); Map client = new HashMap(); client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_ARN); @@ -165,10 +162,9 @@ public void createCredentialsProviderTest() throws STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider = Mockito.mock(STSAssumeRoleSessionCredentialsProvider.class); Mockito.when(stsAssumeRoleSessionCredentialsProvider.getCredentials()).thenReturn(null); - CredentialsProviderFactory factory = Mockito.mock(CredentialsProviderFactory.class); - Mockito.when(factory.createCredentialsProvider(Mockito.any(STSAssumeRoleSessionCredentialsProvider.class))). + Mockito.when(Mockito.any(STSAssumeRoleSessionCredentialsProvider.class)). thenReturn(stsAssumeRoleSessionCredentialsProvider); - Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); + Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client)); client.clear(); client.put(AuthConstant.INI_TYPE, AuthConstant.INI_TYPE_KEY_PAIR); @@ -179,9 +175,9 @@ public void createCredentialsProviderTest() throws STSGetSessionAccessKeyCredentialsProvider stsGetSessionAccessKeyCredentialsProvider = Mockito.mock(STSGetSessionAccessKeyCredentialsProvider.class); Mockito.when(stsGetSessionAccessKeyCredentialsProvider.getCredentials()).thenReturn(null); - Mockito.when(factory.createCredentialsProvider(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class))). + Mockito.when(Mockito.any(STSGetSessionAccessKeyCredentialsProvider.class)). thenReturn(stsGetSessionAccessKeyCredentialsProvider); - Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); + Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client)); AuthUtils.setPrivateKey(null); client.clear(); @@ -190,9 +186,9 @@ public void createCredentialsProviderTest() throws InstanceProfileCredentialsProvider instanceProfileCredentialsProvider = Mockito.mock(InstanceProfileCredentialsProvider.class); Mockito.when(instanceProfileCredentialsProvider.getCredentials()).thenReturn(null); - Mockito.when(factory.createCredentialsProvider(Mockito.any(InstanceProfileCredentialsProvider.class))). + Mockito.when(Mockito.any(InstanceProfileCredentialsProvider.class)). thenReturn(instanceProfileCredentialsProvider); - Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client, factory)); + Assert.assertNull(createCredential.invoke(profileCredentialsProvider, client)); } @Test