Search in sources :

Example 1 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method deleteConnection.

/**
 * Delete a connection in the given namespace
 */
@DELETE
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}")
public void deleteConnection(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Deleting connection in system namespace is currently not supported");
            return;
        }
        ConnectionId connectionId = new ConnectionId(namespaceSummary, connection);
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, connectionId.getConnectionId()), StandardPermission.DELETE);
        Connection oldConnection = store.getConnection(connectionId);
        store.deleteConnection(connectionId);
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, oldConnection.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_DELETED_COUNT, 1);
        responder.sendStatus(HttpURLConnection.HTTP_OK);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 2 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method checkPutConnectionPermissions.

private void checkPutConnectionPermissions(ConnectionId connectionId) {
    boolean connectionExists;
    try {
        store.getConnection(connectionId);
        connectionExists = true;
    } catch (ConnectionNotFoundException e) {
        connectionExists = false;
    }
    if (connectionExists) {
        contextAccessEnforcer.enforce(new ConnectionEntityId(connectionId.getNamespace().getName(), connectionId.getConnectionId()), StandardPermission.UPDATE);
    } else {
        contextAccessEnforcer.enforce(new ConnectionEntityId(connectionId.getNamespace().getName(), connectionId.getConnectionId()), StandardPermission.CREATE);
    }
}
Also used : ConnectionNotFoundException(io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId)

Example 3 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method getConnection.

/**
 * Returns the specific connection information in the given namespace
 */
@GET
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}")
public void getConnection(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Getting connection in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.GET);
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, conn.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_GET_COUNT, 1);
        responder.sendJson(conn);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 4 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class ConnectionHandler method spec.

/**
 * Retrieve the spec for the connector, which can be used in a source/sink
 */
@POST
@TransactionPolicy(value = TransactionControl.EXPLICIT)
@Path(API_VERSION + "/contexts/{context}/connections/{connection}/specification")
public void spec(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("context") String namespace, @PathParam("connection") String connection) {
    respond(namespace, responder, namespaceSummary -> {
        if (namespaceSummary.getName().equalsIgnoreCase(NamespaceId.SYSTEM.getNamespace())) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Generating connector spec in system namespace is currently not supported");
            return;
        }
        contextAccessEnforcer.enforce(new ConnectionEntityId(namespace, ConnectionId.getConnectionId(connection)), StandardPermission.USE);
        String specGenerationRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
        SpecGenerationRequest specRequest = GSON.fromJson(specGenerationRequestString, SpecGenerationRequest.class);
        if (specRequest == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "The request body is empty");
            return;
        }
        if (specRequest.getPath() == null) {
            responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Path is not provided in the sample request");
            return;
        }
        Connection conn = store.getConnection(new ConnectionId(namespaceSummary, connection));
        if (getContext().isRemoteTaskEnabled()) {
            executeRemotely(namespace, specGenerationRequestString, conn, RemoteConnectionSpecTask.class, responder);
        } else {
            specGenerationLocally(namespaceSummary.getName(), specRequest, conn, responder);
        }
        Metrics child = metrics.child(ImmutableMap.of(Constants.Metrics.Tag.APP_ENTITY_TYPE, Constants.CONNECTION_SERVICE_NAME, Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, conn.getConnectionType()));
        child.count(Constants.Metrics.Connection.CONNECTION_SPEC_COUNT, 1);
    });
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Metrics(io.cdap.cdap.api.metrics.Metrics) HttpURLConnection(java.net.HttpURLConnection) Connection(io.cdap.cdap.etl.proto.connection.Connection) SpecGenerationRequest(io.cdap.cdap.etl.proto.connection.SpecGenerationRequest) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) TransactionPolicy(io.cdap.cdap.api.annotation.TransactionPolicy)

Example 5 with ConnectionEntityId

use of io.cdap.cdap.proto.id.ConnectionEntityId in project cdap by caskdata.

the class DataPipelineConnectionTest method testConnectionAuthorization.

@Test
public void testConnectionAuthorization() throws Exception {
    File directory = TEMP_FOLDER.newFolder();
    List<BrowseEntity> entities = addFilesInDirectory(directory);
    user = ALICE_NAME;
    // Check Alice can't list requests
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    listConnections(NamespaceId.DEFAULT.getNamespace());
    // Grant Alice nesessary privileges
    getAccessController().grant(Authorizable.fromEntityId(NamespaceId.DEFAULT), ALICE_PRINCIPAL, EnumSet.of(StandardPermission.GET));
    getAccessController().grant(Authorizable.fromEntityId(NamespaceId.DEFAULT, EntityType.SYSTEM_APP_ENTITY), ALICE_PRINCIPAL, EnumSet.of(StandardPermission.LIST));
    // Check Alice can list requests now
    expectedCode = HttpURLConnection.HTTP_OK;
    listConnections(NamespaceId.DEFAULT.getNamespace());
    // Check Bob still can't do it
    user = "bob";
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    listConnections(NamespaceId.DEFAULT.getNamespace());
    // Check Alice can't do it for another namespace
    user = ALICE_NAME;
    listConnections("testNamespace");
    // Check Alice still can't create, delete and use connections
    String conn = "test_connection";
    ConnectionCreationRequest creationRequest = new ConnectionCreationRequest("", new PluginInfo(FileConnector.NAME, Connector.PLUGIN_TYPE, null, Collections.emptyMap(), // in set up we add "-mocks" as the suffix for the artifact id
    new ArtifactSelectorConfig("system", APP_ARTIFACT_ID.getArtifact() + "-mocks", APP_ARTIFACT_ID.getVersion())));
    addConnection(conn, creationRequest);
    deleteConnection(conn);
    browseConnection(conn, directory.getCanonicalPath(), 10);
    sampleConnection(conn, entities.get(1).getPath(), 100);
    getConnectionSpec(conn, directory.getCanonicalPath(), null, null);
    // Grant Alice permissions to create connection
    ConnectionEntityId connectionEntityId = new ConnectionEntityId(NamespaceId.DEFAULT.getNamespace(), ConnectionId.getConnectionId(conn));
    getAccessController().grant(Authorizable.fromEntityId(connectionEntityId), ALICE_PRINCIPAL, EnumSet.of(StandardPermission.CREATE));
    expectedCode = HttpURLConnection.HTTP_OK;
    addConnection(conn, creationRequest);
    // Grant Alice permission to use connection
    getAccessController().grant(Authorizable.fromEntityId(connectionEntityId), ALICE_PRINCIPAL, EnumSet.of(StandardPermission.USE));
    browseConnection(conn, directory.getCanonicalPath(), 10);
    sampleConnection(conn, entities.get(1).getPath(), 100);
    getConnectionSpec(conn, directory.getCanonicalPath(), null, null);
    // but Alice still can't update or delete connection
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    addConnection(conn, creationRequest);
    deleteConnection(conn);
    // Grant Alice permission to delete connection
    getAccessController().grant(Authorizable.fromEntityId(connectionEntityId), ALICE_PRINCIPAL, EnumSet.of(StandardPermission.DELETE));
    expectedCode = HttpURLConnection.HTTP_OK;
    deleteConnection(conn);
}
Also used : ArtifactSelectorConfig(io.cdap.cdap.etl.proto.ArtifactSelectorConfig) BrowseEntity(io.cdap.cdap.etl.api.connector.BrowseEntity) ConnectionCreationRequest(io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest) PluginInfo(io.cdap.cdap.etl.proto.connection.PluginInfo) File(java.io.File) ConnectionEntityId(io.cdap.cdap.proto.id.ConnectionEntityId) Test(org.junit.Test)

Aggregations

ConnectionEntityId (io.cdap.cdap.proto.id.ConnectionEntityId)7 TransactionPolicy (io.cdap.cdap.api.annotation.TransactionPolicy)5 Metrics (io.cdap.cdap.api.metrics.Metrics)5 Connection (io.cdap.cdap.etl.proto.connection.Connection)5 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)5 HttpURLConnection (java.net.HttpURLConnection)5 Path (javax.ws.rs.Path)5 POST (javax.ws.rs.POST)3 BrowseEntity (io.cdap.cdap.etl.api.connector.BrowseEntity)1 BrowseRequest (io.cdap.cdap.etl.api.connector.BrowseRequest)1 SampleRequest (io.cdap.cdap.etl.api.connector.SampleRequest)1 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)1 ConnectionCreationRequest (io.cdap.cdap.etl.proto.connection.ConnectionCreationRequest)1 ConnectionNotFoundException (io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException)1 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)1 SpecGenerationRequest (io.cdap.cdap.etl.proto.connection.SpecGenerationRequest)1 File (java.io.File)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Test (org.junit.Test)1