-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for all ram auth plugin. (#18)
- Loading branch information
1 parent
889d67b
commit 4402082
Showing
15 changed files
with
857 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/main/java/com/alibaba/nacos/client/aliyun/auth/provider/StsTokenCredentialsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...t/java/com/alibaba/nacos/client/aliyun/auth/AliyunExtensionClientAuthServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.alibaba.nacos.client.aliyun.auth; | ||
|
||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.alibaba.nacos.client.aliyun.auth.provider.ExtensionCredentialsProvider; | ||
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext; | ||
import com.alibaba.nacos.plugin.auth.api.RequestResource; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Properties; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class AliyunExtensionClientAuthServiceImplTest { | ||
|
||
AliyunExtensionClientAuthServiceImpl clientAuthService; | ||
|
||
RequestResource resource; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
clientAuthService = new AliyunExtensionClientAuthServiceImpl(); | ||
resource = RequestResource.configBuilder().build(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws NacosException { | ||
clientAuthService.shutdown(); | ||
} | ||
|
||
@Test | ||
void loginNoMatch() { | ||
assertFalse(clientAuthService.login(new Properties())); | ||
} | ||
|
||
@Test | ||
void loginWithException() { | ||
Properties properties = new Properties(); | ||
properties.setProperty(ExtensionAuthPropertyKey.SECRET_NAME.getKey(), "secret"); | ||
assertFalse(clientAuthService.login(properties)); | ||
} | ||
|
||
@Test | ||
void loginSuccess() { | ||
Properties properties = new Properties(); | ||
properties.setProperty(ExtensionAuthPropertyKey.SECURITY_TOKEN.getKey(), "securityToken"); | ||
properties.setProperty(ExtensionAuthPropertyKey.ACCESS_KEY_ID.getKey(), "accessKeyId"); | ||
properties.setProperty(ExtensionAuthPropertyKey.ACCESS_KEY_SECRET.getKey(), "accessKeySecret"); | ||
assertTrue(clientAuthService.login(properties)); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextForStsToken() throws NoSuchFieldException, IllegalAccessException { | ||
injectMockProvider(true, true); | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertEquals("accessKey", context.getParameter("Spas-AccessKey")); | ||
assertEquals("securityToken", context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNotNull(context.getParameter("Spas-Signature")); | ||
assertNotNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextForAkSk() throws NoSuchFieldException, IllegalAccessException { | ||
injectMockProvider(false, true); | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertEquals("accessKey", context.getParameter("Spas-AccessKey")); | ||
assertNull(context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNotNull(context.getParameter("Spas-Signature")); | ||
assertNotNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextForStsTokenInvalid() throws NoSuchFieldException, IllegalAccessException { | ||
injectMockProvider(true, false); | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertNull(context.getParameter("Spas-AccessKey")); | ||
assertNull(context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNull(context.getParameter("Spas-Signature")); | ||
assertNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextForAkSkInvalid() throws NoSuchFieldException, IllegalAccessException { | ||
injectMockProvider(false, false); | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertNull(context.getParameter("Spas-AccessKey")); | ||
assertNull(context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNull(context.getParameter("Spas-Signature")); | ||
assertNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextForNoInjector() throws NoSuchFieldException, IllegalAccessException { | ||
injectMockProvider(true, true); | ||
resource.setType("Mock"); | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertNull(context.getParameter("Spas-AccessKey")); | ||
assertNull(context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNull(context.getParameter("Spas-Signature")); | ||
assertNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
@Test | ||
void getLoginIdentityContextWithoutInit() { | ||
LoginIdentityContext context = clientAuthService.getLoginIdentityContext(resource); | ||
assertNull(context.getParameter("Spas-AccessKey")); | ||
assertNull(context.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
assertNull(context.getParameter("Spas-Signature")); | ||
assertNull(context.getParameter("Timestamp")); | ||
} | ||
|
||
private void injectMockProvider(boolean ephemeralAccessKeyId, boolean validate) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
MockCredentialsProvider mockProvider = new MockCredentialsProvider(); | ||
mockProvider.ephemeralAccessKeyId = ephemeralAccessKeyId; | ||
mockProvider.validate = validate; | ||
Field matchedProviderField = clientAuthService.getClass().getDeclaredField("matchedProvider"); | ||
matchedProviderField.setAccessible(true); | ||
matchedProviderField.set(clientAuthService, mockProvider); | ||
} | ||
|
||
private static class MockCredentialsProvider implements ExtensionCredentialsProvider { | ||
|
||
boolean ephemeralAccessKeyId = true; | ||
|
||
boolean validate; | ||
|
||
@Override | ||
public boolean matchProvider(Properties properties) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void init(Properties properties) { | ||
} | ||
|
||
@Override | ||
public ExtensionRamContext getCredentialsForNacosClient() { | ||
ExtensionRamContext ramContext = new ExtensionRamContext(); | ||
ramContext.setEphemeralAccessKeyId(ephemeralAccessKeyId); | ||
if (validate) { | ||
ramContext.setSecretKey("secretKey"); | ||
ramContext.setAccessKey("accessKey"); | ||
ramContext.setSecurityToken(ephemeralAccessKeyId ? "securityToken" : ""); | ||
} else { | ||
ramContext.setSecurityToken(ephemeralAccessKeyId ? "" : "securityToken"); | ||
} | ||
return ramContext; | ||
} | ||
|
||
@Override | ||
public void shutdown() throws NacosException { | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
.../com/alibaba/nacos/client/aliyun/auth/injector/AbstractExtensionResourceInjectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.alibaba.nacos.client.aliyun.auth.injector; | ||
|
||
import com.alibaba.nacos.client.aliyun.auth.ExtensionAuthConstants; | ||
import com.alibaba.nacos.client.aliyun.auth.ExtensionRamContext; | ||
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext; | ||
import com.alibaba.nacos.plugin.auth.api.RequestResource; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
class AbstractExtensionResourceInjectorTest { | ||
|
||
AbstractExtensionResourceInjector resourceInjector; | ||
|
||
ExtensionRamContext ramContext; | ||
|
||
RequestResource resource; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
resourceInjector = new MockExtensionResourceInjector(); | ||
ramContext = new ExtensionRamContext(); | ||
ramContext.setSecretKey("secret"); | ||
ramContext.setEphemeralAccessKeyId(false); | ||
resource = new RequestResource(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
} | ||
|
||
@Test | ||
void doInjectForV4WithoutRegionId() { | ||
LoginIdentityContext result = new LoginIdentityContext(); | ||
resourceInjector.doInject(resource, ramContext, result); | ||
assertEquals("secret", result.getParameter("sk")); | ||
assertNull(result.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
} | ||
|
||
@Test | ||
void doInjectForV4WithRegionId() { | ||
ramContext.setExtensionSignatureRegionId("cn-hangzhou"); | ||
LoginIdentityContext result = new LoginIdentityContext(); | ||
resourceInjector.doInject(resource, ramContext, result); | ||
assertNotEquals("secret", result.getParameter("sk")); | ||
assertNull(result.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
} | ||
|
||
@Test | ||
void doInjectForV4WithRegionIdAndStsToken() { | ||
ramContext.setExtensionSignatureRegionId("cn-hangzhou"); | ||
ramContext.setSecurityToken("token"); | ||
ramContext.setEphemeralAccessKeyId(true); | ||
LoginIdentityContext result = new LoginIdentityContext(); | ||
resourceInjector.doInject(resource, ramContext, result); | ||
assertNotEquals("secret", result.getParameter("sk")); | ||
assertEquals("token", result.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
} | ||
|
||
@Test | ||
void doInjectForV1WithRegionId() throws NoSuchFieldException, IllegalAccessException { | ||
Field supportV4signatureField = resourceInjector.getClass().getSuperclass() | ||
.getDeclaredField("supportV4signature"); | ||
supportV4signatureField.setAccessible(true); | ||
supportV4signatureField.set(resourceInjector, false); | ||
ramContext.setExtensionSignatureRegionId("cn-hangzhou"); | ||
LoginIdentityContext result = new LoginIdentityContext(); | ||
resourceInjector.doInject(resource, ramContext, result); | ||
assertEquals("secret", result.getParameter("sk")); | ||
assertNull(result.getParameter(ExtensionAuthConstants.SECURITY_TOKEN_HEADER)); | ||
} | ||
|
||
private static class MockExtensionResourceInjector extends AbstractExtensionResourceInjector { | ||
|
||
@Override | ||
protected String getAccessKeyHeaderKey() { | ||
return "Mock"; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> calculateSignature(RequestResource resource, String actualSecretKey, | ||
ExtensionRamContext ramContext) { | ||
return Collections.singletonMap("sk", actualSecretKey); | ||
} | ||
} | ||
} |
Oops, something went wrong.