Search in sources :

Example 41 with Credential

use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.

the class InternalAccessEnforcerTest method testInternalAccessEnforceSuccess.

@Test
public void testInternalAccessEnforceSuccess() 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.INTERNAL);
    Principal principal = new Principal(SYSTEM_PRINCIPAL, Principal.PrincipalType.USER, null, credential);
    internalAccessEnforcer.enforce(ns, principal, StandardPermission.GET);
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Example 42 with Credential

use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.

the class DefaultAccessEnforcerTest method testIsVisibleWithEncryptedCredential.

@Test
public void testIsVisibleWithEncryptedCredential() throws IOException, AccessException, CipherException, GeneralSecurityException {
    SConfiguration sConfCopy = enableCredentialEncryption();
    TinkCipher cipher = new TinkCipher(sConfCopy);
    String cred = cipher.encryptToBase64("credential".getBytes(StandardCharsets.UTF_8), null);
    Principal userWithCredEncrypted = new Principal("userFoo", Principal.PrincipalType.USER, null, new Credential(cred, Credential.CredentialType.EXTERNAL_ENCRYPTED));
    ControllerWrapper controllerWrapper = createControllerWrapper(CCONF, sConfCopy, null);
    AccessController accessController = controllerWrapper.accessController;
    DefaultAccessEnforcer accessEnforcer = controllerWrapper.defaultAccessEnforcer;
    Set<NamespaceId> namespaces = ImmutableSet.of(NS);
    Assert.assertEquals(0, accessEnforcer.isVisible(namespaces, userWithCredEncrypted).size());
    accessController.grant(Authorizable.fromEntityId(NS), userWithCredEncrypted, ImmutableSet.of(StandardPermission.GET, StandardPermission.UPDATE));
    Assert.assertEquals(1, accessEnforcer.isVisible(namespaces, userWithCredEncrypted).size());
    // Verify the metrics context was called with correct metrics
    verify(controllerWrapper.mockMetricsContext, times(2)).increment(Constants.Metrics.Authorization.NON_INTERNAL_VISIBILITY_CHECK_COUNT, 1);
    verify(controllerWrapper.mockMetricsContext, times(2)).gauge(eq(Constants.Metrics.Authorization.EXTENSION_VISIBILITY_MILLIS), any(Long.class));
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) NoOpAccessController(io.cdap.cdap.security.spi.authorization.NoOpAccessController) AccessController(io.cdap.cdap.security.spi.authorization.AccessController) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) TinkCipher(io.cdap.cdap.security.auth.TinkCipher) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Example 43 with Credential

use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.

the class InternalAccessEnforcerTest method testInternalAccessIsVisibleExpiredCredential.

@Test
public void testInternalAccessIsVisibleExpiredCredential() throws IOException {
    NamespaceId ns = new NamespaceId("namespace");
    Set<EntityId> entities = Collections.singleton(ns);
    long currentTime = System.currentTimeMillis();
    UserIdentity userIdentity = new UserIdentity(SYSTEM_PRINCIPAL, UserIdentity.IdentifierType.INTERNAL, Collections.emptyList(), currentTime - 10 * MINUTE_MILLIS, currentTime - 5 * MINUTE_MILLIS);
    String encodedIdentity = Base64.getEncoder().encodeToString(accessTokenCodec.encode(tokenManager.signIdentifier(userIdentity)));
    Credential credential = new Credential(encodedIdentity, Credential.CredentialType.INTERNAL);
    Principal principal = new Principal(SYSTEM_PRINCIPAL, Principal.PrincipalType.USER, null, credential);
    Assert.assertEquals(Collections.emptySet(), internalAccessEnforcer.isVisible(entities, principal));
}
Also used : EntityId(io.cdap.cdap.proto.id.EntityId) Credential(io.cdap.cdap.proto.security.Credential) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Example 44 with Credential

use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.

the class InternalAccessEnforcerTest method testInternalAccessEnforceNonInternalCredentialType.

@Test(expected = IllegalStateException.class)
public void testInternalAccessEnforceNonInternalCredentialType() 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.enforce(ns, principal, StandardPermission.GET);
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Example 45 with Credential

use of io.cdap.cdap.proto.security.Credential in project cdap by caskdata.

the class InternalAccessEnforcerTest method testInternalAccessIsVisibleNonInternalCredentialType.

@Test(expected = IllegalStateException.class)
public void testInternalAccessIsVisibleNonInternalCredentialType() throws IOException {
    NamespaceId ns = new NamespaceId("namespace");
    Set<EntityId> entities = Collections.singleton(ns);
    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.isVisible(entities, principal);
}
Also used : EntityId(io.cdap.cdap.proto.id.EntityId) Credential(io.cdap.cdap.proto.security.Credential) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Aggregations

Credential (io.cdap.cdap.proto.security.Credential)79 Principal (io.cdap.cdap.proto.security.Principal)58 Test (org.junit.Test)53 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)32 UserIdentity (io.cdap.cdap.security.auth.UserIdentity)26 EntityId (io.cdap.cdap.proto.id.EntityId)12 AccessController (io.cdap.cdap.security.spi.authorization.AccessController)10 NoOpAccessController (io.cdap.cdap.security.spi.authorization.NoOpAccessController)10 TinkCipher (io.cdap.cdap.security.auth.TinkCipher)8 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)6 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)6 IOException (java.io.IOException)6 HttpURLConnection (java.net.HttpURLConnection)6 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)4 PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)4 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)4 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)4 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)4 PreviewConfig (io.cdap.cdap.proto.artifact.preview.PreviewConfig)4