use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.
the class InternalAccessEnforcerTest method testInternalAccessEnforceOnParentNonInternalCredentialType.
@Test(expected = IllegalStateException.class)
public void testInternalAccessEnforceOnParentNonInternalCredentialType() throws IOException {
NamespaceId ns = new NamespaceId("namespace");
long currentTime = System.currentTimeMillis();
UserIdentity userIdentity = new UserIdentity(SYSTEM_PRINCIPAL, UserIdentity.IdentifierType.INTERNAL, Collections.emptyList(), currentTime, currentTime + 5 * MINUTE_MILLIS);
String encodedIdentity = Base64.getEncoder().encodeToString(accessTokenCodec.encode(tokenManager.signIdentifier(userIdentity)));
Credential credential = new Credential(encodedIdentity, Credential.CredentialType.EXTERNAL);
Principal principal = new Principal(SYSTEM_PRINCIPAL, Principal.PrincipalType.USER, null, credential);
internalAccessEnforcer.enforceOnParent(EntityType.APPLICATION, ns, principal, StandardPermission.GET);
}
use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.
the class InternalAccessEnforcerTest method testInternalAccessEnforceInvalidCredential.
@Test(expected = AccessException.class)
public void testInternalAccessEnforceInvalidCredential() throws IOException {
NamespaceId ns = new NamespaceId("namespace");
Credential credential = new Credential("invalid", Credential.CredentialType.INTERNAL);
Principal principal = new Principal(SYSTEM_PRINCIPAL, Principal.PrincipalType.USER, null, credential);
internalAccessEnforcer.enforce(ns, principal, StandardPermission.GET);
}
use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.
the class InternalAccessEnforcerTest method testInternalAccessEnforceOnParentInvalidCredential.
@Test(expected = AccessException.class)
public void testInternalAccessEnforceOnParentInvalidCredential() throws IOException {
NamespaceId ns = new NamespaceId("namespace");
Credential credential = new Credential("invalid", Credential.CredentialType.INTERNAL);
Principal principal = new Principal(SYSTEM_PRINCIPAL, Principal.PrincipalType.USER, null, credential);
internalAccessEnforcer.enforceOnParent(EntityType.APPLICATION, ns, principal, StandardPermission.GET);
}
use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.
the class RemoteClientAuthenticatorTest method testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader.
@Test
public void testRemoteClientWithRemoteAuthenticatorIncludesAuthorizationHeader() throws Exception {
String mockAuthenticatorName = "mock-remote-authenticator";
Credential expectedCredential = new Credential("test-credential", Credential.CredentialType.EXTERNAL_BEARER);
RemoteAuthenticator mockRemoteAuthenticator = mock(RemoteAuthenticator.class);
when(mockRemoteAuthenticator.getName()).thenReturn(mockAuthenticatorName);
when(mockRemoteAuthenticator.getCredentials()).thenReturn(expectedCredential);
mockRemoteAuthenticatorProvider.setAuthenticator(mockRemoteAuthenticator);
RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
int responseCode = conn.getResponseCode();
// Verify that the request received the expected headers.
HttpHeaders headers = testHttpHandler.getRequest().headers();
Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
Assert.assertEquals(String.format("%s %s", expectedCredential.getType().getQualifiedName(), expectedCredential.getValue()), headers.get(javax.ws.rs.core.HttpHeaders.AUTHORIZATION));
}
use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.
the class DefaultAccessEnforcerTest method testAuthEnforceWithBadEncryptedCredential.
@Test
public void testAuthEnforceWithBadEncryptedCredential() throws IOException, AccessException, CipherException, GeneralSecurityException {
thrown.expect(Exception.class);
thrown.expectMessage("Failed to decrypt credential in principle:");
SConfiguration sConfCopy = enableCredentialEncryption();
TinkCipher cipher = new TinkCipher(sConfCopy);
String badCipherCred = Base64.getEncoder().encodeToString("invalid encrypted credential".getBytes());
Principal userWithCredEncrypted = new Principal("userFoo", Principal.PrincipalType.USER, null, new Credential(badCipherCred, Credential.CredentialType.EXTERNAL_ENCRYPTED));
ControllerWrapper controllerWrapper = createControllerWrapper(CCONF, sConfCopy, null);
AccessController accessController = controllerWrapper.accessController;
DefaultAccessEnforcer accessEnforcer = controllerWrapper.defaultAccessEnforcer;
accessController.grant(Authorizable.fromEntityId(NS), userWithCredEncrypted, ImmutableSet.of(StandardPermission.GET, StandardPermission.GET));
accessEnforcer.enforce(NS, userWithCredEncrypted, StandardPermission.GET);
// Verify the metrics context was not called
verify(controllerWrapper.mockMetricsContext, times(0)).increment(any(String.class), any(Long.class));
verify(controllerWrapper.mockMetricsContext, times(0)).gauge(any(String.class), any(Long.class));
}
Aggregations