Esempio n. 1
0
def edit_connection_gpg(connection_id: int,
                        gpg_id: int) -> Union[str, Response]:
    """Edit a GPG connection."""
    gpg = ConnectionGpg.query.filter_by(
        id=gpg_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/gpg_edit.html.j2",
            gpg=gpg,
            connection=gpg.connection,
            title=f"Editing Connection {gpg.connection}",
        )

    form = request.form
    this_gpg = ConnectionGpg.query.filter_by(id=gpg.id)
    this_gpg.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "key": (em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
                if form.get("key", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: GPG Connection edited. ({gpg.id}) {gpg}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 2
0
def new_connection_gpg(connection_id: int) -> Union[Response, str]:
    """Create a GPG connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/gpg_edit.html.j2",
            connection=connection,
            title="New GPG Connection",
        )
    form = request.form

    gpg = ConnectionGpg(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        key=(em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
             if form.get("key", type=str) else None),
    )

    db.session.add(gpg)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: GPG Connection added. ({gpg.id}) {gpg}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 3
0
def edit_connection_sftp(connection_id: int,
                         sftp_id: int) -> Union[Response, str]:
    """Edit a SFTP connection."""
    sftp = ConnectionSftp.query.filter_by(
        id=sftp_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/sftp_edit.html.j2",
            sftp=sftp,
            connection=sftp.connection,
            title=f"Editing Connection {sftp.connection}",
        )

    form = request.form
    this_sftp = ConnectionSftp.query.filter_by(id=sftp.id)
    this_sftp.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "address":
        form.get("address", "", type=str).strip(),
        "port":
        form.get("port", 22, type=int),
        "path":
        form.get("path", "", type=str).strip(),
        "username":
        form.get("username", "", type=str).strip(),
        "key": (em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
                if form.get("key", type=str) else None),
        "password": (em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                     if form.get("password", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SFTP Connection edited. ({sftp_id}) {sftp}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 4
0
def new_connection_sftp(connection_id: int) -> Union[str, Response]:
    """Create a SFTP connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/sftp_edit.html.j2",
            connection=connection,
            title="New SFTP Connection",
        )
    form = request.form

    sftp = ConnectionSftp(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        address=form.get("address", "", type=str).strip(),
        port=form.get("port", 22, type=int),
        path=form.get("path", "", type=str).strip(),
        username=form.get("username", "", type=str).strip(),
        key=(em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
             if form.get("key", type=str) else None),
        password=(em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                  if form.get("password", type=str) else None),
    )

    db.session.add(sftp)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SFTP Connection added. ({sftp.id}) {sftp}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 5
0
def edit_connection_smb(connection_id: int,
                        smb_id: int) -> Union[Response, str]:
    """Edit a SMB connection."""
    smb = ConnectionSmb.query.filter_by(
        id=smb_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/smb_edit.html.j2",
            smb=smb,
            connection=smb.connection,
            title=f"Editing Connection {smb.connection}",
        )

    form = request.form
    this_smb = ConnectionSmb.query.filter_by(id=smb.id)
    this_smb.update({
        "name":
        form.get("name", "undefined", type=str),
        "server_name":
        form.get("server_name", "", type=str).strip(),
        "server_ip":
        form.get("server_ip", "", type=str).strip(),
        "share_name":
        form.get("share_name", "", type=str).strip(),
        "path":
        form.get("path", "", type=str).strip(),
        "username":
        form.get("username", "", type=str).strip(),
        "password": (em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                     if form.get("password", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SMB Connection added. ({smb.id}) {smb}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 6
0
def edit_connection_database(connection_id: int,
                             database_id: int) -> Union[Response, str]:
    """Edit a database connection."""
    database = ConnectionDatabase.query.filter_by(
        id=database_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/database_edit.html.j2",
            database=database,
            connection=database.connection,
            database_types=ConnectionDatabaseType.query.all(),
            title=f"Editing Database Connection {database.connection}",
        )

    form = request.form
    this_database = ConnectionDatabase.query.filter_by(id=database.id)
    this_database.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "type_id":
        form.get("database_type", 1, type=int),
        "connection_string": (em_encrypt(form.get(
            "connection_string", None), app.config["PASS_KEY"]) if form.get(
                "connection_string", None) else None),
        "timeout":
        form.get("timeout", app.config["DEFAULT_SQL_TIMEOUT"], type=int),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Database Connection edited. ({database.id}) {database}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Changes saved.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
def test_connection_failure(client_fixture: fixture) -> None:
    p_id, t_id = create_demo_task()

    # make a db connection
    conn = Connection(name="demo")
    db.session.add(conn)
    db.session.commit()

    sftp = ConnectionSftp(
        name="demo sql",
        address="nowhere",
        port=99,
        username="******",
        password=em_encrypt("nothing", client_fixture.application.config["PASS_KEY"]),
        connection_id=conn.id,
    )

    task = Task.query.filter_by(id=t_id).first()
    task.source_sftp_conn = sftp

    db.session.commit()

    temp_dir = Path(Path(__file__).parent.parent / "temp" / "tests" / "postgres")
    temp_dir.mkdir(parents=True, exist_ok=True)

    with pytest.raises(RunnerException) as e:
        sftp_server = Sftp(task, None, task.source_sftp_conn, temp_dir)
        assert "nodename nor servname provided, or not known" in e
        assert "SFTP connection failed." in e

    # test with bad key
    sftp.key = sftp.password
    db.session.commit()

    task = Task.query.filter_by(id=t_id).first()
    with pytest.raises(RunnerException) as e:
        sftp_server = Sftp(task, None, task.source_sftp_conn, temp_dir)
        assert "nodename nor servname provided, or not known" in e
        assert "SFTP connection failed." in e
Esempio n. 8
0
def new_connection_smb(connection_id: int) -> Union[Response, str]:
    """Create a SMB connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/smb_edit.html.j2",
            connection=connection,
            title="New SMB Connection",
        )
    form = request.form

    smb = ConnectionSmb(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str),
        server_name=form.get("server_name", "", type=str).strip(),
        server_ip=form.get("server_ip", "", type=str).strip(),
        share_name=form.get("share_name", "", type=str).strip(),
        path=form.get("path", "", type=str).strip(),
        username=form.get("username", "", type=str).strip(),
        password=(em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                  if form.get("password", type=str) else None),
    )

    db.session.add(smb)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SMB Connection added. ({smb.id}) {smb}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
Esempio n. 9
0
def new_connection_database(connection_id: int) -> Union[Response, str]:
    """Create a database connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/database_edit.html.j2",
            connection=connection,
            database_types=ConnectionDatabaseType.query.all(),
            title="New Database Connection",
        )
    form = request.form
    database = ConnectionDatabase(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        type_id=form.get("database_type", 1, type=int),
        connection_string=(em_encrypt(form.get("connection_string"),
                                      app.config["PASS_KEY"])
                           if form.get("connection_string") else None),
        timeout=form.get("timeout",
                         app.config["DEFAULT_SQL_TIMEOUT"],
                         type=int),
    )

    db.session.add(database)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Database Connection added. ({database.id}) {database}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))