Compare commits

..

129 Commits

Author SHA1 Message Date
fluzzi32 1bd9bd62c5 Implement hooking system for classes and methods. Cleanup of code 2024-05-03 17:32:45 -03:00
fluzzi32 87bb6302ff Update hooks functionality 2024-04-22 18:17:11 -03:00
fluzzi32 c6a31cb710 fix setup config for new files 2024-04-17 17:06:12 -03:00
fluzzi32 b7528027ac add hooks and sync to google 2024-04-17 16:27:02 -03:00
fluzzi32 f96fe77aed merge plugin feature, change version and update docs 2023-12-21 17:37:57 -03:00
fluzzi32 26ea2e588d add update plugin, better completion for plugins 2023-12-19 18:26:09 -03:00
fluzzi32 e07f7ff130 documentation and completion for plugins 2023-12-15 12:27:51 -03:00
fluzzi32 54a539c51a bug fix 2023-12-14 18:36:22 -03:00
fluzzi32 4f8497ff26 Add new plugin feature beta1 2023-12-14 16:56:59 -03:00
fluzzi32 9975d60a91 change version and docs 2023-12-06 17:46:39 -03:00
fluzzi32 3d5db06343 add docker image to run it in windows 2023-12-06 17:46:10 -03:00
fluzzi32 3e32aa958c merge Jumphost, modify license 2023-12-06 10:53:42 -03:00
fluzzi32 ea3bfeee9e fixes for jumphost 2023-12-04 11:11:58 -03:00
fluzzi32 fd883a4821 Add new jumphost feature to nodes
bug fix when adding a node without name
2023-12-01 18:30:29 -03:00
fluzzi32 137524b176 bug fixes 2023-12-01 15:31:17 -03:00
fluzzi32 d6880d5956 bug fixes 2023-12-01 13:40:49 -03:00
fluzzi32 efe1428f0d bug fix 2023-11-03 17:27:04 -03:00
fluzzi32 a3d0e39ba8 add sftp option 2023-11-03 11:59:00 -03:00
fluzzi32 97c039459c add completion for new features 2023-10-27 13:34:32 -03:00
fluzzi32 00905575fc Fix:
- Fix not allowing to use some regex symbols when passing arguments.
    - Fix AI requests timing out when list of nodes is big.
    - Fix error when forwarding connpy run commands to a file

Features:
    - Improve AI response time changing list of devices to list of OS,
      reducing the lenght of request.
    - Update GPT model to last one.
    - Add filtering option to list command, Also format can be passed to
      format the output as needed.
    - Allow to use regular expresions to match nodes in: run command
      (using yaml file or directly), remove command.
    - When there is a connectivity error, now it shows the error number
      plus the protocol error.
2023-10-26 17:33:44 -03:00
fluzzi32 d96910092b Fix completion and save logs during session 2023-10-05 15:21:17 -03:00
fluzzi32 0813b927b0 Add more export functionalities 2023-09-21 17:28:09 -03:00
fluzzi32 7856dcb9a3 add comlpetion for new import/export 2023-09-13 15:36:06 -03:00
fluzzi32 4373a34711 fix export import with path, add pyfzf as required 2023-09-13 11:17:56 -03:00
fluzzi32 98b85628de Add bulk edit with regex and export/import folder 2023-09-12 12:33:33 -03:00
fluzzi32 be40b2accd bug fix chat history in ai 2023-07-31 12:18:10 -03:00
fluzzi32 54fa5845af Add More AI functions, migrate AI to openai new function support 2023-07-11 19:33:21 -03:00
fluzzi32 06501eccc9 update 2023-05-24 18:14:01 -03:00
fluzzi32 bcbbd4765d change exceptions 2023-05-24 18:13:38 -03:00
fluzzi32 acbfb03b10 bug fixes 2023-05-23 16:15:03 -03:00
fluzzi32 51f86f214a docs 2023-05-22 18:32:49 -03:00
fluzzi32 a0a0e68c49 bug fixes, prompt better 2023-05-22 18:32:31 -03:00
fluzzi32 d5ca894d55 bug fixes 2023-05-18 18:27:25 -03:00
fluzzi32 fc85314e9b fix 2023-05-12 18:30:47 -03:00
fluzzi32 6e70b38524 fix 2023-05-12 18:30:24 -03:00
fluzzi32 5a1dbc04e1 add confirmation and fixes 2023-05-12 12:05:25 -03:00
fluzzi32 0e34ea79c6 add history in ai chat 2023-05-10 12:34:19 -03:00
fluzzi32 a74d055993 cleanup 2023-05-05 14:19:18 -03:00
fluzzi32 8828471c1b add new AI feature. Add tags feature, used tags: OS for AI, screen_length_command for automation, prompt for automation. fixes 2023-05-05 13:41:32 -03:00
fluzzi32 404d874771 bug fix 2023-04-18 18:00:43 -03:00
fluzzi32 1cb0962fac fix README.md 2023-04-15 22:40:02 -03:00
fluzzi32 7d10409ad1 add api debug option 2023-04-15 22:38:52 -03:00
fluzzi32 98a85154cb bug fixes 2023-04-14 18:30:58 -03:00
fluzzi32 8235de23ec fix dependencies 2023-04-14 13:46:05 -03:00
fluzzi32 150268b11d update requirements 2023-04-14 13:40:44 -03:00
fluzzi32 b268f8a372 update requirements 2023-04-14 13:14:22 -03:00
fluzzi32 0b16de5db8 update completion with new features 2023-04-14 11:57:11 -03:00
fluzzi32 65fed3a1a2 update version to 3.0 2023-04-14 11:46:16 -03:00
fluzzi32 51bdc4e59a add api 2023-04-14 11:44:56 -03:00
fluzzi32 68b63baeac starting api 2023-04-06 18:47:29 -03:00
fluzzi32 8329ca25de Add fuzzy non case sensistive search 2023-03-21 18:23:29 -03:00
fluzzi32 bc157a990c bug fixes, add reconnect 2023-01-05 16:39:22 -03:00
fluzzi32 9440611f1e bug fix ver 2022-08-18 18:05:18 -03:00
fluzzi32 943865958d bug fix 2022-08-18 18:00:55 -03:00
fluzzi32 0fad67513f bug fix 2022-07-08 13:03:17 -03:00
fluzzi32 2f5b5fcf6b bug fixes, preparing for unittesting 2022-06-10 13:24:26 -03:00
fluzzi32 3061b54059 fix github actions publish - pick 'publish' branch, fix typos 2022-05-26 11:33:35 -03:00
fluzzi32 ffed88189f fix build issue 2022-05-25 17:43:02 -03:00
fluzzi32 9893f2ed51 Merge branch 'main' of github.com:fluzzi/connpy 2022-05-25 17:27:25 -03:00
fluzzi32 2aa4934288 improve keepalive, logging and output 2022-05-25 17:25:02 -03:00
fluzzi32 feb34ad638 Rename .github/worksflows/publish.yml to .github/workflows/publish.yml 2022-05-25 17:16:36 -03:00
fluzzi32 59821d6c16 Create publish.yml 2022-05-25 17:08:36 -03:00
fluzzi32 38eb2e2d37 update version 2022-05-19 17:30:52 -03:00
fluzzi32 860e57be02 bugfix logfile 2022-05-19 17:30:05 -03:00
fluzzi32 a78aa4c75e fix edit bug, tune run/test automation, prepare for testing 2022-05-19 16:11:41 -03:00
fluzzi32 cc68ff0545 new version add automation in cmd 2022-05-11 14:25:43 -03:00
fluzzi32 638db44aa5 update doc/readme 2022-04-28 12:49:31 -03:00
fluzzi32 b4660254cd working in automation upgrade 2022-04-23 17:11:38 -03:00
fluzzi32 c706ac893c add fzf and change completion 2022-04-18 19:19:25 -03:00
fluzzi32 3072128d31 readme 2022-04-17 00:31:07 -03:00
fluzzi32 53480ec39b readme 2022-04-17 00:29:34 -03:00
fluzzi32 0e90a5aca1 readme 2022-04-17 00:29:00 -03:00
fluzzi32 8c28fbcaa6 readme 2022-04-17 00:27:40 -03:00
fluzzi32 32ab9d3e2d readme 2022-04-17 00:06:13 -03:00
fluzzi32 d61346b3e9 readme 2022-04-17 00:05:12 -03:00
fluzzi32 d689504eec update version 2022-04-05 21:06:38 -03:00
fluzzi32 815c161544 add -v --version command 2022-04-05 20:04:18 -03:00
fluzzi32 c83a2cd28f change version for pip 2022-04-05 18:32:07 -03:00
fluzzi32 118ca1d14e fix getitem folder with subfolders 2022-04-05 18:31:19 -03:00
fluzzi32 fa250e2ae3 fix completion 2022-04-05 15:09:05 -03:00
fluzzi32 1f5fe13805 add new commands to completion 2022-04-04 19:33:47 -03:00
fluzzi32 5c9c605184 added completion bash/zsh 2022-04-04 19:09:00 -03:00
fluzzi32 881eca6181 fix completion 2022-04-04 16:38:10 -03:00
fluzzi32 4348e353a2 fix completion 2022-04-04 16:37:54 -03:00
fluzzi32 d1df2a4cf6 fixes extra arguments 2022-04-04 14:54:37 -03:00
fluzzi32 c09703053b add 1 character arguments 2022-04-04 14:41:51 -03:00
fluzzi32 e4e82ef1c6 updates 2022-04-04 14:14:25 -03:00
fluzzi32 3e0a6b223d update 2022-04-04 13:56:21 -03:00
fluzzi32 12f6baefad updates 2022-04-04 13:52:26 -03:00
fluzzi32 8a605dfb9c update 2022-04-04 12:33:04 -03:00
fluzzi32 2a32b84849 rename package 2022-04-04 11:41:41 -03:00
fluzzi32 65b2a5da0b rename package 2022-04-04 11:40:51 -03:00
fluzzi32 950b88a2ea fix setup.cfg 2022-04-03 18:38:07 -03:00
fluzzi32 67fa4e1e6d finish doc 1.0 2022-04-03 18:25:58 -03:00
fluzzi32 de2c2ab21b working on documentation 2022-04-03 12:00:35 -03:00
fluzzi32 5769d4a5af update docs 2022-04-03 10:26:08 -03:00
fluzzi32 c4950ed029 working on doc 2022-04-03 09:38:00 -03:00
fluzzi32 b5df984498 failed docs 2022-04-02 23:37:39 -03:00
fluzzi32 8e25d5de2a update 2022-04-02 23:30:00 -03:00
fluzzi32 27212b1009 documentation full transformation 2022-04-02 23:25:53 -03:00
fluzzi32 b5d6894865 change setup and versions 2022-04-01 17:53:51 -03:00
fluzzi32 cba3f8d2d9 update node on nodes 2022-03-31 13:45:05 -03:00
fluzzi32 2b9e754ff5 add node on nodes feature 2022-03-31 13:42:25 -03:00
fluzzi32 fd8b367d52 adding threading nodes automation 2022-03-30 19:51:54 -03:00
fluzzi32 59b38bb58a prepare for nodes parallel 2022-03-30 17:36:27 -03:00
fluzzi32 3b7bee233e update setup 2022-03-29 19:14:29 -03:00
fluzzi32 8f13b0b2bf change yaml to json for speed. Add completion 2022-03-29 18:57:27 -03:00
fluzzi32 9f3cb6f6d9 finish setup 2022-03-29 12:45:17 -03:00
fluzzi32 b199ddc8ac fix setup 2022-03-28 16:27:18 -03:00
fluzzi32 7b9bd44ae5 fix setup 2022-03-28 16:26:37 -03:00
fluzzi32 940f9964f7 adding setup 2022-03-28 15:46:42 -03:00
fluzzi32 8f6c1703ac update readme 2022-03-28 10:23:35 -03:00
fluzzi32 d81254deb2 remove test.py 2022-03-28 10:20:27 -03:00
fluzzi32 9898920ab2 update 2022-03-28 10:20:00 -03:00
fluzzi32 2042178cbe update 2022-03-26 16:30:37 -03:00
fluzzi32 555b285d36 add carousel 2022-03-25 18:24:05 -03:00
fluzzi32 79dfa66247 update fix bugtests 2022-03-25 17:55:43 -03:00
fluzzi32 1c6bdddbdc main app done 2022-03-25 12:25:59 -03:00
fluzzi32 43e8325890 working on bulk 2022-03-23 19:33:56 -03:00
fluzzi32 d4121bcbc0 updates 2022-03-23 18:14:29 -03:00
fluzzi32 506044b9fb updates 2022-03-23 17:28:53 -03:00
fluzzi32 56bd92d1f1 a lot of progress 2022-03-22 19:54:05 -03:00
fluzzi32 221d7170ce update 2022-03-19 20:51:51 -03:00
fluzzi32 b3418d48de start cli app 2022-03-19 20:41:35 -03:00
fluzzi32 5113aef8c2 updates + ad requriements 2022-03-18 16:16:31 -03:00
fluzzi32 255b2bd4ef update big 2022-03-18 15:32:48 -03:00
fluzzi32 4a593f2016 update 2022-03-17 19:07:20 -03:00
fluzzi32 6b58e71c6c first commit 2022-03-17 19:05:23 -03:00
fluzzi32 5467e4f4bc Initial commit 2022-03-17 19:00:57 -03:00
183 changed files with 5696 additions and 86957 deletions
-29
View File
@@ -1,29 +0,0 @@
.git
__pycache__
*.pyc
*.pyo
*.pyd
.pytest_cache
.venv
venv
env
node_modules
dist
build
*.egg-info
docker
docker-compose.yml
.gemini
.github
docs
scratch
testall
testremote
automation-template.yaml
# Sensitive local files and credentials
auth.json
key.db
config.db
*.db
testnew/
+2 -2
View File
@@ -22,11 +22,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
with:
ref: publish
- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Install dependencies
-46
View File
@@ -50,7 +50,6 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
scratch/
# Translations
*.mo
@@ -131,48 +130,3 @@ dmypy.json
#clients
*sync_client*
#App
connpy-completion-helper
# Gemini & AI Tools
.gemini/
GEMINI.md
# Node.js (used by Gemini CLI or plugins)
node_modules/
package-lock.json
package.json
# Development docs
connpy_roadmap.md
testfew/
testnew/
testall/
testremote/
*.db
*.patch
scratch.py
connpy.code-workspace
# Internal planning and implementation docs
PLAN_CAPA_SERVICIOS.md
implementation_plan.md
remote-plugin-implementation-plan.md
NETWORK_COMMAND_CENTER_PLAN.md
ssm_implemmetaiton_plan.md
async_interact_plan.md
repo_consolidado_limpio.md
connpy_roadmap.md
MULTI_USER_PLAN.md
COPILOT_PLAN.md
ARCHITECTURAL_DEBT_REFACTOR.md
COPILOT_UI_FEATURES.md
MULTI_USER_IMPLEMENTATION_STEPS.md
#themes
nord.yml
theme.py
#ai auth
auth.json
+8 -123
View File
@@ -1,131 +1,16 @@
# PolyForm Noncommercial License 1.0.0
Custom Software License
<https://polyformproject.org/licenses/noncommercial/1.0.0>
Copyright (c) 2022 Federico Luzzi
## Acceptance
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, and modify the Software, subject to the following conditions:
In order to get any license under these terms, you must agree
to them as both strict obligations and conditions to all
your licenses.
Commercial Use: The use of the Software for commercial purposes, including but not limited to selling, sublicensing, or generating revenue in any form, is expressly prohibited for individuals and entities other than the copyright holder.
## Copyright License
Personal and Non-commercial Use: Individuals and entities are permitted to use, copy, and modify the Software for personal and non-commercial purposes.
The licensor grants you a copyright license for the
software to do everything you might do with the software
that would otherwise infringe the licensor's copyright
in it for any permitted purpose. However, you may
only distribute the software according to [Distribution
License](#distribution-license) and make changes or new works
based on the software according to [Changes and New Works
License](#changes-and-new-works-license).
Distribution: Redistribution of the original or modified Software is allowed, provided the Software is not sold or sublicensed and this license notice is included in all copies or substantial portions of the Software.
## Distribution License
Support and Sale: The copyright holder reserves the exclusive right to sell or offer support services for the Software to any company or commercial entity.
The licensor grants you an additional copyright license
to distribute copies of the software. Your license
to distribute covers distributing the software with
changes and new works permitted by [Changes and New Works
License](#changes-and-new-works-license).
Disclaimer: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## Notices
You must ensure that anyone who gets a copy of any part of
the software from you also gets a copy of these terms or the
URL for them above, as well as copies of any plain-text lines
beginning with `Required Notice:` that the licensor provided
with the software. For example:
> Required Notice: Copyright (c) 2022-2026 Federico Luzzi (<https://github.com/fluzzi/connpy>)
## Changes and New Works License
The licensor grants you an additional copyright license to
make changes and new works based on the software for any
permitted purpose.
## Patent License
The licensor grants you a patent license for the software that
covers patent claims the licensor can license, or becomes able
to license, that you would infringe by using the software.
## Noncommercial Purposes
Any noncommercial purpose is a permitted purpose.
## Personal Uses
Personal use for research, experiment, and testing for
the benefit of public knowledge, personal study, private
entertainment, hobby projects, amateur pursuits, or religious
observance, without any anticipated commercial application,
is use for a permitted purpose.
## Noncommercial Organizations
Use by any charitable organization, educational institution,
public research organization, public safety or health
organization, environmental protection organization,
or government institution is use for a permitted purpose
regardless of the source of funding or obligations resulting
from the funding.
## Fair Use
You may have "fair use" rights for the software under the
law. These terms do not limit them.
## No Other Rights
These terms do not allow you to sublicense or transfer any of
your licenses to anyone else, or prevent the licensor from
granting licenses to anyone else. These terms do not imply
any other licenses.
## Patent Defense
If you make any written claim that the software infringes or
contributes to infringement of any patent, your patent license
for the software granted under these terms ends immediately. If
your company makes such a claim, your patent license ends
immediately for work on behalf of your company.
## Violations
The first time you are notified in writing that you have
violated any of these terms, or done anything with the software
not covered by your licenses, your licenses can nonetheless
continue if you come into full compliance with these terms,
and take practical steps to correct past violations, within
32 days of receiving notice. Otherwise, all your licenses
end immediately.
## No Liability
***As far as the law allows, the software comes as is, without
any warranty or condition, and the licensor will not be liable
to you for any damages arising out of these terms or the use
or nature of the software, under any kind of legal claim.***
## Definitions
The **licensor** is the individual or entity offering these
terms, and the **software** is the software the licensor makes
available under these terms.
**You** refers to the individual or entity agreeing to these
terms.
**Your company** is any legal entity, sole proprietorship,
or other kind of organization that you work for, plus all
organizations that have control over, are under the control of,
or are under common control with that organization. **Control**
means ownership of substantially all the assets of an entity,
or the power to direct its management and policies by vote,
contract, or otherwise. Control can be direct or indirect.
**Your licenses** are all the licenses granted to you for the
software under these terms.
**Use** means anything you do with the software requiring one
of your licenses.
-8
View File
@@ -1,8 +0,0 @@
include LICENSE
include README.md
include requirements.txt
recursive-include connpy/core_plugins *
recursive-include connpy/proto *
recursive-include connpy/grpc_layer *
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
-47
View File
@@ -1,47 +0,0 @@
# Privacy Policy
## Introduction
Welcome to Connpy ("we", "our", "us"). Connpy is committed to protecting your privacy. This Privacy Policy explains how we collect, use, disclose, and safeguard your information when you use our app, which utilizes Google Login to manage its own files in your Google Drive. Please read this privacy policy carefully.
## Information We Collect
### Personal Information
When you use Connpy, we may collect the following information:
- **Google Account Information**: Your email address and basic profile information provided by Google during the login process.
### App-Specific Google Drive Files
Connpy requests access only to the files it creates and manages within your Google Drive. We do not access, read, or manipulate any other files in your Google Drive.
## How We Use Your Information
We use the information we collect in the following ways:
- **Authentication**: To log you into the app using your Google account.
- **File Management**: To upload, manage, and organize the files that Connpy creates in your Google Drive.
## Sharing Your Information
We do not share your personal information or any data related to your Google Drive files with third parties, except in the following cases:
- **Legal Obligations**: If required by law, we may disclose your information to comply with legal processes.
## Data Security
We implement appropriate technical and organizational measures to protect your personal information and the files managed by Connpy from unauthorized access, disclosure, alteration, or destruction.
## Your Rights
You have the following rights regarding your information:
- **Access and Update**: You can access and update your profile information through your Google account settings.
- **Revoke Access**: You can revoke Connpy's access to your Google Drive at any time via your Google account permissions settings.
- **Delete Data**: You can delete the files created by Connpy in your Google Drive at any time.
## Changes to This Privacy Policy
We may update this Privacy Policy from time to time. We will notify you of any changes by posting the new Privacy Policy on our GitHub repository. You are advised to review this Privacy Policy periodically for any changes.
## Contact Us
If you have any questions about this Privacy Policy, please contact us at:
- **GitHub**: [https://github.com/fluzzi/connpy](https://github.com/fluzzi/connpy)
+373 -173
View File
@@ -1,220 +1,420 @@
<p align="center">
<img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
</p>
# Connpy
# Conn
[![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/l/connpy.svg?style=flat-square)](https://github.com/fluzzi/connpy/blob/main/LICENSE)
[![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
**Connpy** is a powerful Connection Manager and Network Automation Platform for Linux, Mac, and Docker. It provides a unified interface for **SSH, SFTP, Telnet, kubectl, Docker pods, and AWS SSM**.
The v6 release introduces the **AI Copilot**, an interactive terminal assistant that understands your network context and helps you manage your infrastructure more intelligently.
## 🤖 AI Copilot (New in v6)
The AI Copilot is deeply integrated into your terminal workflow:
- **Terminal Context Awareness**: The Copilot can "see" your screen output, helping you diagnose errors or analyze command results in real-time.
- **Dynamic Context Selection**: Flexibly select single, range, or line-based terminal blocks to feed the Copilot, filtering out interactive scrolling garbage automatically (e.g., Cisco IOS/XR scrolling, paginators).
- **Hybrid Multi-Agent System**: Automatically escalates complex tasks between the **Network Engineer** (execution) and the **Network Architect** (strategy).
- **MCP Integration**: Dynamically load tools from external providers (6WIND, AWS, etc.) via the Model Context Protocol.
- **Flexible Auth & Keyless AI**: Support for advanced LiteLLM credentials (`--engineer-auth` / `--architect-auth`) allowing keyless local models (Ollama), cloud engines (Vertex AI), or custom endpoints.
- **Enhanced Session Management**: Uniquely generated sessions, robust pagination, and interactive styling translating prompt themes directly to terminal escapes.
- **Semantic Prompt Integration**: Emit standard OSC prompt sequences (`\x1b]133;B`) for real-time remote/web front-end command tracking.
- **Interactive Chat**: Launch with `conn ai` for a collaborative troubleshooting session.
## Core Features
- **Multi-Protocol**: Native support for SSH, SFTP, Telnet, kubectl, Docker exec, and AWS SSM.
- **Context Management**: Set regex-based contexts to manage specific nodes across different environments (work, home, clients).
- **Advanced Inventory**:
- Organize nodes in folders (`@folder`) and subfolders (`@subfolder@folder`).
- Use Global Profiles (`@profilename`) to manage shared credentials easily.
- Bulk creation, copying, moving, and export/import of nodes.
- **Modern UI**: High-performance terminal experience with `prompt-toolkit`, including:
- Fuzzy search integration with `fzf`.
- Advanced tab completion.
- Syntax highlighting and customizable themes.
- **Automation Engine**: Run parallel tasks and playbooks on multiple devices with variable support.
- **Plugin System**: Build and execute custom Python scripts locally or on a remote gRPC server.
- **gRPC Architecture**: Fully decoupled Client/Server model for distributed management.
- **Privacy & Sync**: Local-first encrypted storage (RSA/OAEP) with optional Google Drive backup.
Connpy is a ssh and telnet connection manager and automation module for Linux, Mac and Docker
## Installation
```bash
pip install connpy
```
### Run it in Windows/Linux using Docker
```bash
### Run it in Windows using docker
```
git clone https://github.com/fluzzi/connpy
cd connpy
docker compose build
# Run it like a native app (completely silent)
docker compose run --rm --remove-orphans connpy-app [command]
# Pro Tip: Add this alias for a 100% native experience from any folder
alias conn='docker compose -f /path/to/connpy/docker-compose.yml run --rm --remove-orphans connpy-app'
docker compose -f path/to/folder/docker-compose.yml build
docker compose -f path/to/folder/docker-compose.yml run -it connpy-app
```
---
## Connection manager
### Features
- You can generate profiles and reference them from nodes using @profilename so you dont
need to edit multiple nodes when changing password or other information.
- Nodes can be stored on @folder or @subfolder@folder to organize your devices. Then can
be referenced using node@subfolder@folder or node@folder
- If you have too many nodes. Get completion script using: conn config --completion.
Or use fzf installing pyfzf and running conn config --fzf true
- Create in bulk, copy, move, export and import nodes for easy management.
- Run automation scripts in network devices.
- use GPT AI to help you manage your devices.
- Add plugins with your own scripts.
- Much more!
## 🔒 Privacy & Integration
### Privacy Policy
Connpy is committed to protecting your privacy:
- **Local Storage**: All server addresses, usernames, and passwords are encrypted and stored **only** on your machine. No data is transmitted to our servers.
- **Data Access**: Data is used solely for managing and automating your connections.
### Google Integration
Used strictly for backup:
- **Backup**: Sync your encrypted configuration with your Google Drive account.
- **Scoped Access**: Connpy only accesses its own backup files.
---
## Usage
```bash
### Usage:
```
usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]
conn {profile,move,copy,list,bulk,export,import,ai,run,api,plugin,config,sync,context} ...
conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config} ...
positional arguments:
node|folder node[@subfolder][@folder]
Connect to specific node or show all matching nodes
[@subfolder][@folder]
Show all available connections globaly or in specified path
```
### Basic Examples:
```bash
# Add a folder and subfolder
conn --add @office
conn --add @datacenter@office
# Add a node with a profile
conn --add server1@datacenter@office --profile @myuser
# Connect to a node (fuzzy match)
conn server1
# Start the AI Copilot
conn ai
# Run a command on all nodes in a folder
conn run @office "uptime"
### Options:
```
-h, --help show this help message and exit
-v, --version Show version
-a, --add Add new node[@subfolder][@folder] or [@subfolder]@folder
-r, --del, --rm Delete node[@subfolder][@folder] or [@subfolder]@folder
-e, --mod, --edit Modify node[@subfolder][@folder]
-s, --show Show node[@subfolder][@folder]
-d, --debug Display all conections steps
-t, --sftp Connects using sftp instead of ssh
```
### 🔑 SSO / OIDC Provider Management
In remote mode, `connpy` supports Single Sign-On (SSO) login. You can manage the configured identity providers (IdPs) directly from the local CLI using the `conn sso` command suite:
### Commands:
```
profile Manage profiles
move(mv) Move node
copy(cp) Copy node
list(ls) List profiles, nodes or folders
bulk Add nodes in bulk
export Export connection folder to Yaml file
import Import connection folder to config from Yaml file
ai Make request to an AI
run Run scripts or commands on nodes
api Start and stop connpy api
plugin Manage plugins
config Manage app config
sync Sync config with Google
```
- **List configured providers**:
```bash
conn sso --list
```
- **Show provider details** (sensitive credentials like secrets are masked):
```bash
conn sso --show <provider_name>
```
- **Add or update a provider** (opens an interactive configuration wizard):
```bash
conn sso --add <provider_name>
```
- **Delete a provider**:
```bash
conn sso --del <provider_name>
```
### Manage profiles:
```
usage: conn profile [-h] (--add | --del | --mod | --show) profile
#### Security Recommendation (Secret Reference Env Vars)
To keep sensitive client secrets or shared secrets out of git-tracked configuration files, you can input a variable name prefixed with a `$` instead of the literal secret during the `conn sso --add` prompts (e.g., `$CONN_SSO_MYPROVIDER_SECRET`). The backend gRPC server will dynamically resolve the value from its environment variables at runtime.
positional arguments:
profile Name of profile to manage
---
options:
-h, --help show this help message and exit
-a, --add Add new profile
-r, --del, --rm Delete profile
-e, --mod, --edit Modify profile
-s, --show Show profile
## 🔌 Plugin System
Connpy supports a robust plugin architecture where scripts can run transparently on a remote gRPC server.
### Structure
Plugins must be Python files containing:
- **Class `Parser`**: Defines `argparse` arguments.
- **Class `Entrypoint`**: Execution logic.
- **Class `Preload`**: (Optional) Hooks and modifications to the core app.
See the [Plugin Requirements section](#plugin-requirements-for-connpy) for full technical details.
---
```
### Examples:
```
conn profile --add office-user
conn --add @office
conn --add @datacenter@office
conn --add server@datacenter@office
conn --add pc@office
conn --show server@datacenter@office
conn pc@office
conn server
```
## Plugin Requirements for Connpy
### Remote Plugin Execution
When Connpy operates in remote mode, plugins are executed **transparently on the server**:
- The client automatically downloads the plugin source code (`Parser` class context) to generate the local `argparse` structure and provide autocompletion.
- The execution phase (`Entrypoint` class) is redirected via gRPC streams to execute in the server's memory.
- You can manage remote plugins using the `--remote` flag.
### General Structure
- The plugin script must define specific classes:
1. **Class `Parser`**: Handles `argparse.ArgumentParser` initialization.
2. **Class `Entrypoint`**: Main execution logic (receives `args`, `parser`, and `connapp`).
3. **Class `Preload`**: (Optional) For modifying core app behavior or registering hooks.
- The plugin script must be a Python file.
- Only the following top-level elements are allowed in the plugin script:
- Class definitions
- Function definitions
- Import statements
- The `if __name__ == "__main__":` block for standalone execution
- Pass statements
### Specific Class Requirements
- The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:
1. **Class `Parser`**:
- **Purpose**: Handles parsing of command-line arguments.
- **Requirements**:
- Must contain only one method: `__init__`.
- The `__init__` method must initialize at least two attributes:
- `self.parser`: An instance of `argparse.ArgumentParser`.
- `self.description`: A string containing the description of the parser.
2. **Class `Entrypoint`**:
- **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.
- **Requirements**:
- Must have an `__init__` method that accepts exactly three parameters besides `self`:
- `args`: Arguments passed to the plugin.
- The parser instance (typically `self.parser` from the `Parser` class).
- The Connapp instance to interact with the Connpy app.
3. **Class `Preload`**:
- **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.
- **Requirements**:
- Contains at least an `__init__` method that accepts parameter connapp besides `self`.
### Class Dependencies and Combinations
- **Dependencies**:
- `Parser` and `Entrypoint` are interdependent and must both be present if one is included.
- `Preload` is independent and may exist alone or alongside the other classes.
- **Valid Combinations**:
- `Parser` and `Entrypoint` together.
- `Preload` alone.
- All three classes (`Parser`, `Entrypoint`, `Preload`).
### Preload Modifications and Hooks
You can customize the behavior of core classes using hooks:
- **`modify(method)`**: Alter class instances (e.g., `connapp.config`, `connapp.ai`).
- **`register_pre_hook(method)`**: Logic to run before a method execution.
- **`register_post_hook(method)`**: Logic to run after a method execution.
### Command Completion Support
Plugins can provide intelligent tab completion:
1. **Tree-based Completion (Recommended)**: Define `_connpy_tree(info)` returning a navigation dictionary.
2. **Legacy Completion**: Define `_connpy_completion(wordsnumber, words, info)`.
In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.
---
#### Modifying Classes with `modify`
The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.
## ⚙️ gRPC Service Architecture
Connpy can operate in a decoupled mode:
1. **Start the API (Server)**: `conn api -s 50051`
2. **Configure the Client**:
```bash
conn config --service-mode remote
conn config --remote-host localhost:50051
```
All inventory management and execution will now happen on the server.
- **Usage**: Modify a class to include additional configurations or changes
- **Modify Method Signature**:
- `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.
- **Modification Method Signature**:
- **Arguments**:
- `cls`: This function accepts a single argument, the class instance, which it then modifies.
- **Modifiable Classes**:
- `connapp.config`
- `connapp.node`
- `connapp.nodes`
- `connapp.ai`
- ```python
def modify_config(cls):
# Example modification: adding a new attribute or modifying an existing one
cls.new_attribute = 'New Value'
---
class Preload:
def __init__(self, connapp):
# Applying modification to the config class instance
connapp.config.modify(modify_config)
```
## 🐍 Automation Module (API)
You can use `connpy` as a Python library for your own scripts.
#### Implementing Method Hooks
There are 2 methods that allows you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.
### Basic Execution
```python
- **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.
- **Registration Methods Signature**:
- `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.
- `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.
- **Method Signatures for Pre-Hooks**
- `pre_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.
- **Return**:
- Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.
- **Method Signatures for Post-Hooks**:
- `post_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.
- `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.
- **Return**:
- Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result.
- ```python
def pre_processing_hook(*args, **kwargs):
print("Pre-processing logic here")
# Modify arguments or perform any checks
return args, kwargs # Return modified or unmodified args and kwargs
def post_processing_hook(*args, **kwargs):
print("Post-processing logic here")
# Modify the result or perform any final logging or cleanup
return kwargs["result"] # Return the modified or unmodified result
class Preload:
def __init__(self, connapp):
# Registering a pre-hook
connapp.ai.some_method.register_pre_hook(pre_processing_hook)
# Registering a post-hook
connapp.node.another_method.register_post_hook(post_processing_hook)
```
### Executable Block
- The plugin script can include an executable block:
- `if __name__ == "__main__":`
- This block allows the plugin to be run as a standalone script for testing or independent use.
### Script Verification
- The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards.
- Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.
### Example Script
For a practical example of how to write a compatible plugin script, please refer to the following example:
[Example Plugin Script](https://github.com/fluzzi/awspy)
This script demonstrates the required structure and implementation details according to the plugin system's standards.
## Automation module usage
### Standalone module
```
import connpy
router = connpy.node("uniqueName", "1.1.1.1", user="admin")
router.run(["show ip int brief"])
router = connpy.node("uniqueName","ip/host", user="username", password="password")
router.run(["term len 0","show run"])
print(router.output)
hasip = router.test("show ip int brief","1.1.1.1")
if hasip:
print("Router has ip 1.1.1.1")
else:
print("router does not have ip 1.1.1.1")
```
### Parallel Tasks with Variables
```python
### Using manager configuration
```
import connpy
conf = connpy.configfile()
device = conf.getitem("router@office")
router = connpy.node("unique name", **device, config=conf)
result = router.run("show ip int brief")
print(result)
```
### Running parallel tasks on multiple devices
```
import connpy
conf = connpy.configfile()
#You can get the nodes from the config from a folder and fitlering in it
nodes = conf.getitem("@office", ["router1", "router2", "router3"])
#You can also get each node individually:
nodes = {}
nodes["router1"] = conf.getitem("router1@office")
nodes["router2"] = conf.getitem("router2@office")
nodes["router10"] = conf.getitem("router10@datacenter")
#Also, you can create the nodes manually:
nodes = {}
nodes["router1"] = {"host": "1.1.1.1", "user": "user", "password": "password1"}
nodes["router2"] = {"host": "1.1.1.2", "user": "user", "password": "password2"}
nodes["router3"] = {"host": "1.1.1.2", "user": "user", "password": "password3"}
#Finally you run some tasks on the nodes
mynodes = connpy.nodes(nodes, config = conf)
result = mynodes.test(["show ip int br"], "1.1.1.2")
for i in result:
print("---" + i + "---")
print(result[i])
print()
# Or for one specific node
mynodes.router1.run(["term len 0". "show run"], folder = "/home/user/logs")
```
### Using variables
```
import connpy
config = connpy.configfile()
nodes = config.getitem("@office", ["router1", "router2"])
routers = connpy.nodes(nodes, config=config)
variables = {
"router1@office": {"id": "1"},
"__global__": {"mask": "255.255.255.0"}
}
routers.run(["interface lo{id}", "ip address 10.0.0.{id} {mask}"], variables)
nodes = config.getitem("@office", ["router1", "router2", "router3"])
commands = []
commands.append("config t")
commands.append("interface lo {id}")
commands.append("ip add {ip} {mask}")
commands.append("end")
variables = {}
variables["router1@office"] = {"ip": "10.57.57.1"}
variables["router2@office"] = {"ip": "10.57.57.2"}
variables["router3@office"] = {"ip": "10.57.57.3"}
variables["__global__"] = {"id": "57"}
variables["__global__"]["mask"] = "255.255.255.255"
expected = "!"
routers = connpy.nodes(nodes, config = config)
routers.run(commands, variables)
routers.test("ping {ip}", expected, variables)
for key in routers.result:
print(key, ' ---> ', ("pass" if routers.result[key] else "fail"))
```
### Using AI
```
### AI Programmatic Use
```python
import connpy
myai = connpy.ai(connpy.configfile())
response = myai.ask("What is the status of the BGP neighbors in the office?")
conf = connpy.configfile()
organization = 'openai-org'
api_key = "openai-key"
myia = connpy.ai(conf, organization, api_key)
input = "go to router 1 and get me the full configuration"
result = myia.ask(input, dryrun = False)
print(result)
```
## http API
With the Connpy API you can run commands on devices using http requests
### 1. List Nodes
**Endpoint**: `/list_nodes`
**Method**: `POST`
**Description**: This route returns a list of nodes. It can also filter the list based on a given keyword.
#### Request Body:
```json
{
"filter": "<keyword>"
}
```
* `filter` (optional): A keyword to filter the list of nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered list of nodes.
---
*For detailed developer notes and plugin hooks documentation, see the [Documentation](https://fluzzi.github.io/connpy/).*
## 📜 License
[PolyForm Noncommercial 1.0.0](LICENSE)
### 2. Get Nodes
**Endpoint**: `/get_nodes`
**Method**: `POST`
**Description**: This route returns a dictionary of nodes with all their attributes. It can also filter the nodes based on a given keyword.
#### Request Body:
```json
{
"filter": "<keyword>"
}
```
* `filter` (optional): A keyword to filter the nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered nodes.
---
### 3. Run Commands
**Endpoint**: `/run_commands`
**Method**: `POST`
**Description**: This route runs commands on selected nodes based on the provided action, nodes, and commands. It also supports executing tests by providing expected results.
#### Request Body:
```json
{
"action": "<action>",
"nodes": "<nodes>",
"commands": "<commands>",
"expected": "<expected>",
"options": "<options>"
}
```
* `action` (required): The action to be performed. Possible values: `run` or `test`.
* `nodes` (required): A list of nodes or a single node on which the commands will be executed. The nodes can be specified as individual node names or a node group with the `@` prefix. Node groups can also be specified as arrays with a list of nodes inside the group.
* `commands` (required): A list of commands to be executed on the specified nodes.
* `expected` (optional, only used when the action is `test`): A single expected result for the test.
* `options` (optional): Array to pass options to the run command, options are: `prompt`, `parallel`, `timeout`
#### Response:
- A JSON object with the results of the executed commands on the nodes.
---
### 4. Ask AI
**Endpoint**: `/ask_ai`
**Method**: `POST`
**Description**: This route sends to chatgpt IA a request that will parse it into an understandable output for the application and then run the request.
#### Request Body:
```json
{
"input": "<user input request>",
"dryrun": true or false
}
```
* `input` (required): The user input requesting the AI to perform an action on some devices or get the devices list.
* `dryrun` (optional): If set to true, it will return the parameters to run the request but it won't run it. default is false.
#### Response:
- A JSON array containing the action to run and the parameters and the result of the action.
+371 -184
View File
@@ -1,212 +1,402 @@
#!/usr/bin/env python3
'''
<p align="center">
<img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
</p>
## Connection manager
Connpy is a connection manager that allows you to store nodes to connect them fast and password free.
# Connpy
[![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/l/connpy.svg?style=flat-square)](https://github.com/fluzzi/connpy/blob/main/LICENSE)
[![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
### Features
- You can generate profiles and reference them from nodes using @profilename so you dont
need to edit multiple nodes when changing password or other information.
- Nodes can be stored on @folder or @subfolder@folder to organize your devices. Then can
be referenced using node@subfolder@folder or node@folder
- If you have too many nodes. Get completion script using: conn config --completion.
Or use fzf installing pyfzf and running conn config --fzf true
- Create in bulk, copy, move, export and import nodes for easy management.
- Run automation scripts in network devices.
- use GPT AI to help you manage your devices.
- Add plugins with your own scripts.
- Much more!
**Connpy** is a powerful Connection Manager and Network Automation Platform for Linux, Mac, and Docker. It provides a unified interface for **SSH, SFTP, Telnet, kubectl, Docker pods, and AWS SSM**.
The v6 release introduces the **AI Copilot**, an interactive terminal assistant that understands your network context and helps you manage your infrastructure more intelligently.
## 🤖 AI Copilot (New in v6)
The AI Copilot is deeply integrated into your terminal workflow:
- **Terminal Context Awareness**: The Copilot can "see" your screen output, helping you diagnose errors or analyze command results in real-time.
- **Dynamic Context Selection**: Flexibly select single, range, or line-based terminal blocks to feed the Copilot, filtering out interactive scrolling garbage automatically (e.g., Cisco IOS/XR scrolling, paginators).
- **Hybrid Multi-Agent System**: Automatically escalates complex tasks between the **Network Engineer** (execution) and the **Network Architect** (strategy).
- **MCP Integration**: Dynamically load tools from external providers (6WIND, AWS, etc.) via the Model Context Protocol.
- **Flexible Auth & Keyless AI**: Support for advanced LiteLLM credentials (`--engineer-auth` / `--architect-auth`) allowing keyless local models (Ollama), cloud engines (Vertex AI), or custom endpoints.
- **Enhanced Session Management**: Uniquely generated sessions, robust pagination, and interactive styling translating prompt themes directly to terminal escapes.
- **Semantic Prompt Integration**: Emit standard OSC prompt sequences (`\x1b]133;B`) for real-time remote/web front-end command tracking.
- **Interactive Chat**: Launch with `conn ai` for a collaborative troubleshooting session.
## Core Features
- **Multi-Protocol**: Native support for SSH, SFTP, Telnet, kubectl, Docker exec, and AWS SSM.
- **Context Management**: Set regex-based contexts to manage specific nodes across different environments (work, home, clients).
- **Advanced Inventory**:
- Organize nodes in folders (`@folder`) and subfolders (`@subfolder@folder`).
- Use Global Profiles (`@profilename`) to manage shared credentials easily.
- Bulk creation, copying, moving, and export/import of nodes.
- **Modern UI**: High-performance terminal experience with `prompt-toolkit`, including:
- Fuzzy search integration with `fzf`.
- Advanced tab completion.
- Syntax highlighting and customizable themes.
- **Automation Engine**: Run parallel tasks and playbooks on multiple devices with variable support.
- **Plugin System**: Build and execute custom Python scripts locally or on a remote gRPC server.
- **gRPC Architecture**: Fully decoupled Client/Server model for distributed management.
- **Privacy & Sync**: Local-first encrypted storage (RSA/OAEP) with optional Google Drive backup.
## Installation
```bash
pip install connpy
### Usage
```
### Run it in Windows/Linux using Docker
```bash
git clone https://github.com/fluzzi/connpy
cd connpy
docker compose build
# Run it like a native app (completely silent)
docker compose run --rm --remove-orphans connpy-app [command]
# Pro Tip: Add this alias for a 100% native experience from any folder
alias conn='docker compose -f /path/to/connpy/docker-compose.yml run --rm --remove-orphans connpy-app'
```
---
## 🔒 Privacy & Integration
### Privacy Policy
Connpy is committed to protecting your privacy:
- **Local Storage**: All server addresses, usernames, and passwords are encrypted and stored **only** on your machine. No data is transmitted to our servers.
- **Data Access**: Data is used solely for managing and automating your connections.
### Google Integration
Used strictly for backup:
- **Backup**: Sync your encrypted configuration with your Google Drive account.
- **Scoped Access**: Connpy only accesses its own backup files.
---
## Usage
```bash
usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]
conn {profile,move,copy,list,bulk,export,import,ai,run,api,plugin,config,sync,context} ...
conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config} ...
positional arguments:
node|folder node[@subfolder][@folder]
Connect to specific node or show all matching nodes
[@subfolder][@folder]
Show all available connections globaly or in specified path
Options:
-h, --help show this help message and exit
-v, --version Show version
-a, --add Add new node[@subfolder][@folder] or [@subfolder]@folder
-r, --del, --rm Delete node[@subfolder][@folder] or [@subfolder]@folder
-e, --mod, --edit Modify node[@subfolder][@folder]
-s, --show Show node[@subfolder][@folder]
-d, --debug Display all conections steps
-t, --sftp Connects using sftp instead of ssh
Commands:
profile Manage profiles
move(mv) Move node
copy(cp) Copy node
list(ls) List profiles, nodes or folders
bulk Add nodes in bulk
export Export connection folder to Yaml file
import Import connection folder to config from Yaml file
ai Make request to an AI
run Run scripts or commands on nodes
api Start and stop connpy api
plugin Manage plugins
config Manage app config
sync Sync config with Google
```
### Basic Examples:
```bash
# Add a folder and subfolder
conn --add @office
conn --add @datacenter@office
### Manage profiles
```
usage: conn profile [-h] (--add | --del | --mod | --show) profile
# Add a node with a profile
conn --add server1@datacenter@office --profile @myuser
positional arguments:
profile Name of profile to manage
# Connect to a node (fuzzy match)
conn server1
options:
-h, --help show this help message and exit
-a, --add Add new profile
-r, --del, --rm Delete profile
-e, --mod, --edit Modify profile
-s, --show Show profile
# Start the AI Copilot
conn ai
# Run a command on all nodes in a folder
conn run @office "uptime"
```
### 🔑 SSO / OIDC Provider Management
In remote mode, `connpy` supports Single Sign-On (SSO) login. You can manage the configured identity providers (IdPs) directly from the local CLI using the `conn sso` command suite:
- **List configured providers**:
```bash
conn sso --list
```
- **Show provider details** (sensitive credentials like secrets are masked):
```bash
conn sso --show <provider_name>
```
- **Add or update a provider** (opens an interactive configuration wizard):
```bash
conn sso --add <provider_name>
```
- **Delete a provider**:
```bash
conn sso --del <provider_name>
```
#### Security Recommendation (Secret Reference Env Vars)
To keep sensitive client secrets or shared secrets out of git-tracked configuration files, you can input a variable name prefixed with a `$` instead of the literal secret during the `conn sso --add` prompts (e.g., `$CONN_SSO_MYPROVIDER_SECRET`). The backend gRPC server will dynamically resolve the value from its environment variables at runtime.
---
### Examples
```
conn profile --add office-user
conn --add @office
conn --add @datacenter@office
conn --add server@datacenter@office
conn --add pc@office
conn --show server@datacenter@office
conn pc@office
conn server
```
## Plugin Requirements for Connpy
### Remote Plugin Execution
When Connpy operates in remote mode, plugins are executed **transparently on the server**:
- The client automatically downloads the plugin source code (`Parser` class context) to generate the local `argparse` structure and provide autocompletion.
- The execution phase (`Entrypoint` class) is redirected via gRPC streams to execute in the server's memory.
- You can manage remote plugins using the `--remote` flag.
### General Structure
- The plugin script must define specific classes:
1. **Class `Parser`**: Handles `argparse.ArgumentParser` initialization.
2. **Class `Entrypoint`**: Main execution logic (receives `args`, `parser`, and `connapp`).
3. **Class `Preload`**: (Optional) For modifying core app behavior or registering hooks.
- The plugin script must be a Python file.
- Only the following top-level elements are allowed in the plugin script:
- Class definitions
- Function definitions
- Import statements
- The `if __name__ == "__main__":` block for standalone execution
- Pass statements
### Specific Class Requirements
- The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:
1. **Class `Parser`**:
- **Purpose**: Handles parsing of command-line arguments.
- **Requirements**:
- Must contain only one method: `__init__`.
- The `__init__` method must initialize at least two attributes:
- `self.parser`: An instance of `argparse.ArgumentParser`.
- `self.description`: A string containing the description of the parser.
2. **Class `Entrypoint`**:
- **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.
- **Requirements**:
- Must have an `__init__` method that accepts exactly three parameters besides `self`:
- `args`: Arguments passed to the plugin.
- The parser instance (typically `self.parser` from the `Parser` class).
- The Connapp instance to interact with the Connpy app.
3. **Class `Preload`**:
- **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.
- **Requirements**:
- Contains at least an `__init__` method that accepts parameter connapp besides `self`.
### Class Dependencies and Combinations
- **Dependencies**:
- `Parser` and `Entrypoint` are interdependent and must both be present if one is included.
- `Preload` is independent and may exist alone or alongside the other classes.
- **Valid Combinations**:
- `Parser` and `Entrypoint` together.
- `Preload` alone.
- All three classes (`Parser`, `Entrypoint`, `Preload`).
### Preload Modifications and Hooks
You can customize the behavior of core classes using hooks:
- **`modify(method)`**: Alter class instances (e.g., `connapp.config`, `connapp.ai`).
- **`register_pre_hook(method)`**: Logic to run before a method execution.
- **`register_post_hook(method)`**: Logic to run after a method execution.
### Command Completion Support
Plugins can provide intelligent tab completion:
1. **Tree-based Completion (Recommended)**: Define `_connpy_tree(info)` returning a navigation dictionary.
2. **Legacy Completion**: Define `_connpy_completion(wordsnumber, words, info)`.
In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.
---
#### Modifying Classes with `modify`
The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.
## ⚙️ gRPC Service Architecture
Connpy can operate in a decoupled mode:
1. **Start the API (Server)**: `conn api -s 50051`
2. **Configure the Client**:
```bash
conn config --service-mode remote
conn config --remote-host localhost:50051
```
All inventory management and execution will now happen on the server.
- **Usage**: Modify a class to include additional configurations or changes
- **Modify Method Signature**:
- `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.
- **Modification Method Signature**:
- **Arguments**:
- `cls`: This function accepts a single argument, the class instance, which it then modifies.
- **Modifiable Classes**:
- `connapp.config`
- `connapp.node`
- `connapp.nodes`
- `connapp.ai`
- ```python
def modify_config(cls):
# Example modification: adding a new attribute or modifying an existing one
cls.new_attribute = 'New Value'
---
class Preload:
def __init__(self, connapp):
# Applying modification to the config class instance
connapp.config.modify(modify_config)
```
## 🐍 Automation Module (API)
You can use `connpy` as a Python library for your own scripts.
#### Implementing Method Hooks
There are 2 methods that allows you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.
### Basic Execution
```python
import connpy
router = connpy.node("uniqueName", "1.1.1.1", user="admin")
router.run(["show ip int brief"])
print(router.output)
- **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.
- **Registration Methods Signature**:
- `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.
- `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.
- **Method Signatures for Pre-Hooks**
- `pre_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.
- **Return**:
- Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.
- **Method Signatures for Post-Hooks**:
- `post_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.
- `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.
- **Return**:
- Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result.
- ```python
def pre_processing_hook(*args, **kwargs):
print("Pre-processing logic here")
# Modify arguments or perform any checks
return args, kwargs # Return modified or unmodified args and kwargs
def post_processing_hook(*args, **kwargs):
print("Post-processing logic here")
# Modify the result or perform any final logging or cleanup
return kwargs["result"] # Return the modified or unmodified result
class Preload:
def __init__(self, connapp):
# Registering a pre-hook
connapp.ai.some_method.register_pre_hook(pre_processing_hook)
# Registering a post-hook
connapp.node.another_method.register_post_hook(post_processing_hook)
```
### Executable Block
- The plugin script can include an executable block:
- `if __name__ == "__main__":`
- This block allows the plugin to be run as a standalone script for testing or independent use.
### Script Verification
- The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards.
- Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.
-
### Example Script
For a practical example of how to write a compatible plugin script, please refer to the following example:
[Example Plugin Script](https://github.com/fluzzi/awspy)
This script demonstrates the required structure and implementation details according to the plugin system's standards.
## http API
With the Connpy API you can run commands on devices using http requests
### 1. List Nodes
**Endpoint**: `/list_nodes`
**Method**: `POST`
**Description**: This route returns a list of nodes. It can also filter the list based on a given keyword.
#### Request Body:
```json
{
"filter": "<keyword>"
}
```
### Parallel Tasks with Variables
```python
* `filter` (optional): A keyword to filter the list of nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered list of nodes.
---
### 2. Get Nodes
**Endpoint**: `/get_nodes`
**Method**: `POST`
**Description**: This route returns a dictionary of nodes with all their attributes. It can also filter the nodes based on a given keyword.
#### Request Body:
```json
{
"filter": "<keyword>"
}
```
* `filter` (optional): A keyword to filter the nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered nodes.
---
### 3. Run Commands
**Endpoint**: `/run_commands`
**Method**: `POST`
**Description**: This route runs commands on selected nodes based on the provided action, nodes, and commands. It also supports executing tests by providing expected results.
#### Request Body:
```json
{
"action": "<action>",
"nodes": "<nodes>",
"commands": "<commands>",
"expected": "<expected>",
"options": "<options>"
}
```
* `action` (required): The action to be performed. Possible values: `run` or `test`.
* `nodes` (required): A list of nodes or a single node on which the commands will be executed. The nodes can be specified as individual node names or a node group with the `@` prefix. Node groups can also be specified as arrays with a list of nodes inside the group.
* `commands` (required): A list of commands to be executed on the specified nodes.
* `expected` (optional, only used when the action is `test`): A single expected result for the test.
* `options` (optional): Array to pass options to the run command, options are: `prompt`, `parallel`, `timeout`
#### Response:
- A JSON object with the results of the executed commands on the nodes.
---
### 4. Ask AI
**Endpoint**: `/ask_ai`
**Method**: `POST`
**Description**: This route sends to chatgpt IA a request that will parse it into an understandable output for the application and then run the request.
#### Request Body:
```json
{
"input": "<user input request>",
"dryrun": true or false
}
```
* `input` (required): The user input requesting the AI to perform an action on some devices or get the devices list.
* `dryrun` (optional): If set to true, it will return the parameters to run the request but it won't run it. default is false.
#### Response:
- A JSON array containing the action to run and the parameters and the result of the action.
## Automation module
The automation module
### Standalone module
```
import connpy
router = connpy.node("uniqueName","ip/host", user="user", password="pass")
router.run(["term len 0","show run"])
print(router.output)
hasip = router.test("show ip int brief","1.1.1.1")
if hasip:
print("Router has ip 1.1.1.1")
else:
print("router does not have ip 1.1.1.1")
```
### Using manager configuration
```
import connpy
conf = connpy.configfile()
device = conf.getitem("server@office")
server = connpy.node("unique name", **device, config=conf)
result = server.run(["cd /", "ls -la"])
print(result)
```
### Running parallel tasks
```
import connpy
conf = connpy.configfile()
#You can get the nodes from the config from a folder and fitlering in it
nodes = conf.getitem("@office", ["router1", "router2", "router3"])
#You can also get each node individually:
nodes = {}
nodes["router1"] = conf.getitem("router1@office")
nodes["router2"] = conf.getitem("router2@office")
nodes["router10"] = conf.getitem("router10@datacenter")
#Also, you can create the nodes manually:
nodes = {}
nodes["router1"] = {"host": "1.1.1.1", "user": "user", "password": "pass1"}
nodes["router2"] = {"host": "1.1.1.2", "user": "user", "password": "pass2"}
nodes["router3"] = {"host": "1.1.1.2", "user": "user", "password": "pass3"}
#Finally you run some tasks on the nodes
mynodes = connpy.nodes(nodes, config = conf)
result = mynodes.test(["show ip int br"], "1.1.1.2")
for i in result:
print("---" + i + "---")
print(result[i])
print()
# Or for one specific node
mynodes.router1.run(["term len 0". "show run"], folder = "/home/user/logs")
```
### Using variables
```
import connpy
config = connpy.configfile()
nodes = config.getitem("@office", ["router1", "router2"])
routers = connpy.nodes(nodes, config=config)
variables = {
"router1@office": {"id": "1"},
"__global__": {"mask": "255.255.255.0"}
}
routers.run(["interface lo{id}", "ip address 10.0.0.{id} {mask}"], variables)
nodes = config.getitem("@office", ["router1", "router2", "router3"])
commands = []
commands.append("config t")
commands.append("interface lo {id}")
commands.append("ip add {ip} {mask}")
commands.append("end")
variables = {}
variables["router1@office"] = {"ip": "10.57.57.1"}
variables["router2@office"] = {"ip": "10.57.57.2"}
variables["router3@office"] = {"ip": "10.57.57.3"}
variables["__global__"] = {"id": "57"}
variables["__global__"]["mask"] = "255.255.255.255"
expected = "!"
routers = connpy.nodes(nodes, config = config)
routers.run(commands, variables)
routers.test("ping {ip}", expected, variables)
for key in routers.result:
print(key, ' ---> ', ("pass" if routers.result[key] else "fail"))
```
### Using AI
```
### AI Programmatic Use
```python
import connpy
myai = connpy.ai(connpy.configfile())
response = myai.ask("What is the status of the BGP neighbors in the office?")
conf = connpy.configfile()
organization = 'openai-org'
api_key = "openai-key"
myia = connpy.ai(conf, organization, api_key)
input = "go to router 1 and get me the full configuration"
result = myia.ask(input, dryrun = False)
print(result)
```
---
*For detailed developer notes and plugin hooks documentation, see the [Documentation](https://fluzzi.github.io/connpy/).*
## 📜 License
[PolyForm Noncommercial 1.0.0](LICENSE)
'''
from .core import node,nodes
from .configfile import configfile
@@ -215,9 +405,9 @@ from .api import *
from .ai import ai
from .plugins import Plugins
from ._version import __version__
from . import printer
from pkg_resources import get_distribution
__all__ = ["node", "nodes", "configfile", "connapp", "ai", "Plugins", "printer"]
__all__ = ["node", "nodes", "configfile", "connapp", "ai", "Plugins"]
__author__ = "Federico Luzzi"
__pdoc__ = {
'core': False,
@@ -232,8 +422,5 @@ __pdoc__ = {
'node.deferred_class_hooks': False,
'nodes.deferred_class_hooks': False,
'connapp': False,
'connapp.encrypt': True,
'printer': False,
'tests': False
'connapp.encrypt': True
}
+2 -1
View File
@@ -1 +1,2 @@
__version__ = "6.0.3"
__version__ = "4.0.0"
+426 -1897
View File
File diff suppressed because it is too large Load Diff
+152 -113
View File
@@ -1,147 +1,186 @@
from flask import Flask, request, jsonify
from connpy import configfile, node, nodes, hooks
from connpy.ai import ai as myai
from waitress import serve
import os
import signal
import time
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
os.environ["GRPC_VERBOSITY"] = "NONE"
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
from connpy import hooks, printer
from connpy.configfile import configfile
app = Flask(__name__)
conf = configfile()
PID_FILE1 = "/run/connpy.pid"
PID_FILE2 = "/tmp/connpy.pid"
def _wait_for_termination():
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
pass
@app.route("/")
def root():
return jsonify({
'message': 'Welcome to Connpy api',
'version': '1.0',
'documentation': 'https://fluzzi.github.io/connpy/'
})
@app.route("/list_nodes", methods=["POST"])
def list_nodes():
conf = app.custom_config
case = conf.config["case"]
try:
data = request.get_json()
filter = data["filter"]
if not case:
if isinstance(filter, list):
filter = [item.lower() for item in filter]
else:
filter = filter.lower()
output = conf._getallnodes(filter)
except:
output = conf._getallnodes()
return jsonify(output)
@app.route("/get_nodes", methods=["POST"])
def get_nodes():
conf = app.custom_config
case = conf.config["case"]
try:
data = request.get_json()
filter = data["filter"]
if not case:
if isinstance(filter, list):
filter = [item.lower() for item in filter]
else:
filter = filter.lower()
output = conf._getallnodesfull(filter)
except:
output = conf._getallnodesfull()
return jsonify(output)
@app.route("/ask_ai", methods=["POST"])
def ask_ai():
conf = app.custom_config
data = request.get_json()
input = data["input"]
if "dryrun" in data:
dryrun = data["dryrun"]
else:
dryrun = False
if "chat_history" in data:
chat_history = data["chat_history"]
else:
chat_history = None
ai = myai(conf)
return ai.ask(input, dryrun, chat_history)
@app.route("/confirm", methods=["POST"])
def confirm():
conf = app.custom_config
data = request.get_json()
input = data["input"]
ai = myai(conf)
return str(ai.confirm(input))
@app.route("/run_commands", methods=["POST"])
def run_commands():
conf = app.custom_config
data = request.get_json()
case = conf.config["case"]
mynodes = {}
args = {}
try:
action = data["action"]
nodelist = data["nodes"]
args["commands"] = data["commands"]
if action == "test":
args["expected"] = data["expected"]
except KeyError as e:
error = "'{}' is mandatory".format(e.args[0])
return({"DataError": error})
if isinstance(nodelist, list):
mynodes = conf.getitems(nodelist)
else:
if not case:
nodelist = nodelist.lower()
if nodelist.startswith("@"):
mynodes = conf.getitem(nodelist)
else:
mynodes[nodelist] = conf.getitem(nodelist)
mynodes = nodes(mynodes, config=conf)
try:
args["vars"] = data["vars"]
except:
pass
try:
options = data["options"]
thisoptions = {k: v for k, v in options.items() if k in ["prompt", "parallel", "timeout"]}
args.update(thisoptions)
except:
options = None
if action == "run":
output = mynodes.run(**args)
elif action == "test":
output = {}
output["result"] = mynodes.test(**args)
output["output"] = mynodes.output
else:
error = "Wrong action '{}'".format(action)
return({"DataError": error})
return output
@hooks.MethodHook
def stop_api():
# Read the process ID (pid) from the file
try:
with open(PID_FILE1, "r") as f:
pid = int(f.readline().strip())
port_line = f.readline().strip()
port = int(port_line) if port_line else None
PID_FILE = PID_FILE1
except (FileNotFoundError, ValueError, OSError):
port = int(f.readline().strip())
PID_FILE=PID_FILE1
except:
try:
with open(PID_FILE2, "r") as f:
pid = int(f.readline().strip())
port_line = f.readline().strip()
port = int(port_line) if port_line else None
PID_FILE = PID_FILE2
except (FileNotFoundError, ValueError, OSError):
printer.warning("Connpy API server is not running.")
return None
port = int(f.readline().strip())
PID_FILE=PID_FILE2
except:
print("Connpy api server is not running.")
return
# Send a SIGTERM signal to the process
try:
os.kill(pid, signal.SIGTERM)
except OSError as e:
printer.warning(f"Process kill failed (maybe already dead): {e}")
except:
pass
# Delete the PID file
os.remove(PID_FILE)
printer.info(f"Server with process ID {pid} stopped.")
print(f"Server with process ID {pid} stopped.")
return port
def debug_api(port=8048, config=None):
# Check if already running via PID file verification
for pid_file in [PID_FILE1, PID_FILE2]:
if os.path.exists(pid_file):
try:
with open(pid_file, "r") as f:
pid = int(f.readline().strip())
os.kill(pid, 0)
# If we get here, process exists
printer.info(f"API is already running (PID {pid})")
return
except (ValueError, OSError, ProcessLookupError):
# Stale PID file, ignore here
pass
@hooks.MethodHook
def debug_api(port=8048):
app.custom_config = configfile()
app.run(debug=True, port=port)
# Create PID file for the debug process
written_pid_file = None
my_pid = os.getpid()
try:
with open(PID_FILE1, "w") as f:
f.write(str(my_pid) + "\n" + str(port))
written_pid_file = PID_FILE1
except OSError:
try:
with open(PID_FILE2, "w") as f:
f.write(str(my_pid) + "\n" + str(port))
written_pid_file = PID_FILE2
except OSError:
pass
try:
from .grpc_layer.server import serve
conf = config or configfile()
server = serve(conf, port=port, debug=True)
printer.info(f"gRPC Server running in debug mode on port {port}...")
_wait_for_termination()
server.stop(0)
from .ai import cleanup
cleanup()
finally:
if written_pid_file and os.path.exists(written_pid_file):
try:
os.remove(written_pid_file)
except OSError:
pass
def start_server(port=8048, config=None):
try:
import sys
# Ensure project root is in path for the child process
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if base_dir not in sys.path:
sys.path.insert(0, base_dir)
from connpy.grpc_layer.server import serve
conf = config or configfile()
server = serve(conf, port=port, debug=False)
_wait_for_termination()
server.stop(0)
from .ai import cleanup
cleanup()
except Exception as e:
printer.error(f"Background API failed to start: {e}")
os._exit(1)
def start_api(port=8048, config=None):
# Check if already running via PID file verification
for pid_file in [PID_FILE1, PID_FILE2]:
if os.path.exists(pid_file):
try:
with open(pid_file, "r") as f:
pid = int(f.readline().strip())
os.kill(pid, 0)
# If we get here, process exists
printer.info(f"API is already running (PID {pid})")
return
except (ValueError, OSError, ProcessLookupError):
# Stale PID file, ignore here, start_api will overwrite
pass
@hooks.MethodHook
def start_server(port=8048):
app.custom_config = configfile()
serve(app, host='0.0.0.0', port=port)
@hooks.MethodHook
def start_api(port=8048):
if os.path.exists(PID_FILE1) or os.path.exists(PID_FILE2):
print("Connpy server is already running.")
return
pid = os.fork()
if pid == 0:
# Child process: detached from terminal
os.setsid()
start_server(port, config=config)
start_server(port)
else:
# Parent process: record PID and exit
try:
with open(PID_FILE1, "w") as f:
f.write(str(pid) + "\n" + str(port))
except OSError:
except:
try:
with open(PID_FILE2, "w") as f:
f.write(str(pid) + "\n" + str(port))
except OSError:
printer.error("Couldn't create PID file.")
exit(1)
printer.start(f"gRPC Server started with process ID {pid} on port {port}")
except:
print("Cound't create PID file")
return
print(f'Server is running with process ID {pid} in port {port}')
-11
View File
@@ -1,11 +0,0 @@
from .node_handler import NodeHandler
from .profile_handler import ProfileHandler
from .config_handler import ConfigHandler
from .run_handler import RunHandler
from .ai_handler import AIHandler
from .api_handler import APIHandler
from .plugin_handler import PluginHandler
from .import_export_handler import ImportExportHandler
from .context_handler import ContextHandler
from .sso_handler import SSOHandler
-295
View File
@@ -1,295 +0,0 @@
import sys
from rich.panel import Panel
from rich.markdown import Markdown
from rich.rule import Rule
from rich.prompt import Prompt
from .. import printer
console = printer.console
mdprint = console.print
class AIHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
if args.list_sessions:
limit = 20 if not getattr(args, "all", False) else None
sessions, total = self.app.services.ai.list_sessions(limit=limit)
if not sessions:
printer.info("No saved AI sessions found.")
return
columns = ["ID", "Title", "Created At", "Model"]
rows = [[s["id"], s["title"], s["created_at"], s["model"]] for s in sessions]
title = "AI Persisted Sessions"
if limit and total > limit:
title += f" (Showing last {limit} of {total})"
printer.table(title, columns, rows)
if limit and total > limit:
printer.info(f"Use '--list --all' to see all {total} sessions.")
return
if args.delete_session:
try:
self.app.services.ai.delete_session(args.delete_session[0])
printer.success(f"Session {args.delete_session[0]} deleted.")
except Exception as e:
printer.error(str(e))
return
if args.mcp is not None:
return self.configure_mcp(args)
# Determine session_id to resume
session_id = None
if args.resume:
sessions, _ = self.app.services.ai.list_sessions()
session_id = sessions[0]["id"] if sessions else None
if not session_id:
printer.warning("No previous session found to resume.")
elif args.session:
session_id = args.session[0]
# Configure additional arguments for the AI service
# Priority: CLI Args > Local Config
settings = self.app.services.config_svc.get_settings().get("ai", {})
arguments = {}
for key in ["engineer_model", "engineer_api_key", "architect_model", "architect_api_key"]:
cli_val = getattr(args, key, None)
if cli_val:
arguments[key] = cli_val[0]
elif settings.get(key):
arguments[key] = settings.get(key)
for key in ["engineer_auth", "architect_auth"]:
cli_val = getattr(args, key, None)
if cli_val:
arguments[key] = self._parse_auth_value(cli_val[0])
elif settings.get(key):
arguments[key] = settings.get(key)
# Check keys only if running in local mode (not remote)
if getattr(self.app.services, "mode", "local") == "local":
if not arguments.get("engineer_api_key") and not arguments.get("engineer_auth"):
printer.error("Engineer API key/auth not configured. The chat cannot start.")
printer.info("Use 'connpy config --engineer-api-key <key>' or 'connpy config --engineer-auth <auth>' to set it.")
sys.exit(1)
if not arguments.get("architect_api_key") and not arguments.get("architect_auth"):
printer.warning("Architect API key/auth not configured. Architect will be unavailable.")
printer.info("Use 'connpy config --architect-api-key <key>' or 'connpy config --architect-auth <auth>' to enable it.")
# The rest of the interaction is handled by the CLI with the underlying agent
self.app.myai = self.app.services.ai
self.ai_overrides = arguments
if args.ask:
self.single_question(args, session_id)
else:
self.interactive_chat(args, session_id)
def single_question(self, args, session_id):
query = " ".join(args.ask)
with console.status("[ai_status]Agent is thinking and analyzing...[/ai_status]") as status:
result = self.app.myai.ask(query, status=status, debug=args.debug, session_id=session_id, trust=args.trust, **self.ai_overrides)
responder = result.get("responder", "engineer")
border = "architect" if responder == "architect" else "engineer"
title = "[architect][bold]Network Architect[/bold][/architect]" if responder == "architect" else "[engineer][bold]Network Engineer[/bold][/engineer]"
if not result.get("streamed"):
mdprint(Panel(Markdown(result["response"]), title=title, border_style=border, expand=False))
if "usage" in result:
u = result["usage"]
console.print(f"[debug]Tokens: {u['total']} (Input: {u['input']}, Output: {u['output']})[/debug]")
def interactive_chat(self, args, session_id):
history = None
if session_id:
session_data = self.app.myai.load_session_data(session_id)
if session_data:
history = session_data.get("history", [])
mdprint(Rule(title=f"[header] Resuming Session: {session_data.get('title')} [/header]", style="border"))
if history:
mdprint(f"[debug]Analyzing {len(history)} previous messages...[/debug]\n")
else:
printer.info(f"Session '{session_id}' not found. Starting clean.")
if not history:
mdprint(Rule(style="engineer"))
mdprint(Markdown("**Networking Expert Agent**: Hi! I'm your assistant. I can help you diagnose issues, run commands, and manage your nodes.\nType 'exit' to quit.\n"))
mdprint(Rule(style="engineer"))
while True:
try:
user_query = Prompt.ask("[user_prompt]User[/user_prompt]")
if not user_query.strip(): continue
if user_query.lower() in ['exit', 'quit', 'bye', 'cancel']: break
with console.status("[ai_status]Agent is thinking...[/ai_status]") as status:
result = self.app.myai.ask(user_query, chat_history=history, status=status, debug=args.debug, trust=args.trust, session_id=session_id, **self.ai_overrides)
new_history = result.get("chat_history")
if new_history is not None:
history = new_history
responder = result.get("responder", "engineer")
border = "architect" if responder == "architect" else "engineer"
title = "[architect][bold]Network Architect[/bold][/architect]" if responder == "architect" else "[engineer][bold]Network Engineer[/bold][/engineer]"
if not result.get("streamed"):
response_text = result.get("response", "")
if response_text:
mdprint(Panel(Markdown(response_text), title=title, border_style=border, expand=False))
if "usage" in result:
u = result["usage"]
console.print(f"[debug]Tokens: {u['total']} (Input: {u['input']}, Output: {u['output']})[/debug]")
except (KeyboardInterrupt, EOFError):
console.print("\n[dim]Session closed.[/dim]")
break
def configure_mcp(self, args):
"""Handle MCP server configuration via CLI tokens or interactive wizard."""
mcp_args = args.mcp
# 1. Non-interactive CLI Mode (if arguments are provided)
if mcp_args:
action = mcp_args[0].lower()
if action == "list":
mcp_servers = self.app.services.ai.list_mcp_servers()
if not mcp_servers:
printer.info("No MCP servers configured.")
else:
columns = ["Name", "URL", "Enabled", "Auto-load OS"]
rows = []
for name, cfg in mcp_servers.items():
rows.append([
name,
cfg.get("url", ""),
"[green]Yes[/green]" if cfg.get("enabled", True) else "[red]No[/red]",
cfg.get("auto_load_on_os", "Any")
])
printer.table("Configured MCP Servers", columns, rows)
return
elif action == "add":
if len(mcp_args) < 3:
printer.error("Usage: connpy ai --mcp add <name> <url> [os_filter]")
return
name, url = mcp_args[1], mcp_args[2]
os_filter = mcp_args[3] if len(mcp_args) > 3 else None
try:
self.app.services.ai.configure_mcp(name, url=url, auto_load_on_os=os_filter)
printer.success(f"MCP server '{name}' added/updated.")
except Exception as e:
printer.error(str(e))
return
elif action == "remove":
if len(mcp_args) < 2:
printer.error("Usage: connpy ai --mcp remove <name>")
return
name = mcp_args[1]
try:
self.app.services.ai.configure_mcp(name, remove=True)
printer.success(f"MCP server '{name}' removed.")
except Exception as e:
printer.error(str(e))
return
elif action in ["enable", "disable"]:
if len(mcp_args) < 2:
printer.error(f"Usage: connpy ai --mcp {action} <name>")
return
name = mcp_args[1]
enabled = (action == "enable")
try:
self.app.services.ai.configure_mcp(name, enabled=enabled)
printer.success(f"MCP server '{name}' {'enabled' if enabled else 'disabled'}.")
except Exception as e:
printer.error(str(e))
return
else:
printer.error(f"Unknown MCP action: {action}")
printer.info("Available actions: list, add, remove, enable, disable")
return
# 2. Interactive Wizard Mode (if no arguments provided)
# Import forms dynamically to avoid circular dependencies if any
if not hasattr(self.app, "cli_forms"):
from .forms import Forms
self.app.cli_forms = Forms(self.app)
mcp_servers = self.app.services.ai.list_mcp_servers()
result = self.app.cli_forms.mcp_wizard(mcp_servers)
if not result:
return
action = result["action"]
try:
if action == "list":
# Recursive call to the non-interactive list logic
args.mcp = ["list"]
return self.configure_mcp(args)
elif action == "add":
self.app.services.ai.configure_mcp(
result["name"],
url=result["url"],
enabled=result["enabled"],
auto_load_on_os=result["os"]
)
printer.success(f"MCP server '{result['name']}' saved.")
elif action == "update": # Used for toggle
self.app.services.ai.configure_mcp(
result["name"],
enabled=result["enabled"]
)
printer.success(f"MCP server '{result['name']}' updated.")
elif action == "remove":
self.app.services.ai.configure_mcp(result["name"], remove=True)
printer.success(f"MCP server '{result['name']}' removed.")
except Exception as e:
printer.error(str(e))
def _parse_auth_value(self, value):
if not value or value.lower() in ["none", "clear"]:
return None
import os
import yaml
import json
if os.path.exists(value):
try:
with open(value, "r") as f:
content = f.read()
try:
return json.loads(content)
except ValueError:
return yaml.safe_load(content)
except Exception as e:
printer.error(f"Failed to read/parse auth file '{value}': {e}")
sys.exit(1)
try:
return json.loads(value)
except ValueError:
try:
parsed = yaml.safe_load(value)
if isinstance(parsed, dict):
return parsed
raise ValueError()
except Exception:
printer.error("Auth parameter must be a valid JSON/YAML string, or a path to a JSON/YAML file.")
sys.exit(1)
-53
View File
@@ -1,53 +0,0 @@
import sys
from .. import printer
from ..services.exceptions import ConnpyError
class APIHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
try:
status = self.app.services.system.get_api_status()
if args.command == "stop":
if not status["running"]:
printer.warning("API does not seem to be running.")
else:
stopped = self.app.services.system.stop_api()
if stopped:
printer.success("API stopped successfully.")
elif args.command == "restart":
port = args.data if args.data and isinstance(args.data, int) else None
if status["running"]:
printer.info(f"Stopping server with process ID {status['pid']}...")
# Service handles port preservation if port is None
self.app.services.system.restart_api(port=port)
if status["running"]:
printer.info(f"Server with process ID {status['pid']} stopped.")
# Re-fetch status to show the actual port used
new_status = self.app.services.system.get_api_status()
printer.success(f"API restarted on port {new_status.get('port', 'unknown')}.")
elif args.command == "start":
if status["running"]:
msg = f"Connpy server is already running (PID: {status['pid']}"
if status.get("port"):
msg += f", Port: {status['port']}"
msg += ")."
printer.warning(msg)
else:
port = args.data if args.data and isinstance(args.data, int) else 8048
self.app.services.system.start_api(port=port)
printer.success(f"API started on port {port}.")
elif args.command == "debug":
port = args.data if args.data and isinstance(args.data, int) else 8048
self.app.services.system.debug_api(port=port)
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
-185
View File
@@ -1,185 +0,0 @@
import sys
import yaml
from .. import printer
from ..services.exceptions import ConnpyError, InvalidConfigurationError
from .help_text import get_instructions
class ConfigHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
actions = {
"completion": self.show_completion,
"fzf_wrapper": self.show_fzf_wrapper,
"case": self.set_case,
"fzf": self.set_fzf,
"idletime": self.set_idletime,
"configfolder": self.set_configfolder,
"theme": self.set_theme,
"engineer_model": self.set_ai_config,
"engineer_api_key": self.set_ai_config,
"engineer_auth": self.set_ai_config,
"architect_model": self.set_ai_config,
"architect_api_key": self.set_ai_config,
"architect_auth": self.set_ai_config,
"trusted_commands": self.set_ai_config,
"service_mode": self.set_service_mode,
"remote_host": self.set_remote_host,
"sync_remote": self.set_sync_remote
}
handler = actions.get(getattr(args, "command", None))
if handler:
return handler(args)
# If no specific command was triggered, show current configuration
return self.show_config(args)
def show_config(self, args):
settings = self.app.services.config_svc.get_settings()
yaml_str = yaml.dump(settings, sort_keys=False, default_flow_style=False)
printer.data("Current Configuration", yaml_str)
def set_service_mode(self, args):
new_mode = args.data[0]
if new_mode == "remote":
settings = self.app.services.config_svc.get_settings()
if not settings.get("remote_host"):
printer.error("Remote host must be configured before switching to remote mode")
return
self.app.services.config_svc.update_setting("service_mode", new_mode)
# Immediate sync of fzf/text cache files for the new mode
try:
# 1. Clear old cache files to avoid discrepancies if fetch fails
self.app.config._generate_nodes_cache(nodes=[], folders=[], profiles=[])
# 2. Re-initialize services for the new mode
from ..services.provider import ServiceProvider
settings = self.app.services.config_svc.get_settings()
new_services = ServiceProvider(self.app.config, mode=new_mode, remote_host=settings.get("remote_host"))
# 3. Fetch data from new mode and generate cache
nodes = new_services.nodes.list_nodes()
folders = new_services.nodes.list_folders()
profiles = new_services.profiles.list_profiles()
new_services.nodes.generate_cache(nodes=nodes, folders=folders, profiles=profiles)
printer.success("Config saved")
except Exception as e:
printer.success("Config saved")
printer.warning(f"Note: Could not synchronize fzf cache: {e}")
def set_remote_host(self, args):
self.app.services.config_svc.update_setting("remote_host", args.data[0])
printer.success("Config saved")
def set_theme(self, args):
try:
valid_styles = self.app.services.config_svc.apply_theme_from_file(args.data[0])
# Apply immediately to current session
printer.apply_theme(valid_styles)
printer.success(f"Theme '{args.data[0]}' applied and saved")
except (ConnpyError, InvalidConfigurationError) as e:
printer.error(str(e))
def show_fzf_wrapper(self, args):
print(get_instructions("fzf_wrapper_" + args.data[0]))
def show_completion(self, args):
print(get_instructions(args.data[0] + "completion"))
def set_case(self, args):
val = (args.data[0].lower() == "true")
self.app.services.config_svc.update_setting("case", val)
self.app.case = val
printer.success("Config saved")
def set_fzf(self, args):
val = (args.data[0].lower() == "true")
self.app.services.config_svc.update_setting("fzf", val)
self.app.fzf = val
printer.success("Config saved")
def set_idletime(self, args):
try:
val = max(0, int(args.data[0]))
self.app.services.config_svc.update_setting("idletime", val)
printer.success("Config saved")
except ValueError:
printer.error("Keepalive must be an integer.")
def set_configfolder(self, args):
try:
self.app.services.config_svc.set_config_folder(args.data[0])
printer.success("Config saved")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def set_sync_remote(self, args):
val = (args.data[0].lower() == "true")
self.app.services.config_svc.update_setting("sync_remote", val)
self.app.services.sync.sync_remote = val
printer.success("Config saved")
def set_ai_config(self, args):
try:
settings = self.app.services.config_svc.get_settings()
aiconfig = settings.get("ai", {})
val = args.data[0]
# Check for unset/clear request
if val.lower() in ["none", "clear", ""]:
if args.command in aiconfig:
del aiconfig[args.command]
else:
# If configuring auth, parse as dictionary (JSON/YAML or file path)
if args.command in ["engineer_auth", "architect_auth"]:
parsed_val = self._parse_auth_value(val)
if parsed_val is not None:
aiconfig[args.command] = parsed_val
else:
if args.command in aiconfig:
del aiconfig[args.command]
else:
aiconfig[args.command] = val
self.app.services.config_svc.update_setting("ai", aiconfig)
printer.success("Config saved")
except (ConnpyError, InvalidConfigurationError) as e:
printer.error(str(e))
def _parse_auth_value(self, value):
if value.lower() in ["none", "clear", ""]:
return None
# Check if it's a file path
import os
if os.path.exists(value):
try:
with open(value, "r") as f:
content = f.read()
import json
try:
return json.loads(content)
except ValueError:
return yaml.safe_load(content)
except Exception as e:
raise InvalidConfigurationError(f"Failed to read/parse auth file '{value}': {e}")
# Try parsing as inline JSON/YAML
try:
import json
return json.loads(value)
except ValueError:
try:
parsed = yaml.safe_load(value)
if isinstance(parsed, dict):
return parsed
raise ValueError()
except Exception:
raise InvalidConfigurationError("Auth parameter must be a valid JSON/YAML string, or a path to a JSON/YAML file.")
-77
View File
@@ -1,77 +0,0 @@
import sys
import yaml
from .. import printer
from ..services.exceptions import ConnpyError
class ContextHandler:
def __init__(self, app):
self.app = app
self.service = self.app.services.context
def dispatch(self, args):
try:
if args.add:
if len(args.add) < 2:
printer.error("--add requires name and at least one regex")
return
self.service.add_context(args.add[0], args.add[1:])
printer.success(f"Context '{args.add[0]}' added successfully.")
elif args.rm:
if not args.context_name:
printer.error("--rm requires a context name")
return
self.service.delete_context(args.context_name)
printer.success(f"Context '{args.context_name}' deleted successfully.")
elif args.ls:
contexts = self.service.list_contexts()
for ctx in contexts:
if ctx["active"]:
printer.success(f"{ctx['name']} (active)")
else:
printer.custom(" ", ctx["name"])
elif args.set:
if not args.context_name:
printer.error("--set requires a context name")
return
self.service.set_active_context(args.context_name)
printer.success(f"Context set to: {args.context_name}")
elif args.show:
if not args.context_name:
printer.error("--show requires a context name")
return
contexts = self.service.contexts
if args.context_name not in contexts:
printer.error(f"Context '{args.context_name}' does not exist")
return
yaml_output = yaml.dump(contexts[args.context_name], sort_keys=False, default_flow_style=False)
printer.custom(args.context_name, "")
print(yaml_output)
elif args.edit:
if len(args.edit) < 2:
printer.error("--edit requires name and at least one regex")
return
self.service.update_context(args.edit[0], args.edit[1:])
printer.success(f"Context '{args.edit[0]}' modified successfully.")
else:
# Default behavior if no flags: show list
self.dispatch_ls(args)
except ValueError as e:
printer.error(str(e))
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def dispatch_ls(self, args):
contexts = self.service.list_contexts()
for ctx in contexts:
if ctx["active"]:
printer.success(f"{ctx['name']} (active)")
else:
printer.custom(" ", ctx["name"])
-280
View File
@@ -1,280 +0,0 @@
import ast
import inquirer
from .validators import Validators
class Forms:
def __init__(self, app):
self.app = app
self.validators = Validators(app)
def questions_edit(self):
questions = []
questions.append(inquirer.Confirm("host", message="Edit Hostname/IP?"))
questions.append(inquirer.Confirm("protocol", message="Edit Protocol/app?"))
questions.append(inquirer.Confirm("port", message="Edit Port?"))
questions.append(inquirer.Confirm("options", message="Edit Options?"))
questions.append(inquirer.Confirm("logs", message="Edit logging path/file?"))
questions.append(inquirer.Confirm("tags", message="Edit tags?"))
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
questions.append(inquirer.Confirm("user", message="Edit User?"))
questions.append(inquirer.Confirm("password", message="Edit password?"))
return inquirer.prompt(questions)
def questions_nodes(self, unique, uniques=None, edit=None):
try:
defaults = self.app.services.nodes.get_node_details(unique)
if "tags" not in defaults:
defaults["tags"] = ""
if "jumphost" not in defaults:
defaults["jumphost"] = ""
except Exception:
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
node = {}
if edit is None:
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
questions = []
if edit["host"]:
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
else:
node["host"] = defaults["host"]
if edit["protocol"]:
questions.append(inquirer.Text("protocol", message="Select Protocol/app", validate=self.validators.protocol_validation, default=defaults["protocol"]))
else:
node["protocol"] = defaults["protocol"]
if edit["port"]:
questions.append(inquirer.Text("port", message="Select Port Number", validate=self.validators.port_validation, default=defaults["port"]))
else:
node["port"] = defaults["port"]
if edit["options"]:
questions.append(inquirer.Text("options", message="Pass extra options to protocol/app", validate=self.validators.default_validation, default=defaults["options"]))
else:
node["options"] = defaults["options"]
if edit["logs"]:
questions.append(inquirer.Text("logs", message="Pick logging path/file ", validate=self.validators.default_validation, default=defaults["logs"].replace("{", "{{").replace("}", "}}")))
else:
node["logs"] = defaults["logs"]
if edit["tags"]:
questions.append(inquirer.Text("tags", message="Add tags dictionary", validate=self.validators.tags_validation, default=str(defaults["tags"]).replace("{", "{{").replace("}", "}}")))
else:
node["tags"] = defaults["tags"]
if edit["jumphost"]:
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation, default=str(defaults["jumphost"]).replace("{", "{{").replace("}", "}}")))
else:
node["jumphost"] = defaults["jumphost"]
if edit["user"]:
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation, default=defaults["user"]))
else:
node["user"] = defaults["user"]
if edit["password"]:
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
else:
node["password"] = defaults["password"]
answer = inquirer.prompt(questions)
if answer is None:
return False
if "password" in answer:
if answer["password"] == "Local Password":
passq = [inquirer.Password("password", message="Set Password")]
passa = inquirer.prompt(passq)
if passa is None:
return False
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
elif answer["password"] == "Profiles":
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq)
if passa is None:
return False
answer["password"] = passa["password"].split(",")
elif answer["password"] == "No Password":
answer["password"] = ""
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
answer["tags"] = ast.literal_eval(answer["tags"])
result = {**uniques, **answer, **node}
result["type"] = "connection"
return result
def questions_profiles(self, unique, edit=None):
try:
defaults = self.app.services.profiles.get_profile(unique, resolve=False)
if "tags" not in defaults:
defaults["tags"] = ""
if "jumphost" not in defaults:
defaults["jumphost"] = ""
except Exception:
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
profile = {}
if edit is None:
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
questions = []
if edit["host"]:
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
else:
profile["host"] = defaults["host"]
if edit["protocol"]:
questions.append(inquirer.Text("protocol", message="Select Protocol/app", validate=self.validators.profile_protocol_validation, default=defaults["protocol"]))
else:
profile["protocol"] = defaults["protocol"]
if edit["port"]:
questions.append(inquirer.Text("port", message="Select Port Number", validate=self.validators.profile_port_validation, default=defaults["port"]))
else:
profile["port"] = defaults["port"]
if edit["options"]:
questions.append(inquirer.Text("options", message="Pass extra options to protocol/app", default=defaults["options"]))
else:
profile["options"] = defaults["options"]
if edit["logs"]:
questions.append(inquirer.Text("logs", message="Pick logging path/file ", default=defaults["logs"].replace("{", "{{").replace("}", "}}")))
else:
profile["logs"] = defaults["logs"]
if edit["tags"]:
questions.append(inquirer.Text("tags", message="Add tags dictionary", validate=self.validators.profile_tags_validation, default=str(defaults["tags"]).replace("{", "{{").replace("}", "}}")))
else:
profile["tags"] = defaults["tags"]
if edit["jumphost"]:
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.profile_jumphost_validation, default=str(defaults["jumphost"]).replace("{", "{{").replace("}", "}}")))
else:
profile["jumphost"] = defaults["jumphost"]
if edit["user"]:
questions.append(inquirer.Text("user", message="Pick username", default=defaults["user"]))
else:
profile["user"] = defaults["user"]
if edit["password"]:
questions.append(inquirer.Password("password", message="Set Password"))
else:
profile["password"] = defaults["password"]
answer = inquirer.prompt(questions)
if answer is None:
return False
if "password" in answer:
if answer["password"] != "":
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
if "tags" in answer and answer["tags"]:
answer["tags"] = ast.literal_eval(answer["tags"])
result = {**answer, **profile}
result["id"] = unique
return result
def questions_bulk(self, nodes="", hosts=""):
questions = []
questions.append(inquirer.Text("ids", message="add a comma separated list of nodes to add", default=nodes, validate=self.validators.bulk_node_validation))
questions.append(inquirer.Text("location", message="Add a @folder, @subfolder@folder or leave empty", validate=self.validators.bulk_folder_validation))
questions.append(inquirer.Text("host", message="Add comma separated list of Hostnames or IPs", default=hosts, validate=self.validators.bulk_host_validation))
questions.append(inquirer.Text("protocol", message="Select Protocol/app", validate=self.validators.protocol_validation))
questions.append(inquirer.Text("port", message="Select Port Number", validate=self.validators.port_validation))
questions.append(inquirer.Text("options", message="Pass extra options to protocol/app", validate=self.validators.default_validation))
questions.append(inquirer.Text("logs", message="Pick logging path/file ", validate=self.validators.default_validation))
questions.append(inquirer.Text("tags", message="Add tags dictionary", validate=self.validators.tags_validation))
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
answer = inquirer.prompt(questions)
if answer is None:
return False
if "password" in answer:
if answer["password"] == "Local Password":
passq = [inquirer.Password("password", message="Set Password")]
passa = inquirer.prompt(passq)
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
elif answer["password"] == "Profiles":
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq)
answer["password"] = passa["password"].split(",")
elif answer["password"] == "No Password":
answer["password"] = ""
answer["type"] = "connection"
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
answer["tags"] = ast.literal_eval(answer["tags"])
return answer
def mcp_wizard(self, mcp_servers):
"""Interactive wizard to manage MCP servers."""
from .helpers import theme
while True:
options = [
("List Configured Servers", "list"),
("Add/Update Server", "add"),
("Enable/Disable Server", "toggle"),
("Remove Server", "remove"),
("Back", "exit")
]
questions = [
inquirer.List("action", message="MCP Configuration", choices=options)
]
answers = inquirer.prompt(questions, theme=theme)
if not answers or answers["action"] == "exit":
return None
action = answers["action"]
if action == "list":
if not mcp_servers:
print("\nNo MCP servers configured.\n")
else:
return {"action": "list"}
elif action == "add":
questions = [
inquirer.Text("name", message="Server Name (identifier)"),
inquirer.Text("url", message="SSE URL (e.g., http://localhost:8000/sse)"),
inquirer.Confirm("enabled", message="Enabled?", default=True),
inquirer.Text("auto_load_os", message="Auto-load on specific OS (blank for any)")
]
answers = inquirer.prompt(questions, theme=theme)
if answers:
return {
"action": "add",
"name": answers["name"],
"url": answers["url"],
"enabled": answers["enabled"],
"os": answers["auto_load_os"]
}
elif action == "toggle":
if not mcp_servers:
print("\nNo servers to toggle.\n")
continue
choices = []
for name, cfg in mcp_servers.items():
status = "[Enabled]" if cfg.get("enabled", True) else "[Disabled]"
choices.append((f"{name} {status}", name))
questions = [
inquirer.List("name", message="Select server to toggle", choices=choices + [("Cancel", None)])
]
answers = inquirer.prompt(questions, theme=theme)
if answers and answers["name"]:
current = mcp_servers[answers["name"]].get("enabled", True)
return {
"action": "update",
"name": answers["name"],
"enabled": not current
}
elif action == "remove":
if not mcp_servers:
print("\nNo servers to remove.\n")
continue
questions = [
inquirer.List("name", message="Select server to remove", choices=list(mcp_servers.keys()) + ["Cancel"])
]
answers = inquirer.prompt(questions, theme=theme)
if answers and answers["name"] != "Cancel":
return {"action": "remove", "name": answers["name"]}
return None
-215
View File
@@ -1,215 +0,0 @@
import os
def get_help(type, parsers=None):
if type == "export":
return "Export /path/to/file.yml \[@subfolder1]\[@folder1] \[@subfolderN]\[@folderN]"
if type == "import":
return "Import /path/to/file.yml"
if type == "node":
return "node\[@subfolder]\[@folder]\nConnect to specific node or show all matching nodes\n\[@subfolder]\[@folder]\nShow all available connections globally or in specified path"
if type == "usage":
commands = []
for subcommand, subparser in parsers.choices.items():
if subparser.description != None:
commands.append(subcommand)
commands = ",".join(commands)
usage_help = f"connpy [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]\n connpy {{{commands}}} ..."
return usage_help
return get_instructions(type)
def get_instructions(type="add"):
if type == "add":
return """
Welcome to Connpy node Addition Wizard!
Here are some important instructions and tips for configuring your new node:
1. **Profiles**:
- You can use the configured settings in a profile using `@profilename`.
2. **Available Protocols and Apps**:
- ssh
- telnet
- kubectl (`kubectl exec`)
- docker (`docker exec`)
- ssm (`aws ssm start-session`)
3. **Optional Values**:
- You can leave any value empty except for the hostname/IP.
4. **Passwords**:
- You can pass one or more passwords using comma-separated `@profiles`.
5. **Logging**:
- You can use the following variables in the logging file name:
- `${id}`
- `${unique}`
- `${host}`
- `${port}`
- `${user}`
- `${protocol}`
6. **Well-Known Tags**:
- `os`: Identified by AI to generate commands based on the operating system.
- `screen_length_command`: Used by automation to avoid pagination on different devices (e.g., `terminal length 0` for Cisco devices).
- `prompt`: Replaces default app prompt to identify the end of output or where the user can start inputting commands.
- `kube_command`: Replaces the default command (`/bin/bash`) for `kubectl exec`.
- `docker_command`: Replaces the default command for `docker exec`.
- `region`: AWS Region used for `aws ssm start-session`.
- `profile`: AWS Profile used for `aws ssm start-session`.
- `ssh_options`: Additional SSH options injected when an SSM node is used as a jumphost (e.g., `-i ~/.ssh/key.pem`).
- `nc_command`: Replaces the default `nc` command used when bridging connections through Docker or Kubernetes (e.g., `ip netns exec global-vrf nc`).
"""
if type == "bashcompletion":
return '''
# Bash completion for connpy
# Run: eval "$(connpy config --completion bash)"
# Or add it to your .bashrc
_connpy_autocomplete()
{
local strings
strings=$(python3 -m connpy.completion bash ${#COMP_WORDS[@]} "${COMP_WORDS[@]}")
local IFS=$'\\t'
COMPREPLY=( $(compgen -W "$strings" -- "${COMP_WORDS[$COMP_CWORD]}") )
}
complete -o nosort -F _connpy_autocomplete conn
complete -o nosort -F _connpy_autocomplete connpy
'''
if type == "zshcompletion":
return '''
# Zsh completion for connpy
# Run: eval "$(connpy config --completion zsh)"
# Or add it to your .zshrc
# Make sure compinit is loaded
autoload -U compinit && compinit
_connpy_autocomplete()
{
local COMP_WORDS num strings
COMP_WORDS=( $words )
num=${#COMP_WORDS[@]}
if [[ $words =~ '.* $' ]]; then
num=$(($num + 1))
fi
strings=$(python3 -m connpy.completion zsh ${num} ${COMP_WORDS[@]})
local IFS=$'\\t'
compadd "$@" -- ${=strings}
}
compdef _connpy_autocomplete conn
compdef _connpy_autocomplete connpy
'''
if type == "fzf_wrapper_bash":
return '''\n#Here starts bash 0ms fzf wrapper for connpy
connpy() {
if [ $# -eq 0 ]; then
local selected
local configdir=$(cat ~/.config/conn/.folder 2>/dev/null || echo ~/.config/conn)
if [ -s "$configdir/.fzf_nodes_cache.txt" ]; then
selected=$(cat "$configdir/.fzf_nodes_cache.txt" | fzf-tmux -i -d 25%)
else
command connpy
return
fi
if [ -n "$selected" ]; then
command connpy "$selected"
fi
else
command connpy "$@"
fi
}
alias c="connpy"
#Here ends bash 0ms fzf wrapper for connpy
'''
if type == "fzf_wrapper_zsh":
return '''\n#Here starts zsh 0ms fzf wrapper for connpy
connpy() {
if [ $# -eq 0 ]; then
local selected
local configdir=$(cat ~/.config/conn/.folder 2>/dev/null || echo ~/.config/conn)
if [ -s "$configdir/.fzf_nodes_cache.txt" ]; then
selected=$(cat "$configdir/.fzf_nodes_cache.txt" | fzf-tmux -i -d 25%)
else
command connpy
return
fi
if [ -n "$selected" ]; then
command connpy "$selected"
fi
else
command connpy "$@"
fi
}
alias c="connpy"
#Here ends zsh 0ms fzf wrapper for connpy
'''
if type == "run":
return "node[@subfolder][@folder] commmand to run\nRun the specific command on the node and print output\n/path/to/file.yaml\nUse a yaml file to run an automation script"
if type == "generate":
return r'''---
tasks:
- name: "Config"
action: 'run' #Action can be test or run. Mandatory
nodes: #List of nodes to work on. Mandatory
- 'router1@office' #You can add specific nodes
- '@aws' #entire folders or subfolders
- 'router.*@office' #or use regex to filter inside a folder
commands: #List of commands to send, use {name} to pass variables
- 'term len 0'
- 'conf t'
- 'interface {if}'
- 'ip address 10.100.100.{id} 255.255.255.255'
- '{commit}'
- 'end'
variables: #Variables to use on commands and expected. Optional
__global__: #Global variables to use on all nodes, fallback if missing in the node.
commit: ''
if: 'loopback100'
router1@office:
id: 1
router2@office:
id: 2
commit: 'commit'
router3@office:
id: 3
vrouter1@aws:
id: 4
vrouterN@aws:
id: 5
output: /home/user/logs #Type of output, if null you only get Connection and test result. Choices are: null,stdout,/path/to/folder. Folder path works on both 'run' and 'test' actions.
options:
prompt: r'>$|#$|\$$|>.$|#.$|\$.$' #Optional prompt to check on your devices, default should work on most devices.
parallel: 10 #Optional number of nodes to run commands on parallel. Default 10.
timeout: 20 #Optional time to wait in seconds for prompt, expected or EOF. Default 20.
- name: "TestConfig"
action: 'test'
nodes:
- 'router1@office'
- '@aws'
commands:
- 'ping 10.100.100.{id}'
expected: '!' #Expected text to find when running test action. Mandatory for 'test'
variables:
router1@office:
id: 1
router2@office:
id: 2
commit: 'commit'
router3@office:
id: 3
vrouter1@aws:
id: 4
vrouterN@aws:
id: 5
output: null
...'''
return ""
-151
View File
@@ -1,151 +0,0 @@
import os
import inquirer
from inquirer.themes import Default, term
try:
from pyfzf.pyfzf import FzfPrompt
except ImportError:
FzfPrompt = None
def hex_to_blessed(hex_str):
"""Convert hex color string to blessed/ansi format."""
if not hex_str or not isinstance(hex_str, str):
return term.normal
# Check for bold prefix
prefix = ""
if hex_str.startswith('bold '):
prefix = term.bold
hex_str = hex_str.replace('bold ', '').strip()
# If it's a standard color name
if not hex_str.startswith('#'):
return prefix + getattr(term, hex_str, term.normal)
# Parse hex
try:
h = hex_str.lstrip('#')
if len(h) == 3:
h = ''.join([c*2 for c in h])
r = int(h[0:2], 16)
g = int(h[2:4], 16)
b = int(h[4:6], 16)
# Try RGB, fallback to standard cyan if it fails or returns empty
try:
c = term.color_rgb(r, g, b)
if not c: # Some terms return empty for RGB
return prefix + term.cyan
return prefix + c
except:
return prefix + term.cyan
except:
return prefix + term.normal
# Custom inquirer theme matching connpy colors
class ConnpyTheme(Default):
def __init__(self):
super().__init__()
try:
from ..printer import _global_active_styles
# Use user_prompt as primary accent, fallback to info/cyan
accent = _global_active_styles.get("user_prompt", _global_active_styles.get("info", "cyan"))
accent_color = hex_to_blessed(accent)
self.Question.mark_color = accent_color
self.List.selection_color = accent_color
self.List.selection_cursor = ">"
except:
# Absolute fallback to standard cyan
self.Question.mark_color = term.cyan
self.List.selection_color = term.bold_cyan
self.List.selection_cursor = ">"
def get_theme():
"""Returns a fresh instance of the theme with current colors."""
return ConnpyTheme()
class ThemeProxy:
"""Proxy to ensure theme colors are resolved at runtime."""
def __getattr__(self, name):
return getattr(get_theme(), name)
def __iter__(self):
return iter(get_theme())
def __getitem__(self, item):
return get_theme()[item]
theme = ThemeProxy()
def get_config_dir():
home = os.path.expanduser("~")
defaultdir = os.path.join(home, '.config/conn')
pathfile = os.path.join(defaultdir, '.folder')
try:
with open(pathfile, "r") as f:
return f.read().strip()
except:
return defaultdir
def nodes_completer(prefix, parsed_args, **kwargs):
configdir = get_config_dir()
cache_file = os.path.join(configdir, '.fzf_nodes_cache.txt')
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
return [line.strip() for line in f if line.startswith(prefix)]
return []
def folders_completer(prefix, parsed_args, **kwargs):
configdir = get_config_dir()
cache_file = os.path.join(configdir, '.folders_cache.txt')
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
return [line.strip() for line in f if line.startswith(prefix)]
return []
def profiles_completer(prefix, parsed_args, **kwargs):
configdir = get_config_dir()
cache_file = os.path.join(configdir, '.profiles_cache.txt')
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
return [line.strip() for line in f if line.startswith(prefix)]
return []
def choose(app, list_, name, action):
# Generates an inquirer list to pick
# Safeguard: Never prompt if running in autocomplete shell
if os.environ.get("_ARGCOMPLETE") or os.environ.get("COMP_LINE"):
return None
if FzfPrompt and app.fzf and os.environ.get("_ARGCOMPLETE") is None and os.environ.get("COMP_LINE") is None:
fzf_prompt = FzfPrompt(executable_path="fzf-tmux")
if not app.case:
fzf_prompt = FzfPrompt(executable_path="fzf-tmux -i")
answer = fzf_prompt.prompt(list_, fzf_options="-d 25%")
if len(answer) == 0:
return None
else:
return answer[0]
else:
questions = [inquirer.List(name, message="Pick {} to {}:".format(name,action), choices=list_, carousel=True)]
answer = inquirer.prompt(questions, theme=theme)
if answer == None:
return None
else:
return answer[name]
def toplevel_completer(prefix, parsed_args, **kwargs):
commands = ["node", "profile", "move", "mv", "copy", "cp", "list", "ls", "bulk", "export", "import", "ai", "run", "api", "context", "plugin", "config", "sync"]
configdir = get_config_dir()
cache_file = os.path.join(configdir, '.fzf_nodes_cache.txt')
nodes = []
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
nodes = [line.strip() for line in f if line.startswith(prefix)]
cache_folders = os.path.join(configdir, '.folders_cache.txt')
if os.path.exists(cache_folders):
with open(cache_folders, "r") as f:
nodes += [line.strip() for line in f if line.startswith(prefix)]
return [c for c in commands + nodes if c.startswith(prefix)]
-85
View File
@@ -1,85 +0,0 @@
import os
import sys
import inquirer
from .. import printer
from ..services.exceptions import ConnpyError
from .forms import Forms
class ImportExportHandler:
def __init__(self, app):
self.app = app
self.forms = Forms(app)
def dispatch_import(self, args):
file_path = args.data[0]
try:
printer.warning("This could overwrite your current configuration!")
question = [inquirer.Confirm("import", message=f"Are you sure you want to import {file_path}?")]
confirm = inquirer.prompt(question)
if confirm == None or not confirm["import"]:
sys.exit(7)
self.app.services.import_export.import_from_file(file_path)
printer.success(f"File {file_path} imported successfully.")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def dispatch_export(self, args):
file_path = args.data[0]
folders = args.data[1:] if len(args.data) > 1 else None
try:
self.app.services.import_export.export_to_file(file_path, folders=folders)
printer.success(f"File {file_path} generated successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
sys.exit()
def bulk(self, args):
if args.file and os.path.isfile(args.file[0]):
with open(args.file[0], 'r') as f:
lines = f.readlines()
# Expecting exactly 2 lines
if len(lines) < 2:
printer.error("The file must contain at least two lines: one for nodes, one for hosts.")
sys.exit(11)
nodes = lines[0].strip()
hosts = lines[1].strip()
newnodes = self.forms.questions_bulk(nodes, hosts)
else:
newnodes = self.forms.questions_bulk()
if newnodes == False:
sys.exit(7)
if not self.app.case:
newnodes["location"] = newnodes["location"].lower()
newnodes["ids"] = newnodes["ids"].lower()
# Handle the case where location might be a file reference (e.g. from a prompt)
location = newnodes["location"]
if location.startswith("@") and "/" in location:
# Extract the actual @folder part (e.g. @testall from @testall/.folders_cache.txt)
location = location.split("/")[0]
newnodes["location"] = location
ids = newnodes["ids"].split(",")
# Append location to each id for proper folder assignment
location = newnodes["location"]
if location:
ids = [f"{i}{location}" for i in ids]
hosts = newnodes["host"].split(",")
try:
count = self.app.services.nodes.bulk_add(ids, hosts, newnodes)
if count > 0:
printer.success(f"Successfully added {count} nodes.")
else:
printer.info("0 nodes added")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
-143
View File
@@ -1,143 +0,0 @@
import os
import sys
import getpass
from .. import printer
from ..services.exceptions import ConnpyError
class LoginHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
action = getattr(args, "action", None)
if action == "login":
return self.login(args)
elif action == "logout":
return self.logout(args)
else:
printer.error(f"Unknown action: {action}")
sys.exit(1)
def login(self, args):
if getattr(args, "status", False):
return self.show_status()
if self.app.services.mode != "remote":
printer.warning("Note: Your current configuration is set to local mode. Logging in will save credentials, but they will only apply when service-mode is set to 'remote'.")
username = getattr(args, "username", None)
if not username:
try:
username = input("Username: ").strip()
if not username:
printer.error("Username cannot be empty.")
sys.exit(1)
except (KeyboardInterrupt, EOFError):
printer.warning("\nOperation cancelled.")
sys.exit(130)
try:
password = getpass.getpass("Password: ")
if not password:
printer.error("Password cannot be empty.")
sys.exit(1)
except (KeyboardInterrupt, EOFError):
printer.warning("\nOperation cancelled.")
sys.exit(130)
# Make the gRPC login call via self.app.services.auth stub
# We need to make sure auth is initialized in remote mode.
# If we are in local mode, self.app.services.auth is not initialized on ServiceProvider.
# Let's instantiate it dynamically if it's not present.
auth_service = getattr(self.app.services, "auth", None)
if not auth_service:
import grpc
from ..grpc_layer.stubs import AuthStub
remote_host = self.app.services.remote_host or self.app.config.config.get("remote_host")
if not remote_host:
printer.error("Remote host is not configured. Run 'connpy config --remote HOST:PORT' first.")
sys.exit(1)
try:
channel = grpc.insecure_channel(remote_host)
auth_service = AuthStub(channel, remote_host=remote_host)
except Exception as e:
printer.error(f"Failed to connect to remote server for login: {e}")
sys.exit(1)
try:
res = auth_service.login(username, password)
token = res["token"]
# Save token to ~/.config/conn/.token
token_path = os.path.join(self.app.config.defaultdir, ".token")
with open(token_path, "w") as f:
f.write(token)
os.chmod(token_path, 0o600)
printer.success(f"Logged in successfully as '{username}'. Session expires in 8 hours.")
except ConnpyError as e:
printer.error(f"Login failed: {e}")
sys.exit(1)
except Exception as e:
printer.error(f"Login failed with unexpected error: {e}")
sys.exit(1)
def logout(self, args):
token_path = os.path.join(self.app.config.defaultdir, ".token")
if os.path.exists(token_path):
try:
os.remove(token_path)
printer.success("Logged out successfully. Local session cleared.")
except Exception as e:
printer.error(f"Failed to clear session: {e}")
sys.exit(1)
else:
printer.info("No active session found (already logged out).")
def show_status(self):
import base64
import json
import datetime
token_path = os.path.join(self.app.config.defaultdir, ".token")
if not os.path.exists(token_path):
printer.warning("No active session found. You can log in using 'connpy login'.")
return
try:
with open(token_path, "r") as f:
token = f.read().strip()
parts = token.split(".")
if len(parts) != 3:
printer.error("Invalid local session token format.")
return
payload_b64 = parts[1]
payload_b64 += "=" * ((4 - len(payload_b64) % 4) % 4)
payload_bytes = base64.urlsafe_b64decode(payload_b64)
payload = json.loads(payload_bytes.decode("utf-8"))
username = payload.get("sub")
exp = payload.get("exp")
if not exp:
printer.success(f"Active session as '{username}' (Indefinite expiration).")
return
now = datetime.datetime.now(datetime.timezone.utc).timestamp()
if now > exp:
printer.error("Session has expired. Please log in again using 'connpy login'.")
return
remaining = exp - now
hours = int(remaining // 3600)
minutes = int((remaining % 3600) // 60)
printer.success(f"Logged in as '{username}'")
printer.info(f"Time remaining: {hours}h {minutes}m")
exp_dt = datetime.datetime.fromtimestamp(exp, datetime.timezone.utc)
printer.info(f"Expires at: {exp_dt.strftime('%Y-%m-%d %H:%M:%S UTC')}")
except Exception as e:
printer.error(f"Failed to check local session status: {e}")
-260
View File
@@ -1,260 +0,0 @@
import sys
import yaml
import inquirer
from rich.markdown import Markdown
from .. import printer
from ..services.exceptions import ConnpyError, InvalidConfigurationError
from .helpers import choose
from .forms import Forms
from .help_text import get_instructions
class NodeHandler:
def __init__(self, app):
self.app = app
self.forms = Forms(app)
def _filter_exact_match(self, matches, query):
if not query or len(matches) <= 1:
return matches
exact_matches = []
for m in matches:
if self.app.case:
if m == query:
exact_matches.append(m)
else:
if m.lower() == query.lower():
exact_matches.append(m)
if len(exact_matches) == 1:
return exact_matches
return matches
def dispatch(self, args):
if not self.app.case and args.data != None:
args.data = args.data.lower()
actions = {"version": self.version, "connect": self.connect, "add": self.add, "del": self.delete, "mod": self.modify, "show": self.show}
return actions.get(args.action)(args)
def version(self, args):
from .._version import __version__
printer.info(f"Connpy {__version__}")
def connect(self, args):
if args.data == None:
try:
matches = self.app.services.nodes.list_nodes()
except Exception as e:
printer.error(f"Failed to list nodes: {e}")
sys.exit(1)
if len(matches) == 0:
printer.warning("There are no nodes created")
printer.info("try: connpy --help")
sys.exit(9)
else:
try:
matches = self.app.services.nodes.list_nodes(args.data)
matches = self._filter_exact_match(matches, args.data)
except Exception:
matches = []
if len(matches) == 0:
printer.error(f"{args.data} not found")
sys.exit(2)
elif len(matches) > 1:
matches[0] = choose(self.app, matches, "node", "connect")
if matches[0] == None:
sys.exit(7)
try:
self.app.services.nodes.connect_node(
matches[0],
sftp=args.sftp,
debug=args.debug,
logger=self.app._service_logger
)
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def delete(self, args):
if args.data == None:
printer.error("Missing argument node")
sys.exit(3)
is_folder = args.data.startswith("@")
try:
if is_folder:
matches = self.app.services.nodes.list_folders(args.data)
else:
matches = self.app.services.nodes.list_nodes(args.data)
matches = self._filter_exact_match(matches, args.data)
except Exception:
matches = []
if len(matches) == 0:
printer.error(f"{args.data} not found")
sys.exit(2)
printer.info(f"Removing: {matches}")
question = [inquirer.Confirm("delete", message="Are you sure you want to continue?")]
confirm = inquirer.prompt(question)
if confirm == None or not confirm["delete"]:
sys.exit(7)
try:
for i, item in enumerate(matches):
save_on_last = (i == len(matches) - 1)
self.app.services.nodes.delete_node(item, is_folder=is_folder, save=save_on_last)
if len(matches) == 1:
printer.success(f"{matches[0]} deleted successfully")
else:
printer.success(f"{len(matches)} items deleted successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def add(self, args):
try:
args.data = self.app._type_node(args.data)
except ValueError as e:
printer.error(str(e))
sys.exit(3)
if args.data == None:
printer.error("Missing argument node")
sys.exit(3)
is_folder = args.data.startswith("@")
try:
if is_folder:
uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques:
raise InvalidConfigurationError(f"Invalid folder {args.data}")
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f"{args.data} added successfully")
else:
if args.data in self.app.nodes_list:
printer.error(f"Node '{args.data}' already exists.")
sys.exit(1)
uniques = self.app.services.nodes.explode_unique(args.data)
# Fast fail if parent folder does not exist
self.app.services.nodes.validate_parent_folder(args.data)
printer.console.print(Markdown(get_instructions()))
new_node_data = self.forms.questions_nodes(args.data, uniques)
if not new_node_data:
sys.exit(7)
self.app.services.nodes.add_node(args.data, new_node_data)
printer.success(f"{args.data} added successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def show(self, args):
if args.data == None:
printer.error("Missing argument node")
sys.exit(3)
try:
matches = self.app.services.nodes.list_nodes(args.data)
matches = self._filter_exact_match(matches, args.data)
except Exception:
matches = []
if len(matches) == 0:
printer.error(f"{args.data} not found")
sys.exit(2)
elif len(matches) > 1:
matches[0] = choose(self.app, matches, "node", "show")
if matches[0] == None:
sys.exit(7)
try:
node = self.app.services.nodes.get_node_details(matches[0])
yaml_output = yaml.dump(node, sort_keys=False, default_flow_style=False)
printer.data(matches[0], yaml_output)
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def modify(self, args):
if args.data == None:
printer.error("Missing argument node")
sys.exit(3)
try:
matches = self.app.services.nodes.list_nodes(args.data)
matches = self._filter_exact_match(matches, args.data)
except Exception:
matches = []
if len(matches) == 0:
printer.error(f"No connection found with filter: {args.data}")
sys.exit(2)
unique = matches[0] if len(matches) == 1 else None
uniques = self.app.services.nodes.explode_unique(unique) if unique else {"id": None, "folder": None}
printer.info(f"Editing: {matches}")
node_details = {}
for i in matches:
node_details[i] = self.app.services.nodes.get_node_details(i)
edits = self.forms.questions_edit()
if edits == None:
sys.exit(7)
# Use first match as base for defaults if multiple matches exist
base_unique = matches[0]
base_uniques = self.app.services.nodes.explode_unique(base_unique)
updatenode = self.forms.questions_nodes(base_unique, base_uniques, edit=edits)
if not updatenode:
sys.exit(7)
try:
if len(matches) == 1:
# Comparison for "Nothing to do"
current = node_details[matches[0]].copy()
current.update(uniques)
current["type"] = "connection"
if sorted(updatenode.items()) == sorted(current.items()):
printer.info("Nothing to do here")
return
self.app.services.nodes.update_node(matches[0], updatenode)
printer.success(f"{args.data} edited successfully")
else:
changed_items = []
for k in matches:
updated_item = self.app.services.nodes.explode_unique(k)
updated_item["type"] = "connection"
updated_item.update(node_details[k])
this_item_changed = False
for key, should_edit in edits.items():
if should_edit:
this_item_changed = True
updated_item[key] = updatenode[key]
if this_item_changed:
changed_items.append((k, updated_item))
editcount = len(changed_items)
for i, (k, updated_item) in enumerate(changed_items):
save_on_last = (i == editcount - 1)
self.app.services.nodes.update_node(k, updated_item, save=save_on_last)
if editcount == 0:
printer.info("Nothing to do here")
else:
printer.success(f"{matches} edited successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
-150
View File
@@ -1,150 +0,0 @@
import sys
import yaml
from .. import printer
from ..services.exceptions import ConnpyError
class PluginHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
try:
# We determine the target PluginService/PluginStub based on standard 'mode'
# But wait, local plugins should go to app.services._init_local version
# Or we can just use the provided app.services.plugins and pass the appropriate grpc calls if needed.
is_remote = getattr(args, "remote", False)
if is_remote and self.app.services.mode != "remote":
printer.error("Cannot use --remote flag when not running in remote mode.")
return
if args.add:
self.app.services.plugins.add_plugin(args.add[0], args.add[1])
printer.success(f"Plugin {args.add[0]} added successfully{' remotely' if is_remote else ''}.")
elif args.update:
self.app.services.plugins.add_plugin(args.update[0], args.update[1], update=True)
printer.success(f"Plugin {args.update[0]} updated successfully{' remotely' if is_remote else ''}.")
elif args.delete:
self.app.services.plugins.delete_plugin(args.delete[0])
printer.success(f"Plugin {args.delete[0]} deleted successfully{' remotely' if is_remote else ''}.")
elif args.enable:
name = args.enable[0]
if is_remote:
self.app.plugins.preferences[name] = "remote"
else:
if name in self.app.plugins.preferences:
del self.app.plugins.preferences[name]
self.app.plugins._save_preferences(self.app.services.config_svc.get_default_dir())
# Always try to enable it locally (remove .bkp) if it exists
# regardless of mode, to keep files consistent with "enabled" state
try:
# We use a local service instance to ensure we touch local files
from ..services.plugin_service import PluginService
local_svc = PluginService(self.app.services.config)
local_svc.enable_plugin(name)
except Exception:
pass # Ignore if not found locally or already enabled
if is_remote and self.app.services.mode == "remote":
self.app.services.plugins.enable_plugin(name)
printer.success(f"Plugin {name} enabled successfully{' remotely' if is_remote else ' locally'}.")
elif args.disable:
name = args.disable[0]
success = False
if is_remote:
if self.app.services.mode == "remote":
self.app.services.plugins.disable_plugin(name)
success = True
else:
# Disable locally
from ..services.plugin_service import PluginService
local_svc = PluginService(self.app.services.config)
try:
if local_svc.disable_plugin(name):
success = True
except Exception as e:
printer.warning(f"Could not disable local plugin: {e}")
if success:
printer.success(f"Plugin {name} disabled successfully{' remotely' if is_remote else ' locally'}.")
# If any remote operation was performed, trigger a sync to update local cache immediately
if is_remote and self.app.services.mode == "remote":
try:
import os
cache_dir = os.path.join(self.app.services.config_svc.get_default_dir(), "remote_plugins")
# We use a dummy subparser choice check bypass by passing force_sync=True
# or just letting the hasher handle it.
self.app.plugins._import_remote_plugins_to_argparse(
self.app.services.plugins,
self.app.subparsers, # We'll need to make sure this is available
cache_dir,
force_sync=True
)
except Exception:
pass
elif getattr(args, "sync", False):
# The actual sync logic is performed in connapp.py during init
# if the --sync flag is detected in sys.argv
printer.success("Remote plugins synchronized successfully.")
elif args.list:
# We need to fetch both local and remote if in remote mode
local_plugins = {}
remote_plugins = {}
# Fetch depending on mode
if self.app.services.mode == "remote":
# For local we need to instantiate a local plugin service bypassing stub
from ..services.plugin_service import PluginService
local_svc = PluginService(self.app.services.config)
local_plugins = local_svc.list_plugins()
remote_plugins = self.app.services.plugins.list_plugins()
else:
local_plugins = self.app.services.plugins.list_plugins()
from rich.table import Table
table = Table(title="Available Plugins", show_header=True, header_style="bold cyan")
table.add_column("Plugin", style="cyan")
table.add_column("State", style="bold")
table.add_column("Origin", style="magenta")
# Populate local plugins
for name, details in local_plugins.items():
state = "Disabled" if not details.get("enabled", True) else "Active"
color = "red" if state == "Disabled" else "green"
if self.app.services.mode == "remote" and state == "Active":
if self.app.plugins.preferences.get(name) == "remote":
state = "Shadowed (Override by Remote)"
color = "yellow"
table.add_row(name, f"[{color}]{state}[/{color}]", "Local")
# Populate remote plugins
if self.app.services.mode == "remote":
for name, details in remote_plugins.items():
state = "Disabled" if not details.get("enabled", True) else "Active"
color = "red" if state == "Disabled" else "green"
if state == "Active":
pref = self.app.plugins.preferences.get(name, "local")
# If preference isn't remote and the plugin exists locally, local takes priority
if pref != "remote" and name in local_plugins:
state = "Shadowed (Override by Local)"
color = "yellow"
table.add_row(name, f"[{color}]{state}[/{color}]", "Remote")
if not local_plugins and not remote_plugins:
printer.console.print(" No plugins found.")
else:
printer.console.print(table)
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
-96
View File
@@ -1,96 +0,0 @@
import sys
import yaml
import inquirer
from .. import printer
from ..services.exceptions import ConnpyError, ProfileNotFoundError
from .forms import Forms
class ProfileHandler:
def __init__(self, app):
self.app = app
self.forms = Forms(app)
def dispatch(self, args):
if not self.app.case:
args.data[0] = args.data[0].lower()
actions = {"add": self.add, "del": self.delete, "mod": self.modify, "show": self.show}
return actions.get(args.action)(args)
def delete(self, args):
name = args.data[0]
try:
self.app.services.profiles.get_profile(name)
except ProfileNotFoundError:
printer.error(f"{name} not found")
sys.exit(2)
if name == "default":
printer.error("Can't delete default profile")
sys.exit(6)
question = [inquirer.Confirm("delete", message=f"Are you sure you want to delete {name}?")]
confirm = inquirer.prompt(question)
if confirm == None or not confirm["delete"]:
sys.exit(7)
try:
self.app.services.profiles.delete_profile(name)
printer.success(f"{name} deleted successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(8)
def show(self, args):
try:
profile = self.app.services.profiles.get_profile(args.data[0])
yaml_output = yaml.dump(profile, sort_keys=False, default_flow_style=False)
printer.data(args.data[0], yaml_output)
except ProfileNotFoundError:
printer.error(f"{args.data[0]} not found")
sys.exit(2)
def add(self, args):
name = args.data[0]
if name in self.app.services.profiles.list_profiles():
printer.error(f"Profile '{name}' already exists.")
sys.exit(4)
new_profile_data = self.forms.questions_profiles(name)
if not new_profile_data:
sys.exit(7)
try:
self.app.services.profiles.add_profile(name, new_profile_data)
printer.success(f"{name} added successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def modify(self, args):
name = args.data[0]
try:
profile = self.app.services.profiles.get_profile(name, resolve=False)
except ProfileNotFoundError:
printer.error(f"Profile '{name}' not found")
sys.exit(2)
old_profile = {"id": name, **profile}
edits = self.forms.questions_edit()
if edits == None:
sys.exit(7)
update_profile_data = self.forms.questions_profiles(name, edit=edits)
if not update_profile_data:
sys.exit(7)
if sorted(update_profile_data.items()) == sorted(old_profile.items()):
printer.info("Nothing to do here")
return
try:
self.app.services.profiles.update_profile(name, update_profile_data)
printer.success(f"{name} edited successfully")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
-484
View File
@@ -1,484 +0,0 @@
import os
import sys
import yaml
import threading
from rich.rule import Rule
from .. import printer
from ..services.exceptions import ConnpyError
from .help_text import get_instructions
class RunHandler:
def __init__(self, app):
self.app = app
self.print_lock = threading.Lock()
def dispatch(self, args):
if len(args.data) > 1:
args.action = "noderun"
actions = {
"noderun": self.node_run,
"generate": self.yaml_generate,
"generate_ai": self.ai_generate,
"run": self.yaml_run
}
return actions.get(args.action)(args)
def node_run(self, args):
nodes_filter = args.data[0]
# Resolve and filter nodes through context-aware list_nodes
try:
matched_nodes = self.app.services.nodes.list_nodes(nodes_filter)
except Exception:
matched_nodes = []
if not matched_nodes:
printer.error(f"No nodes found matching filter: {nodes_filter}")
sys.exit(2)
commands = [" ".join(args.data[1:])]
# Check for Preflight AI simulation
if getattr(args, "preflight_ai", False):
matched_node_names = [n.get("name") if isinstance(n, dict) else n for n in matched_nodes]
renderer = printer.BlockMarkdownRenderer()
first_chunk = True
status_context = printer.console.status("[ai_status]Simulating execution...[/ai_status]")
def callback(chunk):
nonlocal first_chunk
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[engineer][bold]Preflight AI Simulation[/bold][/engineer]", style="engineer"))
first_chunk = False
renderer.feed(chunk)
try:
status_context.start()
self.app.services.ai.predict_execution_results(
matched_node_names,
commands,
chunk_callback=callback
)
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[engineer][bold]Preflight AI Simulation[/bold][/engineer]", style="engineer"))
renderer.flush()
printer.console.print(Rule(style="engineer"))
except Exception as e:
printer.error(f"Preflight AI simulation failed: {e}")
sys.exit(1)
sys.exit(0)
try:
header_printed = False
if hasattr(args, 'test_expected') and args.test_expected:
# Mode: Test
def _on_node_complete(unique, node_output, node_status, node_result):
nonlocal header_printed
with self.print_lock:
if not header_printed:
printer.console.print(Rule("OUTPUT", style="header"))
header_printed = True
printer.test_panel(unique, node_output, node_status, node_result)
results = self.app.services.execution.test_commands(
nodes_filter=matched_nodes,
commands=commands,
expected=args.test_expected,
on_node_complete=_on_node_complete
)
printer.test_summary(results)
else:
# Mode: Normal Run
def _on_node_complete(unique, node_output, node_status):
nonlocal header_printed
with self.print_lock:
if not header_printed:
printer.console.print(Rule("OUTPUT", style="header"))
header_printed = True
printer.node_panel(unique, node_output, node_status)
results = self.app.services.execution.run_commands(
nodes_filter=matched_nodes,
commands=commands,
on_node_complete=_on_node_complete
)
printer.run_summary(results)
# Analyze execution results if requested
if getattr(args, "analyze", None) is not None:
printer.console.print()
renderer = printer.BlockMarkdownRenderer()
first_chunk = True
status_context = printer.console.status("[ai_status]Analyzing execution results...[/ai_status]")
def callback(chunk):
nonlocal first_chunk
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[architect][bold]Network Architect AI Analysis[/bold][/architect]", style="architect"))
first_chunk = False
renderer.feed(chunk)
query = args.analyze if args.analyze else " ".join(args.data[1:])
try:
status_context.start()
self.app.services.ai.analyze_execution_results(
results,
query=query,
chunk_callback=callback
)
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[architect][bold]Network Architect AI Analysis[/bold][/architect]", style="architect"))
renderer.flush()
printer.console.print(Rule(style="architect"))
except Exception as e:
printer.error(f"AI Analysis failed: {e}")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
def yaml_generate(self, args):
if os.path.exists(args.data[0]):
printer.error(f"File '{args.data[0]}' already exists.")
sys.exit(14)
else:
with open(args.data[0], "w") as file:
file.write(get_instructions("generate"))
printer.success(f"File {args.data[0]} generated successfully")
sys.exit()
def yaml_run(self, args):
path = args.data[0]
try:
with open(path, "r") as f:
playbook = yaml.load(f, Loader=yaml.FullLoader)
# Check preflight first before any task runs
if getattr(args, "preflight_ai", False):
preflight_failed = False
for task in playbook.get("tasks", []):
name = task.get("name", "Task")
nodelist = task.get("nodes", [])
commands = task.get("commands", [])
# Resolve nodes to names
try:
if isinstance(nodelist, str):
resolved_nodes = self.app.services.nodes.list_nodes(nodelist)
elif isinstance(nodelist, list):
resolved_nodes = []
for item in nodelist:
matches = self.app.services.nodes.list_nodes(item)
for m in matches:
if m not in resolved_nodes:
resolved_nodes.append(m)
else:
resolved_nodes = []
except Exception:
resolved_nodes = []
resolved_names = [n.get("name") if isinstance(n, dict) else n for n in resolved_nodes]
printer.console.print(f"\n[bold]Task: {name}[/bold] (Preflight for {len(resolved_names)} nodes)")
renderer = printer.BlockMarkdownRenderer()
first_chunk = True
status_context = printer.console.status("[ai_status]Simulating execution...[/ai_status]")
def callback(chunk):
nonlocal first_chunk
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title=f"[engineer][bold]Preflight AI Simulation: {name}[/bold][/engineer]", style="engineer"))
first_chunk = False
renderer.feed(chunk)
try:
status_context.start()
self.app.services.ai.predict_execution_results(
resolved_names,
commands,
chunk_callback=callback
)
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title=f"[engineer][bold]Preflight AI Simulation: {name}[/bold][/engineer]", style="engineer"))
renderer.flush()
printer.console.print(Rule(style="engineer"))
except Exception as e:
printer.error(f"Preflight AI simulation failed for task {name}: {e}")
preflight_failed = True
if preflight_failed:
sys.exit(1)
sys.exit(0)
# Standard run
results_all = {}
for task in playbook.get("tasks", []):
task_res = self.cli_run(task)
if task_res:
results_all.update(task_res)
# If analyze is enabled, run analysis on accumulated results
if getattr(args, "analyze", None) is not None:
printer.console.print()
renderer = printer.BlockMarkdownRenderer()
first_chunk = True
status_context = printer.console.status("[ai_status]Analyzing playbook execution results...[/ai_status]")
def callback(chunk):
nonlocal first_chunk
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[architect][bold]Network Architect AI Playbook Analysis[/bold][/architect]", style="architect"))
first_chunk = False
renderer.feed(chunk)
query = args.analyze if args.analyze else f"Playbook: {path}"
try:
status_context.start()
self.app.services.ai.analyze_execution_results(
results_all,
query=query,
chunk_callback=callback
)
if first_chunk:
try: status_context.stop()
except: pass
printer.console.print(Rule(title="[architect][bold]Network Architect AI Playbook Analysis[/bold][/architect]", style="architect"))
renderer.flush()
printer.console.print(Rule(style="architect"))
except Exception as e:
printer.error(f"AI Analysis failed: {e}")
except Exception as e:
printer.error(f"Failed to run playbook {path}: {e}")
sys.exit(10)
def cli_run(self, script):
name = script.get("name", "Task")
try:
action = script["action"]
nodelist = script["nodes"]
commands = script["commands"]
variables = script.get("variables")
output_cfg = script["output"]
options = script.get("options", {})
except KeyError as e:
printer.error(f"[{name}] '{e.args[0]}' is mandatory in script")
sys.exit(11)
stdout = (output_cfg == "stdout")
folder = output_cfg if output_cfg not in [None, "stdout"] else None
prompt = options.get("prompt")
# Resolve and filter nodes through context-aware list_nodes
try:
if isinstance(nodelist, str):
resolved_nodes = self.app.services.nodes.list_nodes(nodelist)
elif isinstance(nodelist, list):
resolved_nodes = []
for item in nodelist:
matches = self.app.services.nodes.list_nodes(item)
for m in matches:
if m not in resolved_nodes:
resolved_nodes.append(m)
else:
resolved_nodes = []
except Exception:
resolved_nodes = []
if not resolved_nodes:
printer.error(f"[{name}] No nodes found matching filter: {nodelist}")
sys.exit(11)
nodelist = resolved_nodes
results = {}
try:
header_printed = False
if action == "run":
# If stdout is true, we stream results as they arrive
def _on_run_complete(unique, node_output, node_status):
nonlocal header_printed
if stdout:
with self.print_lock:
if not header_printed:
printer.console.print(Rule(name.upper(), style="header"))
header_printed = True
printer.node_panel(unique, node_output, node_status)
results = self.app.services.execution.run_commands(
nodes_filter=nodelist,
commands=commands,
variables=variables,
parallel=options.get("parallel", 10),
timeout=options.get("timeout", 20),
folder=folder,
prompt=prompt,
on_node_complete=_on_run_complete
)
# Final Summary
if not stdout and not folder:
with self.print_lock:
printer.console.print(Rule(name.upper(), style="header"))
for unique, data in results.items():
output = data["output"] if isinstance(data, dict) else data
printer.node_panel(unique, output, 0)
# ALWAYS show the aggregate execution summary at the end
printer.run_summary(results)
elif action == "test":
expected = script.get("expected", [])
# Show test_panel per node ONLY if stdout is True
def _on_test_complete(unique, node_output, node_status, node_result):
nonlocal header_printed
if stdout:
with self.print_lock:
if not header_printed:
printer.console.print(Rule(name.upper(), style="header"))
header_printed = True
printer.test_panel(unique, node_output, node_status, node_result)
results = self.app.services.execution.test_commands(
nodes_filter=nodelist,
commands=commands,
expected=expected,
variables=variables,
parallel=options.get("parallel", 10),
timeout=options.get("timeout", 20),
folder=folder,
prompt=prompt,
on_node_complete=_on_test_complete
)
# ALWAYS show the aggregate summary at the end
printer.test_summary(results)
return results
except ConnpyError as e:
printer.error(str(e))
return {}
def ai_generate(self, args):
from rich.prompt import Prompt
from rich.rule import Rule
from rich.panel import Panel
from rich.syntax import Syntax
dest_file = args.data[0]
if os.path.exists(dest_file):
printer.error(f"File '{dest_file}' already exists.")
sys.exit(14)
chat_history = []
# Consistent layout opening matching global AI (engineer style)
from rich.markdown import Markdown
printer.console.print(Rule(style="engineer"))
printer.console.print(Markdown("**Playbook Builder AI**: Welcome! Describe the automation workflow you want to design.\nType **exit** to quit.\n"))
printer.console.print(Rule(style="engineer"))
while True:
try:
user_prompt = Prompt.ask("[user_prompt]User[/user_prompt]")
except (KeyboardInterrupt, EOFError):
printer.console.print()
printer.warning("Operation cancelled by user.")
break
if user_prompt.strip().lower() in ["exit", "quit"]:
printer.info("Exiting AI Assistant.")
break
if not user_prompt.strip():
continue
printer.console.print()
renderer = printer.BlockMarkdownRenderer()
first_chunk = True
status_context = printer.console.status("[ai_status]Agent is thinking...[/ai_status]")
def callback(chunk):
nonlocal first_chunk
if first_chunk:
try:
status_context.stop()
except:
pass
printer.console.print(Rule(title="[engineer][bold]Playbook Builder AI[/bold][/engineer]", style="engineer"))
first_chunk = False
renderer.feed(chunk)
try:
status_context.start()
res = self.app.services.ai.build_playbook_chat(
user_prompt,
chat_history=chat_history,
chunk_callback=callback
)
if first_chunk:
try:
status_context.stop()
except:
pass
renderer.flush()
if not first_chunk:
printer.console.print(Rule(style="engineer"))
# Update history
if res and "chat_history" in res:
chat_history = res["chat_history"]
# Check if the agent returned a validated playbook YAML
if res and "playbook_yaml" in res and res["playbook_yaml"]:
yaml_content = res["playbook_yaml"]
printer.console.print()
printer.success("Playbook YAML successfully generated and validated.")
# Show the YAML inside a beautiful panel matching AI style (with engineer borders)
syntax = Syntax(yaml_content, "yaml", theme="ansi_dark", word_wrap=True, background_color="default")
panel = Panel(syntax, title="[engineer][bold]Resulting Playbook[/bold][/engineer]", border_style="engineer", expand=False)
printer.console.print(panel)
# Ask if the user wants to save it
try:
save_confirm = Prompt.ask(
f"\nDo you want to save this playbook to '{dest_file}'?",
choices=["y", "n", "run"],
default="y"
)
except (KeyboardInterrupt, EOFError):
printer.console.print()
printer.warning("Saving skipped.")
break
choice = save_confirm.strip().lower()
if choice in ["y", "yes", "run"]:
with open(dest_file, "w") as f:
f.write(yaml_content)
printer.success(f"Playbook saved successfully to '{dest_file}'")
if choice == "run":
printer.console.print()
printer.info("Executing the saved playbook...")
self.yaml_run(args)
break
else:
printer.warning("Playbook not saved. You can continue describing changes or exit.")
except Exception as e:
printer.error(f"Error in AI chat: {e}")
-162
View File
@@ -1,162 +0,0 @@
import sys
import yaml
import inquirer
from .. import printer
class SSOHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
if self.app.services.mode == "remote":
printer.error("SSO management commands are only available in local/server-side mode.")
sys.exit(1)
# Parse actions from argparse mutually exclusive options
if getattr(args, "add", None):
args.action = "add"
args.provider = args.add[0]
elif getattr(args, "delete", None):
args.action = "del"
args.provider = args.delete[0]
elif getattr(args, "list", False):
args.action = "list"
elif getattr(args, "show", None):
args.action = "show"
args.provider = args.show[0]
action = getattr(args, "action", None)
if action == "add":
return self.add_provider(args)
elif action == "del":
return self.delete_provider(args)
elif action == "list":
return self.list_providers(args)
elif action == "show":
return self.show_provider(args)
else:
printer.error(f"Unknown action: {action}")
sys.exit(1)
def add_provider(self, args):
provider = args.provider
sso = self.app.config.config.get("sso", {})
providers = sso.setdefault("providers", {})
existing = providers.get(provider, {})
if existing:
printer.warning(f"SSO Provider '{provider}' already exists. Overwriting/Editing it.")
# Interactive questionnaire
questions = [
inquirer.Text("jwks_url", message="JWKS URL (optional, press Enter to skip)", default=existing.get("jwks_url", "")),
inquirer.Text("secret", message="Client Secret / Shared Secret (optional, press Enter to skip)", default=existing.get("secret", "")),
inquirer.Text("username_claim", message="Username Claim", default=existing.get("username_claim", "sub")),
inquirer.Text("algorithms", message="Algorithms (comma separated)", default=",".join(existing.get("algorithms", ["RS256"]))),
inquirer.Text("allowed_domains", message="Allowed/Trusted Email Domains (comma separated, optional)", default=",".join(existing.get("allowed_domains", [])))
]
answers = inquirer.prompt(questions)
if not answers:
printer.warning("Operation cancelled.")
sys.exit(130)
jwks_url = answers["jwks_url"].strip()
secret = answers["secret"].strip()
username_claim = answers["username_claim"].strip()
algorithms_str = answers["algorithms"].strip()
allowed_domains_str = answers.get("allowed_domains", "").strip()
if not jwks_url and not secret:
printer.error("You must configure either a JWKS URL or a Secret.")
sys.exit(1)
if not username_claim:
printer.error("Username claim cannot be empty.")
sys.exit(1)
algorithms = [alg.strip() for alg in algorithms_str.split(",") if alg.strip()]
if not algorithms:
algorithms = ["RS256"]
allowed_domains = [domain.strip() for domain in allowed_domains_str.split(",") if domain.strip()]
provider_data = {
"username_claim": username_claim,
"algorithms": algorithms
}
if jwks_url:
provider_data["jwks_url"] = jwks_url
if secret:
provider_data["secret"] = secret
if allowed_domains:
provider_data["allowed_domains"] = allowed_domains
providers[provider] = provider_data
# Save config
try:
self.app.services.config_svc.update_setting("sso", sso)
printer.success(f"SSO Provider '{provider}' saved successfully.")
except Exception as e:
printer.error(f"Failed to save SSO configuration: {e}")
sys.exit(1)
def delete_provider(self, args):
provider = args.provider
sso = self.app.config.config.get("sso", {})
providers = sso.get("providers", {})
if provider not in providers:
printer.error(f"SSO Provider '{provider}' not found.")
sys.exit(1)
# Confirm delete
questions = [inquirer.Confirm("confirm", message=f"Are you sure you want to delete SSO Provider '{provider}'?", default=False)]
answers = inquirer.prompt(questions)
if not answers or not answers["confirm"]:
printer.info("Delete cancelled.")
return
del providers[provider]
# Save config
try:
self.app.services.config_svc.update_setting("sso", sso)
printer.success(f"SSO Provider '{provider}' deleted successfully.")
except Exception as e:
printer.error(f"Failed to save SSO configuration: {e}")
sys.exit(1)
def list_providers(self, args):
sso = self.app.config.config.get("sso", {})
providers = sso.get("providers", {})
if not providers:
printer.warning("No SSO providers configured.")
return
# Print list in YAML format
providers_list = list(providers.keys())
yaml_str = yaml.dump(providers_list, sort_keys=False, default_flow_style=False)
printer.data("Configured SSO Providers", yaml_str)
def show_provider(self, args):
provider = args.provider
sso = self.app.config.config.get("sso", {})
providers = sso.get("providers", {})
if provider not in providers:
printer.error(f"SSO Provider '{provider}' not found.")
sys.exit(1)
data = providers[provider]
# Mask client secret for display if it's sensitive and not an env var starting with $
display_data = data.copy()
secret = display_data.get("secret")
if secret and not secret.startswith("$"):
display_data["secret"] = "********"
yaml_str = yaml.dump(display_data, sort_keys=False, default_flow_style=False)
printer.data(f"SSO Provider: {provider}", yaml_str)
-126
View File
@@ -1,126 +0,0 @@
import sys
import yaml
from .. import printer
class SyncHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
action = getattr(args, "action", None)
actions = {
"login": self.login,
"logout": self.logout,
"status": self.status,
"list": self.list_backups,
"once": self.once,
"restore": self.restore,
"start": self.start,
"stop": self.stop
}
handler = actions.get(action)
if handler:
return handler(args)
return self.status(args)
def login(self, args):
self.app.services.sync.login()
def logout(self, args):
self.app.services.sync.logout()
def status(self, args):
status = self.app.services.sync.check_login_status()
enabled = self.app.services.sync.sync_enabled
remote = self.app.services.sync.sync_remote
printer.info(f"Login Status: {status}")
printer.info(f"Auto-Sync: {'Enabled' if enabled else 'Disabled'}")
printer.info(f"Sync Remote Nodes: {'Yes' if remote else 'No'}")
def list_backups(self, args):
backups = self.app.services.sync.list_backups()
if backups:
yaml_output = yaml.dump(backups, sort_keys=False, default_flow_style=False)
printer.custom("backups", "")
print(yaml_output)
else:
printer.info("No backups found or not logged in.")
def once(self, args):
# Manual backup. We check if we should include remote nodes
remote_data = None
if self.app.services.sync.sync_remote and self.app.services.mode == "remote":
inventory = self.app.services.nodes.get_inventory()
# Merge with local settings
local_settings = self.app.services.config_svc.get_settings()
local_settings.pop("configfolder", None)
# Maintain proper config structure: {config: {}, connections: {}, profiles: {}}
remote_data = {
"config": local_settings,
"connections": inventory.get("connections", {}),
"profiles": inventory.get("profiles", {})
}
if self.app.services.sync.compress_and_upload(remote_data):
printer.success("Manual backup completed.")
def restore(self, args):
import inquirer
file_id = getattr(args, "id", None)
# Segmented flags
restore_config = getattr(args, "restore_config", False)
restore_nodes = getattr(args, "restore_nodes", False)
# If neither is specified, we restore ALL (backwards compatibility)
if not restore_config and not restore_nodes:
restore_config = True
restore_nodes = True
# 1. Analyze what we are about to restore
info = self.app.services.sync.analyze_backup_content(file_id)
if not info:
printer.error("Could not analyze backup content.")
return
# 2. Show detailed info
printer.info("Restoration Details:")
if restore_config:
print(f" - Local Settings: Yes")
print(f" - RSA Key (.osk): {'Yes' if info['has_key'] else 'No'}")
if restore_nodes:
target = "REMOTE" if self.app.services.mode == "remote" else "LOCAL"
print(f" - Nodes: {info['nodes']}")
print(f" - Folders: {info['folders']}")
print(f" - Profiles: {info['profiles']}")
print(f" - Destination: {target}")
print("")
questions = [inquirer.Confirm("confirm", message="Do you want to proceed with the restoration?", default=False)]
answers = inquirer.prompt(questions)
if not answers or not answers["confirm"]:
printer.info("Restore cancelled.")
return
# 3. Perform the actual restore
if self.app.services.sync.restore_backup(
file_id=file_id,
restore_config=restore_config,
restore_nodes=restore_nodes,
app_instance=self.app
):
printer.success("Restore completed successfully.")
def start(self, args):
self.app.services.config_svc.update_setting("sync", True)
self.app.services.sync.sync_enabled = True
printer.success("Auto-sync enabled.")
def stop(self, args):
self.app.services.config_svc.update_setting("sync", False)
self.app.services.sync.sync_enabled = False
printer.success("Auto-sync disabled.")
-499
View File
@@ -1,499 +0,0 @@
import os
import re
import sys
import time
import asyncio
import fcntl
import termios
import tty
from typing import Any, Dict, List, Optional, Callable
from textwrap import dedent
from rich.console import Console
from rich.panel import Panel
from rich.markdown import Markdown
from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.history import InMemoryHistory
from ..printer import connpy_theme
from connpy.utils import log_cleaner
from ..services.ai_service import AIService
class CopilotInterface:
def __init__(self, config, history=None, pt_input=None, pt_output=None, rich_file=None, session_state=None):
self.config = config
self.history = history or InMemoryHistory()
self.pt_input = pt_input
self.pt_output = pt_output
self.ai_service = AIService(config)
self.session_state = session_state if session_state is not None else {
'persona': 'engineer',
'trust_mode': False,
'memories': [],
'os': None,
'prompt': None
}
if rich_file:
self.console = Console(theme=connpy_theme, force_terminal=True, file=rich_file)
else:
self.console = Console(theme=connpy_theme)
self.mode_range, self.mode_single, self.mode_lines = 0, 1, 2
def _get_theme_color(self, style_name: str, fallback: str = "white") -> str:
"""Extract Hex or ANSI color name from the active rich theme."""
try:
style = connpy_theme.styles.get(style_name)
if style and style.color:
# If it's a standard color like 'green', Rich might return its hex triplet
if style.color.is_default: return fallback
return style.color.triplet.hex if style.color.triplet else style.color.name
except: pass
return fallback
async def run_session(self,
raw_bytes: bytes,
node_info: dict,
on_ai_call: Callable,
cmd_byte_positions: List[tuple] = None,
blocks: List[tuple] = None):
"""
Runs the interactive Copilot session.
on_ai_call: async function(active_buffer, question) -> result_dict
"""
from rich.rule import Rule
try:
# Prepare UI state
buffer = log_cleaner(raw_bytes.decode(errors='replace'))
# Use pre-calculated blocks if provided (remote mode), otherwise calculate locally (local mode)
if blocks is None:
last_line = buffer.split('\n')[-1].strip() if buffer.strip() else "(prompt)"
blocks = self.ai_service.build_context_blocks(raw_bytes, cmd_byte_positions, node_info, last_line=last_line)
state = {
'context_cmd': 1,
'total_cmds': len(blocks),
'total_lines': len(buffer.split('\n')),
'context_lines': min(50, len(buffer.split('\n'))),
'context_mode': self.mode_range,
'cancelled': False,
'toolbar_msg': '',
'msg_expiry': 0
}
# 1. Visual Separation
self.console.print("") # Real line break
self.console.print(Rule(title="[bold cyan] AI TERMINAL COPILOT [/bold cyan]", style="cyan"))
self.console.print(Panel(
"[dim]Type your question. Enter to send, Escape/Ctrl+C to cancel. Type / for commands.\n"
"Tab to change context mode. Ctrl+\u2191/\u2193 to adjust context. \u2191\u2193 for question history.[/dim]",
border_style="cyan"
))
self.console.print("\n") # Small space before the copilot prompt
bindings = KeyBindings()
@bindings.add('c-up')
def _(event):
if state['context_mode'] == self.mode_lines:
state['context_lines'] = min(state['context_lines'] + 50, state['total_lines'])
else:
state['context_cmd'] = min(state['context_cmd'] + 1, state['total_cmds'])
event.app.invalidate()
@bindings.add('c-down')
def _(event):
if state['context_mode'] == self.mode_lines:
state['context_lines'] = max(state['context_lines'] - 50, min(50, state['total_lines']))
else:
state['context_cmd'] = max(state['context_cmd'] - 1, 1)
event.app.invalidate()
@bindings.add('tab')
def _(event):
buf = event.current_buffer
# If typing a slash command (no spaces yet), use tab to autocomplete inline
if buf.text.startswith('/') and ' ' not in buf.text:
buf.complete_next()
else:
state['context_mode'] = (state['context_mode'] + 1) % 3
event.app.invalidate()
@bindings.add('escape', eager=True)
@bindings.add('c-c')
def _(event):
state['cancelled'] = True
event.app.exit(result='')
def get_active_buffer():
if state['context_mode'] == self.mode_lines:
return '\n'.join(buffer.split('\n')[-state['context_lines']:])
idx = max(0, state['total_cmds'] - state['context_cmd'])
start, end, preview = blocks[idx]
if state['context_mode'] == self.mode_single:
active_raw = raw_bytes[start:end]
else:
# Concat only the bytes of valid blocks to skip intermediate empty/cancelled prompt noise
active_raw = b"".join(raw_bytes[b[0]:b[1]] for b in blocks[idx:])
return preview + "\n" + log_cleaner(active_raw.decode(errors='replace'))
def get_prompt_text():
import html
# Always use user_prompt color for the Ask prompt
color = self._get_theme_color("user_prompt", "cyan")
if state['context_mode'] == self.mode_lines:
text = html.escape(f"Ask [Ctx: {state['context_lines']}/{state['total_lines']}L]: ")
return HTML(f'<style fg="{color}">{text}</style>')
active = get_active_buffer()
lines_count = len(active.split('\n'))
mode_str = {self.mode_range: "Range", self.mode_single: "Cmd"}[state['context_mode']]
text = html.escape(f"Ask [{mode_str} {state['context_cmd']} ~{lines_count}L]: ")
return HTML(f'<style fg="{color}">{text}</style>')
from prompt_toolkit.application.current import get_app
def get_toolbar():
import html
app = get_app()
c_warning = self._get_theme_color("warning", "yellow")
if app and app.current_buffer:
text = app.current_buffer.text
# Only show command help if typing the first command and there are no spaces
if text.startswith('/') and ' ' not in text:
commands = ['/os', '/prompt', '/architect', '/engineer', '/trust', '/untrust', '/memorize', '/clear']
matches = [c for c in commands if c.startswith(text.lower())]
if matches:
m_text = html.escape(f"Available: {' '.join(matches)}")
return HTML(f'<style fg="{c_warning}">{m_text}</style>' + " " * 20)
m_label = {self.mode_range: "RANGE", self.mode_single: "SINGLE", self.mode_lines: "LINES"}[state['context_mode']]
if state['context_mode'] == self.mode_lines:
base_str = f'\u25b6 Ctrl+\u2191/\u2193 adjusts by 50 lines [Tab: {m_label}]'
else:
idx = max(0, state['total_cmds'] - state['context_cmd'])
def clean_preview(text):
# Clean newlines and the initial prompt (all up to #, > or $) to leave only the command
original = text.strip().replace('\r', '').replace('\n', ' ')
cleaned = re.sub(r'^.*?[#>\$]\s*', '', original)
# If cleaning the prompt leaves us with an empty string (e.g. it was just "iol#"), return the original
return cleaned if cleaned else original
if state['context_mode'] == self.mode_range:
range_blocks = blocks[idx:]
# If there is more than one block, the last one is always the empty/current prompt. We omit it visually.
if len(range_blocks) > 1:
range_blocks = range_blocks[:-1]
# Clean and truncate very long commands so they don't break the UI
previews = []
for b in range_blocks:
p = clean_preview(b[2])
if p:
# Truncar comandos individuales largos
if len(p) > 25: p = p[:22] + "..."
previews.append(p)
if not previews:
desc = clean_preview(blocks[idx][2])
elif len(previews) <= 3:
desc = " + ".join(previews)
else:
desc = f"{previews[0]} + {previews[1]} + {previews[2]} ... (+{len(previews)-3})"
else:
# Modo SINGLE original
desc = clean_preview(blocks[idx][2])
base_str = f'\u25b6 {desc} [Tab: {m_label}]'
# Wrap base_str in a style to maintain consistency and avoid glitches
# The fg color will be inherited from bottom-toolbar global style if not specified here
base_html = f'<span>{html.escape(base_str)}</span>'
res_html = base_html
if state.get('toolbar_msg'):
if time.time() < state.get('msg_expiry', 0):
msg = html.escape(state['toolbar_msg'])
res_html = f'<style fg="{c_warning}">⚙️ {msg}</style> | ' + base_html
else:
state['toolbar_msg'] = ''
# Pad with spaces to ensure the line is cleared when the message disappears
return HTML(res_html + " " * 20)
from prompt_toolkit.completion import Completer, Completion
class SlashCommandCompleter(Completer):
def get_completions(self, document, complete_event):
text = document.text_before_cursor
if text.startswith('/'):
parts = text.split()
# Only autocomplete the first word
if len(parts) <= 1 or (len(parts) == 1 and not text.endswith(' ')):
cmd_part = parts[0] if parts else text
commands = [
('/os', 'Set device OS (e.g. cisco_ios)'),
('/prompt', 'Override prompt regex'),
('/architect', 'Switch to Architect persona'),
('/engineer', 'Switch to Engineer persona'),
('/trust', 'Enable auto-execute'),
('/untrust', 'Disable auto-execute'),
('/memorize', 'Add fact to memory'),
('/clear', 'Clear memory')
]
for cmd, desc in commands:
if cmd.startswith(cmd_part.lower()):
yield Completion(cmd, start_position=-len(cmd_part), display_meta=desc)
copilot_completer = SlashCommandCompleter()
while True:
# 2. Ask question
from prompt_toolkit.styles import Style
c_contrast = self._get_theme_color("contrast", "gray")
ui_style = Style.from_dict({
'bottom-toolbar': f'fg:{c_contrast}',
})
session = PromptSession(
history=self.history,
input=self.pt_input,
output=self.pt_output,
completer=copilot_completer,
reserve_space_for_menu=0,
style=ui_style
)
try:
# We use an internal try/finally to ensure that if something fails in prompt_async,
# we don't leave the terminal in a strange state.
question = await session.prompt_async(
get_prompt_text,
key_bindings=bindings,
bottom_toolbar=get_toolbar
)
except (KeyboardInterrupt, EOFError):
state['cancelled'] = True
question = ""
if state['cancelled'] or not question.strip() or question.strip().lower() in ['cancel', 'exit', 'quit']:
return "cancel", None, None
# 3. Process Input via AIService
directive = self.ai_service.process_copilot_input(question, self.session_state)
if directive["action"] == "state_update":
state['toolbar_msg'] = directive['message']
state['msg_expiry'] = time.time() + 3 # 3 seconds timeout
async def delayed_refresh():
await asyncio.sleep(3.1)
# Only invalidate if the message hasn't been replaced by a newer one
if state.get('toolbar_msg') == directive['message']:
state['toolbar_msg'] = '' # Explicitly clear
try:
from prompt_toolkit.application.current import get_app
app = get_app()
if app: app.invalidate()
except: pass
asyncio.create_task(delayed_refresh())
# Move the cursor up and clean the line so the new prompt replaces the previous one
sys.stdout.write('\x1b[1A\x1b[2K')
sys.stdout.flush()
continue
else:
# Clean the toolbar message when a real question is asked
state['toolbar_msg'] = ''
clean_question = directive.get("clean_prompt", question)
overrides = directive.get("overrides", {})
# Merge node_info with session_state and overrides
merged_node_info = node_info.copy()
if self.session_state['os']: merged_node_info['os'] = self.session_state['os']
if self.session_state['prompt']: merged_node_info['prompt'] = self.session_state['prompt']
merged_node_info['persona'] = self.session_state['persona']
merged_node_info['trust'] = self.session_state['trust_mode']
merged_node_info['memories'] = list(self.session_state['memories'])
for k, v in overrides.items():
merged_node_info[k] = v
# Enrich question
past = self.history.get_strings()
if len(past) > 1:
clean_past = [q for q in past[-6:-1] if not q.startswith('/')]
if clean_past:
history_text = "\n".join(f"- {q}" for q in clean_past)
clean_question = f"Previous questions:\n{history_text}\n\nCurrent Question:\n{clean_question}"
# 3. AI Execution
# Use persona from overrides (one-shot) or from session state
active_persona = merged_node_info.get('persona', self.session_state.get('persona', 'engineer'))
persona_color = self._get_theme_color(active_persona, fallback="cyan")
persona_title = "Network Architect" if active_persona == "architect" else "Network Engineer"
active_buffer = get_active_buffer()
live_text = ""
first_chunk = True
from rich.rule import Rule
from rich.status import Status
from connpy.printer import IncrementalMarkdownParser
md_parser = IncrementalMarkdownParser(console=self.console)
status_spinner = Status(
f"[bold {persona_color}]{persona_title}:[/bold {persona_color}] [dim]Thinking...[/dim]",
console=self.console,
spinner="dots"
)
status_spinner.start()
def on_chunk(text):
nonlocal live_text, first_chunk
if first_chunk:
status_spinner.stop()
# Print header rule before first chunk arrives
self.console.print(Rule(
f"[bold {persona_color}]{persona_title}[/bold {persona_color}]",
style=persona_color
))
first_chunk = False
live_text += text
md_parser.feed(text)
# Check for interruption during AI call
ai_task = asyncio.create_task(on_ai_call(active_buffer, clean_question, on_chunk, merged_node_info))
try:
while not ai_task.done():
await asyncio.sleep(0.05)
result = await ai_task
except asyncio.CancelledError:
status_spinner.stop()
return "cancel", None, None
# Ensure spinner is stopped if no chunks arrived
if first_chunk:
status_spinner.stop()
# Close the streamed output with a Rule
if not first_chunk:
md_parser.flush()
self.console.print(Rule(style=persona_color))
if not result or result.get("error"):
if first_chunk and result and result.get("error"):
self.console.print(f"[red]Error: {result['error']}[/red]")
return "cancel", None, None
# If no chunks were streamed but we have a guide, print it as a panel
if first_chunk and result and result.get("guide"):
self.console.print(Panel(Markdown(result["guide"]), title=f"[bold {persona_color}]{persona_title}[/bold {persona_color}]", border_style=persona_color))
commands = result.get("commands", [])
if not commands:
self.console.print("")
return "continue", None, None
risk = result.get("risk_level", "low")
risk_style = {"low": "success", "high": "warning", "destructive": "error"}.get(risk, "success")
style_color = self._get_theme_color(risk_style, fallback="green")
cmd_text = "\n".join(f" {i+1}. {c}" for i, c in enumerate(commands))
# Explicitly use 'bold style_color' for both TITLE and BORDER to ensure maximum consistency
self.console.print(Panel(cmd_text, title=f"[bold {style_color}]Suggested Commands [{risk.upper()}][/bold {style_color}]", border_style=f"bold {style_color}"))
if merged_node_info.get('trust', False) and risk != "destructive":
self.console.print(f"[dim]⚙️ Auto-executing (Trust Mode)[/dim]")
return "send_all", commands, None
confirm_session = PromptSession(input=self.pt_input, output=self.pt_output)
c_bindings = KeyBindings()
@c_bindings.add('escape', eager=True)
@c_bindings.add('c-c')
def _(ev): ev.app.exit(result='n')
import html
try:
p_text = html.escape(f"Send? (y/n/e/range) [n]: ")
# Use the EXACT same style_color and force bold="true" for Prompt-Toolkit
action = await confirm_session.prompt_async(HTML(f'<style fg="{style_color}" bold="true">{p_text}</style>'), key_bindings=c_bindings)
except (KeyboardInterrupt, EOFError):
self.console.print("")
return "continue", None, None
def parse_indices(text, max_len):
"""Helper to parse '1-3, 5, 7' into [0, 1, 2, 4, 6]."""
indices = []
# Replace commas with spaces and split
parts = text.replace(',', ' ').split()
for part in parts:
if '-' in part:
try:
start, end = map(int, part.split('-'))
# Ensure inclusive and 0-indexed
indices.extend(range(start-1, end))
except: continue
elif part.isdigit():
indices.append(int(part)-1)
# Filter valid indices and remove duplicates
return [i for i in sorted(set(indices)) if 0 <= i < max_len]
action_l = (action or "n").lower().strip()
if action_l in ('y', 'yes', 'all'):
return "send_all", commands, None
# Check for numeric selection (e.g., "1, 2-4")
if re.match(r'^[0-9,\-\s]+$', action_l):
selected_idxs = parse_indices(action_l, len(commands))
if selected_idxs:
return "send_all", [commands[i] for i in selected_idxs], None
elif action_l.startswith('e'):
# Check if it's a selective edit like 'e1-2'
selection_str = action_l[1:].strip()
if selection_str:
idxs = parse_indices(selection_str, len(commands))
cmds_to_edit = [commands[i] for i in idxs] if idxs else commands
else:
cmds_to_edit = commands
target = "\n".join(cmds_to_edit)
e_bindings = KeyBindings()
@e_bindings.add('c-j')
def _(ev): ev.app.exit(result=ev.app.current_buffer.text)
@e_bindings.add('escape', 'enter')
def _(ev): ev.app.exit(result=ev.app.current_buffer.text)
@e_bindings.add('escape')
def _(ev): ev.app.exit(result='')
c_edit = self._get_theme_color("user_prompt", "cyan")
import html
e_text = html.escape("Edit (Ctrl+Enter or Esc+Enter to submit):\n")
try:
edited = await confirm_session.prompt_async(
HTML(f'<style fg="{c_edit}">{e_text}</style>'),
default=target, multiline=True, key_bindings=e_bindings
)
except (KeyboardInterrupt, EOFError):
self.console.print("")
return "continue", None, None
if edited and edited.strip():
# Split by lines to ensure core.py applies delay between each command
lines = [l.strip() for l in edited.split('\n') if l.strip()]
return "custom", None, lines
self.console.print("")
return "continue", None, None
return "cancel", None, None
finally:
state['cancelled'] = True
-190
View File
@@ -1,190 +0,0 @@
import sys
import os
import getpass
import yaml
from .. import printer
from ..services.exceptions import ConnpyError
class UserHandler:
def __init__(self, app):
self.app = app
def dispatch(self, args):
if self.app.services.mode == "remote":
printer.error("User management commands are only available in local/server-side mode.")
sys.exit(1)
# Parse actions from argparse mutually exclusive options
if getattr(args, "add", None):
args.action = "add"
args.username = args.add[0]
elif getattr(args, "delete", None):
args.action = "del"
args.username = args.delete[0]
elif getattr(args, "list", False):
args.action = "list"
elif getattr(args, "show", None):
args.action = "show"
args.username = args.show[0]
elif getattr(args, "regen_password", None):
args.action = "regen_password"
args.username = args.regen_password[0]
action = getattr(args, "action", None)
if action == "add":
return self.add_user(args)
elif action == "del":
return self.delete_user(args)
elif action == "list":
return self.list_users(args)
elif action == "show":
return self.show_user(args)
elif action == "regen_password":
return self.regen_password(args)
else:
printer.error(f"Unknown action: {action}")
sys.exit(1)
def add_user(self, args):
username = getattr(args, "username", None)
if not username:
printer.error("Username is required. Usage: connpy user --add <username>")
sys.exit(1)
custom_path = getattr(args, "path", None)
if custom_path:
custom_path = custom_path[0] if isinstance(custom_path, list) else custom_path
try:
password = getpass.getpass("Enter password for new user: ")
if not password:
printer.error("Password cannot be empty.")
sys.exit(1)
confirm = getpass.getpass("Confirm password: ")
if password != confirm:
printer.error("Passwords do not match.")
sys.exit(1)
except (KeyboardInterrupt, EOFError):
printer.warning("\nOperation cancelled.")
sys.exit(130)
try:
self.app.services.users.create_user(username, password, config_path=custom_path)
printer.success(f"User '{username}' created successfully.")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
except ValueError as e:
printer.error(str(e))
sys.exit(1)
except Exception as e:
printer.error(f"Failed to create user: {e}")
sys.exit(1)
def delete_user(self, args):
username = getattr(args, "username", None)
if not username:
printer.error("Username is required. Usage: connpy user --del <username>")
sys.exit(1)
try:
self.app.services.users.delete_user(username)
printer.success(f"User '{username}' deleted successfully.")
except ConnpyError as e:
printer.error(str(e))
sys.exit(1)
except ValueError as e:
printer.error(str(e))
sys.exit(1)
except Exception as e:
printer.error(f"Failed to delete user: {e}")
sys.exit(1)
def list_users(self, args):
try:
users = self.app.services.users.list_users()
if not users:
printer.warning("No users registered.")
return
# Format custom config path, falling back to computed default path instead of null/None
formatted_users = []
for u in users:
formatted_u = u.copy()
if not formatted_u.get("config_path"):
formatted_u["config_path"] = os.path.join(self.app.services.users.users_dir, formatted_u["username"])
formatted_users.append(formatted_u)
yaml_str = yaml.dump(formatted_users, sort_keys=False, default_flow_style=False)
printer.data("Registered Users", yaml_str)
except Exception as e:
printer.error(f"Failed to list users: {e}")
sys.exit(1)
def show_user(self, args):
username = getattr(args, "username", None)
if not username:
printer.error("Username is required. Usage: connpy user --show <username>")
sys.exit(1)
try:
user = self.app.services.users.get_user(username)
if not user:
printer.error(f"User '{username}' not found.")
sys.exit(1)
# Hide the password hash from the CLI output for safety
safe_user = {k: v for k, v in user.items() if k != "password_hash"}
if not safe_user.get("config_path"):
safe_user["config_path"] = os.path.join(self.app.services.users.users_dir, username)
yaml_str = yaml.dump(safe_user, sort_keys=False, default_flow_style=False)
printer.data(f"User: {username}", yaml_str)
except ValueError as e:
printer.error(str(e))
sys.exit(1)
except Exception as e:
printer.error(f"Failed to retrieve user details: {e}")
sys.exit(1)
def regen_password(self, args):
username = getattr(args, "username", None)
if not username:
printer.error("Username is required. Usage: connpy user --regen-password <username>")
sys.exit(1)
try:
user = self.app.services.users.get_user(username)
if not user:
printer.error(f"User '{username}' not found.")
sys.exit(1)
except ValueError as e:
printer.error(str(e))
sys.exit(1)
except Exception as e:
printer.error(f"Failed to retrieve user details: {e}")
sys.exit(1)
try:
new_password = getpass.getpass("Enter new password: ")
if not new_password:
printer.error("Password cannot be empty.")
sys.exit(1)
confirm = getpass.getpass("Confirm new password: ")
if new_password != confirm:
printer.error("Passwords do not match.")
sys.exit(1)
except (KeyboardInterrupt, EOFError):
printer.warning("\nOperation cancelled.")
sys.exit(130)
try:
self.app.services.users.admin_change_password(username, new_password)
printer.success(f"Password for user '{username}' regenerated successfully.")
except ValueError as e:
printer.error(str(e))
sys.exit(1)
except Exception as e:
printer.error(f"Failed to regenerate password: {e}")
sys.exit(1)
-139
View File
@@ -1,139 +0,0 @@
import re
import ast
import inquirer
class Validators:
def __init__(self, app):
self.app = app
def host_validation(self, answers, current, regex = "^.+$"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
return True
def profile_protocol_validation(self, answers, current, regex = "(^ssh$|^telnet$|^kubectl$|^docker$|^ssm$|^$)"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Pick between ssh, telnet, kubectl, docker, ssm or leave empty")
return True
def protocol_validation(self, answers, current, regex = "(^ssh$|^telnet$|^kubectl$|^docker$|^ssm$|^$|^@.+$)"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Pick between ssh, telnet, kubectl, docker, ssm, leave empty or @profile")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
return True
def profile_port_validation(self, answers, current, regex = "(^[0-9]*$)"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Pick a port between 1-65535, @profile o leave empty")
try:
port = int(current)
except ValueError:
port = 0
if current != "" and not 1 <= int(port) <= 65535:
raise inquirer.errors.ValidationError("", reason="Pick a port between 1-65535 or leave empty")
return True
def port_validation(self, answers, current, regex = "(^[0-9]*$|^@.+$)"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Pick a port between 1-65535, @profile or leave empty")
try:
port = int(current)
except ValueError:
port = 0
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
elif current != "" and not 1 <= int(port) <= 65535:
raise inquirer.errors.ValidationError("", reason="Pick a port between 1-65535, @profile o leave empty")
return True
def pass_validation(self, answers, current, regex = "(^@.+$)"):
profiles = current.split(",")
for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(i))
return True
def tags_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
elif current != "":
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
raise inquirer.errors.ValidationError("", reason="Tags should be a python dictionary.".format(current))
return True
def profile_tags_validation(self, answers, current):
if current != "":
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
raise inquirer.errors.ValidationError("", reason="Tags should be a python dictionary.".format(current))
return True
def jumphost_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
elif current != "":
if current not in self.app.nodes_list:
raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current))
return True
def profile_jumphost_validation(self, answers, current):
if current != "":
if current not in self.app.nodes_list:
raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current))
return True
def default_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
return True
def bulk_node_validation(self, answers, current, regex = "^[0-9a-zA-Z_.,$#-]+$"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
return True
def bulk_folder_validation(self, answers, current):
if not self.app.case:
current = current.lower()
candidate = current
if "/" in current:
candidate = current.split("/")[0]
matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != "" and len(matches) == 0:
raise inquirer.errors.ValidationError("", reason="Location {} don't exist".format(current))
return True
def bulk_host_validation(self, answers, current, regex = "^.+$"):
if not re.match(regex, current):
raise inquirer.errors.ValidationError("", reason="Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
hosts = current.split(",")
nodes = answers["ids"].split(",")
if len(hosts) > 1 and len(hosts) != len(nodes):
raise inquirer.errors.ValidationError("", reason="Hosts list should be the same length of nodes list")
return True
+116 -420
View File
@@ -1,23 +1,39 @@
import sys
import os
import json
import glob
import importlib.util
def load_txt_cache(filepath):
try:
with open(filepath, "r") as f:
return f.read().splitlines()
except FileNotFoundError:
return []
def _getallnodes(config):
#get all nodes on configfile
nodes = []
layer1 = [k for k,v in config["connections"].items() if isinstance(v, dict) and v["type"] == "connection"]
folders = [k for k,v in config["connections"].items() if isinstance(v, dict) and v["type"] == "folder"]
nodes.extend(layer1)
for f in folders:
layer2 = [k + "@" + f for k,v in config["connections"][f].items() if isinstance(v, dict) and v["type"] == "connection"]
nodes.extend(layer2)
subfolders = [k for k,v in config["connections"][f].items() if isinstance(v, dict) and v["type"] == "subfolder"]
for s in subfolders:
layer3 = [k + "@" + s + "@" + f for k,v in config["connections"][f][s].items() if isinstance(v, dict) and v["type"] == "connection"]
nodes.extend(layer3)
return nodes
def get_cwd(words, option=None, folderonly=False):
import glob
def _getallfolders(config):
#get all folders on configfile
folders = ["@" + k for k,v in config["connections"].items() if isinstance(v, dict) and v["type"] == "folder"]
subfolders = []
for f in folders:
s = ["@" + k + f for k,v in config["connections"][f[1:]].items() if isinstance(v, dict) and v["type"] == "subfolder"]
subfolders.extend(s)
folders.extend(subfolders)
return folders
def _getcwd(words, option, folderonly=False):
# Expand tilde to home directory if present
if words[-1].startswith("~"):
words[-1] = os.path.expanduser(words[-1])
# If option is not provided, try to infer it from the first word
if option is None and words:
option = words[0]
if words[-1] == option:
path = './*'
else:
@@ -35,21 +51,6 @@ def get_cwd(words, option=None, folderonly=False):
def _get_plugins(which, defaultdir):
# Path to core_plugins relative to this script
core_path = os.path.dirname(os.path.realpath(__file__)) + "/core_plugins"
remote_path = os.path.join(defaultdir, "remote_plugins")
# Load preferences
import json
pref_path = os.path.join(defaultdir, "plugin_preferences.json")
try:
with open(pref_path) as f:
preferences = json.load(f)
except Exception:
preferences = {}
# Load service mode
# We try to infer if we are in remote mode by checking config.yaml or .folder
# but for completion usually we just want to know if remote cache exists.
# However, to be strict we should check preferences.
def get_plugins_from_directory(directory):
enabled_files = []
@@ -60,38 +61,21 @@ def _get_plugins(which, defaultdir):
for file in os.listdir(directory):
# Check if the file is a Python file
if file.endswith('.py'):
name = os.path.splitext(file)[0]
enabled_files.append(name)
all_plugins[name] = os.path.join(directory, file)
enabled_files.append(os.path.splitext(file)[0])
all_plugins[os.path.splitext(file)[0]] = os.path.join(directory, file)
# Check if the file is a Python backup file
elif file.endswith('.py.bkp'):
name = os.path.splitext(os.path.splitext(file)[0])[0]
disabled_files.append(name)
disabled_files.append(os.path.splitext(os.path.splitext(file)[0])[0])
return enabled_files, disabled_files, all_plugins
# Get plugins from all directories
# Get plugins from both directories
user_enabled, user_disabled, user_all_plugins = get_plugins_from_directory(defaultdir + "/plugins")
core_enabled, core_disabled, core_all_plugins = get_plugins_from_directory(core_path)
remote_enabled, remote_disabled, remote_all_plugins = get_plugins_from_directory(remote_path)
# Calculate final paths respecting priorities and preferences
# Priority: User Local > Core Local > Remote (unless preferred)
# Start with core
final_all_plugins = core_all_plugins.copy()
# Override with user local
final_all_plugins.update(user_all_plugins)
# For remote, we only use them if:
# 1. They don't exist locally OR
# 2. Preference is explicitly 'remote'
for name, path in remote_all_plugins.items():
if name not in final_all_plugins or preferences.get(name) == "remote":
final_all_plugins[name] = path
# Combine enabled/disabled for the helper commands
enabled_files = list(set(user_enabled + core_enabled + [k for k,v in remote_all_plugins.items() if preferences.get(k) == "remote"]))
disabled_files = list(set(user_disabled + core_disabled))
# Combine the results from user and core plugins
enabled_files = user_enabled
disabled_files = user_disabled
all_plugins = {**user_all_plugins, **core_all_plugins} # Merge dictionaries
# Return based on the command
if which == "--disable":
@@ -102,320 +86,7 @@ def _get_plugins(which, defaultdir):
all_files = enabled_files + disabled_files
return all_files
elif which == "all":
return final_all_plugins
def _get_users(configdir):
import yaml
registry_file = os.path.join(configdir, "users", "registry.yaml")
if not os.path.exists(registry_file):
return []
try:
with open(registry_file, "r") as f:
data = yaml.safe_load(f) or {}
if isinstance(data, dict) and "users" in data:
return list(data["users"].keys())
except Exception:
pass
return []
def _get_sso_providers(configdir):
import yaml
config_file = os.path.join(configdir, "config.yaml")
if not os.path.exists(config_file):
return []
try:
with open(config_file, "r") as f:
data = yaml.safe_load(f) or {}
config_data = data.get("config", {})
if isinstance(config_data, dict):
sso = config_data.get("sso", {})
if isinstance(sso, dict):
providers = sso.get("providers", {})
if isinstance(providers, dict):
return list(providers.keys())
except Exception:
pass
return []
def _build_tree(nodes, folders, profiles, plugins, configdir):
"""Build the declarative CLI navigation tree.
Structure:
- dict: keys are completions + subnavigation.
"__extra__" adds dynamic data.
"__exclude_used__" filters already-typed words.
"*" absorbs unknown positional words and loops to a specific node.
- list: static choice completions.
- callable: dynamic completions (called with `words`, returns list).
- None: no further completions.
"""
_nodes = lambda w=None: list(nodes)
_folders = lambda w=None: list(folders)
_profiles = lambda w=None: list(profiles)
_nodes_folders = lambda w=None: list(nodes) + list(folders)
_profile_values = {"__extra__": _profiles}
# --- Stateful/Looping Nodes ---
# list nodes
list_nodes = {"__exclude_used__": True}
list_nodes.update({
"--format": {"*": list_nodes},
"--filter": {"*": list_nodes},
"*": list_nodes
})
# export / import / run loops
export_dict = {"--help": None, "-h": None}
export_dict.update({
"*": export_dict,
"__extra__": lambda w: get_cwd(w, "export", True) + [f for f in folders if not any(x in f for x in w[1:-1])]
})
import_dict = {"--help": None, "-h": None}
import_dict.update({
"*": import_dict,
"__extra__": lambda w: get_cwd(w, "import")
})
# --- Run Loop ---
# After the first positional argument (Node filter or YAML file),
# we stop suggesting nodes and only allow flags or commands.
run_after_node = {"--help": None, "-h": None}
run_after_node.update({
"--test": {"*": run_after_node},
"-t": {"*": run_after_node},
"--analyze": {"*": run_after_node},
"--preflight-ai": run_after_node,
"*": run_after_node # Consume commands
})
run_dict = {
"--generate": {"__extra__": lambda w: get_cwd(w, "--generate")},
"-g": {"__extra__": lambda w: get_cwd(w, "-g")},
"--generate-ai": {"__extra__": lambda w: get_cwd(w, "--generate-ai")},
"--analyze": {"*": run_after_node},
"--preflight-ai": run_after_node,
"--test": {"*": None},
"-t": {"*": None},
"--help": None,
"-h": None,
"__extra__": lambda w: get_cwd(w, "run") + list(nodes),
"*": run_after_node
}
# State Machine Definitions
mcp_dict = {
"list": None,
"add": {"*": {"*": {"*": None}}}, # name url [os]
"remove": {"*": None},
"enable": {"*": None},
"disable": {"*": None},
"--help": None, "-h": None
}
ai_dict = {"__exclude_used__": True, "--help": None, "-h": None}
for opt in ["--engineer-model", "--engineer-api-key", "--architect-model", "--architect-api-key"]:
ai_dict[opt] = {"*": ai_dict} # takes value, loops back
ai_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": ai_dict}
ai_dict["--architect-auth"] = {"__extra__": lambda w: get_cwd(w, "--architect-auth"), "*": ai_dict}
for opt in ["--debug", "--trust", "--list", "--list-sessions", "--session", "--resume", "--delete", "--delete-session", "-y"]:
ai_dict[opt] = ai_dict # takes no value, loops back
ai_dict["--mcp"] = mcp_dict
ai_dict["*"] = ai_dict
config_dict = {
"--allow-uppercase": ["true", "false"],
"--fzf": ["true", "false"],
"--completion": ["bash", "zsh"],
"--fzf-wrapper": ["bash", "zsh"],
"--service-mode": ["local", "remote"],
"--sync-remote": ["true", "false"],
"--help": None, "-h": None,
}
for opt in ["--keepalive", "--engineer-model", "--engineer-api-key", "--architect-model", "--architect-api-key", "--theme", "--remote", "--trusted-commands"]:
config_dict[opt] = {"*": config_dict}
config_dict["--configfolder"] = {"__extra__": lambda w: get_cwd(w, "--configfolder", True), "*": config_dict}
config_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": config_dict}
config_dict["--architect-auth"] = {"__extra__": lambda w: get_cwd(w, "--architect-auth"), "*": config_dict}
_users = lambda w=None: _get_users(configdir)
user_dict = {
"--add": {"*": {"--path": {"__extra__": lambda w: get_cwd(w, "--path", True), "*": None}}},
"--del": {"__extra__": _users},
"--rm": {"__extra__": _users},
"--show": {"__extra__": _users},
"--regen-password": {"__extra__": _users},
"--list": None,
"--ls": None,
"--help": None, "-h": None
}
_sso_providers = lambda w=None: _get_sso_providers(configdir)
sso_dict = {
"--add": {"__extra__": _sso_providers, "*": None},
"--del": {"__extra__": _sso_providers},
"--rm": {"__extra__": _sso_providers},
"--show": {"__extra__": _sso_providers},
"--list": None,
"--ls": None,
"--help": None, "-h": None
}
mv_state = {"__extra__": _nodes, "--help": None, "-h": None}
cp_state = {"__extra__": _nodes, "--help": None, "-h": None}
ls_state = {
"profiles": None,
"nodes": list_nodes,
"folders": None,
}
# --- Connect (default command) ---
# Long flags are offered; short forms (-d/-t) only used for navigation.
# Two states: before node (offer nodes + remaining long flags)
# after node (offer only remaining long flags, no more nodes)
connect_flags_long = ["--debug", "--sftp"]
connect_flags_all = ["--debug", "-d", "--sftp", "-t"]
# Post-node: only offer remaining long flags
connect_after_node = {"__exclude_used__": True}
for f in connect_flags_all:
connect_after_node[f] = connect_after_node
# Pre-node: offer nodes + remaining long flags, consume node → post-node state
connect_dict = {"__exclude_used__": True}
connect_dict["__extra__"] = lambda w: (
list(nodes) + list(folders) + (list(plugins.keys()) if plugins else [])
)
connect_dict["*"] = connect_after_node
for f in connect_flags_all:
connect_dict[f] = connect_dict
# --- Main Tree ---
return {
# Root: offer nodes + long flags; after a node go to post-node state
"__extra__": lambda w: list(nodes) + list(folders) + (list(plugins.keys()) if plugins else []),
"*": connect_after_node,
"--debug": connect_dict,
"-d": connect_dict,
"--sftp": connect_dict,
"-t": connect_dict,
"--add": {"profile": _profile_values},
"--del": {"profile": _profile_values, "__extra__": _nodes_folders},
"--rm": {"profile": _profile_values, "__extra__": _nodes_folders},
"--edit": {"profile": _profile_values, "__extra__": _nodes},
"--mod": {"profile": _profile_values, "__extra__": _nodes},
"--show": {"profile": _profile_values, "__extra__": _nodes},
"--help": None,
"-a": {"profile": _profile_values},
"-r": {"profile": _profile_values, "__extra__": _nodes_folders},
"-e": {"profile": _profile_values, "__extra__": _nodes},
"-s": {"profile": _profile_values, "__extra__": _nodes},
"profile": {
"--add": None, "--rm": _profiles, "--del": _profiles,
"--edit": _profiles, "--mod": _profiles, "--show": _profiles,
"--help": None,
"-a": None, "-r": _profiles, "-e": _profiles, "-s": _profiles, "-h": None,
},
"move": mv_state,
"mv": mv_state,
"copy": cp_state,
"cp": cp_state,
"list": ls_state,
"ls": ls_state,
"bulk": {"--file": None, "--help": None, "-f": None, "-h": None},
"run": run_dict,
"export": export_dict,
"import": import_dict,
"ai": ai_dict,
"api": {
"--start": None, "--restart": None, "--stop": None, "--debug": None,
"--help": None,
"-s": None, "-r": None, "-x": None, "-d": None, "-h": None,
},
"context": {
"--add": None, "--rm": None, "--del": None,
"--ls": None, "--set": None,
"--show": None, "--edit": None, "--mod": None,
"--help": None,
"-a": None, "-r": None, "-s": None, "-e": None, "-h": None,
},
"plugin": {
"--add": {"*": lambda w: get_cwd(w, "--add")},
"--update": {"*": lambda w: get_cwd(w, "--update")},
"--del": lambda w: _get_plugins("--del", configdir),
"--enable": lambda w: _get_plugins("--enable", configdir),
"--disable": lambda w: _get_plugins("--disable", configdir),
"--list": None, "--help": None,
"-h": None,
},
"user": user_dict,
"sso": sso_dict,
"login": {"--help": None, "-h": None, "*": None},
"logout": {"--help": None, "-h": None},
"config": config_dict,
"sync": {
"--login": None, "--logout": None,
"--status": None, "--list": None,
"--once": None, "--restore": None,
"--start": None, "--stop": None,
"--id": None, "--nodes": None, "--config": None,
"--help": None, "-h": None,
},
}
def resolve_completion(words, tree):
"""Navigate the tree following typed words, properly handling dynamic state loops."""
current = tree
for word in words[:-1]:
if isinstance(current, dict):
if word in current:
current = current[word]
elif "*" in current:
current = current["*"]
else:
return []
else:
return []
results = []
if isinstance(current, dict):
results = [k for k in current
if not k.startswith("__")
and not k.startswith("*")
and not (len(k) == 2 and k in ["mv", "cp", "ls"])
and not (len(k) == 2 and k[0] == "-" and k[1] != "-")]
if current.get("__exclude_used__"):
results = [r for r in results if r not in words[:-1]]
extra = current.get("__extra__")
if callable(extra):
results.extend(extra(words))
elif isinstance(extra, list):
results.extend(extra)
elif isinstance(current, list):
results = list(current)
elif callable(current):
results = list(current(words))
return results
return all_plugins
def main():
home = os.path.expanduser("~")
@@ -424,17 +95,17 @@ def main():
try:
with open(pathfile, "r") as f:
configdir = f.read().strip()
except (FileNotFoundError, IOError):
except:
configdir = defaultdir
cachefile = configdir + '/.config.cache.json'
nodes = load_txt_cache(configdir + '/.fzf_nodes_cache.txt')
folders = load_txt_cache(configdir + '/.folders_cache.txt')
profiles = load_txt_cache(configdir + '/.profiles_cache.txt')
plugins = _get_plugins("all", configdir)
defaultfile = configdir + '/config.json'
jsonconf = open(defaultfile)
config = json.load(jsonconf)
nodes = _getallnodes(config)
folders = _getallfolders(config)
profiles = list(config["profiles"].keys())
plugins = _get_plugins("all", defaultdir)
info = {}
info["config"] = None
info["config"] = config
info["nodes"] = nodes
info["folders"] = folders
info["profiles"] = profiles
@@ -446,62 +117,87 @@ def main():
positions = [1,3]
wordsnumber = int(sys.argv[positions[0]])
words = sys.argv[positions[1]:]
if wordsnumber == 2:
strings=["--add", "--del", "--rm", "--edit", "--mod", "--show", "mv", "move", "ls", "list", "cp", "copy", "profile", "run", "bulk", "config", "api", "ai", "export", "import", "--help", "plugin"]
if plugins:
strings.extend(plugins.keys())
strings.extend(nodes)
strings.extend(folders)
# --- Plugin completion ---
# Try new tree API first: _connpy_tree integrates into the main tree.
# Fall back to legacy _connpy_completion for older plugins.
if wordsnumber >= 3 and plugins and words[0] in plugins:
import importlib.util
plugin_path = plugins[words[0]]
elif wordsnumber >=3 and words[0] in plugins.keys():
try:
spec = importlib.util.spec_from_file_location("module.name", plugin_path)
spec = importlib.util.spec_from_file_location("module.name", plugins[words[0]])
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.get_cwd = get_cwd
except Exception:
plugin_completion = getattr(module, "_connpy_completion")
strings = plugin_completion(wordsnumber, words, info)
except:
exit()
# New API: _connpy_tree → integrate into main tree and use resolver
if hasattr(module, "_connpy_tree"):
plugin_node = module._connpy_tree(info)
tree = _build_tree(nodes, folders, profiles, plugins, configdir)
tree[words[0]] = plugin_node
strings = resolve_completion(words, tree)
# Legacy API: _connpy_completion → delegate entirely
elif hasattr(module, "_connpy_completion"):
import json
try:
with open(cachefile, "r") as jsonconf:
info["config"] = json.load(jsonconf)
except Exception:
try:
import yaml
with open(configdir + '/config.yaml', "r") as yamlconf:
info["config"] = yaml.safe_load(yamlconf)
except Exception:
info["config"] = {}
try:
plugin_completion = getattr(module, "_connpy_completion")
strings = plugin_completion(wordsnumber, words, info)
except Exception:
exit()
elif wordsnumber >= 3 and words[0] == "ai":
if wordsnumber == 3:
strings = ["--help", "--org", "--model", "--api_key"]
else:
exit()
strings = ["--org", "--model", "--api_key"]
elif wordsnumber == 3:
strings=[]
if words[0] == "profile":
strings=["--add", "--rm", "--del", "--edit", "--mod", "--show", "--help"]
if words[0] == "config":
strings=["--allow-uppercase", "--keepalive", "--completion", "--fzf", "--configfolder", "--openai-org", "--openai-org-api-key", "--openai-org-model","--help"]
if words[0] == "api":
strings=["--start", "--stop", "--restart", "--debug", "--help"]
if words[0] in ["--mod", "--edit", "-e", "--show", "-s", "--add", "-a", "--rm", "--del", "-r"]:
strings=["profile"]
if words[0] in ["list", "ls"]:
strings=["profiles", "nodes", "folders"]
if words[0] in ["bulk", "mv", "cp", "copy"]:
strings=["--help"]
if words[0] in ["--rm", "--del", "-r"]:
strings.extend(folders)
if words[0] in ["--rm", "--del", "-r", "--mod", "--edit", "-e", "--show", "-s", "mv", "move", "cp", "copy"]:
strings.extend(nodes)
if words[0] == "plugin":
strings = ["--help", "--add", "--update", "--del", "--enable", "--disable", "--list"]
if words[0] in ["run", "import", "export"]:
strings = ["--help"]
if words[0] == "export":
pathstrings = _getcwd(words, words[0], True)
else:
pathstrings = _getcwd(words, words[0])
strings.extend(pathstrings)
if words[0] == "run":
strings.extend(nodes)
# --- Tree-based completion ---
elif wordsnumber >= 4 and words[0] == "export" and words[1] != "--help":
strings = [item for item in folders if not any(word in item for word in words[:-1])]
elif wordsnumber >= 4 and words[0] in ["list", "ls"] and words[1] == "nodes":
options = ["--format", "--filter"]
strings = [item for item in options if not any(word in item for word in words[:-1])]
elif wordsnumber == 4:
strings=[]
if words[0] == "profile" and words[1] in ["--rm", "--del", "-r", "--mod", "--edit", "-e", "--show", "-s"]:
strings.extend(profiles)
if words[1] == "profile" and words[0] in ["--rm", "--del", "-r", "--mod", "--edit", "-e", "--show", "-s"]:
strings.extend(profiles)
if words[0] == "config" and words[1] == "--completion":
strings=["bash", "zsh"]
if words[0] == "config" and words[1] in ["--fzf", "--allow-uppercase"]:
strings=["true", "false"]
if words[0] == "config" and words[1] in ["--configfolder"]:
strings=_getcwd(words,words[1],True)
if words[0] == "plugin" and words[1] in ["--update", "--del", "--enable", "--disable"]:
strings=_get_plugins(words[1], defaultdir)
elif wordsnumber == 5 and words[0] == "plugin" and words[1] in ["--add", "--update"]:
strings=_getcwd(words, words[2])
else:
tree = _build_tree(nodes, folders, profiles, plugins, configdir)
strings = resolve_completion(words, tree)
exit()
current_word = words[-1] if len(words) > 0 else ""
matches = [s for s in strings if s.startswith(current_word)]
if app == "bash":
strings = [s if s.endswith('/') else f"'{s} '" for s in matches]
else:
strings = matches
strings = [s if s.endswith('/') else f"'{s} '" for s in strings]
print('\t'.join(strings))
if __name__ == '__main__':
+101 -309
View File
@@ -3,19 +3,15 @@
import json
import os
import re
import sys
import yaml
import shutil
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from pathlib import Path
from copy import deepcopy
from .hooks import MethodHook, ClassHook
from . import printer
class NoAliasDumper(yaml.SafeDumper):
def ignore_aliases(self, data):
return True
#functions and classes
@ClassHook
class configfile:
@@ -43,14 +39,13 @@ class configfile:
passwords.
'''
def __init__(self, conf = None, key = None, shared_config = None):
self._shared_config = shared_config
def __init__(self, conf = None, key = None):
'''
### Optional Parameters:
- conf (str): Path/file to config file. If left empty default
path is ~/.config/conn/config.yaml
path is ~/.config/conn/config.json
- key (str): Path/file to RSA key file. If left empty default
path is ~/.config/conn/.osk
@@ -58,243 +53,77 @@ class configfile:
'''
home = os.path.expanduser("~")
defaultdir = home + '/.config/conn'
if conf is None:
# Standard path: use ~/.config/conn and respect .folder redirection
self.anchor_path = defaultdir
self.defaultdir = defaultdir
Path(defaultdir).mkdir(parents=True, exist_ok=True)
pathfile = defaultdir + '/.folder'
try:
with open(pathfile, "r") as f:
configdir = f.read().strip()
except (FileNotFoundError, IOError):
with open(pathfile, "w") as f:
f.write(str(defaultdir))
configdir = defaultdir
self.defaultdir = configdir
self.file = configdir + '/config.yaml'
self.key = key or (configdir + '/.osk')
# Ensure redirected directories exist
Path(configdir).mkdir(parents=True, exist_ok=True)
Path(f"{configdir}/plugins").mkdir(parents=True, exist_ok=True)
# Backwards compatibility: Migrate from JSON to YAML only for default path
legacy_json = configdir + '/config.json'
legacy_noext = configdir + '/config'
legacy_file = None
if os.path.exists(legacy_json): legacy_file = legacy_json
elif os.path.exists(legacy_noext): legacy_file = legacy_noext
if not os.path.exists(self.file) and legacy_file:
try:
with open(legacy_file, 'r') as f:
old_data = json.load(f)
if not self._validate_config(old_data):
printer.warning(f"Legacy config {legacy_file} has invalid structure, skipping migration.")
else:
with open(self.file, 'w') as f:
yaml.dump(old_data, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
# Verify the written YAML can be read back correctly
with open(self.file, 'r') as f:
verify = yaml.safe_load(f)
if not self._validate_config(verify):
os.remove(self.file)
printer.warning("YAML verification failed after migration, keeping legacy config.")
else:
# Note: cachefile is derived later, we use temp one for migration sync
temp_cache = configdir + '/.config.cache.json'
with open(temp_cache, 'w') as f:
json.dump(old_data, f)
shutil.move(legacy_file, legacy_file + ".backup")
printer.success(f"Migrated legacy config ({len(old_data.get('connections',{}))} folders/nodes) into YAML and Cache successfully!")
except Exception as e:
if os.path.exists(self.file):
try: os.remove(self.file)
except OSError: pass
printer.warning(f"Failed to migrate legacy config: {e}")
self.defaultdir = defaultdir
Path(defaultdir).mkdir(parents=True, exist_ok=True)
Path(f"{defaultdir}/plugins").mkdir(parents=True, exist_ok=True)
pathfile = defaultdir + '/.folder'
try:
with open(pathfile, "r") as f:
configdir = f.read().strip()
except:
with open(pathfile, "w") as f:
f.write(str(defaultdir))
configdir = defaultdir
defaultfile = configdir + '/config.json'
defaultkey = configdir + '/.osk'
if conf == None:
self.file = defaultfile
else:
# Custom path (common in tests): isolate everything to the conf parent directory
self.file = os.path.abspath(conf)
configdir = os.path.dirname(self.file)
self.anchor_path = configdir
self.defaultdir = configdir
self.key = os.path.abspath(key) if key else (configdir + '/.osk')
# Sidecar files always live next to the config file (or in the redirected configdir)
self.cachefile = configdir + '/.config.cache.json'
self.fzf_cachefile = configdir + '/.fzf_nodes_cache.txt'
self.folders_cachefile = configdir + '/.folders_cache.txt'
self.profiles_cachefile = configdir + '/.profiles_cache.txt'
self.file = conf
if key == None:
self.key = defaultkey
else:
self.key = key
if os.path.exists(self.file):
config = self._loadconfig(self.file)
else:
config = self._createconfig(self.file)
self.config = config["config"]
self.connections = config["connections"]
self.profiles = config["profiles"]
if not os.path.exists(self.key):
self._createkey(self.key)
with open(self.key) as f:
self.privatekey = RSA.import_key(f.read())
f.close()
self.publickey = self.privatekey.publickey()
# Self-heal text caches if they are missing
if not os.path.exists(self.fzf_cachefile) or not os.path.exists(self.folders_cachefile) or not os.path.exists(self.profiles_cachefile):
self._generate_nodes_cache()
def get_effective_setting(self, key, default=None):
"""Get config setting with shared fallback for inheritable keys."""
val = self.config.get(key)
if key == "ai":
if val is not None:
if self._shared_config:
import copy
# Deep merge: shared as base, user overrides
base = copy.deepcopy(self._shared_config.config.get(key, {}))
if isinstance(base, dict) and isinstance(val, dict):
# Credential isolation:
# If user defines engineer credentials, discard shared ones
if "engineer_api_key" in val or "engineer_auth" in val:
base.pop("engineer_api_key", None)
base.pop("engineer_auth", None)
# If user defines architect credentials, discard shared ones
if "architect_api_key" in val or "architect_auth" in val:
base.pop("architect_api_key", None)
base.pop("architect_auth", None)
# Recursive update for inner dictionaries (like mcp_servers or model details)
def deep_merge(d1, d2):
for k, v in d2.items():
if isinstance(v, dict) and k in d1 and isinstance(d1[k], dict):
deep_merge(d1[k], v)
else:
d1[k] = copy.deepcopy(v)
deep_merge(base, val)
return base
return val
elif self._shared_config:
return self._shared_config.config.get(key, default)
return val if val is not None else default
def _validate_config(self, data):
"""Verify config data has the required structure."""
if not isinstance(data, dict):
return False
required = {"config", "connections", "profiles"}
return required.issubset(data.keys())
def _loadconfig(self, conf):
#Loads config file using dual cache
cache_exists = os.path.exists(self.cachefile)
yaml_time = os.path.getmtime(conf) if os.path.exists(conf) else 0
cache_time = os.path.getmtime(self.cachefile) if cache_exists else 0
if not cache_exists or yaml_time > cache_time:
with open(conf, 'r') as f:
data = yaml.safe_load(f)
if not self._validate_config(data):
# YAML is broken, try to recover from cache
if cache_exists:
printer.warning("Config file appears corrupt, recovering from cache...")
with open(self.cachefile, 'r') as f:
data = json.load(f)
if self._validate_config(data):
# Re-write the YAML from good cache
with open(conf, 'w') as f:
yaml.dump(data, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
return data
# Both broken or no cache - create fresh
printer.error("Config file is corrupt and no valid cache exists. Creating default config.")
return self._createconfig(conf)
try:
with open(self.cachefile, 'w') as f:
json.dump(data, f)
except Exception:
pass
return data
else:
with open(self.cachefile, 'r') as f:
data = json.load(f)
if not self._validate_config(data):
# Cache broken, try yaml
with open(conf, 'r') as f:
data = yaml.safe_load(f)
if self._validate_config(data):
return data
# Both broken
printer.error("Both config and cache are corrupt. Creating default config.")
return self._createconfig(conf)
return data
#Loads config file
jsonconf = open(conf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
def _createconfig(self, conf):
#Create config file (always writes defaults, safe for recovery)
#Create config file
defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":""}}}
with open(conf, "w") as f:
yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
os.chmod(conf, 0o600)
try:
with open(self.cachefile, 'w') as f:
json.dump(defaultconfig, f)
except Exception:
pass
return defaultconfig
if not os.path.exists(conf):
with open(conf, "w") as f:
json.dump(defaultconfig, f, indent = 4)
f.close()
os.chmod(conf, 0o600)
jsonconf = open(conf)
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
@MethodHook
def _saveconfig(self, conf):
#Save config file atomically to prevent corruption
#Save config file
newconfig = {"config":{}, "connections": {}, "profiles": {}}
newconfig["config"] = self.config
newconfig["connections"] = self.connections
newconfig["profiles"] = self.profiles
tmpfile = conf + '.tmp'
try:
with open(tmpfile, "w") as f:
yaml.dump(newconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
# Atomic replace: only overwrite original if write succeeded
shutil.move(tmpfile, conf)
with open(self.cachefile, "w") as f:
json.dump(newconfig, f)
self._generate_nodes_cache()
except (IOError, OSError) as e:
printer.error(f"Failed to save config: {e}")
# Clean up temp file if it exists
if os.path.exists(tmpfile):
try:
os.remove(tmpfile)
except OSError:
pass
with open(conf, "w") as f:
json.dump(newconfig, f, indent = 4)
f.close()
except:
return 1
return 0
def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None):
try:
if nodes is None:
nodes = self._getallnodes()
if folders is None:
folders = self._getallfolders()
if profiles is None:
profiles = list(self.profiles.keys())
with open(self.fzf_cachefile, "w") as f:
f.write("\n".join(nodes))
with open(self.folders_cachefile, "w") as f:
f.write("\n".join(folders))
with open(self.profiles_cachefile, "w") as f:
f.write("\n".join(profiles))
except Exception:
pass
def _createkey(self, keyfile):
#Create key file
key = RSA.generate(2048)
@@ -326,7 +155,7 @@ class configfile:
return result
@MethodHook
def getitem(self, unique, keys = None, extract = False):
def getitem(self, unique, keys = None):
'''
Get an node or a group of nodes from configfile which can be passed to node/nodes class
@@ -340,8 +169,6 @@ class configfile:
- keys (list): In case you pass a folder as unique, you can filter
nodes inside the folder passing a list.
- extract (bool): If True, extract information from profiles.
Default False.
### Returns:
@@ -357,35 +184,21 @@ class configfile:
folder = self.connections[uniques["folder"]]
newfolder = deepcopy(folder)
newfolder.pop("type")
for node_name in folder.keys():
if node_name == "type":
for node in folder.keys():
if node == "type":
continue
if "type" in newfolder[node_name].keys():
if newfolder[node_name]["type"] == "subfolder":
newfolder.pop(node_name)
if "type" in newfolder[node].keys():
if newfolder[node]["type"] == "subfolder":
newfolder.pop(node)
else:
newfolder[node_name].pop("type")
if keys != None:
newfolder = dict((k, newfolder[k]) for k in keys)
if extract:
for node_name, node_keys in newfolder.items():
for key, value in node_keys.items():
profile = re.search("^@(.*)", str(value))
if profile:
try:
newfolder[node_name][key] = self.profiles[profile.group(1)][key]
except KeyError:
newfolder[node_name][key] = ""
elif value == '' and key == "protocol":
try:
newfolder[node_name][key] = self.profiles["default"][key]
except KeyError:
newfolder[node_name][key] = "ssh"
newfolder = {"{}{}".format(k,unique):v for k,v in newfolder.items()}
return newfolder
newfolder[node].pop("type")
if keys == None:
newfolder = {"{}{}".format(k,unique):v for k,v in newfolder.items()}
return newfolder
else:
f_newfolder = dict((k, newfolder[k]) for k in keys)
f_newfolder = {"{}{}".format(k,unique):v for k,v in f_newfolder.items()}
return f_newfolder
else:
if uniques.keys() >= {"folder", "subfolder"}:
node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]]
@@ -395,24 +208,10 @@ class configfile:
node = self.connections[uniques["id"]]
newnode = deepcopy(node)
newnode.pop("type")
if extract:
for key, value in newnode.items():
profile = re.search("^@(.*)", str(value))
if profile:
try:
newnode[key] = self.profiles[profile.group(1)][key]
except KeyError:
newnode[key] = ""
elif value == '' and key == "protocol":
try:
newnode[key] = self.profiles["default"][key]
except KeyError:
newnode[key] = "ssh"
return newnode
@MethodHook
def getitems(self, uniques, extract = False):
def getitems(self, uniques):
'''
Get a group of nodes from configfile which can be passed to node/nodes class
@@ -422,11 +221,6 @@ class configfile:
from the connection manager. It can be a
list of strings.
### Optional Parameters:
- extract (bool): If True, extract information from profiles.
Default False.
### Returns:
dict: Dictionary containing information of node or multiple
@@ -437,15 +231,23 @@ class configfile:
if isinstance(uniques, str):
uniques = [uniques]
for i in uniques:
if i.startswith("@"):
if isinstance(i, dict):
name = list(i.keys())[0]
mylist = i[name]
if not self.config["case"]:
name = name.lower()
mylist = [item.lower() for item in mylist]
this = self.getitem(name, mylist)
nodes.update(this)
elif i.startswith("@"):
if not self.config["case"]:
i = i.lower()
this = self.getitem(i, extract = extract)
this = self.getitem(i)
nodes.update(this)
else:
if not self.config["case"]:
i = i.lower()
this = self.getitem(i, extract = extract)
this = self.getitem(i)
nodes[i] = this
return nodes
@@ -505,58 +307,48 @@ class configfile:
def _getallnodes(self, filter = None):
#get all nodes on configfile
nodes = []
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "connection"]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"]
nodes.extend(layer1)
for f in folders:
layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"]
layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "connection"]
nodes.extend(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "subfolder"]
for s in subfolders:
layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"]
layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v["type"] == "connection"]
nodes.extend(layer3)
if filter:
flat_filter = []
if isinstance(filter, str):
flat_filter = [filter]
nodes = [item for item in nodes if re.search(filter, item)]
elif isinstance(filter, list):
for item in filter:
if isinstance(item, str):
flat_filter.append(item)
nodes = [item for item in nodes if any(re.search(pattern, item) for pattern in filter)]
else:
printer.error("Filter must be a string or a list of strings")
sys.exit(1)
flags = re.IGNORECASE if not self.config.get("case", False) else 0
nodes = [item for item in nodes if any(re.search(pattern, item, flags) for pattern in flat_filter)]
raise ValueError("filter must be a string or a list of strings")
return nodes
@MethodHook
def _getallnodesfull(self, filter = None, extract = True):
#get all nodes on configfile with all their attributes.
nodes = {}
layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"}
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "connection"}
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"]
nodes.update(layer1)
for f in folders:
layer2 = {k + "@" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"}
layer2 = {k + "@" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "connection"}
nodes.update(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "subfolder"]
for s in subfolders:
layer3 = {k + "@" + s + "@" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"}
layer3 = {k + "@" + s + "@" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v["type"] == "connection"}
nodes.update(layer3)
if filter:
flat_filter = []
if isinstance(filter, str):
flat_filter = [filter]
filter = "^(?!.*@).+$" if filter == "@" else filter
nodes = {k: v for k, v in nodes.items() if re.search(filter, k)}
elif isinstance(filter, list):
for item in filter:
if isinstance(item, str):
flat_filter.append(item)
filter = ["^(?!.*@).+$" if item == "@" else item for item in filter]
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in filter)}
else:
printer.error("Filter must be a string or a list of strings")
sys.exit(1)
flat_filter = ["^(?!.*@).+$" if item == "@" else item for item in flat_filter]
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in flat_filter)}
raise ValueError("filter must be a string or a list of strings")
if extract:
for node, keys in nodes.items():
for key, value in keys.items():
@@ -564,12 +356,12 @@ class configfile:
if profile:
try:
nodes[node][key] = self.profiles[profile.group(1)][key]
except KeyError:
except:
nodes[node][key] = ""
elif value == '' and key == "protocol":
try:
nodes[node][key] = self.profiles["default"][key]
except KeyError:
nodes[node][key] = config.profiles["default"][key]
except:
nodes[node][key] = "ssh"
return nodes
@@ -577,27 +369,27 @@ class configfile:
@MethodHook
def _getallfolders(self):
#get all folders on configfile
folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"]
subfolders = []
for f in folders:
s = ["@" + k + f for k,v in self.connections[f[1:]].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
s = ["@" + k + f for k,v in self.connections[f[1:]].items() if isinstance(v, dict) and v["type"] == "subfolder"]
subfolders.extend(s)
folders.extend(subfolders)
return folders
@MethodHook
def _profileused(self, profile):
#Return all the nodes that uses this profile.
#Check if profile is used before deleting it
nodes = []
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "connection" and ("@" + profile in v.values() or ( isinstance(v["password"],list) and "@" + profile in v["password"]))]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"]
nodes.extend(layer1)
for f in folders:
layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "connection" and ("@" + profile in v.values() or ( isinstance(v["password"],list) and "@" + profile in v["password"]))]
nodes.extend(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v["type"] == "subfolder"]
for s in subfolders:
layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v["type"] == "connection" and ("@" + profile in v.values() or ( isinstance(v["password"],list) and "@" + profile in v["password"]))]
nodes.extend(layer3)
return nodes
+1416 -465
View File
File diff suppressed because it is too large Load Diff
+149 -814
View File
File diff suppressed because it is too large Load Diff
-402
View File
@@ -1,402 +0,0 @@
import argparse
import sys
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser(description="Capture packets remotely using a saved SSH node", epilog="All unknown arguments will be passed to tcpdump.")
self.parser.add_argument("node", nargs='?', help="Name of the saved node (must use SSH)")
self.parser.add_argument("interface", nargs='?', help="Network interface to capture on")
self.parser.add_argument("--ns", "--namespace", dest="namespace", help="Optional network namespace")
self.parser.add_argument("-w","--wireshark", action="store_true", help="Open live capture in Wireshark")
self.parser.add_argument("--set-wireshark-path", metavar="PATH", help="Set the default path to Wireshark binary")
self.parser.add_argument(
"-f", "--filter",
dest="tcpdump_filter",
metavar="ARG",
nargs="*",
default=["not", "port", "22"],
help="tcpdump filter expression (e.g., -f port 443 and udp). Default: not port 22"
)
self.parser.add_argument(
"--unknown-args",
action="store_true",
default=True,
help=argparse.SUPPRESS
)
class Entrypoint:
@staticmethod
def get_remote_capture_class():
import subprocess
import random
import socket
import time
import threading
from pexpect import TIMEOUT
from connpy import printer
class RemoteCapture:
def __init__(self, connapp, node_name, interface, namespace=None, use_wireshark=False, tcpdump_filter=None, tcpdump_args=None):
self.connapp = connapp
self.node_name = node_name
self.interface = interface
self.namespace = namespace
self.use_wireshark = use_wireshark
self.tcpdump_filter = tcpdump_filter or []
self.tcpdump_args = tcpdump_args if isinstance(tcpdump_args, list) else []
if node_name.startswith("@"): # fuzzy match
matches = self.connapp.services.nodes.list_nodes(node_name)
else:
matches = self.connapp.services.nodes.list_nodes(f"^{node_name}")
if not matches:
printer.error(f"Node '{node_name}' not found.")
sys.exit(2)
elif len(matches) > 1:
from ..cli.helpers import choose
matches[0] = choose(self.connapp, matches, "node", "capture")
if matches[0] is None:
sys.exit(7)
node_data = self.connapp.services.nodes.get_node_details(matches[0])
self.node = self.connapp.node(matches[0], **node_data, config=self.connapp.config)
if self.node.protocol != "ssh":
printer.error(f"Node '{self.node.unique}' must be an SSH connection.")
sys.exit(2)
settings = self.connapp.services.config_svc.get_settings()
self.wireshark_path = settings.get("wireshark_path")
def _start_local_listener(self, port, ws_proc=None):
self.fake_connection = False
self.listener_active = True
self.listener_conn = None
self.listener_connected = threading.Event()
def listen():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("localhost", port))
s.listen(1)
printer.start(f"Listening on localhost:{port}")
conn, addr = s.accept()
self.listener_conn = conn
if not self.fake_connection:
printer.start(f"Connection from {addr}")
self.listener_connected.set()
try:
while self.listener_active:
data = conn.recv(4096)
if not data:
break
if self.use_wireshark and ws_proc:
try:
ws_proc.stdin.write(data)
ws_proc.stdin.flush()
except BrokenPipeError:
printer.info("Wireshark closed the pipe.")
break
else:
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
except Exception as e:
if isinstance(e, BrokenPipeError):
printer.info("Listener closed due to broken pipe.")
else:
printer.error(f"Listener error: {e}")
finally:
conn.close()
self.listener_conn = None
self.listener_thread = threading.Thread(target=listen)
self.listener_thread.daemon = True
self.listener_thread.start()
def _is_port_in_use(self, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
def _find_free_port(self, start=20000, end=30000):
for _ in range(10):
port = random.randint(start, end)
if not self._is_port_in_use(port):
return port
printer.error("No free port found for SSH tunnel.")
sys.exit(1)
def _monitor_wireshark(self, ws_proc):
try:
while True:
try:
ws_proc.wait(timeout=1)
self.listener_active = False
if self.listener_conn:
printer.info("Wireshark exited, stopping listener.")
try:
self.listener_conn.shutdown(socket.SHUT_RDWR)
self.listener_conn.close()
except Exception:
pass
break
except subprocess.TimeoutExpired:
if not self.listener_active:
break
time.sleep(0.2)
except Exception as e:
printer.warning(f"Error in monitor_wireshark: {e}")
def _detect_sudo_requirement(self):
base_cmd = f"tcpdump -i {self.interface} -w - -U -c 1"
if self.namespace:
base_cmd = f"ip netns exec {self.namespace} {base_cmd}"
cmds = [base_cmd, f"sudo {base_cmd}"]
printer.info(f"Verifying sudo requirement")
for cmd in cmds:
try:
self.node.child.sendline(cmd)
start_time = time.time()
while time.time() - start_time < 3:
try:
index = self.node.child.expect([
r'listening on',
r'permission denied',
r'cannot',
r'No such file or directory',
], timeout=1)
if index == 0:
self.node.child.send("\x03")
return "sudo" in cmd
else:
break
except Exception:
continue
self.node.child.send("\x03")
time.sleep(0.5)
try:
self.node.child.read_nonblocking(size=1024, timeout=0.5)
except Exception:
pass
except Exception as e:
printer.warning(f"Error during sudo detection: {e}")
continue
printer.error(f"Failed to run tcpdump on remote node '{self.node.unique}'")
sys.exit(4)
def _monitor_capture_output(self):
try:
index = self.node.child.expect([
r'Broken pipe',
r'packet[s]? captured'
], timeout=None)
if index == 0:
printer.error("Tcpdump failed: Broken pipe.")
else:
printer.success("Tcpdump finished capturing packets.")
self.listener_active = False
except Exception:
pass
def _sendline_until_connected(self, cmd, retries=5, interval=2):
for attempt in range(1, retries + 1):
printer.info(f"Attempt {attempt}/{retries} to connect listener...")
self.node.child.sendline(cmd)
try:
index = self.node.child.expect([
r'listening on',
TIMEOUT,
r'permission',
r'not permitted',
r'invalid',
r'unrecognized',
r'Unable',
r'No such',
r'illegal',
r'not found',
r'non-ether',
r'syntax error'
], timeout=5)
if index == 0:
self.monitor_end = threading.Thread(target=self._monitor_capture_output)
self.monitor_end.daemon = True
self.monitor_end.start()
if self.listener_connected.wait(timeout=interval):
printer.success("Listener successfully received a connection.")
return True
else:
printer.warning("No connection yet. Retrying...")
elif index == 1:
error = f"tcpdump did not respond within the expected time.\nCommand used:\n{cmd}\n\u2192 Please verify the command syntax."
return f"{error}"
else:
before_last_line = self.node.child.before.decode().splitlines()[-1]
error = f"Tcpdump error detected: {before_last_line}{self.node.child.after.decode()}{self.node.child.readline().decode()}".rstrip()
return f"{error}"
except Exception as e:
printer.warning(f"Unexpected error during tcpdump startup: {e}")
return False
return False
def _build_tcpdump_command(self):
base = f"tcpdump -i {self.interface}"
if self.use_wireshark:
base += " -w - -U"
else:
base += " -l"
if self.namespace:
base = f"ip netns exec {self.namespace} {base}"
if self.requires_sudo:
base = f"sudo {base}"
if self.tcpdump_args:
base += " " + " ".join(self.tcpdump_args)
if self.tcpdump_filter:
base += " " + " ".join(self.tcpdump_filter)
base += f" | nc localhost {self.local_port}"
return base
def run(self):
if self.use_wireshark:
if not self.wireshark_path:
printer.error("Wireshark path not set in config.\nUse '--set-wireshark-path /full/path/to/wireshark' to configure it.")
sys.exit(1)
self.local_port = self._find_free_port()
self.node.options += f" -o ExitOnForwardFailure=yes -R {self.local_port}:localhost:{self.local_port}"
connection = self.node._connect()
if connection is not True:
printer.error(f"Could not connect to {self.node.unique}\n{connection}")
sys.exit(1)
self.requires_sudo = self._detect_sudo_requirement()
tcpdump_cmd = self._build_tcpdump_command()
ws_proc = None
monitor_thread = None
if self.use_wireshark:
printer.info(f"Live capture from {self.node.unique}:{self.interface}, launching Wireshark...")
try:
ws_proc = subprocess.Popen([self.wireshark_path, "-k", "-i", "-"], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
except Exception as e:
printer.error(f"Failed to launch Wireshark: {e}\nMake sure the path is correct and Wireshark is installed.")
exit(1)
monitor_thread = threading.Thread(target=self._monitor_wireshark, args=(ws_proc,))
monitor_thread.daemon = True
monitor_thread.start()
else:
printer.info(f"Live text capture from {self.node.unique}:{self.interface}")
printer.info("Press Ctrl+C to stop.\n")
try:
self._start_local_listener(self.local_port, ws_proc=ws_proc)
time.sleep(1)
result = self._sendline_until_connected(tcpdump_cmd, retries=5, interval=2)
if result is not True:
if isinstance(result, str):
printer.error(f"{result}")
else:
printer.error("Listener connection failed after all retries.")
self.listener_active = False
return
while self.listener_active:
time.sleep(0.5)
except KeyboardInterrupt:
print("")
printer.warning("Capture interrupted by user.")
self.listener_active = False
finally:
if self.listener_conn:
try:
self.listener_conn.shutdown(socket.SHUT_RDWR)
self.listener_conn.close()
except OSError: pass
if hasattr(self.node, "child"):
self.node.child.close(force=True)
return RemoteCapture
def __init__(self, args, parser, connapp):
from connpy import printer
if "--" in args.unknown_args:
args.unknown_args.remove("--")
if args.set_wireshark_path:
connapp.services.config_svc.update_setting("wireshark_path", args.set_wireshark_path)
printer.success(f"Wireshark path updated to: {args.set_wireshark_path}")
return
if not args.node or not args.interface:
parser.error("node and interface are required unless --set-wireshark-path is used")
RemoteCapture = self.get_remote_capture_class()
capture = RemoteCapture(
connapp=connapp, node_name=args.node, interface=args.interface,
namespace=args.namespace, use_wireshark=args.wireshark,
tcpdump_filter=args.tcpdump_filter, tcpdump_args=args.unknown_args
)
capture.run()
def _connpy_tree(info=None):
"""Declarative completion tree for the capture plugin following completion.py patterns."""
nodes = info.get("nodes", []) if info else []
# State 2: Main capture loop (No setup flag here)
capture_main = {"__exclude_used__": True}
# Inline logic to suggest nodes only if no positional has been provided yet
get_nodes = lambda w: nodes if not [x for x in w[:-1] if not x.startswith("-") and x != "capture"] else []
capture_main["__extra__"] = get_nodes
capture_main["*"] = capture_main
for f in ["--wireshark", "-w", "--help", "-h"]:
capture_main[f] = capture_main
for f in ["--namespace", "--filter", "-f"]:
capture_main[f] = {"*": capture_main}
# State 1: Start (Highly discoverable configuration)
capture_start = {
"__exclude_used__": True,
"__extra__": get_nodes,
"--set-wireshark-path": {"__extra__": lambda w: get_cwd(w, "--set-wireshark-path")}
}
# Transitions from start to main
for f in ["--wireshark", "-w", "--help", "-h"]:
capture_start[f] = capture_main
for f in ["--namespace", "--filter", "-f"]:
capture_start[f] = {"*": capture_main}
capture_start["*"] = capture_main
return capture_start
+378
View File
@@ -0,0 +1,378 @@
#!/usr/bin/python3
import argparse
import os
import time
import zipfile
import tempfile
import io
import yaml
import threading
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from google.auth.exceptions import RefreshError
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import MediaFileUpload,MediaIoBaseDownload
from googleapiclient.errors import HttpError
from datetime import datetime
class sync:
def __init__(self, connapp):
self.scopes = ['https://www.googleapis.com/auth/drive.appdata']
self.token_file = f"{connapp.config.defaultdir}/gtoken.json"
self.file = connapp.config.file
self.key = connapp.config.key
self.google_client = f"{os.path.dirname(os.path.abspath(__file__))}/sync_client"
self.connapp = connapp
try:
self.sync = self.connapp.config.config["sync"]
except:
self.sync = False
def login(self):
creds = None
# The file token.json stores the user's access and refresh tokens.
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_file(self.token_file, self.scopes)
try:
# If there are no valid credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
self.google_client, self.scopes)
creds = flow.run_local_server(port=0, access_type='offline')
# Save the credentials for the next run
with open(self.token_file, 'w') as token:
token.write(creds.to_json())
print("Logged in successfully.")
except RefreshError as e:
# If refresh fails, delete the invalid token file and start a new login flow
if os.path.exists(self.token_file):
os.remove(self.token_file)
print("Existing token was invalid and has been removed. Please log in again.")
flow = InstalledAppFlow.from_client_secrets_file(
self.google_client, self.scopes)
creds = flow.run_local_server(port=0, access_type='offline')
with open(self.token_file, 'w') as token:
token.write(creds.to_json())
print("Logged in successfully after re-authentication.")
def logout(self):
if os.path.exists(self.token_file):
os.remove(self.token_file)
print("Logged out successfully.")
else:
print("No credentials file found. Already logged out.")
def get_credentials(self):
# Load credentials from token.json
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_file(self.token_file, self.scopes)
else:
print("Credentials file not found.")
return 0
# If there are no valid credentials available, ask the user to log in again
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except RefreshError:
print("Could not refresh access token. Please log in again.")
return 0
else:
print("Credentials are missing or invalid. Please log in.")
return 0
return creds
def check_login_status(self):
# Check if the credentials file exists
if os.path.exists(self.token_file):
# Load credentials from token.json
creds = Credentials.from_authorized_user_file(self.token_file)
# If credentials are expired, refresh them
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except RefreshError:
pass
# Check if the credentials are valid after refresh
if creds.valid:
return True
else:
return "Invalid"
else:
return False
def status(self):
print(f"Login: {self.check_login_status()}")
print(f"Sync: {self.sync}")
def get_appdata_files(self):
creds = self.get_credentials()
if not creds:
return 0
try:
# Create the Google Drive service
service = build("drive", "v3", credentials=creds)
# List files in the appDataFolder
response = (
service.files()
.list(
spaces="appDataFolder",
fields="files(id, name, appProperties)",
pageSize=10,
)
.execute()
)
files_info = []
for file in response.get("files", []):
# Extract file information
file_id = file.get("id")
file_name = file.get("name")
timestamp = file.get("appProperties", {}).get("timestamp")
human_readable_date = file.get("appProperties", {}).get("date")
files_info.append({"name": file_name, "id": file_id, "date": human_readable_date, "timestamp": timestamp})
return files_info
except HttpError as error:
print(f"An error occurred: {error}")
return 0
def dump_appdata_files_yaml(self):
files_info = self.get_appdata_files()
if not files_info:
print("Failed to retrieve files or no files found.")
return
# Pretty print as YAML
yaml_output = yaml.dump(files_info, sort_keys=False, default_flow_style=False)
print(yaml_output)
def backup_file_to_drive(self, file_path, timestamp):
creds = self.get_credentials()
if not creds:
return 1
# Create the Google Drive service
service = build('drive', 'v3', credentials=creds)
# Convert timestamp to a human-readable date
human_readable_date = datetime.fromtimestamp(timestamp/1000).strftime('%Y-%m-%d %H:%M:%S')
# Upload the file to Google Drive with timestamp metadata
file_metadata = {
'name': os.path.basename(file_path),
'parents': ["appDataFolder"],
'appProperties': {
'timestamp': str(timestamp),
'date': human_readable_date # Add human-readable date attribute
}
}
media = MediaFileUpload(file_path)
try:
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
return 0
except Exception as e:
return f"An error occurred: {e}"
def delete_file_by_id(self, file_id):
creds = self.get_credentials()
if not creds:
return 1
try:
# Create the Google Drive service
service = build("drive", "v3", credentials=creds)
# Delete the file
service.files().delete(fileId=file_id).execute()
return 0
except Exception as e:
return f"An error occurred: {e}"
def compress_specific_files(self, zip_path):
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.write(self.file, "config.json")
zipf.write(self.key, ".osk")
def compress_and_upload(self):
# Read the file content to get the folder path
timestamp = int(time.time() * 1000)
# Create a temporary directory for storing the zip file
with tempfile.TemporaryDirectory() as tmp_dir:
# Compress specific files from the folder path to a zip file in the temporary directory
zip_path = os.path.join(tmp_dir, f"connpy-backup-{timestamp}.zip")
self.compress_specific_files(zip_path)
# Get the files in the app data folder
app_data_files = self.get_appdata_files()
if app_data_files == 0:
return 1
# If there are 10 or more files, remove the oldest one based on timestamp
if len(app_data_files) >= 10:
oldest_file = min(app_data_files, key=lambda x: x['timestamp'])
delete_old = self.delete_file_by_id(oldest_file['id'])
if delete_old:
print(delete_old)
return 1
# Upload the new file
upload_new = self.backup_file_to_drive(zip_path, timestamp)
if upload_new:
print(upload_new)
return 1
print("Backup to google uploaded successfully.")
return 0
def decompress_zip(self, zip_path):
try:
with zipfile.ZipFile(zip_path, 'r') as zipf:
# Extract the specific file to the specified destination
zipf.extract("config.json", os.path.dirname(self.file))
zipf.extract(".osk", os.path.dirname(self.key))
return 0
except Exception as e:
print(f"An error occurred: {e}")
return 1
def download_file_by_id(self, file_id, destination_path):
creds = self.get_credentials()
if not creds:
return 1
try:
# Create the Google Drive service
service = build('drive', 'v3', credentials=creds)
# Download the file
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(destination_path, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
return 0
except Exception as e:
return f"An error occurred: {e}"
def restore_last_config(self, file_id=None):
# Get the files in the app data folder
app_data_files = self.get_appdata_files()
if not app_data_files:
print("No files found in app data folder.")
return 1
# Check if a specific file_id was provided and if it exists in the list
if file_id:
selected_file = next((f for f in app_data_files if f['id'] == file_id), None)
if not selected_file:
print(f"No file found with ID: {file_id}")
return 1
else:
# Find the latest file based on timestamp
selected_file = max(app_data_files, key=lambda x: x['timestamp'])
# Download the selected file to a temporary location
temp_download_path = os.path.join(tempfile.gettempdir(), 'connpy-backup.zip')
if self.download_file_by_id(selected_file['id'], temp_download_path):
return 1
# Unzip the downloaded file to the destination folder
if self.decompress_zip(temp_download_path):
print("Failed to decompress the file.")
return 1
print(f"Backup from Google Drive restored successfully: {selected_file['name']}")
return 0
def config_listener_post(self, args, kwargs):
if self.sync:
if self.check_login_status() == True:
if not kwargs["result"]:
self.compress_and_upload()
else:
print("Sync cannot be performed. Please check your login status.")
return kwargs["result"]
def config_listener_pre(self, *args, **kwargs):
try:
self.sync = self.connapp.config.config["sync"]
except:
self.sync = False
return args, kwargs
def start_post_thread(self, *args, **kwargs):
post_thread = threading.Thread(target=self.config_listener_post, args=(args,kwargs))
post_thread.start()
class Preload:
def __init__(self, connapp):
syncapp = sync(connapp)
connapp.config._saveconfig.register_post_hook(syncapp.start_post_thread)
connapp.config._saveconfig.register_pre_hook(syncapp.config_listener_pre)
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser(description="Sync config with Google")
self.description = "Sync config with Google"
subparsers = self.parser.add_subparsers(title="Commands", dest='command',metavar="")
login_parser = subparsers.add_parser("login", help="Login to Google to enable synchronization")
logout_parser = subparsers.add_parser("logout", help="Logout from Google")
start_parser = subparsers.add_parser("start", help="Start synchronizing with Google")
stop_parser = subparsers.add_parser("stop", help="Stop any ongoing synchronization")
restore_parser = subparsers.add_parser("restore", help="Restore data from Google")
backup_parser = subparsers.add_parser("once", help="Backup current configuration to Google once")
restore_parser.add_argument("--id", type=str, help="Optional file ID to restore a specific backup", required=False)
status_parser = subparsers.add_parser("status", help="Check the current status of synchronization")
list_parser = subparsers.add_parser("list", help="List all backups stored on Google")
class Entrypoint:
def __init__(self, args, parser, connapp):
syncapp = sync(connapp)
if args.command == 'login':
syncapp.login()
elif args.command == "status":
syncapp.status()
elif args.command == "start":
connapp._change_settings("sync", True)
elif args.command == "stop":
connapp._change_settings("sync", False)
elif args.command == "list":
syncapp.dump_appdata_files_yaml()
elif args.command == "once":
syncapp.compress_and_upload()
elif args.command == "restore":
syncapp.restore_last_config(args.id)
elif args.command == "logout":
syncapp.logout()
def _connpy_completion(wordsnumber, words, info = None):
if wordsnumber == 3:
result = ["--help", "login", "status", "start", "stop", "list", "once", "restore", "logout"]
#NETMASK_completion
if wordsnumber == 4 and words[1] == "restore":
result = ["--help", "--id"]
return result
-8
View File
@@ -1,8 +0,0 @@
import sys
import os
# gRPC generated files use absolute imports that assume their directory is in sys.path.
# We add this directory to sys.path to allow imports like 'import connpy_pb2' to succeed.
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
-25
View File
@@ -1,25 +0,0 @@
syntax = "proto3";
package connpy_remote;
message IdRequest {
string id = 1;
}
message StringResponse {
string value = 1;
}
message PluginInvokeRequest {
string name = 1;
string args_json = 2;
}
message OutputChunk {
string text = 1;
bool is_error = 2;
}
service RemotePluginService {
rpc get_plugin_source(IdRequest) returns (StringResponse);
rpc invoke_plugin(PluginInvokeRequest) returns (stream OutputChunk);
}
-44
View File
@@ -1,44 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# NO CHECKED-IN PROTOBUF GENCODE
# source: remote_plugin.proto
# Protobuf Python Version: 6.31.1
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import runtime_version as _runtime_version
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
_runtime_version.ValidateProtobufRuntimeVersion(
_runtime_version.Domain.PUBLIC,
6,
31,
1,
'',
'remote_plugin.proto'
)
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x13remote_plugin.proto\x12\rconnpy_remote\"\x17\n\tIdRequest\x12\n\n\x02id\x18\x01 \x01(\t\"\x1f\n\x0eStringResponse\x12\r\n\x05value\x18\x01 \x01(\t\"6\n\x13PluginInvokeRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x11\n\targs_json\x18\x02 \x01(\t\"-\n\x0bOutputChunk\x12\x0c\n\x04text\x18\x01 \x01(\t\x12\x10\n\x08is_error\x18\x02 \x01(\x08\x32\xb6\x01\n\x13RemotePluginService\x12L\n\x11get_plugin_source\x12\x18.connpy_remote.IdRequest\x1a\x1d.connpy_remote.StringResponse\x12Q\n\rinvoke_plugin\x12\".connpy_remote.PluginInvokeRequest\x1a\x1a.connpy_remote.OutputChunk0\x01\x62\x06proto3')
_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'remote_plugin_pb2', _globals)
if not _descriptor._USE_C_DESCRIPTORS:
DESCRIPTOR._loaded_options = None
_globals['_IDREQUEST']._serialized_start=38
_globals['_IDREQUEST']._serialized_end=61
_globals['_STRINGRESPONSE']._serialized_start=63
_globals['_STRINGRESPONSE']._serialized_end=94
_globals['_PLUGININVOKEREQUEST']._serialized_start=96
_globals['_PLUGININVOKEREQUEST']._serialized_end=150
_globals['_OUTPUTCHUNK']._serialized_start=152
_globals['_OUTPUTCHUNK']._serialized_end=197
_globals['_REMOTEPLUGINSERVICE']._serialized_start=200
_globals['_REMOTEPLUGINSERVICE']._serialized_end=382
# @@protoc_insertion_point(module_scope)
-140
View File
@@ -1,140 +0,0 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import warnings
from . import remote_plugin_pb2 as remote__plugin__pb2
GRPC_GENERATED_VERSION = '1.80.0'
GRPC_VERSION = grpc.__version__
_version_not_supported = False
try:
from grpc._utilities import first_version_is_lower
_version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
except ImportError:
_version_not_supported = True
if _version_not_supported:
raise RuntimeError(
f'The grpc package installed is at version {GRPC_VERSION},'
+ ' but the generated code in remote_plugin_pb2_grpc.py depends on'
+ f' grpcio>={GRPC_GENERATED_VERSION}.'
+ f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
+ f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
)
class RemotePluginServiceStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.get_plugin_source = channel.unary_unary(
'/connpy_remote.RemotePluginService/get_plugin_source',
request_serializer=remote__plugin__pb2.IdRequest.SerializeToString,
response_deserializer=remote__plugin__pb2.StringResponse.FromString,
_registered_method=True)
self.invoke_plugin = channel.unary_stream(
'/connpy_remote.RemotePluginService/invoke_plugin',
request_serializer=remote__plugin__pb2.PluginInvokeRequest.SerializeToString,
response_deserializer=remote__plugin__pb2.OutputChunk.FromString,
_registered_method=True)
class RemotePluginServiceServicer(object):
"""Missing associated documentation comment in .proto file."""
def get_plugin_source(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def invoke_plugin(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_RemotePluginServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'get_plugin_source': grpc.unary_unary_rpc_method_handler(
servicer.get_plugin_source,
request_deserializer=remote__plugin__pb2.IdRequest.FromString,
response_serializer=remote__plugin__pb2.StringResponse.SerializeToString,
),
'invoke_plugin': grpc.unary_stream_rpc_method_handler(
servicer.invoke_plugin,
request_deserializer=remote__plugin__pb2.PluginInvokeRequest.FromString,
response_serializer=remote__plugin__pb2.OutputChunk.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'connpy_remote.RemotePluginService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
server.add_registered_method_handlers('connpy_remote.RemotePluginService', rpc_method_handlers)
# This class is part of an EXPERIMENTAL API.
class RemotePluginService(object):
"""Missing associated documentation comment in .proto file."""
@staticmethod
def get_plugin_source(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(
request,
target,
'/connpy_remote.RemotePluginService/get_plugin_source',
remote__plugin__pb2.IdRequest.SerializeToString,
remote__plugin__pb2.StringResponse.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)
@staticmethod
def invoke_plugin(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_stream(
request,
target,
'/connpy_remote.RemotePluginService/invoke_plugin',
remote__plugin__pb2.PluginInvokeRequest.SerializeToString,
remote__plugin__pb2.OutputChunk.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
_registered_method=True)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
-113
View File
@@ -1,113 +0,0 @@
import os
import threading
from connpy.configfile import configfile
from connpy.services.provider import ServiceProvider
from connpy.services.user_service import UserService
class UserRegistry:
"""Holds per-user ServiceProviders in memory, thread-safe with hot-reloading."""
def __init__(self, server_config_dir):
self.server_config_dir = os.path.abspath(server_config_dir)
self.user_service = UserService(self.server_config_dir)
self._providers = {} # username → ServiceProvider
self._mtimes = {} # username → last loaded mtime (float)
self._lock = threading.Lock()
# Load shared/global config
self._shared_conf_file = os.path.join(self.server_config_dir, "config.yaml")
if os.path.exists(self._shared_conf_file):
self._shared_config = configfile(conf=self._shared_conf_file)
self._shared_mtime = os.path.getmtime(self._shared_conf_file)
else:
self._shared_config = None
self._shared_mtime = 0.0
def _refresh_shared(self):
"""Hot-reload shared config if the file changed on disk."""
if not os.path.exists(self._shared_conf_file):
return
current_mtime = os.path.getmtime(self._shared_conf_file)
if current_mtime > self._shared_mtime:
try:
self._shared_config = configfile(conf=self._shared_conf_file)
self._shared_mtime = current_mtime
# Clear all user providers so they pick up the new shared config
self._providers.clear()
self._mtimes.clear()
except Exception as e:
from connpy import printer
printer.warning(f"Failed to reload shared config: {e}")
def get_provider(self, username) -> ServiceProvider:
"""Get, lazy-load, or hot-reload a user's full ServiceProvider."""
with self._lock:
# Refresh shared/global config if it has changed
self._refresh_shared()
# 1. Resolve physical path of the user's config.yaml file
user_data = self.user_service.get_user(username)
config_path = user_data.get("config_path")
if config_path:
conf_file = os.path.join(config_path, "config.yaml")
else:
conf_file = os.path.join(self.server_config_dir, "users", username, "config.yaml")
# 2. Retrieve actual modification time in disk
current_mtime = os.path.getmtime(conf_file) if os.path.exists(conf_file) else 0.0
# 3. Validate if initial load or hot-reload is required
if username not in self._providers or self._mtimes.get(username, 0.0) < current_mtime:
old_provider = self._providers.get(username)
try:
# Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode="local")
# Successfully loaded, clean up the old provider
if old_provider:
self._providers.pop(username, None)
if hasattr(old_provider, "close"):
try:
old_provider.close()
except Exception:
pass
self._providers[username] = new_provider
self._mtimes[username] = current_mtime
except Exception as e:
# Log warning but fallback to the old stable provider in memory if available
from connpy import printer
printer.warning(f"Failed to hot-reload config for user '{username}' (file may be corrupt/incomplete): {e}")
if old_provider:
# Keep serving with the old cached instance to ensure service continuity
self._mtimes[username] = current_mtime
else:
# No fallback exists, propagate the exception
raise e
return self._providers[username]
def has_users(self) -> bool:
"""Check if any users are registered (enables auth enforcement)."""
return bool(self.user_service.list_users())
def get_shared_config(self):
"""Thread-safe access to the hot-reloaded shared configuration."""
with self._lock:
self._refresh_shared()
return self._shared_config
def evict(self, username):
"""Remove and cleanly shut down cached provider (after delete or password change)."""
with self._lock:
provider = self._providers.pop(username, None)
self._mtimes.pop(username, None)
if provider:
# Explicit cleanup of user-scoped resources if custom close/cleanup exists
if hasattr(provider, "close"):
try:
provider.close()
except Exception:
pass
-30
View File
@@ -1,30 +0,0 @@
import json
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Struct, Value
def to_value(obj):
if obj is None:
v = Value()
v.null_value = 0
return v
json_str = json.dumps(obj)
v = Value()
json_format.Parse(json_str, v)
return v
def from_value(val):
if not val.HasField("kind"):
return None
return json.loads(json_format.MessageToJson(val))
def to_struct(obj):
if not obj:
return Struct()
s = Struct()
json_format.ParseDict(obj, s)
return s
def from_struct(struct):
if not struct:
return {}
return json_format.MessageToDict(struct, preserving_proto_field_name=True)
+10 -14
View File
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
#Imports
from functools import wraps, partial, update_wrapper
from . import printer
#functions and classes
@@ -20,21 +19,18 @@ class MethodHook:
try:
args, kwargs = hook(*args, **kwargs)
except Exception as e:
hook_name = getattr(hook, "__name__", str(hook))
printer.error(f"{self.func.__name__} Pre-hook {hook_name} raised an exception: {e}")
print(f"{self.func.__name__} Pre-hook {hook.__name__} raised an exception: {e}")
result = self.func(*args, **kwargs)
try:
result = self.func(*args, **kwargs)
# Execute post-hooks after the original function
if self.post_hooks:
#printer.info(f"Executing {len(self.post_hooks)} post-hooks for {self.func.__name__}...")
pass
for hook in self.post_hooks:
try:
result = hook(*args, **kwargs, result=result) # Pass result to hooks
except Exception as e:
hook_name = getattr(hook, "__name__", str(hook))
printer.error(f"{self.func.__name__} Post-hook {hook_name} raised an exception: {e}")
finally:
# Execute post-hooks after the original function
for hook in self.post_hooks:
try:
result = hook(*args, **kwargs, result=result) # Pass result to hooks
except Exception as e:
print(f"{self.func.__name__} Post-hook {hook.__name__} raised an exception: {e}")
return result
-174
View File
@@ -1,174 +0,0 @@
import asyncio
import json
import os
import threading
from typing import Any, Dict, List, Optional
import logging
try:
from mcp import ClientSession
from mcp.client.sse import sse_client
MCP_AVAILABLE = True
except ImportError:
MCP_AVAILABLE = False
# Silence noisy MCP and HTTP internal logging
logging.getLogger("mcp").setLevel(logging.CRITICAL)
logging.getLogger("httpx").setLevel(logging.CRITICAL)
logging.getLogger("httpcore").setLevel(logging.CRITICAL)
class MCPClientManager:
"""Manages MCP SSE client connections for connpy."""
_instance = None
_lock = threading.Lock()
def __new__(cls, *args, **kwargs):
with cls._lock:
if cls._instance is None:
cls._instance = super(MCPClientManager, cls).__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self, config=None):
if self._initialized:
return
self.config = config
self.sessions: Dict[str, Dict[str, Any]] = {} # name -> {session, stack}
self.tool_cache: Dict[str, List[Dict[str, Any]]] = {}
self._connecting: Dict[str, asyncio.Future] = {}
self._initialized = True
async def get_tools_for_llm(self, os_filter: Optional[str] = None) -> List[Dict[str, Any]]:
"""
Fetches tools from enabled MCP servers that match the OS filter.
"""
if not MCP_AVAILABLE:
return []
all_llm_tools = []
try:
if hasattr(self.config, "get_effective_setting"):
mcp_config = self.config.get_effective_setting("ai", {}).get("mcp_servers", {})
else:
mcp_config = self.config.config.get("ai", {}).get("mcp_servers", {}) if hasattr(self.config, "config") else {}
except Exception:
return []
async def _fetch(name, cfg):
if not cfg.get("enabled", True): return []
# Filter by OS if specified in config (primarily used for copilot strict matching)
auto_os = cfg.get("auto_load_on_os")
if os_filter is not None and auto_os and os_filter.lower() != auto_os.lower():
return []
try:
session = await self._ensure_connected(name, cfg)
if session:
if name in self.tool_cache: return self.tool_cache[name]
llm_tools = await self._fetch_tools_as_openai(name, session)
self.tool_cache[name] = llm_tools
return llm_tools
except Exception:
pass
return []
tasks = [ _fetch(name, cfg) for name, cfg in mcp_config.items() ]
if tasks:
results = await asyncio.gather(*tasks)
for tools in results:
all_llm_tools.extend(tools)
return all_llm_tools
async def _ensure_connected(self, name: str, cfg: Dict[str, Any]) -> Optional[Any]:
if not MCP_AVAILABLE: return None
if name in self.sessions and self.sessions[name].get("session"):
return self.sessions[name]["session"]
url = cfg.get("url")
if not url:
return None
if name in self._connecting:
try:
return await asyncio.wait_for(asyncio.shield(self._connecting[name]), timeout=10.0)
except Exception:
return None
loop = asyncio.get_running_loop()
fut = loop.create_future()
self._connecting[name] = fut
try:
from contextlib import AsyncExitStack
stack = AsyncExitStack()
async def _do_connect():
read, write = await stack.enter_async_context(sse_client(url))
session = await stack.enter_async_context(ClientSession(read, write))
await session.initialize()
return session
session = await asyncio.wait_for(_do_connect(), timeout=15.0)
self.sessions[name] = {"session": session, "stack": stack}
fut.set_result(session)
return session
except Exception:
fut.set_result(None)
return None
finally:
if name in self._connecting:
del self._connecting[name]
async def _fetch_tools_as_openai(self, server_name: str, session: Any) -> List[Dict[str, Any]]:
try:
result = await asyncio.wait_for(session.list_tools(), timeout=5.0)
openai_tools = []
for tool in result.tools:
# Use mcp_ prefix to ensure valid function name for LiteLLM/Gemini
prefixed_name = f"mcp_{server_name}__{tool.name}"
openai_tools.append({
"type": "function",
"function": {
"name": prefixed_name,
"description": f"[{server_name}] {tool.description}",
"parameters": tool.inputSchema
}
})
return openai_tools
except Exception:
return []
async def call_tool(self, full_tool_name: str, arguments: Dict[str, Any]) -> Any:
"""Calls an MCP tool and returns text result."""
if not MCP_AVAILABLE:
return "Error: MCP SDK is not installed."
if "__" not in full_tool_name:
return f"Error: Tool {full_tool_name} is not a valid MCP tool."
clean_name = full_tool_name[4:] if full_tool_name.startswith("mcp_") else full_tool_name
server_name, tool_name = clean_name.split("__", 1)
if server_name not in self.sessions:
return f"Error: MCP server {server_name} is not connected."
session = self.sessions[server_name]["session"]
try:
result = await asyncio.wait_for(session.call_tool(tool_name, arguments), timeout=60.0)
text_outputs = [content.text for content in result.content if hasattr(content, "text")]
return "\n".join(text_outputs) if text_outputs else str(result)
except Exception as e:
return f"Error calling tool {tool_name} on {server_name}: {str(e)}"
async def shutdown(self):
"""Close all SSE connections."""
for name, data in self.sessions.items():
stack = data.get("stack")
if stack:
await stack.aclose()
self.sessions = {}
+10 -131
View File
@@ -4,34 +4,12 @@ import importlib.util
import sys
import argparse
import os
from connpy import printer
class Plugins:
def __init__(self):
self.plugins = {}
self.plugin_parsers = {}
self.preloads = {}
self.remote_plugins = {}
self.preferences = {}
def _load_preferences(self, config_dir):
import json
path = os.path.join(config_dir, "plugin_preferences.json")
try:
with open(path) as f:
self.preferences = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
self.preferences = {}
def _save_preferences(self, config_dir):
import json
path = os.path.join(config_dir, "plugin_preferences.json")
try:
with open(path, "w") as f:
json.dump(self.preferences, f, indent=4)
except OSError as e:
printer.error(f"Failed to save plugin preferences: {e}")
def verify_script(self, file_path):
"""
@@ -52,7 +30,8 @@ class Plugins:
### Verifications:
- The presence of only allowed top-level elements.
- The existence of two specific classes: 'Parser' and 'Entrypoint'. and/or specific class: Preload.
- 'Parser' class must only have an '__init__' method and must assign 'self.parser'.
- 'Parser' class must only have an '__init__' method and must assign 'self.parser'
and 'self.description'.
- 'Entrypoint' class must have an '__init__' method accepting specific arguments.
If any of these checks fail, the function returns an error message indicating
@@ -83,8 +62,8 @@ class Plugins:
if not (isinstance(node.test, ast.Compare) and
isinstance(node.test.left, ast.Name) and
node.test.left.id == '__name__' and
((hasattr(ast, 'Str') and isinstance(node.test.comparators[0], getattr(ast, 'Str')) and node.test.comparators[0].s == '__main__') or
(hasattr(ast, 'Constant') and isinstance(node.test.comparators[0], getattr(ast, 'Constant')) and node.test.comparators[0].value == '__main__'))):
isinstance(node.test.comparators[0], ast.Str) and
node.test.comparators[0].s == '__main__'):
return "Only __name__ == __main__ If is allowed"
elif not isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Import, ast.ImportFrom, ast.Pass)):
@@ -98,12 +77,11 @@ class Plugins:
if not all(isinstance(method, ast.FunctionDef) and method.name == '__init__' for method in node.body):
return "Parser class should only have __init__ method"
# Check if 'self.parser' is assigned in __init__ method
# Check if 'self.parser' and 'self.description' are assigned in __init__ method
init_method = node.body[0]
assigned_attrs = [target.attr for expr in init_method.body if isinstance(expr, ast.Assign) for target in expr.targets if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name) and target.value.id == 'self']
if 'parser' not in assigned_attrs:
return "Parser class should set self.parser"
if 'parser' not in assigned_attrs or 'description' not in assigned_attrs:
return "Parser class should set self.parser and self.description" # 'self.parser' or 'self.description' not assigned in __init__
elif node.name == 'Entrypoint':
has_entrypoint = True
@@ -135,123 +113,24 @@ class Plugins:
spec.loader.exec_module(module)
return module
def _import_plugins_to_argparse(self, directory, subparsers, remote_enabled=False):
if not os.path.exists(directory):
return
def _import_plugins_to_argparse(self, directory, subparsers):
for filename in os.listdir(directory):
commands = subparsers.choices.keys()
if filename.endswith(".py"):
root_filename = os.path.splitext(filename)[0]
if root_filename in commands:
continue
# Check preferences: if remote is preferred AND remote is enabled, skip local loading
if remote_enabled and self.preferences.get(root_filename) == "remote":
continue
# Construct the full path
filepath = os.path.join(directory, filename)
check_file = self.verify_script(filepath)
if check_file:
printer.error(f"Failed to load plugin: {filename}. Reason: {check_file}")
print(f"Failed to load plugin: {filename}. Reason: {check_file}")
continue
else:
self.plugins[root_filename] = self._import_from_path(filepath)
if hasattr(self.plugins[root_filename], "Parser"):
self.plugin_parsers[root_filename] = self.plugins[root_filename].Parser()
plugin = self.plugin_parsers[root_filename]
# Default to RichHelpFormatter if plugin doesn't set one
try:
from rich_argparse import RichHelpFormatter as _RHF
fmt = plugin.parser.formatter_class
if fmt is argparse.HelpFormatter or fmt is argparse.RawTextHelpFormatter or fmt is argparse.RawDescriptionHelpFormatter:
fmt = _RHF
except ImportError:
fmt = plugin.parser.formatter_class
subparsers.add_parser(root_filename, parents=[self.plugin_parsers[root_filename].parser], add_help=False, help=plugin.parser.description, usage=plugin.parser.usage, description=plugin.parser.description, epilog=plugin.parser.epilog, formatter_class=fmt)
subparsers.add_parser(root_filename, parents=[self.plugin_parsers[root_filename].parser], add_help=False, description=self.plugin_parsers[root_filename].description)
if hasattr(self.plugins[root_filename], "Preload"):
self.preloads[root_filename] = self.plugins[root_filename]
def _import_remote_plugins_to_argparse(self, plugin_stub, subparsers, cache_dir, force_sync=False):
import hashlib
os.makedirs(cache_dir, exist_ok=True)
try:
remote_plugins_info = plugin_stub.list_plugins()
except Exception:
return
# Pruning: Remove local cached files that are no longer on the server
for local_file in os.listdir(cache_dir):
if local_file.endswith(".py"):
name = local_file[:-3]
if name not in remote_plugins_info:
try:
os.remove(os.path.join(cache_dir, local_file))
except Exception:
pass
for name, info in remote_plugins_info.items():
if not info.get("enabled", True):
continue
pref = self.preferences.get(name, "local")
if pref != "remote" and name in self.plugins:
continue
if not force_sync and name in subparsers.choices:
continue
cache_path = os.path.join(cache_dir, f"{name}.py")
# Hash comparison
remote_hash = info.get("hash", "")
local_hash = ""
if os.path.exists(cache_path):
try:
with open(cache_path, "rb") as f:
local_hash = hashlib.md5(f.read()).hexdigest()
except Exception:
pass
# Update only if hash differs or force_sync is True
if force_sync or remote_hash != local_hash or not os.path.exists(cache_path):
try:
source = plugin_stub.get_plugin_source(name)
with open(cache_path, "w") as f:
f.write(source)
except Exception as e:
printer.warning(f"Failed to sync remote plugin {name}: {e}")
continue
# Verify and load
check_file = self.verify_script(cache_path)
if check_file:
printer.warning(f"Remote plugin {name} failed verification: {check_file}")
continue
module = self._import_from_path(cache_path)
if hasattr(module, "Parser"):
self.plugin_parsers[name] = module.Parser()
self.remote_plugins[name] = True
plugin = self.plugin_parsers[name]
try:
from rich_argparse import RichHelpFormatter as _RHF
fmt = plugin.parser.formatter_class
if fmt is argparse.HelpFormatter or fmt is argparse.RawTextHelpFormatter or fmt is argparse.RawDescriptionHelpFormatter:
fmt = _RHF
except ImportError:
fmt = plugin.parser.formatter_class
# If force_sync, we might be re-registering, but argparse subparsers.add_parser
# might fail if it exists. We check if it's already there.
if name not in subparsers.choices:
subparsers.add_parser(
name,
parents=[plugin.parser],
add_help=False,
help=f"[remote] {plugin.parser.description}",
usage=plugin.parser.usage,
description=plugin.parser.description,
epilog=plugin.parser.epilog,
formatter_class=fmt
)
-579
View File
@@ -1,579 +0,0 @@
import sys
import threading
import io
_local = threading.local()
class ThreadLocalStream:
def __init__(self, original):
self._original = original
def _get_stream(self):
s = getattr(_local, 'stream', None)
return s if s is not None else self._original
def write(self, data):
stream = self._get_stream()
if stream:
import time
retries = 0
while True:
try:
stream.write(data)
break
except BlockingIOError:
if retries > 50:
raise
time.sleep(0.01)
retries += 1
def flush(self):
stream = self._get_stream()
if stream:
stream.flush()
def isatty(self):
stream = self._get_stream()
return stream.isatty() if stream else False
def __getattr__(self, name):
# Avoid recursion during initialization or if _original is not yet set
if name in ('_original', '_get_stream'):
raise AttributeError(name)
stream = self._get_stream()
if stream:
return getattr(stream, name)
raise AttributeError(f"'NoneType' object has no attribute '{name}'")
# Patch stdout/stderr only once at module level
if not isinstance(sys.stdout, ThreadLocalStream):
sys.stdout = ThreadLocalStream(sys.stdout)
if not isinstance(sys.stderr, ThreadLocalStream):
sys.stderr = ThreadLocalStream(sys.stderr)
def _get_local():
if not hasattr(_local, 'console'):
_local.console = None
if not hasattr(_local, 'err_console'):
_local.err_console = None
if not hasattr(_local, 'theme') or _local.theme is None:
from rich.theme import Theme
_local.theme = Theme(_global_active_styles)
return _local
def set_thread_stream(stream):
if stream is None:
if hasattr(_local, 'stream'):
del _local.stream
else:
_local.stream = stream
def get_original_stdout():
if isinstance(sys.stdout, ThreadLocalStream):
return sys.stdout._original
return sys.stdout
def get_original_stderr():
if isinstance(sys.stderr, ThreadLocalStream):
return sys.stderr._original
return sys.stderr
# Centralized design system
STYLES = {
"info": "#00ffff", # Cyan
"warning": "#ffff00", # Yellow
"error": "#ff0000", # Red
"success": "#00ff00", # Green
"debug": "#888888",
"header": "bold #00ffff",
"key": "bold #00ffff",
"border": "#00ffff",
"pass": "bold #00ff00",
"fail": "bold #ff0000",
"engineer": "#5fafff", # Sky Blue (lighter than pure blue)
"architect": "#9370db", # Medium Purple
"ai_status": "bold #00ff00",
"user_prompt": "bold #00afd7", # Deep Sky Blue / Soft Cyan
"unavailable": "#d78700",
"contrast": "#bbbbbb",
}
LIGHT_THEME = {
"info": "#00008b", # Navy Blue
"warning": "#d78700", # Orange
"error": "#cd0000", # Dark Red
"success": "#006400", # Dark Green
"debug": "#777777",
"header": "bold #00008b",
"key": "bold #00008b",
"border": "#00008b",
"pass": "bold #006400",
"fail": "bold #cd0000",
"engineer": "#00008b",
"architect": "#8b008b", # Dark Magenta
"ai_status": "bold #006400",
"user_prompt": "bold #00008b",
"unavailable": "#666666",
"contrast": "#777777",
}
_global_active_styles = STYLES.copy()
def _get_console():
local = _get_local()
# Self-healing patch: if sys.stdout was replaced (e.g. by pytest), re-wrap it.
if not isinstance(sys.stdout, ThreadLocalStream):
sys.stdout = ThreadLocalStream(sys.stdout)
current_out = sys.stdout
# Detect if we need to recreate the console (stream changed or closed)
needs_recreate = (local.console is None or
getattr(local, '_last_stdout', None) is not current_out)
# Extra check for closed files in test environments
if not needs_recreate and local.console is not None:
try:
if hasattr(local.console.file, 'closed') and local.console.file.closed:
needs_recreate = True
except Exception:
pass
if needs_recreate:
from rich.console import Console
from rich.theme import Theme
if local.theme is None:
local.theme = Theme(STYLES)
local.console = Console(theme=local.theme, file=current_out)
local._last_stdout = current_out
return local.console
def _get_err_console():
local = _get_local()
# Self-healing patch for stderr
if not isinstance(sys.stderr, ThreadLocalStream):
sys.stderr = ThreadLocalStream(sys.stderr)
current_err = sys.stderr
needs_recreate = (local.err_console is None or
getattr(local, '_last_stderr', None) is not current_err)
if not needs_recreate and local.err_console is not None:
try:
if hasattr(local.err_console.file, 'closed') and local.err_console.file.closed:
needs_recreate = True
except Exception:
pass
if needs_recreate:
from rich.console import Console
from rich.theme import Theme
if local.theme is None:
local.theme = Theme(STYLES)
local.err_console = Console(stderr=True, theme=local.theme, file=current_err)
local._last_stderr = current_err
return local.err_console
def set_thread_console(console):
_get_local().console = console
def set_thread_err_console(console):
_get_local().err_console = console
def clear_thread_state():
"""Removes all thread-local printer state. Useful for gRPC thread reuse."""
for attr in ["stream", "console", "err_console", "theme", "_last_stdout", "_last_stderr"]:
if hasattr(_local, attr):
delattr(_local, attr)
@property
def console():
return _get_console()
@property
def err_console():
return _get_err_console()
@property
def connpy_theme():
local = _get_local()
if local.theme is None:
from rich.theme import Theme
local.theme = Theme(_global_active_styles)
return local.theme
def apply_theme(user_styles=None):
"""
Updates the global console themes with user-defined styles.
If a style is missing in user_styles, it falls back to the default in STYLES.
"""
global _global_active_styles
local = _get_local()
from rich.theme import Theme
# Start with a copy of defaults
active_styles = STYLES.copy()
if user_styles:
# Merge user styles (only if they are valid keys)
for key, value in user_styles.items():
if key in active_styles:
active_styles[key] = value
_global_active_styles = active_styles
local.theme = Theme(active_styles)
if local.console:
local.console.push_theme(local.theme)
if local.err_console:
local.err_console.push_theme(local.theme)
return active_styles
def _format_multiline(tag, message, style=None):
message = str(message)
lines = message.splitlines()
if not lines:
if style:
return f"[{style}]\\[{tag}][/{style}]"
return f"\\[{tag}]"
# Apply style to the tag if provided
styled_tag = f"[{style}]\\[{tag}][/{style}]" if style else f"\\[{tag}]"
if style:
# Include brackets in the styling
styled_tag = f"[{style}]\\[{tag}][/{style}]"
formatted = [f"{styled_tag} {lines[0]}"]
# Indent subsequent lines
indent = " " * (len(tag) + 3)
for line in lines[1:]:
formatted.append(f"{indent}{line}")
return "\n".join(formatted)
def info(message):
_get_console().print(_format_multiline("i", message, style="info"))
def success(message):
_get_console().print(_format_multiline("", message, style="success"))
def start(message):
_get_console().print(_format_multiline("+", message, style="success"))
def warning(message):
_get_console().print(_format_multiline("!", message, style="warning"))
def error(message):
_get_err_console().print(_format_multiline("", message, style="error"))
def debug(message):
_get_console().print(_format_multiline("d", message, style="debug"))
def custom(tag, message):
_get_console().print(_format_multiline(tag, message, style="header"))
def table(title, columns, rows, header_style="header", box=None):
from rich.table import Table
t = Table(title=title, header_style=header_style, box=box)
for col in columns:
t.add_column(col)
for row in rows:
t.add_row(*[str(item) for item in row])
_get_console().print(t)
def data(title, content, language="yaml"):
"""Display structured data with syntax highlighting inside a panel."""
from rich.syntax import Syntax
from rich.panel import Panel
syntax = Syntax(content, language, theme="ansi_dark", word_wrap=True, background_color="default")
panel = Panel(syntax, title=f"[header]{title}[/header]", border_style="border", expand=False)
_get_console().print(panel)
def node_panel(unique, output, status, title_prefix=""):
"""Display node execution result in a styled panel."""
from rich.panel import Panel
from rich.text import Text
from rich.console import Group
import os
try:
cols, _ = os.get_terminal_size()
except OSError:
cols = 80
if status == 0:
status_str = "[pass]✓ PASS[/pass]"
border = "pass"
else:
status_str = f"[fail]✗ FAIL({status})[/fail]"
border = "fail"
title_line = f"{title_prefix}[bold]{unique}[/bold] — {status_str}"
stripped = output.strip() if output else ""
code_block = Text(stripped + "\n") if stripped else Text()
_get_console().print(Panel(Group(Text(), code_block), title=title_line, width=cols, border_style=border))
def test_panel(unique, output, status, result):
"""Display test execution result in a styled panel."""
from rich.panel import Panel
from rich.text import Text
from rich.console import Group
import os
try:
cols, _ = os.get_terminal_size()
except OSError:
cols = 80
is_pass = (status == 0 and result and all(result.values()))
if is_pass:
status_str = "[pass]✓ PASS[/pass]"
border = "pass"
else:
status_str = f"[fail]✗ FAIL[/fail]"
border = "fail"
title_line = f"[bold]{unique}[/bold] — {status_str}"
stripped = output.strip() if output else ""
code_block = Text(stripped + "\n") if stripped else Text()
test_results = Text()
test_results.append("\nTEST RESULTS:\n", style="header")
if result:
max_key_len = max(len(k) for k in result.keys())
for k, v in result.items():
mark = "" if v else ""
style = "success" if v else "error"
test_results.append(f" {k.ljust(max_key_len)} {mark}\n", style=style)
else:
test_results.append(" No results (execution failed)\n", style="error")
_get_console().print(Panel(Group(Text(), code_block, test_results), title=title_line, width=cols, border_style=border))
def test_summary(results):
"""Print an aggregate summary of multiple test results in a single panel."""
from rich.panel import Panel
from rich.text import Text
from rich.console import Group
import os
try:
cols, _ = os.get_terminal_size()
except OSError:
cols = 80
summary_content = Text()
total_passed = 0
total_failed = 0
total_partial = 0
if not results:
summary_content.append(" No test results found.\n", style="error")
else:
for node, test_result in results.items():
summary_content.append(f"", style="border")
summary_content.append(f"{node.ljust(40)}", style="bold")
if test_result:
passed_count = sum(1 for v in test_result.values() if v)
total_count = len(test_result)
if passed_count == total_count:
total_passed += 1
node_style = "success"
mark = "✓ PASS"
elif passed_count > 0:
total_partial += 1
node_style = "warning"
mark = f"⚠ PARTIAL ({passed_count}/{total_count})"
else:
total_failed += 1
node_style = "error"
mark = "✗ FAIL"
summary_content.append(f" {mark}\n", style=node_style)
for k, v in test_result.items():
res_mark = "" if v else ""
res_style = "success" if v else "error"
summary_content.append(f" {k.ljust(38)} {res_mark}\n", style=res_style)
else:
total_failed += 1
summary_content.append(" ✗ FAIL\n", style="error")
summary_content.append(" No results (execution failed)\n", style="error")
status_parts = []
if total_passed: status_parts.append(f"[pass]{total_passed} PASSED[/pass]")
if total_partial: status_parts.append(f"[warning]{total_partial} PARTIAL[/warning]")
if total_failed: status_parts.append(f"[fail]{total_failed} FAILED[/fail]")
status_str = " | ".join(status_parts) if status_parts else "[error]NO RESULTS[/error]"
title_line = f"AGGREGATE TEST SUMMARY — {status_str}"
_get_console().print(Panel(Group(Text(), summary_content), title=title_line, width=cols, border_style="border"))
def run_summary(results):
"""Print an aggregate summary of multiple execution results in a single panel."""
from rich.panel import Panel
from rich.text import Text
from rich.console import Group
import os
try:
cols, _ = os.get_terminal_size()
except OSError:
cols = 80
summary_content = Text()
total_ok = 0
total_err = 0
if not results:
summary_content.append(" No execution results found.\n", style="error")
else:
for node, data in results.items():
summary_content.append(f"", style="border")
summary_content.append(f"{node.ljust(40)}", style="bold")
# Check if we have a status dict or just output (for backward compatibility)
status = data.get("status", 0) if isinstance(data, dict) else 0
if status == 0:
total_ok += 1
summary_content.append(f" ✓ DONE\n", style="success")
else:
total_err += 1
summary_content.append(f" ✗ FAIL({status})\n", style="error")
status_parts = []
if total_ok: status_parts.append(f"[success]{total_ok} DONE[/success]")
if total_err: status_parts.append(f"[error]{total_err} FAILED[/error]")
status_str = " | ".join(status_parts) if status_parts else "[error]NO RESULTS[/error]"
title_line = f"AGGREGATE EXECUTION SUMMARY — {status_str}"
_get_console().print(Panel(Group(Text(), summary_content), title=title_line, width=cols, border_style="border"))
def header(text):
"""Print a section header."""
from rich.rule import Rule
_get_console().print(Rule(text, style="header"))
def kv(key, value):
"""Print an inline key-value pair."""
_get_console().print(f"[key]{key}[/key]: {value}")
def confirm_action(item, action):
"""Print a confirmation pre-action message."""
_get_console().print(f"\\[i] [bold]{action}[/bold]: {item}", style="info")
# Compatibility proxies
class _ConsoleProxy:
def __getattr__(self, name):
return getattr(_get_console(), name)
def __call__(self, *args, **kwargs):
return _get_console()(*args, **kwargs)
def __enter__(self):
return _get_console().__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
return _get_console().__exit__(exc_type, exc_val, exc_tb)
class _ErrConsoleProxy:
def __getattr__(self, name):
return getattr(_get_err_console(), name)
def __call__(self, *args, **kwargs):
return _get_err_console()(*args, **kwargs)
def __enter__(self):
return _get_err_console().__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
return _get_err_console().__exit__(exc_type, exc_val, exc_tb)
console = _ConsoleProxy()
err_console = _ErrConsoleProxy()
# theme also needs to be lazy
class _ThemeProxy:
def __getattr__(self, name):
local = _get_local()
if local.theme is None:
from rich.theme import Theme
local.theme = Theme(_global_active_styles)
return getattr(local.theme, name)
connpy_theme = _ThemeProxy()
class BlockMarkdownRenderer:
"""
Block-buffered streaming markdown renderer.
Accumulates text until block boundaries are detected,
then renders complete blocks using Rich's Markdown.
"""
def __init__(self, console=None):
from rich.console import Console as RichConsole
from .printer import connpy_theme, get_original_stdout
self._console = console or RichConsole(
theme=connpy_theme, file=get_original_stdout()
)
self._line_buf = "" # chars waiting for \n
self._block_lines = [] # complete lines for current block
self._in_code_block = False
def feed(self, text):
self._line_buf += text
while '\n' in self._line_buf:
idx = self._line_buf.index('\n')
line = self._line_buf[:idx + 1]
self._line_buf = self._line_buf[idx + 1:]
self._process_line(line)
def flush(self):
if self._line_buf:
self._block_lines.append(self._line_buf)
self._line_buf = ""
self._flush_block()
def _process_line(self, line):
stripped = line.strip()
if stripped.startswith('```'):
if not self._in_code_block:
# Flush accumulated text before code block
self._flush_block()
self._in_code_block = True
self._block_lines.append(line)
else:
# Include closing fence and flush code block
self._block_lines.append(line)
self._in_code_block = False
self._flush_block()
return
if self._in_code_block:
self._block_lines.append(line)
return
# Blank line = paragraph break
if stripped == '':
self._block_lines.append(line)
self._flush_block()
return
self._block_lines.append(line)
def _flush_block(self):
if not self._block_lines:
return
block_text = ''.join(self._block_lines).strip()
self._block_lines = []
if not block_text:
return
from rich.markdown import Markdown
self._console.print(Markdown(block_text, code_theme="ansi_dark"))
# Alias for backward compatibility
IncrementalMarkdownParser = BlockMarkdownRenderer
View File
-343
View File
@@ -1,343 +0,0 @@
syntax = "proto3";
package connpy;
import "google/protobuf/struct.proto";
import "google/protobuf/empty.proto";
service NodeService {
rpc list_nodes (FilterRequest) returns (ValueResponse) {}
rpc list_folders (FilterRequest) returns (ValueResponse) {}
rpc get_node_details (IdRequest) returns (StructResponse) {}
rpc explode_unique (IdRequest) returns (ValueResponse) {}
rpc generate_cache (google.protobuf.Empty) returns (google.protobuf.Empty) {}
rpc add_node (NodeRequest) returns (google.protobuf.Empty) {}
rpc update_node (NodeRequest) returns (google.protobuf.Empty) {}
rpc delete_node (DeleteRequest) returns (google.protobuf.Empty) {}
rpc move_node (MoveRequest) returns (google.protobuf.Empty) {}
rpc bulk_add (BulkRequest) returns (google.protobuf.Empty) {}
rpc validate_parent_folder (IdRequest) returns (google.protobuf.Empty) {}
rpc set_reserved_names (ListRequest) returns (google.protobuf.Empty) {}
rpc interact_node (stream InteractRequest) returns (stream InteractResponse) {}
rpc full_replace (FullReplaceRequest) returns (google.protobuf.Empty) {}
rpc get_inventory (google.protobuf.Empty) returns (FullReplaceRequest) {}
}
service ProfileService {
rpc list_profiles (FilterRequest) returns (ValueResponse) {}
rpc get_profile (ProfileRequest) returns (StructResponse) {}
rpc add_profile (NodeRequest) returns (google.protobuf.Empty) {}
rpc resolve_node_data (StructRequest) returns (StructResponse) {}
rpc delete_profile (IdRequest) returns (google.protobuf.Empty) {}
rpc update_profile (NodeRequest) returns (google.protobuf.Empty) {}
}
service ConfigService {
rpc get_settings (google.protobuf.Empty) returns (StructResponse) {}
rpc get_default_dir (google.protobuf.Empty) returns (StringResponse) {}
rpc set_config_folder (StringRequest) returns (google.protobuf.Empty) {}
rpc update_setting (UpdateRequest) returns (google.protobuf.Empty) {}
rpc encrypt_password (StringRequest) returns (StringResponse) {}
rpc apply_theme_from_file (StringRequest) returns (StructResponse) {}
}
service PluginService {
rpc list_plugins (google.protobuf.Empty) returns (ValueResponse) {}
rpc add_plugin (PluginRequest) returns (google.protobuf.Empty) {}
rpc delete_plugin (IdRequest) returns (google.protobuf.Empty) {}
rpc enable_plugin (IdRequest) returns (google.protobuf.Empty) {}
rpc disable_plugin (IdRequest) returns (google.protobuf.Empty) {}
}
service ExecutionService {
rpc run_commands (RunRequest) returns (stream NodeRunResult) {}
rpc test_commands (TestRequest) returns (stream NodeRunResult) {}
rpc run_cli_script (ScriptRequest) returns (StructResponse) {}
}
service ImportExportService {
rpc export_to_file (ExportRequest) returns (google.protobuf.Empty) {}
rpc import_from_file (StringRequest) returns (google.protobuf.Empty) {}
rpc set_reserved_names (ListRequest) returns (google.protobuf.Empty) {}
}
service AIService {
rpc ask (stream AskRequest) returns (stream AIResponse) {}
rpc confirm (StringRequest) returns (BoolResponse) {}
rpc ask_copilot (CopilotRequest) returns (CopilotResponse) {}
rpc list_sessions (google.protobuf.Empty) returns (ValueResponse) {}
rpc delete_session (StringRequest) returns (google.protobuf.Empty) {}
rpc configure_provider (ProviderRequest) returns (google.protobuf.Empty) {}
rpc configure_mcp (MCPRequest) returns (google.protobuf.Empty) {}
rpc list_mcp_servers (google.protobuf.Empty) returns (ValueResponse) {}
rpc load_session_data (StringRequest) returns (StructResponse) {}
rpc build_playbook_chat (stream AskRequest) returns (stream AIResponse) {}
rpc analyze_execution_results (AnalyzeRequest) returns (stream AIResponse) {}
rpc predict_execution_results (PreflightRequest) returns (stream AIResponse) {}
}
service SystemService {
rpc start_api (IntRequest) returns (google.protobuf.Empty) {}
rpc debug_api (IntRequest) returns (google.protobuf.Empty) {}
rpc stop_api (google.protobuf.Empty) returns (google.protobuf.Empty) {}
rpc restart_api (IntRequest) returns (google.protobuf.Empty) {}
rpc get_api_status (google.protobuf.Empty) returns (BoolResponse) {}
}
// Request and Response Messages
message InteractRequest {
string id = 1;
bool sftp = 2;
bool debug = 3;
bytes stdin_data = 4;
int32 cols = 5;
int32 rows = 6;
string connection_params_json = 7;
// Copilot fields
string copilot_question = 8;
string copilot_action = 9;
string copilot_context_buffer = 10;
string copilot_node_info_json = 13;
}
message InteractResponse {
bytes stdout_data = 1;
bool success = 2;
string error_message = 3;
// Copilot fields
bool copilot_prompt = 4;
string copilot_buffer_preview = 5;
string copilot_response_json = 6;
string copilot_node_info_json = 7;
string copilot_stream_chunk = 8;
string copilot_injected_command = 9;
}
message FilterRequest {
string filter_str = 1;
string format_str = 2;
}
message ValueResponse {
google.protobuf.Value data = 1;
}
message IdRequest {
string id = 1;
}
message NodeRequest {
string id = 1;
google.protobuf.Struct data = 2;
bool is_folder = 3;
}
message DeleteRequest {
string id = 1;
bool is_folder = 2;
}
message MessageValue {
string value = 1;
}
message MoveRequest {
string src_id = 1;
string dst_id = 2;
bool copy = 3;
}
message BulkRequest {
repeated string ids = 1;
repeated string hosts = 2;
google.protobuf.Struct common_data = 3;
}
message StructResponse {
google.protobuf.Struct data = 1;
}
message ProfileRequest {
string name = 1;
bool resolve = 2;
}
message StructRequest {
google.protobuf.Struct data = 1;
}
message StringRequest {
string value = 1;
}
message StringResponse {
string value = 1;
}
message UpdateRequest {
string key = 1;
google.protobuf.Value value = 2;
}
message PluginRequest {
string name = 1;
string source_file = 2;
bool update = 3;
}
message RunRequest {
repeated string nodes = 1;
repeated string commands = 2;
string folder = 3;
string prompt = 4;
int32 parallel = 5;
google.protobuf.Struct vars = 6;
int32 timeout = 7;
string name = 8;
}
message TestRequest {
repeated string nodes = 1;
repeated string commands = 2;
repeated string expected = 3;
string folder = 4;
string prompt = 5;
int32 parallel = 6;
google.protobuf.Struct vars = 7;
int32 timeout = 8;
string name = 9;
}
message ScriptRequest {
string param1 = 1; // nodes_filter or playbook_path
string param2 = 2; // script_path or ""
int32 parallel = 3;
}
message ExportRequest {
string file_path = 1;
repeated string folders = 2;
}
message ListRequest {
repeated string items = 1;
}
message AskRequest {
string input_text = 1;
bool dryrun = 2;
google.protobuf.Value chat_history = 3;
string session_id = 4;
bool debug = 5;
string engineer_model = 6;
string engineer_api_key = 7;
string architect_model = 8;
string architect_api_key = 9;
bool trust = 10;
string confirmation_answer = 11;
bool interrupt = 12;
google.protobuf.Struct engineer_auth = 13;
google.protobuf.Struct architect_auth = 14;
}
message AIResponse {
string text_chunk = 1;
bool is_final = 2;
google.protobuf.Struct full_result = 3;
string status_update = 4;
string debug_message = 5;
bool requires_confirmation = 6;
string important_message = 7;
}
message BoolResponse {
bool value = 1;
}
message ProviderRequest {
string provider = 1;
string model = 2;
string api_key = 3;
google.protobuf.Struct auth = 4;
}
message IntRequest {
int32 value = 1;
}
message NodeRunResult {
string unique_id = 1;
string output = 2;
int32 status = 3;
google.protobuf.Struct test_result = 4;
}
message FullReplaceRequest {
google.protobuf.Struct connections = 1;
google.protobuf.Struct profiles = 2;
}
message CopilotRequest {
string terminal_buffer = 1;
string user_question = 2;
string node_info_json = 3;
}
message CopilotResponse {
repeated string commands = 1;
string guide = 2;
string risk_level = 3;
string error = 4;
}
message MCPRequest {
string name = 1;
string url = 2;
bool enabled = 3;
string auto_load_on_os = 4;
bool remove = 5;
}
service AuthService {
rpc login (LoginRequest) returns (LoginResponse) {}
rpc login_sso (LoginSSORequest) returns (LoginResponse) {}
rpc change_password (ChangePasswordRequest) returns (google.protobuf.Empty) {}
rpc get_sso_providers (google.protobuf.Empty) returns (SSOProvidersResponse) {}
}
message SSOProvidersResponse {
repeated string providers = 1;
}
message LoginRequest {
string username = 1;
string password = 2;
}
message LoginSSORequest {
string username = 1;
string id_token = 2;
string provider = 3;
}
message LoginResponse {
string token = 1;
string username = 2;
int64 expires_at = 3;
}
message ChangePasswordRequest {
string old_password = 1;
string new_password = 2;
}
message AnalyzeRequest {
google.protobuf.Struct results = 1;
string query = 2;
}
message PreflightRequest {
repeated string target_nodes = 1;
repeated string commands = 2;
}
-28
View File
@@ -1,28 +0,0 @@
from .exceptions import *
from .node_service import NodeService
from .profile_service import ProfileService
from .execution_service import ExecutionService
from .import_export_service import ImportExportService
from .ai_service import AIService
from .plugin_service import PluginService
from .config_service import ConfigService
from .system_service import SystemService
__all__ = [
'NodeService',
'ProfileService',
'ExecutionService',
'ImportExportService',
'AIService',
'PluginService',
'ConfigService',
'SystemService',
'ConnpyError',
'NodeNotFoundError',
'NodeAlreadyExistsError',
'ProfileNotFoundError',
'ProfileAlreadyExistsError',
'ExecutionError',
'InvalidConfigurationError'
]
-355
View File
@@ -1,355 +0,0 @@
import re
from .base import BaseService
from .exceptions import InvalidConfigurationError
from connpy.utils import log_cleaner
class AIService(BaseService):
"""Business logic for interacting with AI agents and LLM configurations."""
def _clean_cisco_scrolling(self, text: str) -> str:
"""Resolves horizontal scrolling artifacts (backspaces, \r, ANSI) by merging overlapping segments."""
def merge_overlapping(s1, s2):
s2_clean = s2.lstrip(' $')
max_overlap = min(len(s1), len(s2_clean))
for i in range(max_overlap, 0, -1):
if s1[-i:] == s2_clean[:i]:
return s1 + s2_clean[i:]
return s1 + s2_clean
scroll_re = re.compile(r'(\x08{5,}\s*\$?|\$\r|\x1b\[\d+[GD]\s*\$?)')
parts = scroll_re.split(text)
merged = ""
for part in parts:
if scroll_re.match(part):
continue
cleaned = log_cleaner(part)
if not merged:
merged = cleaned
else:
merged_lines = merged.split('\n')
cleaned_lines = cleaned.split('\n')
merged_lines[-1] = merge_overlapping(merged_lines[-1], cleaned_lines[0])
merged_lines.extend(cleaned_lines[1:])
merged = "\n".join(merged_lines)
return merged
def build_context_blocks(self, raw_bytes: bytes, cmd_byte_positions: list, node_info: dict, last_line: str = "") -> list:
"""Identifies command blocks in the terminal history."""
blocks = []
if not raw_bytes:
return blocks
default_prompt = r'>$|#$|\$$|>.$|#.$|\$.$'
device_prompt = node_info.get("prompt", default_prompt) if isinstance(node_info, dict) else default_prompt
prompt_re_str = re.sub(r'(?<!\\)\$', '', device_prompt)
try:
prompt_re = re.compile(prompt_re_str)
except Exception:
prompt_re = re.compile(re.sub(r'(?<!\\)\$', '', default_prompt))
parsed_positions = []
if cmd_byte_positions and len(cmd_byte_positions) >= 1:
for i in range(1, len(cmd_byte_positions)):
pos, known_cmd = cmd_byte_positions[i]
prev_pos = cmd_byte_positions[i-1][0]
if known_cmd:
if known_cmd == "CANCELLED":
parsed_positions.append({"pos": pos, "type": "CANCELLED", "preview": ""})
else:
prev_chunk = raw_bytes[prev_pos:pos]
prev_cleaned = self._clean_cisco_scrolling(prev_chunk.decode(errors='replace'))
prev_lines = [l for l in prev_cleaned.split('\n') if l.strip()]
prompt_text = prev_lines[-1].strip() if prev_lines else ""
preview = f"{prompt_text}{known_cmd}" if prompt_text else known_cmd
if len(preview) > 80:
preview = preview[:77] + "..."
parsed_positions.append({"pos": pos, "type": "VALID_CMD", "preview": preview})
else:
chunk = raw_bytes[prev_pos:pos]
cleaned = self._clean_cisco_scrolling(chunk.decode(errors='replace'))
lines = [l for l in cleaned.split('\n') if l.strip()]
found_in_pass1 = False
if lines:
# Search backwards through the last few lines for the prompt
for idx in range(len(lines) - 1, max(-1, len(lines) - 10), -1):
match = prompt_re.search(lines[idx])
if match:
ptxt = match.group(0).strip()
cmd_first_line = lines[idx][match.end():].strip()
cmd_rest = [l.strip() for l in lines[idx+1:]]
cmd_text = " ".join([cmd_first_line] + cmd_rest).strip()
if cmd_text:
pv = f"{ptxt} {cmd_text}".strip()
if len(pv) > 80:
pv = pv[:77] + "..."
parsed_positions.append({"pos": pos, "type": "VALID_CMD", "preview": pv})
else:
parsed_positions.append({"pos": pos, "type": "EMPTY_PROMPT", "preview": ""})
found_in_pass1 = True
break
if not found_in_pass1:
# Fallback: The prompt might have been isolated in the previous chunk
# due to asynchronous network delays splitting the output exactly at the newline.
prev_was_valid_cmd = i >= 2 and parsed_positions[i-2]["type"] == "VALID_CMD"
if prev_pos > 0 and not prev_was_valid_cmd:
# Fetch the very last chunk that we just processed
prev_prev_pos = cmd_byte_positions[i-2][0] if i >= 2 else 0
prev_chunk_text = self._clean_cisco_scrolling(raw_bytes[prev_prev_pos:prev_pos].decode(errors='replace'))
prev_lines_text = [l for l in prev_chunk_text.split('\n') if l.strip()]
if prev_lines_text:
prev_match = prompt_re.search(prev_lines_text[-1])
if prev_match:
ptxt = prev_match.group(0).strip()
cmd_text = " ".join([l.strip() for l in lines]).strip()
if cmd_text:
pv = f"{ptxt} {cmd_text}".strip()
if len(pv) > 80:
pv = pv[:77] + "..."
parsed_positions.append({"pos": pos, "type": "VALID_CMD", "preview": pv})
found_in_pass1 = True
if not found_in_pass1:
parsed_positions.append({"pos": pos, "type": "SCROLLING", "preview": ""})
else:
parsed_positions.append({"pos": pos, "type": "SCROLLING", "preview": ""})
last_newline = raw_bytes.rfind(b'\n')
current_prompt_pos = last_newline + 1 if last_newline != -1 else 0
current_end = len(raw_bytes)
for i, item in enumerate(parsed_positions):
if item["type"] == "VALID_CMD":
start_pos = item["pos"]
preview = item["preview"]
# Find the end position: next VALID_CMD or EMPTY_PROMPT or CANCELLED
end_pos = current_prompt_pos
for j in range(i + 1, len(parsed_positions)):
next_item = parsed_positions[j]
if next_item["type"] in ("VALID_CMD", "EMPTY_PROMPT", "CANCELLED"):
end_pos = next_item["pos"]
break
blocks.append((start_pos, end_pos, preview))
# Always ensure there is a final block representing the current prompt
if not blocks:
blocks.append((current_prompt_pos, current_end, last_line[:80] if last_line else "CURRENT CONTEXT"))
elif blocks[-1][0] < current_prompt_pos:
blocks.append((current_prompt_pos, current_end, last_line[:80] if last_line else "CURRENT CONTEXT"))
return blocks
def process_copilot_input(self, input_text: str, session_state: dict) -> dict:
"""Parses slash commands and manages session state. Returns directive dict."""
text = input_text.strip()
if not text.startswith('/'):
return {"action": "execute", "clean_prompt": text, "overrides": {}}
parts = text.split(maxsplit=1)
cmd = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
# 1. State Commands (Persistent)
if cmd == "/os":
if args:
session_state['os'] = args
return {"action": "state_update", "message": f"OS context changed to {args}"}
elif cmd == "/prompt":
if args:
session_state['prompt'] = args
return {"action": "state_update", "message": f"Prompt regex changed to {args}"}
elif cmd == "/memorize":
if args:
session_state['memories'].append(args)
return {"action": "state_update", "message": f"Memory added: {args}"}
elif cmd == "/clear":
session_state['memories'] = []
return {"action": "state_update", "message": "Memory cleared"}
# 2. Hybrid Commands
elif cmd == "/architect":
if not args:
session_state['persona'] = 'architect'
return {"action": "state_update", "message": "Persona set to Architect"}
else:
return {"action": "execute", "clean_prompt": args, "overrides": {"persona": "architect"}}
elif cmd == "/engineer":
if not args:
session_state['persona'] = 'engineer'
return {"action": "state_update", "message": "Persona set to Engineer"}
else:
return {"action": "execute", "clean_prompt": args, "overrides": {"persona": "engineer"}}
elif cmd == "/trust":
if not args:
session_state['trust_mode'] = True
return {"action": "state_update", "message": "Auto-execute (trust) enabled for session"}
else:
return {"action": "execute", "clean_prompt": args, "overrides": {"trust": True}}
elif cmd == "/untrust":
if not args:
session_state['trust_mode'] = False
return {"action": "state_update", "message": "Auto-execute (trust) disabled for session"}
else:
return {"action": "execute", "clean_prompt": args, "overrides": {"trust": False}}
# Unknown command, execute normally
return {"action": "execute", "clean_prompt": text, "overrides": {}}
def ask(self, input_text, dryrun=False, chat_history=None, status=None, debug=False, session_id=None, console=None, chunk_callback=None, confirm_handler=None, trust=False, **overrides):
"""Send a prompt to the AI agent."""
from connpy.ai import ai
agent = ai(self.config, console=console, confirm_handler=confirm_handler, trust=trust, **overrides)
return agent.ask(input_text, dryrun, chat_history, status=status, debug=debug, session_id=session_id, chunk_callback=chunk_callback)
def confirm(self, input_text, console=None):
"""Ask for a safe confirmation of an action."""
from connpy.ai import ai
agent = ai(self.config, console=console)
return agent.confirm(input_text)
def ask_copilot(self, terminal_buffer, user_question, node_info=None, chunk_callback=None):
"""Ask the AI copilot for terminal assistance."""
from connpy.ai import ai, run_ai_async
agent = ai(self.config)
future = run_ai_async(agent.aask_copilot(terminal_buffer, user_question, node_info, chunk_callback=chunk_callback))
return future.result()
async def aask_copilot(self, terminal_buffer, user_question, node_info=None, chunk_callback=None):
"""Ask the AI copilot for terminal assistance asynchronously."""
from connpy.ai import ai, run_ai_async
import asyncio
agent = ai(self.config)
future = run_ai_async(agent.aask_copilot(terminal_buffer, user_question, node_info, chunk_callback=chunk_callback))
return await asyncio.wrap_future(future)
def list_sessions(self, limit=None):
"""Return a list of saved AI sessions, optionally limited."""
from connpy.ai import ai
agent = ai(self.config)
sessions = agent._get_sessions()
if limit and len(sessions) > limit:
return sessions[:limit], len(sessions)
return sessions, len(sessions)
def delete_session(self, session_id):
"""Delete an AI session by ID."""
import os
sessions_dir = os.path.join(self.config.defaultdir, "ai_sessions")
path = os.path.join(sessions_dir, f"{session_id}.json")
if os.path.exists(path):
os.remove(path)
else:
raise InvalidConfigurationError(f"Session '{session_id}' not found.")
def configure_provider(self, provider, model=None, api_key=None, auth=None):
"""Update AI provider settings in the configuration."""
settings = self.config.config.get("ai", {})
if model:
settings[f"{provider}_model"] = model
if api_key:
settings[f"{provider}_api_key"] = api_key
if auth is not None:
settings[f"{provider}_auth"] = auth
self.config.config["ai"] = settings
self.config._saveconfig(self.config.file)
def configure_mcp(self, name, url=None, enabled=None, auto_load_on_os=None, remove=False):
"""Update MCP server settings in the configuration with smart merging."""
ai_settings = self.config.config.get("ai", {})
mcp_servers = ai_settings.get("mcp_servers", {})
if remove:
if name in mcp_servers:
del mcp_servers[name]
else:
# Get existing or new
server_cfg = mcp_servers.get(name, {})
# Partial updates
if url is not None:
server_cfg["url"] = url
if enabled is not None:
server_cfg["enabled"] = bool(enabled)
elif "enabled" not in server_cfg:
server_cfg["enabled"] = True # Default for new entries
if auto_load_on_os is not None:
if auto_load_on_os == "": # Explicit clear
if "auto_load_on_os" in server_cfg:
del server_cfg["auto_load_on_os"]
else:
server_cfg["auto_load_on_os"] = auto_load_on_os
mcp_servers[name] = server_cfg
ai_settings["mcp_servers"] = mcp_servers
self.config.config["ai"] = ai_settings
self.config._saveconfig(self.config.file)
def list_mcp_servers(self) -> dict:
"""Get the configured MCP servers."""
if hasattr(self.config, "get_effective_setting"):
ai_settings = self.config.get_effective_setting("ai", {})
else:
ai_settings = self.config.config.get("ai", {}) if hasattr(self.config, "config") else {}
return ai_settings.get("mcp_servers", {})
def load_session_data(self, session_id):
"""Load a session's raw data by ID."""
from connpy.ai import ai
agent = ai(self.config)
return agent.load_session_data(session_id)
def build_playbook_chat(self, user_input: str, chat_history: list = None, status=None, chunk_callback=None):
"""Interact with the specialized Playbook Builder Agent."""
from connpy.ai import PlaybookBuilderAgent
agent = PlaybookBuilderAgent(self.config)
return agent.ask(user_input, chat_history=chat_history, status=status, chunk_callback=chunk_callback)
def analyze_execution_results(self, results: dict, query: str = None, status=None, chunk_callback=None):
"""Analyze actual command execution results using Network Architect 1-shot."""
import json
results_str = json.dumps(results, indent=2)
prompt = f"@architect: Please analyze the following actual execution results. Diagnose any issues, highlight successful actions, and suggest strategic remediation steps if needed."
if query:
prompt += f"\nSpecific user request: {query}"
prompt += f"\n\nResults Data:\n{results_str}"
prompt += "\n\nCRITICAL DIRECTIVE: You are running in a strictly 1-shot offline diagnostics mode (--analyze). There is no active conversation loop, and you are NOT conversing with a Network Engineer. You MUST deliver your complete strategic analysis immediately. DO NOT suggest, mention, or attempt to delegate the session back to the engineer."
# Delegate to self.ask, setting stream=True and forwarding callback/status.
# This will invoke standard ai.ask with '@architect:' prefix, forcing 1-shot architect brain.
return self.ask(prompt, status=status, chunk_callback=chunk_callback, one_shot=True)
def predict_execution_results(self, target_nodes: list, commands: list, status=None, chunk_callback=None):
"""Predict and simulate execution results preventively using the Preflight Simulation Agent (1-shot)."""
nodes_str = ", ".join(target_nodes)
commands_str = "\n".join(f"- {cmd}" for cmd in commands)
prompt = f"@engineer: Act as a Preflight Simulation Agent. Simulate and predict the expected outputs and behaviors of the following commands on the target nodes. Alert about potential safety or configuration risks based on node profiles."
prompt += f"\n\nTarget Nodes: {nodes_str}"
prompt += f"\nCommands to simulate:\n{commands_str}"
prompt += "\n\nCRITICAL SCALABILITY DIRECTIVE: If there are many target nodes, DO NOT list predictions node-by-node. Instead, group them by Operating System, vendor, or platform, and provide a highly concise Executive Summary. Detail individual risks only for nodes that present specific anomalies or security concerns. Focus on overall impact."
# Delegate to self.ask, using the standard engineer brain but with the simulated preflight prompt.
return self.ask(prompt, status=status, chunk_callback=chunk_callback)
-33
View File
@@ -1,33 +0,0 @@
from connpy.hooks import MethodHook
class BaseService:
"""Base class for all connpy services, providing common configuration access."""
def __init__(self, config=None):
"""
Initialize the service.
Args:
config: An instance of configfile (or None to instantiate a new one/use global context).
"""
from connpy import configfile
self.config = config or configfile()
self.hooks = MethodHook
self.reserved_names = []
def set_reserved_names(self, names):
"""Inject a list of reserved names (e.g. from the CLI)."""
self.reserved_names = names
def _validate_node_name(self, unique_id):
"""Check if the node name in unique_id is reserved."""
from .exceptions import ReservedNameError
if not self.reserved_names:
return
uniques = self.config._explode_unique(unique_id)
if uniques and "id" in uniques:
# We only validate the 'id' (the actual node name), folders are prefixed with @
node_name = uniques["id"]
if node_name in self.reserved_names:
raise ReservedNameError(f"Node name '{node_name}' is a reserved command.")
-86
View File
@@ -1,86 +0,0 @@
import os
import shutil
import base64
from typing import Any, Dict
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from .base import BaseService
from .exceptions import ConnpyError, InvalidConfigurationError, NodeNotFoundError
class ConfigService(BaseService):
"""Business logic for general application settings and state configuration."""
def get_settings(self) -> Dict[str, Any]:
"""Get the global configuration settings block."""
settings = self.config.config.copy()
settings["configfolder"] = self.config.defaultdir
return settings
def get_default_dir(self) -> str:
"""Get the default configuration directory."""
return self.config.defaultdir
def set_config_folder(self, folder_path: str):
"""Set the default location for config file by writing to ~/.config/conn/.folder"""
if not os.path.isdir(folder_path):
raise ConnpyError(f"readable_dir:{folder_path} is not a valid path")
pathfile = os.path.join(self.config.anchor_path, ".folder")
folder = os.path.abspath(folder_path).rstrip('/')
try:
with open(pathfile, "w") as f:
f.write(str(folder))
except Exception as e:
raise ConnpyError(f"Failed to save config folder: {e}")
def update_setting(self, key, value):
"""Update a setting in the configuration file."""
self.config.config[key] = value
self.config._saveconfig(self.config.file)
def encrypt_password(self, password):
"""Encrypt a password using the application's configuration encryption key."""
return self.config.encrypt(password)
def apply_theme_from_file(self, theme_input):
"""Apply 'dark', 'light' theme or load a YAML theme file and save it to the configuration."""
import yaml
from ..printer import STYLES, LIGHT_THEME
if theme_input == "dark":
valid_styles = {}
self.update_setting("theme", valid_styles)
return valid_styles
elif theme_input == "light":
valid_styles = LIGHT_THEME.copy()
self.update_setting("theme", valid_styles)
return valid_styles
if not os.path.exists(theme_input):
raise InvalidConfigurationError(f"Theme file '{theme_input}' not found.")
try:
with open(theme_input, 'r') as f:
user_styles = yaml.safe_load(f)
except Exception as e:
raise InvalidConfigurationError(f"Failed to parse theme file: {e}")
if not isinstance(user_styles, dict):
raise InvalidConfigurationError("Theme file must be a YAML dictionary.")
# Support both direct styles and nested under 'theme' key
if "theme" in user_styles and isinstance(user_styles["theme"], dict):
user_styles = user_styles["theme"]
# Filter for valid styles only (prevent junk in config)
valid_styles = {k: v for k, v in user_styles.items() if k in STYLES}
if not valid_styles:
raise InvalidConfigurationError("No valid style keys found in theme file.")
# Persist and return merged styles
self.update_setting("theme", valid_styles)
return valid_styles
-87
View File
@@ -1,87 +0,0 @@
import re
from typing import List, Dict, Any
from .base import BaseService
from ..hooks import MethodHook
from .. import printer
class ContextService(BaseService):
"""Business logic for managing and applying regex-based contexts locally."""
@property
def contexts(self) -> Dict[str, List[str]]:
return self.config.config.get("contexts", {"all": [".*"]})
@property
def current_context(self) -> str:
return self.config.config.get("current_context", "all")
def list_contexts(self) -> List[Dict[str, Any]]:
result = []
for name in self.contexts.keys():
result.append({
"name": name,
"active": (name == self.current_context),
"regexes": self.contexts[name]
})
return result
def add_context(self, name: str, regexes: List[str]):
if not name.isalnum():
raise ValueError("Context name must be alphanumeric")
ctxs = self.contexts
if name in ctxs:
raise ValueError(f"Context '{name}' already exists")
ctxs[name] = regexes
self.config.config["contexts"] = ctxs
self.config._saveconfig(self.config.file)
def update_context(self, name: str, regexes: List[str]):
if name == "all":
raise ValueError("Cannot modify default context 'all'")
ctxs = self.contexts
if name not in ctxs:
raise ValueError(f"Context '{name}' does not exist")
ctxs[name] = regexes
self.config.config["contexts"] = ctxs
self.config._saveconfig(self.config.file)
def delete_context(self, name: str):
if name == "all":
raise ValueError("Cannot delete default context 'all'")
if name == self.current_context:
raise ValueError(f"Cannot delete active context '{name}'")
ctxs = self.contexts
if name not in ctxs:
raise ValueError(f"Context '{name}' does not exist")
del ctxs[name]
self.config.config["contexts"] = ctxs
self.config._saveconfig(self.config.file)
def set_active_context(self, name: str):
if name not in self.contexts:
raise ValueError(f"Context '{name}' does not exist")
self.config.config["current_context"] = name
self.config._saveconfig(self.config.file)
def get_active_regexes(self) -> List[re.Pattern]:
patterns = self.contexts.get(self.current_context, [".*"])
return [re.compile(p) for p in patterns]
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
return any(p.match(node_name) for p in patterns)
# Hook handlers for filtering
def filter_node_list(self, *args, **kwargs):
patterns = self.get_active_regexes()
return [node for node in kwargs["result"] if self._match_any(node, patterns)]
def filter_node_dict(self, *args, **kwargs):
patterns = self.get_active_regexes()
return {k: v for k, v in kwargs["result"].items() if self._match_any(k, patterns)}
-31
View File
@@ -1,31 +0,0 @@
class ConnpyError(Exception):
"""Base exception for all connpy services."""
pass
class NodeNotFoundError(ConnpyError):
"""Raised when a connection or folder is not found."""
pass
class NodeAlreadyExistsError(ConnpyError):
"""Raised when a node or folder already exists."""
pass
class ProfileNotFoundError(ConnpyError):
"""Raised when a profile is not found."""
pass
class ProfileAlreadyExistsError(ConnpyError):
"""Raised when a profile with the same name already exists."""
pass
class ExecutionError(ConnpyError):
"""Raised when an execution fails or returns error."""
pass
class InvalidConfigurationError(ConnpyError):
"""Raised when data or configuration input is invalid."""
pass
class ReservedNameError(ConnpyError):
"""Raised when a node name conflicts with a reserved command."""
pass
-109
View File
@@ -1,109 +0,0 @@
from typing import List, Dict, Any, Callable, Optional
import os
from .base import BaseService
from connpy.core import nodes as Nodes
from .exceptions import ConnpyError
class ExecutionService(BaseService):
"""Business logic for executing commands on nodes and running automation scripts."""
def run_commands(
self,
nodes_filter: str,
commands: List[str],
variables: Optional[Dict[str, Any]] = None,
parallel: int = 10,
timeout: int = 20,
folder: Optional[str] = None,
prompt: Optional[str] = None,
on_node_complete: Optional[Callable] = None,
logger: Optional[Callable] = None,
name: Optional[str] = None
) -> Dict[str, str]:
"""Execute commands on a set of nodes."""
try:
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.run(
commands=commands,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
# Combine output and status for the caller
full_results = {}
for unique in results:
full_results[unique] = {
"output": results[unique],
"status": executor.status.get(unique, 1)
}
return full_results
except Exception as e:
raise ConnpyError(f"Execution failed: {e}")
def test_commands(
self,
nodes_filter: str,
commands: List[str],
expected: List[str],
variables: Optional[Dict[str, Any]] = None,
parallel: int = 10,
timeout: int = 20,
folder: Optional[str] = None,
prompt: Optional[str] = None,
on_node_complete: Optional[Callable] = None,
logger: Optional[Callable] = None,
name: Optional[str] = None
) -> Dict[str, Dict[str, bool]]:
"""Run commands and verify expected output on a set of nodes."""
try:
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.test(
commands=commands,
expected=expected,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
return results
except Exception as e:
raise ConnpyError(f"Testing failed: {e}")
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
"""Run a plain-text script containing one command per line."""
if not os.path.exists(script_path):
raise ConnpyError(f"Script file not found: {script_path}")
try:
with open(script_path, "r") as f:
commands = [line.strip() for line in f if line.strip()]
except Exception as e:
raise ConnpyError(f"Failed to read script {script_path}: {e}")
return self.run_commands(nodes_filter, commands, parallel=parallel)
-115
View File
@@ -1,115 +0,0 @@
from .base import BaseService
import yaml
import os
from copy import deepcopy
from .exceptions import InvalidConfigurationError, NodeNotFoundError, ReservedNameError
from ..configfile import NoAliasDumper
class ImportExportService(BaseService):
"""Business logic for YAML/JSON inventory import and export."""
def export_to_file(self, file_path, folders=None):
"""Export nodes/folders to a YAML file."""
if os.path.exists(file_path):
raise InvalidConfigurationError(f"File '{file_path}' already exists.")
data = self.export_to_dict(folders)
try:
with open(file_path, "w") as f:
yaml.dump(data, f, Dumper=NoAliasDumper, default_flow_style=False)
except OSError as e:
raise InvalidConfigurationError(f"Failed to export to '{file_path}': {e}")
def export_to_dict(self, folders=None):
"""Export nodes/folders to a dictionary."""
if not folders:
return deepcopy(self.config.connections)
else:
# Validate folders exist
for f in folders:
if f != "@" and f not in self.config._getallfolders():
raise NodeNotFoundError(f"Folder '{f}' not found.")
flat = self.config._getallnodesfull(folders, extract=False)
nested = {}
for k, v in flat.items():
uniques = self.config._explode_unique(k)
if not uniques:
continue
if "folder" in uniques and "subfolder" in uniques:
f_name = uniques["folder"]
s_name = uniques["subfolder"]
i_name = uniques["id"]
if f_name not in nested:
nested[f_name] = {"type": "folder"}
if s_name not in nested[f_name]:
nested[f_name][s_name] = {"type": "subfolder"}
nested[f_name][s_name][i_name] = v
elif "folder" in uniques:
f_name = uniques["folder"]
i_name = uniques["id"]
if f_name not in nested:
nested[f_name] = {"type": "folder"}
nested[f_name][i_name] = v
else:
i_name = uniques["id"]
nested[i_name] = v
return nested
def import_from_file(self, file_path):
"""Import nodes/folders from a YAML file."""
if not os.path.exists(file_path):
raise InvalidConfigurationError(f"File '{file_path}' does not exist.")
try:
with open(file_path, "r") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
self.import_from_dict(data)
except Exception as e:
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
def import_from_dict(self, data):
"""Import nodes/folders from a dictionary."""
if not isinstance(data, dict):
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.")
def _traverse_import(node_data, current_folder='', current_subfolder=''):
for k, v in node_data.items():
if k == "type":
continue
if isinstance(v, dict):
node_type = v.get("type", "connection")
if node_type == "folder":
self.config._folder_add(folder=k)
_traverse_import(v, current_folder=k, current_subfolder='')
elif node_type == "subfolder":
self.config._folder_add(folder=current_folder, subfolder=k)
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == "connection":
unique_id = k
if current_subfolder:
unique_id = f"{k}@{current_subfolder}@{current_folder}"
elif current_folder:
unique_id = f"{k}@{current_folder}"
self._validate_node_name(unique_id)
kwargs = deepcopy(v)
kwargs['id'] = k
kwargs['folder'] = current_folder
kwargs['subfolder'] = current_subfolder
self.config._connections_add(**kwargs)
else:
# Invalid format skip
pass
_traverse_import(data)
self.config._saveconfig(self.config.file)
-275
View File
@@ -1,275 +0,0 @@
import re
from .base import BaseService
from .exceptions import (
NodeNotFoundError, NodeAlreadyExistsError,
InvalidConfigurationError, ReservedNameError
)
class NodeService(BaseService):
def __init__(self, config=None):
super().__init__(config)
def list_nodes(self, filter_str=None, format_str=None):
"""Return a listed filtered by regex match and formatted if needed."""
nodes = self.config._getallnodes()
case_sensitive = self.config.config.get("case", False)
if filter_str:
flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)]
if not format_str:
return nodes
from .profile_service import ProfileService
profile_service = ProfileService(self.config)
formatted_nodes = []
for n_id in nodes:
# Use ProfileService to resolve profiles for dynamic formatting
details = self.config.getitem(n_id, extract=False)
if details:
details = profile_service.resolve_node_data(details)
name = n_id.split("@")[0]
location = n_id.partition("@")[2] or "root"
# Prepare context for .format() with all details
context = details.copy()
context.update({
"name": name,
"NAME": name.upper(),
"location": location,
"LOCATION": location.upper(),
})
# Add exploded uniques (id, folder, subfolder)
uniques = self.config._explode_unique(n_id)
if uniques:
context.update(uniques)
# Add uppercase versions of all keys for convenience
for k, v in list(context.items()):
if isinstance(v, str):
context[k.upper()] = v.upper()
try:
formatted_nodes.append(format_str.format(**context))
except (KeyError, IndexError, ValueError):
# Fallback to original string if format fails
formatted_nodes.append(n_id)
return formatted_nodes
def list_folders(self, filter_str=None):
"""Return all unique folders, optionally filtered by regex."""
folders = self.config._getallfolders()
case_sensitive = self.config.config.get("case", False)
if filter_str:
if filter_str.startswith("@"):
if not case_sensitive:
folders = [f for f in folders if f.lower() == filter_str.lower()]
else:
folders = [f for f in folders if f == filter_str]
else:
flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)]
return folders
def get_node_details(self, unique_id):
"""Return full configuration dictionary for a specific node."""
try:
details = self.config.getitem(unique_id)
if not details:
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
return details
except (KeyError, TypeError):
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
def explode_unique(self, unique_id):
"""Explode a unique ID into a dictionary of its parts."""
return self.config._explode_unique(unique_id)
def generate_cache(self, nodes=None, folders=None, profiles=None):
"""Generate and update the internal nodes cache."""
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
def validate_parent_folder(self, unique_id, is_folder=False):
"""Check if parent folder exists for a given node unique ID."""
if is_folder:
uniques = self.config._explode_unique(unique_id)
if uniques and "subfolder" in uniques and "folder" in uniques:
parent_folder = f"@{uniques['folder']}"
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
else:
node_folder = unique_id.partition("@")[2]
if node_folder:
parent_folder = f"@{node_folder}"
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
def add_node(self, unique_id, data, is_folder=False):
"""Logic for adding a new node or folder to configuration."""
if not is_folder:
self._validate_node_name(unique_id)
all_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders()
if is_folder:
if unique_id in all_folders:
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
uniques = self.config._explode_unique(unique_id)
if not uniques:
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
# Check if parent folder exists when creating a subfolder
if "subfolder" in uniques:
self.validate_parent_folder(unique_id, is_folder=True)
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
# Check if parent folder exists when creating a node in a folder
self.validate_parent_folder(unique_id)
# Ensure 'id' is in data for config._connections_add
if "id" not in data:
uniques = self.config._explode_unique(unique_id)
if uniques and "id" in uniques:
data["id"] = uniques["id"]
self.config._connections_add(**data)
self.config._saveconfig(self.config.file)
def update_node(self, unique_id, data, save=True):
"""Explicitly update an existing node."""
all_nodes = self.config._getallnodes()
if unique_id not in all_nodes:
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
# Ensure 'id' is in data for config._connections_add
if "id" not in data:
uniques = self.config._explode_unique(unique_id)
if uniques:
data["id"] = uniques["id"]
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
if save:
self.config._saveconfig(self.config.file)
def delete_node(self, unique_id, is_folder=False, save=True):
"""Logic for deleting a node or folder."""
if is_folder:
uniques = self.config._explode_unique(unique_id)
if not uniques:
raise NodeNotFoundError(f"Folder '{unique_id}' not found or invalid.")
self.config._folder_del(**uniques)
else:
uniques = self.config._explode_unique(unique_id)
if not uniques:
raise NodeNotFoundError(f"Node '{unique_id}' not found or invalid.")
self.config._connections_del(**uniques)
if save:
self.config._saveconfig(self.config.file)
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
"""Interact with a node directly."""
from connpy.core import node
from .profile_service import ProfileService
node_data = self.config.getitem(unique_id, extract=False)
if not node_data:
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
# Resolve profiles
profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data)
n = node(unique_id, **resolved_data, config=self.config)
if sftp:
n.protocol = "sftp"
n.interact(debug=debug, logger=logger)
def move_node(self, src_id, dst_id, copy=False):
"""Move or copy a node."""
self._validate_node_name(dst_id)
node_data = self.config.getitem(src_id)
if not node_data:
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
if dst_id in self.config._getallnodes():
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
new_uniques = self.config._explode_unique(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
new_node_data = node_data.copy()
new_node_data.update(new_uniques)
self.config._connections_add(**new_node_data)
if not copy:
src_uniques = self.config._explode_unique(src_id)
self.config._connections_del(**src_uniques)
self.config._saveconfig(self.config.file)
def bulk_add(self, ids, hosts, common_data):
"""Add multiple nodes with shared common configuration."""
count = 0
all_nodes = self.config._getallnodes()
for i, uid in enumerate(ids):
if uid in all_nodes:
continue
try:
self._validate_node_name(uid)
except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue
host = hosts[i] if i < len(hosts) else hosts[0]
uniques = self.config._explode_unique(uid)
if not uniques:
continue
node_data = common_data.copy()
node_data.pop("ids", None)
node_data.pop("location", None)
node_data.update(uniques)
node_data["host"] = host
node_data["type"] = "connection"
self.config._connections_add(**node_data)
count += 1
if count > 0:
self.config._saveconfig(self.config.file)
return count
def full_replace(self, connections, profiles):
"""Replace all connections and profiles with new data."""
self.config.connections = connections
self.config.profiles = profiles
self.config._saveconfig(self.config.file)
def get_inventory(self):
"""Return a full snapshot of connections and profiles."""
return {
"connections": self.config.connections,
"profiles": self.config.profiles
}
-359
View File
@@ -1,359 +0,0 @@
from .base import BaseService
import yaml
import os
from .exceptions import InvalidConfigurationError, NodeNotFoundError
class PluginService(BaseService):
"""Business logic for enabling, disabling, and listing plugins."""
def _get_plugin_path(self, name, include_disabled=True):
"""Resolves the physical path of a plugin by name. Priority: user, shared/global, core."""
import os
# 1. User directory
user_dir = os.path.join(self.config.defaultdir, "plugins")
if os.path.exists(user_dir):
p_file = os.path.join(user_dir, f"{name}.py")
if os.path.exists(p_file):
return p_file, "user", True
if include_disabled:
bkp_file = os.path.join(user_dir, f"{name}.py.bkp")
if os.path.exists(bkp_file):
return bkp_file, "user", False
# 2. Shared/Global directory
if hasattr(self.config, "_shared_config") and self.config._shared_config:
shared_dir = os.path.join(self.config._shared_config.defaultdir, "plugins")
if os.path.exists(shared_dir):
p_file = os.path.join(shared_dir, f"{name}.py")
if os.path.exists(p_file):
return p_file, "shared", True
if include_disabled:
bkp_file = os.path.join(shared_dir, f"{name}.py.bkp")
if os.path.exists(bkp_file):
return bkp_file, "shared", False
# 3. Core plugins
core_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "core_plugins")
p_file = os.path.join(core_dir, f"{name}.py")
if os.path.exists(p_file):
return p_file, "core", True
return None, None, False
def list_plugins(self):
"""List all core and user-defined plugins with their status and hash."""
import os
import hashlib
all_plugin_info = {}
def get_hash(path):
try:
with open(path, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
except Exception:
return ""
# 1. Scan core plugins (lowest priority)
core_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "core_plugins")
if os.path.exists(core_dir):
for f in os.listdir(core_dir):
if f.endswith(".py"):
name = f[:-3]
path = os.path.join(core_dir, f)
all_plugin_info[name] = {"enabled": True, "hash": get_hash(path)}
# 2. Scan shared plugins (medium priority)
if hasattr(self.config, "_shared_config") and self.config._shared_config:
shared_dir = os.path.join(self.config._shared_config.defaultdir, "plugins")
if os.path.exists(shared_dir):
for f in os.listdir(shared_dir):
if f.endswith(".py"):
name = f[:-3]
path = os.path.join(shared_dir, f)
all_plugin_info[name] = {"enabled": True, "hash": get_hash(path)}
elif f.endswith(".py.bkp"):
name = f[:-7]
all_plugin_info[name] = {"enabled": False}
# 3. Scan user plugins (highest priority)
user_dir = os.path.join(self.config.defaultdir, "plugins")
if os.path.exists(user_dir):
for f in os.listdir(user_dir):
if f.endswith(".py"):
name = f[:-3]
path = os.path.join(user_dir, f)
all_plugin_info[name] = {"enabled": True, "hash": get_hash(path)}
elif f.endswith(".py.bkp"):
name = f[:-7]
all_plugin_info[name] = {"enabled": False}
return all_plugin_info
def add_plugin(self, name, source_file, update=False):
"""Add or update a plugin from a local file."""
import os
import shutil
from connpy.plugins import Plugins
if not name.isalpha() or not name.islower() or len(name) > 15:
raise InvalidConfigurationError("Plugin name should be lowercase letters up to 15 characters.")
p_manager = Plugins()
# Check for bad script
error = p_manager.verify_script(source_file)
if error:
raise InvalidConfigurationError(f"Invalid plugin script: {error}")
self._save_plugin_file(name, source_file, update, is_path=True)
def add_plugin_from_bytes(self, name, content, update=False):
"""Add or update a plugin from bytes (gRPC)."""
import tempfile
import os
if not name.isalpha() or not name.islower() or len(name) > 15:
raise InvalidConfigurationError("Plugin name should be lowercase letters up to 15 characters.")
# Write to temp file to verify script
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as tmp:
tmp.write(content)
tmp_path = tmp.name
try:
from connpy.plugins import Plugins
p_manager = Plugins()
error = p_manager.verify_script(tmp_path)
if error:
raise InvalidConfigurationError(f"Invalid plugin script: {error}")
self._save_plugin_file(name, tmp_path, update, is_path=True)
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path)
def _save_plugin_file(self, name, source, update=False, is_path=True):
import os
import shutil
plugin_dir = os.path.join(self.config.defaultdir, "plugins")
os.makedirs(plugin_dir, exist_ok=True)
target_file = os.path.join(plugin_dir, f"{name}.py")
backup_file = f"{target_file}.bkp"
if not update and (os.path.exists(target_file) or os.path.exists(backup_file)):
raise InvalidConfigurationError(f"Plugin '{name}' already exists.")
try:
if is_path:
shutil.copy2(source, target_file)
else:
with open(target_file, "wb") as f:
f.write(source)
except OSError as e:
raise InvalidConfigurationError(f"Failed to save plugin file: {e}")
def delete_plugin(self, name):
"""Remove a plugin file permanently."""
import os
plugin_file = os.path.join(self.config.defaultdir, "plugins", f"{name}.py")
disabled_file = f"{plugin_file}.bkp"
deleted = False
for f in [plugin_file, disabled_file]:
if os.path.exists(f):
try:
os.remove(f)
deleted = True
except OSError as e:
raise InvalidConfigurationError(f"Failed to delete plugin file '{f}': {e}")
if not deleted:
# If not deleted from user directory, check if it's in shared or core
path, origin, enabled = self._get_plugin_path(name, include_disabled=True)
if origin in ["shared", "core"]:
raise InvalidConfigurationError("Global and core plugins are read-only and cannot be deleted by users.")
raise InvalidConfigurationError(f"Plugin '{name}' not found.")
def enable_plugin(self, name):
"""Activate a plugin by renaming its backup file."""
import os
plugin_file = os.path.join(self.config.defaultdir, "plugins", f"{name}.py")
disabled_file = f"{plugin_file}.bkp"
if os.path.exists(disabled_file):
# Check if it is a shadow bkp file (0 bytes shadowing shared/core)
is_shadow = False
if os.path.getsize(disabled_file) == 0:
# Resolve without the local bkp file to verify if shared/core has it
path, origin, enabled = self._get_plugin_path(name, include_disabled=False)
if origin in ["shared", "core"]:
is_shadow = True
if is_shadow:
# Remove shadow file to restore inheritance
try:
os.remove(disabled_file)
return True
except OSError as e:
raise InvalidConfigurationError(f"Failed to remove shadow file '{disabled_file}': {e}")
else:
try:
os.rename(disabled_file, plugin_file)
return True
except OSError as e:
raise InvalidConfigurationError(f"Failed to enable plugin '{name}': {e}")
if os.path.exists(plugin_file):
return False # Already enabled
# If it doesn't exist locally, check if it's already an active shared/core plugin
path, origin, enabled = self._get_plugin_path(name, include_disabled=False)
if origin in ["shared", "core"]:
return False # Already active/enabled through inheritance
raise InvalidConfigurationError(f"Plugin '{name}' not found.")
def disable_plugin(self, name):
"""Deactivate a plugin by renaming it to a backup file."""
import os
plugin_file = os.path.join(self.config.defaultdir, "plugins", f"{name}.py")
disabled_file = f"{plugin_file}.bkp"
if os.path.exists(plugin_file):
# Regular user-level plugin exists. Rename to bkp
try:
os.rename(plugin_file, disabled_file)
return True
except OSError as e:
raise InvalidConfigurationError(f"Failed to disable plugin '{name}': {e}")
if os.path.exists(disabled_file):
return False # Already disabled
# Check if it exists in shared or core
path, origin, enabled = self._get_plugin_path(name, include_disabled=False)
if origin in ["shared", "core"]:
# Shadow disable it by creating an empty .py.bkp in user plugins dir
plugin_dir = os.path.dirname(plugin_file)
os.makedirs(plugin_dir, exist_ok=True)
try:
with open(disabled_file, "w") as f:
f.write("")
return True
except OSError as e:
raise InvalidConfigurationError(f"Failed to create shadow disable file: {e}")
raise InvalidConfigurationError(f"Plugin '{name}' not found or is already disabled.")
def get_plugin_source(self, name):
import os
from ..services.exceptions import InvalidConfigurationError
path, origin, enabled = self._get_plugin_path(name, include_disabled=False)
if not path:
raise InvalidConfigurationError(f"Plugin '{name}' not found")
with open(path, "r") as f:
return f.read()
def invoke_plugin(self, name, args_dict):
import sys, io
from argparse import Namespace
from ..services.exceptions import InvalidConfigurationError
from connpy.plugins import Plugins
class MockApp:
is_mock = True
def __init__(self, config):
from ..core import node, nodes
from ..ai import ai
from ..services.provider import ServiceProvider
self.config = config
self.node = node
self.nodes = nodes
self.ai = ai
self.services = ServiceProvider(config, mode="local")
# Get settings for CLI behavior
settings = self.services.config_svc.get_settings()
self.case = settings.get("case", False)
self.fzf = settings.get("fzf", False)
try:
self.nodes_list = self.services.nodes.list_nodes()
self.folders = self.services.nodes.list_folders()
self.profiles = self.services.profiles.list_profiles()
except Exception:
self.nodes_list = []
self.folders = []
self.profiles = []
args = Namespace(**args_dict)
p_manager = Plugins()
import os
path, origin, enabled = self._get_plugin_path(name, include_disabled=False)
if not path:
raise InvalidConfigurationError(f"Plugin '{name}' not found")
module = p_manager._import_from_path(path)
parser = module.Parser().parser if hasattr(module, "Parser") else None
if "__func_name__" in args_dict and hasattr(module, args_dict["__func_name__"]):
args.func = getattr(module, args_dict["__func_name__"])
app = MockApp(self.config)
from .. import printer
from rich.console import Console
from rich.console import Console
import queue
import threading
q = queue.Queue()
class QueueIO(io.StringIO):
def write(self, s):
q.put(s)
return len(s)
def flush(self):
pass
buf = QueueIO()
old_console = printer._get_console()
old_err_console = printer._get_err_console()
def run_plugin():
printer.set_thread_console(Console(file=buf, theme=printer.connpy_theme, force_terminal=True))
printer.set_thread_err_console(Console(file=buf, theme=printer.connpy_theme, force_terminal=True))
printer.set_thread_stream(buf)
try:
if hasattr(module, "Entrypoint"):
module.Entrypoint(args, parser, app)
except BaseException as e:
if not isinstance(e, SystemExit):
import traceback
printer.err_console.print(traceback.format_exc())
finally:
printer.set_thread_console(old_console)
printer.set_thread_err_console(old_err_console)
printer.set_thread_stream(None)
q.put(None)
t = threading.Thread(target=run_plugin, daemon=True)
t.start()
while True:
item = q.get()
if item is None:
break
yield item
-134
View File
@@ -1,134 +0,0 @@
from .base import BaseService
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError
class ProfileService(BaseService):
"""Business logic for node profiles management."""
def list_profiles(self, filter_str=None):
"""List all profile names, optionally filtered."""
profiles = list(self.config.profiles.keys())
case_sensitive = self.config.config.get("case", False)
if filter_str:
if not case_sensitive:
f_str = filter_str.lower()
return [p for p in profiles if f_str in p.lower()]
else:
return [p for p in profiles if filter_str in p]
return profiles
def get_profile(self, name, resolve=True):
"""Get the profile dictionary, optionally resolved."""
profile = self.config.profiles.get(name)
if not profile:
raise ProfileNotFoundError(f"Profile '{name}' not found.")
if resolve:
return self.resolve_node_data(profile)
return profile
def add_profile(self, name, data):
"""Add a new profile."""
if name in self.config.profiles:
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
# Filter data to match _profiles_add signature and ensure id is passed
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)
def resolve_node_data(self, node_data):
"""Resolve profile references (@profile) in node data and handle inheritance."""
resolved = node_data.copy()
# 1. Identify all referenced profiles to support inheritance
referenced_profiles = []
for value in resolved.values():
if isinstance(value, str) and value.startswith("@"):
referenced_profiles.append(value[1:])
elif isinstance(value, list):
for item in value:
if isinstance(item, str) and item.startswith("@"):
referenced_profiles.append(item[1:])
# 2. Resolve explicit references
for key, value in resolved.items():
if isinstance(value, str) and value.startswith("@"):
profile_name = value[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
resolved[key] = profile.get(key, "")
except ProfileNotFoundError:
resolved[key] = ""
elif isinstance(value, list):
resolved_list = []
for item in value:
if isinstance(item, str) and item.startswith("@"):
profile_name = item[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
if "password" in profile:
resolved_list.append(profile["password"])
except ProfileNotFoundError:
pass
else:
resolved_list.append(item)
resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile
if referenced_profiles:
base_profile_name = referenced_profiles[0]
try:
base_profile = self.get_profile(base_profile_name, resolve=True)
for key, value in base_profile.items():
# Fill if key is missing or empty
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
resolved[key] = value
except ProfileNotFoundError:
pass
# 4. Handle default protocol
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
try:
default_profile = self.get_profile("default", resolve=True)
resolved["protocol"] = default_profile.get("protocol", "ssh")
except ProfileNotFoundError:
resolved["protocol"] = "ssh"
return resolved
def delete_profile(self, name):
"""Delete an existing profile, with safety checks."""
if name not in self.config.profiles:
raise ProfileNotFoundError(f"Profile '{name}' not found.")
if name == "default":
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
used_by = self.config._profileused(name)
if used_by:
# We return the list of nodes using it so the UI can inform the user
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
self.config._profiles_del(id=name)
self.config._saveconfig(self.config.file)
def update_profile(self, name, data):
"""Update an existing profile."""
if name not in self.config.profiles:
raise ProfileNotFoundError(f"Profile '{name}' not found.")
# Merge with existing data
existing = self.get_profile(name, resolve=False)
updated_data = existing.copy()
updated_data.update(data)
# Filter data to match _profiles_add signature
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)
-97
View File
@@ -1,97 +0,0 @@
from .exceptions import InvalidConfigurationError
class RemoteStub:
def __getattr__(self, name):
raise NotImplementedError(
"Remote mode (gRPC) is not yet available. "
"Use local mode or wait for the gRPC implementation."
)
class ServiceProvider:
"""Dynamic service backend. Transparently provides local or remote services."""
def __init__(self, config, mode="local", remote_host=None):
self.mode = mode
self.config = config
self.remote_host = remote_host
if mode == "local":
self._init_local()
elif mode == "remote":
self._init_remote()
else:
raise ValueError(f"Unknown service mode: {mode}")
def _init_local(self):
from .node_service import NodeService
from .profile_service import ProfileService
from .config_service import ConfigService
from .plugin_service import PluginService
from .ai_service import AIService
from .system_service import SystemService
from .execution_service import ExecutionService
from .import_export_service import ImportExportService
from .context_service import ContextService
from .sync_service import SyncService
from .user_service import UserService
self.nodes = NodeService(self.config)
self.profiles = ProfileService(self.config)
self.config_svc = ConfigService(self.config)
self.plugins = PluginService(self.config)
self.ai = AIService(self.config)
self.system = SystemService(self.config)
self.execution = ExecutionService(self.config)
self.import_export = ImportExportService(self.config)
self.context = ContextService(self.config)
self.sync = SyncService(self.config)
self.users = UserService(self.config.defaultdir)
def _init_remote(self):
# Allow ConfigService to work locally so the user can revert the mode
from .config_service import ConfigService
from .context_service import ContextService
from .sync_service import SyncService
self.config_svc = ConfigService(self.config)
self.context = ContextService(self.config)
self.sync = SyncService(self.config)
self.users = None
if not self.remote_host:
raise InvalidConfigurationError("Remote host must be specified in remote mode")
import grpc
import os
from ..grpc_layer.stubs import (
NodeStub, ProfileStub, PluginStub, AIStub,
ExecutionStub, ImportExportStub, SystemStub,
ConfigStub, AuthClientInterceptor, AuthStub
)
def get_token():
token_path = os.path.join(self.config.defaultdir, ".token")
if os.path.exists(token_path):
try:
with open(token_path, "r") as f:
return f.read().strip()
except Exception:
pass
return None
channel = grpc.insecure_channel(self.remote_host)
interceptor = AuthClientInterceptor(get_token)
channel = grpc.intercept_channel(channel, interceptor)
# Surgical fix: Keep ConfigService local for mode/theme management,
# but delegate encryption to the server stub.
config_remote = ConfigStub(channel, remote_host=self.remote_host)
self.config_svc.encrypt_password = config_remote.encrypt_password
self.nodes = NodeStub(channel, remote_host=self.remote_host, config=self.config)
self.profiles = ProfileStub(channel, remote_host=self.remote_host, node_stub=self.nodes)
self.plugins = PluginStub(channel, remote_host=self.remote_host)
self.ai = AIStub(channel, remote_host=self.remote_host)
self.system = SystemStub(channel, remote_host=self.remote_host)
self.execution = ExecutionStub(channel, remote_host=self.remote_host)
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
self.auth = AuthStub(channel, remote_host=self.remote_host)
-389
View File
@@ -1,389 +0,0 @@
import os
import time
import zipfile
import tempfile
import io
import yaml
import threading
from datetime import datetime
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from google.auth.exceptions import RefreshError
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from googleapiclient.errors import HttpError
from .base import BaseService
from .. import printer
class SyncService(BaseService):
"""Business logic for Google Drive synchronization."""
def __init__(self, config):
super().__init__(config)
self.scopes = ['https://www.googleapis.com/auth/drive.appdata']
self.token_file = os.path.join(self.config.defaultdir, "gtoken.json")
# Embedded OAuth config
self.client_config = {
"installed": {
"client_id": "559598250648-cr189kfrga2il1a6d6nkaspq0a9pn5vv." + "apps.googleusercontent.com",
"project_id": "celtic-surface-420323",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "GOCSPX-" + "VVfOSrJLPU90Pl0g7aAXM9GK2xPE",
"redirect_uris": ["http://localhost"]
}
}
# Sync status from config
self.sync_enabled = self.config.config.get("sync", False)
self.sync_remote = self.config.config.get("sync_remote", False)
def login(self):
"""Authenticate with Google Drive."""
creds = None
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_file(self.token_file, self.scopes)
try:
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_config(self.client_config, self.scopes)
creds = flow.run_local_server(port=0, access_type='offline')
with open(self.token_file, 'w') as token:
token.write(creds.to_json())
printer.success("Logged in successfully.")
return True
except RefreshError:
if os.path.exists(self.token_file):
os.remove(self.token_file)
printer.warning("Existing token was invalid and has been removed. Please log in again.")
return False
except Exception as e:
printer.error(f"Login failed: {e}")
return False
def logout(self):
"""Remove Google Drive credentials."""
if os.path.exists(self.token_file):
os.remove(self.token_file)
printer.success("Logged out successfully.")
else:
printer.info("No credentials file found. Already logged out.")
def get_credentials(self):
"""Get valid credentials, refreshing if necessary."""
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_file(self.token_file, self.scopes)
else:
return None
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except RefreshError:
return None
else:
return None
return creds
def check_login_status(self):
"""Check if logged in to Google Drive."""
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_file(self.token_file)
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except RefreshError:
pass
return True if creds.valid else "Invalid"
return False
def list_backups(self):
"""List files in Google Drive appDataFolder."""
creds = self.get_credentials()
if not creds:
printer.error("Not logged in to Google Drive.")
return []
try:
service = build("drive", "v3", credentials=creds)
response = service.files().list(
spaces="appDataFolder",
fields="files(id, name, appProperties)",
pageSize=10,
).execute()
files_info = []
for file in response.get("files", []):
files_info.append({
"name": file.get("name"),
"id": file.get("id"),
"date": file.get("appProperties", {}).get("date"),
"timestamp": file.get("appProperties", {}).get("timestamp")
})
return files_info
except HttpError as error:
printer.error(f"Google Drive API error: {error}")
return []
def compress_and_upload(self, remote_data=None):
"""Compress config and upload to Drive."""
timestamp = int(time.time() * 1000)
with tempfile.TemporaryDirectory() as tmp_dir:
zip_path = os.path.join(tmp_dir, f"connpy-backup-{timestamp}.zip")
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
# If we have remote data, we create a virtual config file
if remote_data:
config_tmp = os.path.join(tmp_dir, "config.yaml")
with open(config_tmp, 'w') as f:
yaml.dump(remote_data, f, default_flow_style=False)
zipf.write(config_tmp, "config.yaml")
else:
# Legacy behavior: use local file
zipf.write(self.config.file, os.path.basename(self.config.file))
# Always include the key if it exists
if os.path.exists(self.config.key):
zipf.write(self.config.key, ".osk")
# Manage retention (max 100 backups)
backups = self.list_backups()
if len(backups) >= 100:
oldest = min(backups, key=lambda x: x['timestamp'] or '0')
self.delete_backup(oldest['id'])
# Upload
return self.upload_file(zip_path, timestamp)
def upload_file(self, file_path, timestamp):
"""Internal method to upload to Drive."""
creds = self.get_credentials()
if not creds: return False
service = build('drive', 'v3', credentials=creds)
date_str = datetime.fromtimestamp(timestamp/1000).strftime('%Y-%m-%d %H:%M:%S')
file_metadata = {
'name': os.path.basename(file_path),
'parents': ["appDataFolder"],
'appProperties': {
'timestamp': str(timestamp),
'date': date_str
}
}
media = MediaFileUpload(file_path)
try:
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
printer.success("Backup uploaded to Google Drive.")
return True
except Exception as e:
printer.error(f"Upload failed: {e}")
return False
def delete_backup(self, file_id):
"""Delete a backup from Drive."""
creds = self.get_credentials()
if not creds: return False
try:
service = build("drive", "v3", credentials=creds)
service.files().delete(fileId=file_id).execute()
return True
except Exception as e:
printer.error(f"Delete failed: {e}")
return False
def restore_backup(self, file_id=None, restore_config=True, restore_nodes=True, app_instance=None):
"""Download and analyze a backup for restoration."""
backups = self.list_backups()
if not backups:
printer.error("No backups found.")
return None
if file_id:
selected = next((f for f in backups if f['id'] == file_id), None)
if not selected:
printer.error(f"Backup {file_id} not found.")
return None
else:
selected = max(backups, key=lambda x: x['timestamp'] or '0')
with tempfile.TemporaryDirectory() as tmp_dir:
zip_path = os.path.join(tmp_dir, 'restore.zip')
if self.download_file(selected['id'], zip_path):
return self.perform_restore(zip_path, restore_config, restore_nodes, app_instance)
return False
def download_file(self, file_id, dest):
"""Internal method to download from Drive."""
creds = self.get_credentials()
if not creds: return False
try:
service = build('drive', 'v3', credentials=creds)
request = service.files().get_media(fileId=file_id)
with io.FileIO(dest, mode='wb') as fh:
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
_, done = downloader.next_chunk()
return True
except Exception as e:
printer.error(f"Download failed: {e}")
return False
def perform_restore(self, zip_path, restore_config=True, restore_nodes=True, app_instance=None):
"""Execute the actual restoration of files or remote nodes."""
try:
with zipfile.ZipFile(zip_path, 'r') as zipf:
names = zipf.namelist()
dest_dir = os.path.dirname(self.config.file)
# We need to read the config content from zip to decide what to do
backup_data = {}
config_filename = "config.yaml" if "config.yaml" in names else ("config.json" if "config.json" in names else None)
if config_filename:
with zipf.open(config_filename) as f:
backup_data = yaml.safe_load(f)
# 1. Restore Key (.osk) - Part of config identity
if restore_config and ".osk" in names:
zipf.extract(".osk", os.path.dirname(self.config.key))
# 2. Restore Config (Local Settings)
if restore_config and backup_data:
local_config = self.config.config.copy()
# Capture current connectivity settings to preserve them
current_mode = local_config.get("service_mode", "local")
current_remote = local_config.get("remote_host")
if "config" in backup_data:
local_config.update(backup_data["config"])
# Restore connectivity settings - we don't want a restore to
# accidentally switch us between local and remote and break connectivity
local_config["service_mode"] = current_mode
if current_remote:
local_config["remote_host"] = current_remote
self.config.config = local_config
self.config._saveconfig(self.config.file)
# 3. Restore Nodes and Profiles
if restore_nodes and backup_data:
connections = backup_data.get("connections", {})
profiles = backup_data.get("profiles", {})
if app_instance and app_instance.services.mode == "remote":
# Push to Remote via gRPC
app_instance.services.nodes.full_replace(connections, profiles)
else:
# Restore to Local config file
self.config.connections = connections
self.config.profiles = profiles
self.config._saveconfig(self.config.file)
# Clear caches
for f in [self.config.cachefile, self.config.fzf_cachefile]:
if os.path.exists(f): os.remove(f)
return True
except Exception as e:
printer.error(f"Restoration failed: {e}")
return False
def analyze_backup_content(self, file_id=None):
"""Analyze a backup without restoring to provide info for confirmation."""
backups = self.list_backups()
if not backups: return None
selected = next((f for f in backups if f['id'] == file_id), None) if file_id else max(backups, key=lambda x: x['timestamp'] or '0')
with tempfile.TemporaryDirectory() as tmp_dir:
zip_path = os.path.join(tmp_dir, 'analyze.zip')
if self.download_file(selected['id'], zip_path):
with zipfile.ZipFile(zip_path, 'r') as zipf:
names = zipf.namelist()
config_filename = "config.yaml" if "config.yaml" in names else ("config.json" if "config.json" in names else None)
if config_filename:
with zipf.open(config_filename) as f:
data = yaml.safe_load(f)
connections = data.get("connections", {})
# Accurate recursive count
nodes_count = 0
folders_count = 0
# Layer 1
for k, v in connections.items():
if isinstance(v, dict):
if v.get("type") == "connection":
nodes_count += 1
elif v.get("type") == "folder":
folders_count += 1
# Layer 2
for k2, v2 in v.items():
if isinstance(v2, dict):
if v2.get("type") == "connection":
nodes_count += 1
elif v2.get("type") == "subfolder":
folders_count += 1
# Layer 3
for k3, v3 in v2.items():
if isinstance(v3, dict) and v3.get("type") == "connection":
nodes_count += 1
return {
"nodes": nodes_count,
"folders": folders_count,
"profiles": len(data.get("profiles", {})),
"has_config": "config" in data,
"has_key": ".osk" in names
}
return None
def perform_sync(self, app_instance):
"""Background sync logic."""
# Always check current config state
sync_enabled = self.config.config.get("sync", False)
sync_remote = self.config.config.get("sync_remote", False)
if not sync_enabled: return
if self.check_login_status() != True:
printer.warning("Auto-sync: Not logged in to Google Drive.")
return
remote_data = None
if sync_remote and app_instance.services.mode == "remote":
try:
inventory = app_instance.services.nodes.get_inventory()
# Merge with local settings
local_settings = app_instance.services.config_svc.get_settings()
local_settings.pop("configfolder", None)
# Maintain proper config structure: {config: {}, connections: {}, profiles: {}}
remote_data = {
"config": local_settings,
"connections": inventory.get("connections", {}),
"profiles": inventory.get("profiles", {})
}
except Exception as e:
printer.warning(f"Could not fetch remote inventory for sync: {e}")
# Run in thread to not block CLI
threading.Thread(
target=self.compress_and_upload,
args=(remote_data,)
).start()
-87
View File
@@ -1,87 +0,0 @@
from .base import BaseService
from .exceptions import ConnpyError
class SystemService(BaseService):
"""Business logic for application lifecycle (API, processes)."""
def start_api(self, port=None):
"""Start the Connpy REST API."""
from connpy.api import start_api
try:
start_api(port, config=self.config)
except Exception as e:
raise ConnpyError(f"Failed to start API: {e}")
def debug_api(self, port=None):
"""Start the Connpy REST API in debug mode."""
from connpy.api import debug_api
try:
debug_api(port, config=self.config)
except Exception as e:
raise ConnpyError(f"Failed to start API in debug mode: {e}")
def stop_api(self):
"""Stop the Connpy REST API."""
try:
import os
import signal
pids = ["/run/connpy.pid", "/tmp/connpy.pid"]
stopped = False
for pid_file in pids:
if os.path.exists(pid_file):
try:
with open(pid_file, "r") as f:
# Read only the first line (PID)
line = f.readline().strip()
if not line:
continue
pid = int(line)
os.kill(pid, signal.SIGTERM)
# Remove the PID file after successful kill
os.remove(pid_file)
stopped = True
except (ValueError, OSError, ProcessLookupError):
# If process is already dead, just remove the stale PID file
try:
os.remove(pid_file)
except OSError:
pass
continue
return stopped
except Exception as e:
raise ConnpyError(f"Failed to stop API: {e}")
def restart_api(self, port=None):
"""Restart the Connpy REST API, maintaining the current port if none provided."""
if port is None:
status = self.get_api_status()
if status["running"] and status.get("port"):
port = status["port"]
self.stop_api()
import time
time.sleep(1)
self.start_api(port)
def get_api_status(self):
"""Check if the API is currently running."""
import os
pids = ["/run/connpy.pid", "/tmp/connpy.pid"]
for pid_file in pids:
if os.path.exists(pid_file):
try:
with open(pid_file, "r") as f:
pid_line = f.readline().strip()
port_line = f.readline().strip()
if not pid_line:
continue
pid = int(pid_line)
port = int(port_line) if port_line else None
# Signal 0 checks for process existence without killing it
os.kill(pid, 0)
return {"running": True, "pid": pid, "port": port, "pid_file": pid_file}
except (ValueError, OSError, ProcessLookupError):
continue
return {"running": False}
-239
View File
@@ -1,239 +0,0 @@
import os
import re
import shutil
import secrets
import datetime
import bcrypt
import jwt
import yaml
from pathlib import Path
from connpy.configfile import configfile
class UserService:
def __init__(self, config_dir):
self.config_dir = os.path.abspath(config_dir)
self.users_dir = os.path.join(self.config_dir, "users")
self.registry_file = os.path.join(self.users_dir, "registry.yaml")
# Ensure users directory exists
os.makedirs(self.users_dir, exist_ok=True)
def _load_registry(self) -> dict:
"""Loads registry from file. If it doesn't exist, initializes it with a new JWT secret."""
if not os.path.exists(self.registry_file):
registry = {
"jwt_secret": secrets.token_hex(32),
"users": {}
}
self._save_registry(registry)
return registry
try:
with open(self.registry_file, "r") as f:
registry = yaml.safe_load(f) or {}
except Exception:
registry = {}
if not isinstance(registry, dict):
registry = {}
if "jwt_secret" not in registry:
registry["jwt_secret"] = secrets.token_hex(32)
if "users" not in registry or not isinstance(registry["users"], dict):
registry["users"] = {}
return registry
def _save_registry(self, data: dict):
"""Safely saves registry structure to registry.yaml."""
tmp_file = self.registry_file + ".tmp"
try:
with open(tmp_file, "w") as f:
yaml.dump(data, f, default_flow_style=False, sort_keys=False)
os.replace(tmp_file, self.registry_file)
os.chmod(self.registry_file, 0o600)
except Exception as e:
if os.path.exists(tmp_file):
try:
os.remove(tmp_file)
except OSError:
pass
raise e
def create_user(self, username, password, config_path=None) -> dict:
"""Creates a new user with bcrypt-hashed credentials.
Mode A: config_path=None (fresh user) -> Generates config.yaml and .osk key.
Mode B: config_path set -> Reuses existing directory after validating its structure.
"""
if not username or not isinstance(username, str):
raise ValueError("Username cannot be empty")
if not re.match(r"^[a-zA-Z0-9_-]+$", username):
raise ValueError("Username must contain only alphanumeric characters, dashes, or underscores")
if not password or not isinstance(password, str):
raise ValueError("Password cannot be empty")
registry = self._load_registry()
if username in registry["users"]:
raise ValueError(f"User '{username}' already exists")
# Resolve path and initialize configuration
if config_path is None:
user_dir = os.path.join(self.users_dir, username)
os.makedirs(user_dir, exist_ok=True)
# Create subdirs for plugins and sessions
os.makedirs(os.path.join(user_dir, "plugins"), exist_ok=True)
os.makedirs(os.path.join(user_dir, "ai_sessions"), exist_ok=True)
# Create default config.yaml & .osk key via configfile
conf_file = os.path.join(user_dir, "config.yaml")
configfile(conf=conf_file)
stored_config_path = None
else:
abs_config_path = os.path.abspath(config_path)
os.makedirs(abs_config_path, exist_ok=True)
# Create subdirs for plugins and sessions in the custom path
os.makedirs(os.path.join(abs_config_path, "plugins"), exist_ok=True)
os.makedirs(os.path.join(abs_config_path, "ai_sessions"), exist_ok=True)
# Create default config.yaml & .osk key via configfile if config.yaml is not present
conf_file = os.path.join(abs_config_path, "config.yaml")
if not os.path.exists(conf_file):
configfile(conf=conf_file)
stored_config_path = abs_config_path
# Hash password securely
password_hash = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
user_entry = {
"password_hash": password_hash,
"config_path": stored_config_path,
"created": datetime.datetime.now(datetime.timezone.utc).isoformat()
}
registry["users"][username] = user_entry
self._save_registry(registry)
return {
"username": username,
"config_path": stored_config_path,
"created": user_entry["created"]
}
def delete_user(self, username):
"""Removes user from the registry and cleans up config directory if server-managed."""
registry = self._load_registry()
if username not in registry["users"]:
raise ValueError(f"User '{username}' not found")
user_data = registry["users"][username]
config_path = user_data.get("config_path")
if config_path is None:
user_dir = os.path.join(self.users_dir, username)
if os.path.exists(user_dir):
shutil.rmtree(user_dir, ignore_errors=True)
del registry["users"][username]
self._save_registry(registry)
def list_users(self) -> list[dict]:
"""Lists all registered users with metadata."""
registry = self._load_registry()
return [
{
"username": name,
"config_path": data.get("config_path"),
"created": data.get("created")
}
for name, data in registry.get("users", {}).items()
]
def get_user(self, username) -> dict:
"""Retrieves raw metadata for a specific user."""
registry = self._load_registry()
if username not in registry["users"]:
raise ValueError(f"User '{username}' not found")
data = registry["users"][username]
return {
"username": username,
"config_path": data.get("config_path"),
"created": data.get("created"),
"password_hash": data.get("password_hash")
}
def change_password(self, username, old_password, new_password):
"""Verifies old password and updates registry with new hashed password."""
if not new_password or not isinstance(new_password, str):
raise ValueError("New password cannot be empty")
registry = self._load_registry()
if username not in registry["users"]:
raise ValueError(f"User '{username}' not found")
user_data = registry["users"][username]
if not bcrypt.checkpw(old_password.encode("utf-8"), user_data["password_hash"].encode("utf-8")):
raise ValueError("Invalid credentials")
# Update hash
user_data["password_hash"] = bcrypt.hashpw(new_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
self._save_registry(registry)
def admin_change_password(self, username, new_password):
"""Administrative password override (does not require old password)."""
if not new_password or not isinstance(new_password, str):
raise ValueError("New password cannot be empty")
registry = self._load_registry()
if username not in registry["users"]:
raise ValueError(f"User '{username}' not found")
user_data = registry["users"][username]
user_data["password_hash"] = bcrypt.hashpw(new_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
self._save_registry(registry)
def authenticate(self, username, password) -> bool:
"""Verifies if the credentials are valid using bcrypt."""
registry = self._load_registry()
if username not in registry["users"]:
return False
user_data = registry["users"][username]
return bcrypt.checkpw(password.encode("utf-8"), user_data["password_hash"].encode("utf-8"))
def generate_jwt(self, username) -> str:
"""Generates a secure JSON Web Token for the user expiring in 12 hours."""
registry = self._load_registry()
if username not in registry["users"]:
raise ValueError(f"User '{username}' not found")
expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=12)
payload = {
"sub": username,
"exp": expiration
}
secret = os.environ.get("CONNPY_JWT_SECRET") or registry["jwt_secret"]
token = jwt.encode(payload, secret, algorithm="HS256")
if isinstance(token, bytes):
token = token.decode("utf-8")
return token
def verify_jwt(self, token) -> str | None:
"""Decodes JWT and returns username if token is valid and unexpired."""
registry = self._load_registry()
try:
secret = os.environ.get("CONNPY_JWT_SECRET") or registry["jwt_secret"]
payload = jwt.decode(token, secret, algorithms=["HS256"])
return payload.get("sub")
except (jwt.ExpiredSignatureError, jwt.InvalidTokenError, KeyError):
return None
-1
View File
@@ -1 +0,0 @@
# Tests package
-193
View File
@@ -1,193 +0,0 @@
"""Shared fixtures for connpy tests.
All tests use tmp_path to create isolated config/keys.
No test touches ~/.config/conn/
"""
import pytest
import json
import yaml
import os
from unittest.mock import patch, MagicMock
from Crypto.PublicKey import RSA
# ---------------------------------------------------------------------------
# Minimal config data
# ---------------------------------------------------------------------------
DEFAULT_CONFIG = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {},
"profiles": {
"default": {
"host": "", "protocol": "ssh", "port": "", "user": "",
"password": "", "options": "", "logs": "", "tags": "", "jumphost": ""
}
}
}
SAMPLE_CONNECTIONS = {
"router1": {
"host": "10.0.0.1", "protocol": "ssh", "port": "22",
"user": "admin", "password": "pass1", "options": "",
"logs": "", "tags": "", "jumphost": "", "type": "connection"
},
"office": {
"type": "folder",
"server1": {
"host": "10.0.1.1", "protocol": "ssh", "port": "",
"user": "root", "password": "pass2", "options": "",
"logs": "", "tags": "", "jumphost": "", "type": "connection"
},
"datacenter": {
"type": "subfolder",
"db1": {
"host": "10.0.2.1", "protocol": "ssh", "port": "",
"user": "dbadmin", "password": "pass3", "options": "",
"logs": "", "tags": "", "jumphost": "", "type": "connection"
}
}
}
}
SAMPLE_PROFILES = {
"default": {
"host": "", "protocol": "ssh", "port": "", "user": "",
"password": "", "options": "", "logs": "", "tags": "", "jumphost": ""
},
"office-user": {
"host": "", "protocol": "ssh", "port": "", "user": "officeadmin",
"password": "officepass", "options": "", "logs": "", "tags": "", "jumphost": ""
}
}
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def tmp_config_dir(tmp_path):
"""Create an isolated config directory with config.json and RSA key."""
config_dir = tmp_path / ".config" / "conn"
config_dir.mkdir(parents=True)
plugins_dir = config_dir / "plugins"
plugins_dir.mkdir()
# Write config.yaml
config_file = config_dir / "config.yaml"
config_file.write_text(yaml.dump(DEFAULT_CONFIG, default_flow_style=False, sort_keys=False))
os.chmod(str(config_file), 0o600)
# Write .folder (points to itself)
folder_file = config_dir / ".folder"
folder_file.write_text(str(config_dir))
# Generate RSA key
key = RSA.generate(2048)
key_file = config_dir / ".osk"
key_file.write_bytes(key.export_key("PEM"))
os.chmod(str(key_file), 0o600)
return config_dir
@pytest.fixture
def config(tmp_config_dir):
"""Create a configfile instance pointing to tmp directory."""
from connpy.configfile import configfile
conf_path = str(tmp_config_dir / "config.yaml")
key_path = str(tmp_config_dir / ".osk")
return configfile(conf=conf_path, key=key_path)
@pytest.fixture
def populated_config(tmp_config_dir):
"""Create a configfile with sample nodes/profiles pre-loaded."""
config_file = tmp_config_dir / "config.yaml"
data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": SAMPLE_CONNECTIONS,
"profiles": SAMPLE_PROFILES
}
config_file.write_text(yaml.dump(data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
return configfile(conf=str(config_file), key=str(tmp_config_dir / ".osk"))
@pytest.fixture
def mock_pexpect():
"""Mock pexpect.spawn for connection tests."""
with patch("connpy.core.pexpect") as mock_pexp:
child = MagicMock()
child.before = b""
child.after = b"router#"
child.readline.return_value = b""
child.child_fd = 3
mock_pexp.spawn.return_value = child
mock_pexp.EOF = object()
mock_pexp.TIMEOUT = object()
# Also mock fdpexpect
with patch("connpy.core.fdpexpect", create=True) as mock_fd:
mock_fd.fdspawn.return_value = MagicMock()
yield {
"pexpect": mock_pexp,
"child": child,
"fdpexpect": mock_fd
}
@pytest.fixture
def mock_litellm():
"""Mock litellm.completion for AI tests."""
with patch("connpy.ai.completion") as mock_comp:
# Create a default response
msg = MagicMock()
msg.content = "Test response from AI"
msg.tool_calls = None
msg.role = "assistant"
msg.model_dump.return_value = {
"role": "assistant",
"content": "Test response from AI"
}
choice = MagicMock()
choice.message = msg
response = MagicMock()
response.choices = [choice]
response.usage = MagicMock()
response.usage.prompt_tokens = 100
response.usage.completion_tokens = 50
response.usage.total_tokens = 150
mock_comp.return_value = response
yield {
"completion": mock_comp,
"response": response,
"message": msg,
"choice": choice
}
@pytest.fixture
def ai_config(tmp_config_dir):
"""Create a configfile with AI keys configured for AI tests."""
config_file = tmp_config_dir / "config.yaml"
data = {
"config": {
"case": False, "idletime": 30, "fzf": False,
"ai": {
"engineer_model": "test/test-model",
"engineer_api_key": "test-engineer-key",
"architect_model": "test/test-architect",
"architect_api_key": "test-architect-key"
}
},
"connections": SAMPLE_CONNECTIONS,
"profiles": SAMPLE_PROFILES
}
config_file.write_text(yaml.dump(data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
return configfile(conf=str(config_file), key=str(tmp_config_dir / ".osk"))
-565
View File
@@ -1,565 +0,0 @@
"""Tests for connpy.ai module."""
import json
import os
import pytest
from unittest.mock import patch, MagicMock
# =========================================================================
# AI Init tests
# =========================================================================
class TestAIInit:
def test_init_with_keys(self, ai_config, mock_litellm):
"""Initializes correctly when keys are configured."""
from connpy.ai import ai
myai = ai(ai_config)
assert myai.engineer_model == "test/test-model"
assert myai.architect_model == "test/test-architect"
def test_ask_missing_engineer_key(self, config):
"""Raises ValueError if engineer key is missing when asking."""
from connpy.ai import ai
myai = ai(config)
with pytest.raises(ValueError) as exc:
myai.ask("hello")
assert "Engineer API key or authentication not configured" in str(exc.value)
def test_init_missing_architect_key_warns(self, ai_config, capsys, mock_litellm):
"""Warns if architect key is missing but doesn't crash."""
# Remove architect key
ai_config.config["ai"]["architect_api_key"] = None
from connpy.ai import ai
# Should not raise
myai = ai(ai_config)
assert myai.architect_key is None
def test_default_models(self, config):
"""Default models are set correctly when not configured."""
config.config["ai"] = {"engineer_api_key": "test-key", "architect_api_key": "test-key"}
from connpy.ai import ai
myai = ai(config)
assert "gemini" in myai.engineer_model.lower()
assert "claude" in myai.architect_model.lower() or "anthropic" in myai.architect_model.lower()
def test_init_loads_memory(self, ai_config, tmp_path, mock_litellm):
"""Loads long-term memory from file if it exists."""
memory_path = os.path.join(ai_config.defaultdir, "ai_memory.md")
from connpy.ai import ai
with patch("os.path.exists", side_effect=lambda p: True if p == memory_path else os.path.exists(p)):
with patch("builtins.open", side_effect=lambda f, *a, **kw: (
__import__("io").StringIO("## Memory\nRouter1 is border router")
if f == memory_path else open(f, *a, **kw)
)):
try:
myai = ai(ai_config)
except Exception:
pass # May fail on other file opens, that's ok
# =========================================================================
# AI Auth Dict tests
# =========================================================================
class TestAIAuthDict:
def test_init_with_auth_dict(self, ai_config):
"""Initializes correctly when auth dicts are configured."""
from connpy.ai import ai
ai_config.config["ai"]["engineer_api_key"] = None
ai_config.config["ai"]["architect_api_key"] = None
ai_config.config["ai"]["engineer_auth"] = {"my_key": "my_val"}
ai_config.config["ai"]["architect_auth"] = {"another_key": "another_val"}
myai = ai(ai_config)
assert myai.engineer_auth == {"my_key": "my_val"}
assert myai.architect_auth == {"another_key": "another_val"}
def test_compat_key_injection(self, ai_config):
"""Injects API key into auth dict if auth is empty or doesn't have it."""
from connpy.ai import ai
ai_config.config["ai"]["engineer_api_key"] = "compat-eng-key"
ai_config.config["ai"]["architect_api_key"] = "compat-arch-key"
ai_config.config["ai"]["engineer_auth"] = {}
ai_config.config["ai"]["architect_auth"] = {}
myai = ai(ai_config)
assert myai.engineer_auth == {"api_key": "compat-eng-key"}
assert myai.architect_auth == {"api_key": "compat-arch-key"}
def test_has_architect_keyless(self, ai_config):
"""Evaluates has_architect correctly for keyless models and auth configs."""
from connpy.ai import ai
# 1. Keyless model (Vertex)
ai_config.config["ai"]["architect_api_key"] = None
ai_config.config["ai"]["architect_auth"] = {}
ai_config.config["ai"]["architect_model"] = "vertex/gemini-pro"
myai = ai(ai_config)
assert myai.has_architect is True
# 2. Architect auth dict is set
ai_config.config["ai"]["architect_model"] = "custom-model"
ai_config.config["ai"]["architect_auth"] = {"vertex_project": "proj-1"}
myai = ai(ai_config)
assert myai.has_architect is True
def test_ask_unpacks_auth_dict(self, ai_config, mock_litellm):
"""Verifies that ask unpacks engineer_auth when calling completion."""
from connpy.ai import ai
ai_config.config["ai"]["engineer_api_key"] = None
ai_config.config["ai"]["engineer_auth"] = {"vertex_project": "my-project", "vertex_location": "us-east1"}
myai = ai(ai_config)
myai.ask("test query", stream=False)
# Check mock_litellm completion call
mock_litellm["completion"].assert_called()
kwargs = mock_litellm["completion"].call_args.kwargs
assert kwargs.get("vertex_project") == "my-project"
assert kwargs.get("vertex_location") == "us-east1"
assert "api_key" not in kwargs
def test_auth_precedence_no_api_key_injection(self, ai_config):
"""Verifies that api_key is not injected into the auth dict when auth is already set (non-empty)."""
from connpy.ai import ai
ai_config.config["ai"]["engineer_api_key"] = "legacy-eng-key"
ai_config.config["ai"]["architect_api_key"] = "legacy-arch-key"
ai_config.config["ai"]["engineer_auth"] = {"vertex_project": "proj-eng"}
ai_config.config["ai"]["architect_auth"] = {"vertex_project": "proj-arch"}
myai = ai(ai_config)
assert myai.engineer_auth == {"vertex_project": "proj-eng"}
assert "api_key" not in myai.engineer_auth
assert myai.architect_auth == {"vertex_project": "proj-arch"}
assert "api_key" not in myai.architect_auth
# =========================================================================
# register_ai_tool tests
# =========================================================================
class TestRegisterAITool:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def _make_tool_def(self, name="my_tool"):
return {
"type": "function",
"function": {
"name": name,
"description": "Test tool",
"parameters": {"type": "object", "properties": {}}
}
}
def test_register_tool_engineer(self, myai):
tool_def = self._make_tool_def()
myai.register_ai_tool(tool_def, lambda self, **kw: "ok", target="engineer")
assert len(myai.external_engineer_tools) == 1
assert len(myai.external_architect_tools) == 0
def test_register_tool_architect(self, myai):
tool_def = self._make_tool_def()
myai.register_ai_tool(tool_def, lambda self, **kw: "ok", target="architect")
assert len(myai.external_architect_tools) == 1
assert len(myai.external_engineer_tools) == 0
def test_register_tool_both(self, myai):
tool_def = self._make_tool_def()
myai.register_ai_tool(tool_def, lambda self, **kw: "ok", target="both")
assert len(myai.external_engineer_tools) == 1
assert len(myai.external_architect_tools) == 1
def test_register_tool_handler(self, myai):
tool_def = self._make_tool_def("custom_tool")
handler = lambda self, **kw: "result"
myai.register_ai_tool(tool_def, handler)
assert "custom_tool" in myai.external_tool_handlers
assert myai.external_tool_handlers["custom_tool"] is handler
def test_register_tool_prompt_extension(self, myai):
tool_def = self._make_tool_def()
myai.register_ai_tool(
tool_def, lambda self, **kw: "ok",
engineer_prompt="- Custom capability",
architect_prompt=" * Custom tool"
)
assert any("Custom capability" in ext for ext in myai.engineer_prompt_extensions)
assert any("Custom tool" in ext for ext in myai.architect_prompt_extensions)
def test_register_tool_status_formatter(self, myai):
tool_def = self._make_tool_def("status_tool")
formatter = lambda args: f"[STATUS] {args}"
myai.register_ai_tool(tool_def, lambda self, **kw: "ok", status_formatter=formatter)
assert "status_tool" in myai.tool_status_formatters
# =========================================================================
# Dynamic prompts tests
# =========================================================================
class TestDynamicPrompts:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_engineer_prompt_without_extensions(self, myai):
prompt = myai.engineer_system_prompt
assert "Plugin Capabilities" not in prompt
assert "TECHNICAL EXECUTION ENGINE" in prompt
def test_engineer_prompt_with_extensions(self, myai):
myai.engineer_prompt_extensions.append("- AWS Cloud Auditing")
prompt = myai.engineer_system_prompt
assert "Plugin Capabilities" in prompt
assert "AWS Cloud Auditing" in prompt
def test_architect_prompt_without_extensions(self, myai):
prompt = myai.architect_system_prompt
assert "Plugin Capabilities" not in prompt
assert "STRATEGIC REASONING ENGINE" in prompt
def test_architect_prompt_with_extensions(self, myai):
myai.architect_prompt_extensions.append(" * Custom tool available")
prompt = myai.architect_system_prompt
assert "Plugin Capabilities" in prompt
assert "Custom tool available" in prompt
# =========================================================================
# _sanitize_messages tests
# =========================================================================
class TestSanitizeMessages:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_sanitize_empty(self, myai):
assert myai._sanitize_messages([]) == []
def test_sanitize_normal_messages(self, myai):
messages = [
{"role": "system", "content": "You are helpful"},
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there"}
]
result = myai._sanitize_messages(messages)
assert len(result) == 3
def test_sanitize_removes_orphan_tool_calls(self, myai):
"""Tool calls at the end without responses are removed."""
messages = [
{"role": "user", "content": "do something"},
{"role": "assistant", "content": None, "tool_calls": [
{"id": "tc1", "function": {"name": "list_nodes", "arguments": "{}"}}
]}
# No tool response follows!
]
result = myai._sanitize_messages(messages)
assert len(result) == 1 # Only user message
assert result[0]["role"] == "user"
def test_sanitize_removes_orphan_tool_responses(self, myai):
"""Tool responses without preceding tool_calls are removed."""
messages = [
{"role": "user", "content": "hello"},
{"role": "tool", "tool_call_id": "tc1", "name": "list_nodes", "content": "[]"}
]
result = myai._sanitize_messages(messages)
assert len(result) == 1
assert result[0]["role"] == "user"
def test_sanitize_preserves_valid_tool_pairs(self, myai):
"""Valid assistant+tool_calls followed by tool responses are preserved."""
messages = [
{"role": "user", "content": "list nodes"},
{"role": "assistant", "content": None, "tool_calls": [
{"id": "tc1", "function": {"name": "list_nodes", "arguments": "{}"}}
]},
{"role": "tool", "tool_call_id": "tc1", "name": "list_nodes", "content": "[\"r1\"]"},
{"role": "assistant", "content": "Found r1"}
]
result = myai._sanitize_messages(messages)
assert len(result) == 4
def test_sanitize_strips_cache_control(self, myai):
"""_sanitize_messages should convert list-based content (with cache_control) back to strings."""
messages = [
{"role": "system", "content": [{"type": "text", "text": "system prompt", "cache_control": {"type": "ephemeral"}}]},
{"role": "user", "content": "hello"}
]
result = myai._sanitize_messages(messages)
assert result[0]["role"] == "system"
assert isinstance(result[0]["content"], str)
assert result[0]["content"] == "system prompt"
# =========================================================================
# _truncate tests
# =========================================================================
class TestTruncate:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_truncate_short_text(self, myai):
text = "short text"
assert myai._truncate(text) == text
def test_truncate_long_text(self, myai):
text = "x" * 100000
result = myai._truncate(text)
assert len(result) < 100000
assert "[... OUTPUT TRUNCATED ...]" in result
def test_truncate_custom_limit(self, myai):
text = "x" * 1000
result = myai._truncate(text, limit=500)
assert len(result) < 1000
assert "[... OUTPUT TRUNCATED ...]" in result
def test_truncate_preserves_head_and_tail(self, myai):
text = "HEAD" + "x" * 100000 + "TAIL"
result = myai._truncate(text)
assert result.startswith("HEAD")
assert result.endswith("TAIL")
# =========================================================================
# Tool methods tests
# =========================================================================
class TestToolMethods:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_list_nodes_tool_found(self, myai):
result = myai.list_nodes_tool("router.*")
parsed = json.loads(result) if isinstance(result, str) else result
assert "router1" in str(parsed)
def test_list_nodes_tool_not_found(self, myai):
result = myai.list_nodes_tool("nonexistent_pattern_xyz")
assert "No nodes found" in str(result)
def test_get_node_info_masks_password(self, myai):
result = myai.get_node_info_tool("router1")
parsed = json.loads(result) if isinstance(result, str) else result
assert parsed["password"] == "***"
def test_is_safe_command_show(self, myai):
assert myai._is_safe_command("show running-config") == True
assert myai._is_safe_command("show ip int brief") == True
def test_is_safe_command_config(self, myai):
assert myai._is_safe_command("config t") == False
assert myai._is_safe_command("write memory") == False
def test_is_safe_command_ls(self, myai):
assert myai._is_safe_command("ls -la") == True
def test_is_safe_command_ping(self, myai):
assert myai._is_safe_command("ping 10.0.0.1") == True
# =========================================================================
# manage_memory_tool tests
# =========================================================================
class TestManageMemory:
@pytest.fixture
def myai(self, ai_config, mock_litellm, tmp_path):
from connpy.ai import ai
myai = ai(ai_config)
myai.memory_path = str(tmp_path / "ai_memory.md")
return myai
def test_manage_memory_append(self, myai):
result = myai.manage_memory_tool("Router1 is border router", action="append")
assert "successfully" in result.lower()
assert os.path.exists(myai.memory_path)
content = open(myai.memory_path).read()
assert "Router1 is border router" in content
def test_manage_memory_replace(self, myai):
myai.manage_memory_tool("old content", action="append")
myai.manage_memory_tool("new content only", action="replace")
content = open(myai.memory_path).read()
assert "new content only" in content
assert "old content" not in content
def test_manage_memory_empty_content(self, myai):
result = myai.manage_memory_tool("", action="append")
assert "error" in result.lower() or "Error" in result
# =========================================================================
# ask() with mock LLM tests
# =========================================================================
class TestAsk:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_ask_basic_response(self, myai, mock_litellm):
result = myai.ask("hello", stream=False)
assert "response" in result
assert "chat_history" in result
assert "usage" in result
assert result["response"] == "Test response from AI"
def test_ask_sticky_brain_engineer(self, myai, mock_litellm):
result = myai.ask("show me the routers", stream=False)
assert result["responder"] == "engineer"
def test_ask_explicit_architect(self, myai, mock_litellm):
result = myai.ask("architect: review the network design", stream=False)
assert result["responder"] == "architect"
def test_ask_returns_usage(self, myai, mock_litellm):
result = myai.ask("test", stream=False)
assert result["usage"]["total"] > 0
def test_ask_with_chat_history(self, myai, mock_litellm):
history = [
{"role": "user", "content": "previous question"},
{"role": "assistant", "content": "previous answer"}
]
result = myai.ask("follow up", chat_history=history, stream=False)
assert result["response"] is not None
# =========================================================================
# _get_engineer_tools / _get_architect_tools tests
# =========================================================================
class TestToolDefinitions:
@pytest.fixture
def myai(self, ai_config, mock_litellm):
from connpy.ai import ai
return ai(ai_config)
def test_engineer_tools_include_core(self, myai):
tools = myai._get_engineer_tools()
names = [t["function"]["name"] for t in tools]
assert "list_nodes" in names
assert "run_commands" in names
assert "get_node_info" in names
assert "consult_architect" in names
assert "escalate_to_architect" in names
def test_engineer_tools_include_external(self, myai):
myai.external_engineer_tools.append({
"type": "function",
"function": {"name": "custom_tool", "description": "test", "parameters": {}}
})
tools = myai._get_engineer_tools()
names = [t["function"]["name"] for t in tools]
assert "custom_tool" in names
def test_architect_tools_include_core(self, myai):
tools = myai._get_architect_tools()
names = [t["function"]["name"] for t in tools]
assert "delegate_to_engineer" in names
assert "return_to_engineer" in names
assert "manage_memory_tool" in names
def test_architect_tools_include_external(self, myai):
myai.external_architect_tools.append({
"type": "function",
"function": {"name": "arch_tool", "description": "test", "parameters": {}}
})
tools = myai._get_architect_tools()
names = [t["function"]["name"] for t in tools]
assert "arch_tool" in names
def test_architect_tools_one_shot(self, ai_config):
from connpy.ai import ai
one_shot_ai = ai(ai_config, one_shot=True)
tools = one_shot_ai._get_architect_tools()
names = [t["function"]["name"] for t in tools]
assert "delegate_to_engineer" not in names
assert "return_to_engineer" not in names
assert "manage_memory_tool" in names
# =========================================================================
# AI Session Management tests
# =========================================================================
class TestAISessions:
@pytest.fixture
def myai(self, ai_config, mock_litellm, tmp_path):
from connpy.ai import ai
ai_config.defaultdir = str(tmp_path)
return ai(ai_config)
def test_sessions_dir_initialization(self, myai, tmp_path):
assert os.path.exists(os.path.join(tmp_path, "ai_sessions"))
assert myai.sessions_dir == str(tmp_path / "ai_sessions")
def test_generate_session_id(self, myai):
session_id = myai._generate_session_id("Any query")
# Format: YYYYMMDD-HHMMSS-suffix
assert len(session_id) == 20
assert "-" in session_id
parts = session_id.split("-")
assert len(parts) == 3
assert len(parts[0]) == 8 # YYYYMMDD
assert len(parts[1]) == 6 # HHMMSS
assert len(parts[2]) == 4 # suffix
def test_save_and_load_session(self, myai):
history = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi"}
]
myai.save_session(history, title="Test Session")
session_id = myai.session_id
# Load it back
loaded = myai.load_session_data(session_id)
assert loaded["title"] == "Test Session"
assert loaded["history"] == history
assert loaded["model"] == myai.engineer_model
def test_list_sessions(self, myai, capsys):
history = [{"role": "user", "content": "Query 1"}]
myai.save_session(history, title="Session 1")
# Use a second instance to list
myai.list_sessions()
captured = capsys.readouterr()
assert "Session 1" in captured.out
assert "AI Persisted Sessions" in captured.out
def test_get_last_session_id(self, myai):
# Save two sessions
myai.session_id = None # Force new
myai.save_session([{"role": "user", "content": "First"}])
first_id = myai.session_id
import time
time.sleep(1.1) # Ensure different timestamp
myai.session_id = None # Force new
myai.save_session([{"role": "user", "content": "Second"}])
second_id = myai.session_id
last_id = myai.get_last_session_id()
assert last_id == second_id
assert last_id != first_id
def test_delete_session(self, myai):
myai.save_session([{"role": "user", "content": "To be deleted"}])
session_id = myai.session_id
assert os.path.exists(myai.session_path)
myai.delete_session(session_id)
assert not os.path.exists(myai.session_path)
-402
View File
@@ -1,402 +0,0 @@
import pytest
from unittest.mock import MagicMock, patch, AsyncMock
import json
import asyncio
from connpy.ai import ai
from connpy.core import node
class DummyConfig:
def __init__(self):
self.config = {"ai": {"engineer_api_key": "test_key", "engineer_model": "test_model"}}
self.defaultdir = "/tmp"
class MockAsyncIterator:
def __init__(self, items):
self.items = items
def __aiter__(self):
return self
async def __anext__(self):
if not self.items:
raise StopAsyncIteration
return self.items.pop(0)
@pytest.fixture
def mock_acompletion():
# Patch acompletion inside connpy.ai.aask_copilot
with patch('litellm.acompletion') as mock:
yield mock
def test_aask_copilot_tool_call(mock_acompletion):
agent = ai(DummyConfig())
# Setup mock response for streaming
class MockDelta:
def __init__(self, content):
self.content = content
class MockChoice:
def __init__(self, content):
self.delta = MockDelta(content)
class MockChunk:
def __init__(self, content):
self.choices = [MockChoice(content)]
# acompletion is awaited and returns an async iterator
async def mock_ac(*args, **kwargs):
return MockAsyncIterator([
MockChunk("<guide>Check the interfaces and running config.</guide>"),
MockChunk("<commands>\nshow ip int br\nshow run\n</commands>"),
MockChunk("<risk>low</risk>")
])
mock_acompletion.side_effect = mock_ac
async def run_test():
return await agent.aask_copilot("Router#", "What do I do?")
result = asyncio.run(run_test())
if result["error"]:
print(f"ERROR OCCURRED: {result['error']}")
assert result["error"] is None
assert result["guide"] == "Check the interfaces and running config."
assert result["risk_level"] == "low"
assert result["commands"] == ["show ip int br", "show run"]
def test_aask_copilot_fallback(mock_acompletion):
agent = ai(DummyConfig())
# Setup mock response for streaming
class MockDelta:
def __init__(self, content):
self.content = content
class MockChoice:
def __init__(self, content):
self.delta = MockDelta(content)
class MockChunk:
def __init__(self, content):
self.choices = [MockChoice(content)]
async def mock_ac(*args, **kwargs):
return MockAsyncIterator([
MockChunk("Here is some text response instead of tool call.")
])
mock_acompletion.side_effect = mock_ac
async def run_test():
return await agent.aask_copilot("Router#", "What do I do?")
result = asyncio.run(run_test())
if result["error"]:
print(f"ERROR OCCURRED: {result['error']}")
assert result["error"] is None
assert result["guide"] == "Here is some text response instead of tool call."
assert result["risk_level"] == "low"
def test_logclean_ansi():
c = node("test_node", "1.2.3.4")
raw = "Router#\x1b[K\x1b[m show ip"
clean = c._logclean(raw, var=True)
assert "\x1b" not in clean
def test_ingress_task_interception():
async def run_test():
c = node("test_node", "1.2.3.4")
c.mylog = MagicMock()
c.mylog.getvalue.return_value = b"Some session log"
c.unique = "test_node"
c.host = "1.2.3.4"
c.tags = {"os": "cisco_ios"}
class MockStream:
def __init__(self):
self.data = [b"a", b"b", b"\x00", b"c", b""]
async def read(self):
if self.data:
return self.data.pop(0)
return b""
def setup(self, resize_callback):
pass
stream = MockStream()
called_copilot = False
async def mock_handler(buffer, node_info, s, child_fd):
nonlocal called_copilot
called_copilot = True
assert buffer == "Some session log"
assert node_info["os"] == "cisco_ios"
c.child = MagicMock()
c.child.child_fd = 123
c.child.after = b""
c.child.buffer = b""
async def mock_ingress():
while True:
data = await stream.read()
if not data:
break
if mock_handler and b'\x00' in data:
buffer = c.mylog.getvalue().decode()
node_info = {"name": getattr(c, 'unique', 'unknown'), "host": getattr(c, 'host', 'unknown')}
if isinstance(getattr(c, 'tags', None), dict):
node_info["os"] = c.tags.get("os", "unknown")
await mock_handler(buffer, node_info, stream, c.child.child_fd)
continue
await mock_ingress()
assert called_copilot
asyncio.run(run_test())
def test_build_context_blocks_horizontal_scrolling():
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "RP/0/RP0/CPU0:xrd#"}
part1 = 'RP/0/RP0/CPU0:xrd#s show interfaces * | inc "rate|is up|escr|test1|test2|test3|test4|test5|teest8|test7|t$'
part2 = '|escr|test1|test2|test3|test4|test5|teest8|test7|te s998"show interfaces * | inc "rate|is up|escr|test1|test2|test3|test4|test5|teest8|test7|$'
# Test with \r (classic IOS)
raw_bytes = (part1 + '\r' + part2).encode()
cmd_byte_positions = [(0, None), (len(raw_bytes), None)]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
assert len(blocks) >= 1
start, end, preview = blocks[0]
assert "RP/0/RP0/CPU0:xrd# s show interfaces * | inc" in preview
def test_build_context_blocks_horizontal_scrolling_ansi():
"""Test with CSI cursor repositioning (\\x1B[1G) instead of raw \\r, as used by Cisco IOS XR."""
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "RP/0/RP0/CPU0:xrd#"}
part1 = 'RP/0/RP0/CPU0:xrd#s show interfaces * | inc "rate|is up|escr|test1|test2|test3|test4|test5|teest8|test7|t'
part2 = '$|escr|test1|test2|test3|test4|test5|teest8|test7|te s998"show interfaces * | inc "rate|is up|escr|test1|test2|test3|test4|test5|teest8|test7|$'
# Test with \x1B[1G (CSI Cursor Horizontal Absolute - IOS XR)
raw_bytes = (part1 + '\x1b[1G' + part2).encode()
cmd_byte_positions = [(0, None), (len(raw_bytes), None)]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
assert len(blocks) >= 1
start, end, preview = blocks[0]
assert "RP/0/RP0/CPU0:xrd# s show interfaces * | inc" in preview
def test_build_context_blocks_cancelled_command():
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "router#"}
# Command 1: cancelled with Ctrl+C. Command 2: executed successfully.
raw_bytes = b"router# show plat\x03\r\nrouter# show ver\r\nrouter# "
# 0: initial boundary
# 18: Ctrl+C pressed (ends Command 1, marked CANCELLED)
# 36: Enter pressed (ends Command 2)
cmd_byte_positions = [(0, None), (18, "CANCELLED"), (36, None)]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
# The cancelled command block (0 to 18) should NOT be registered as a VALID_CMD block.
# The block for "show ver" should be registered (starting at 36, ending at current_prompt_pos).
# Plus, the final block for "CURRENT CONTEXT".
valid_blocks = [b for b in blocks if "CURRENT CONTEXT" not in b[2]]
assert len(valid_blocks) == 1
assert "show ver" in valid_blocks[0][2]
assert "show plat" not in valid_blocks[0][2]
def test_copilot_range_mode_filtering():
from connpy.cli.terminal_ui import CopilotInterface
# We setup dummy raw_bytes with scrolling garbage in the middle:
# 0 to 10: "show ip" (VALID_CMD)
# 10 to 25: "some scrolling garbage we want to skip"
# 25 to 35: "show run" (VALID_CMD)
# 35 to 45: "current prompt" (final context block)
raw_bytes = b"show ip garbage_to_skip_here show run router#"
blocks = [
(0, 10, "router# show ip"),
(25, 35, "router# show run"),
(35, 45, "router#")
]
# Mock Config
class MockConfig:
def __init__(self):
self.config = {"ai": {}}
self.defaultdir = "/tmp"
interface = CopilotInterface(MockConfig())
# Ensure default is RANGE mode
interface.mode_range = 0
interface.mode_single = 1
interface.mode_lines = 2
captured_buffer = None
async def mock_ai_call(active_buffer, question, on_chunk, node_info):
nonlocal captured_buffer
captured_buffer = active_buffer
return {"guide": "Ok", "commands": [], "risk_level": "low"}
# Mock PromptSession.prompt_async to ask a question once then exit
prompt_calls = 0
async def mock_prompt_async(self, *args, **kwargs):
nonlocal prompt_calls
prompt_calls += 1
if prompt_calls == 1:
# Simulate pressing Ctrl+Up key twice to expand context range from 1 to 3 commands
kb = kwargs.get('key_bindings')
if kb:
class DummyApp:
def invalidate(self): pass
class DummyEvent:
app = DummyApp()
# Find and invoke the 'c-up' handler twice
for b in kb.bindings:
if any('up' in str(k).lower() for k in b.keys):
b.handler(DummyEvent())
b.handler(DummyEvent())
return "how are interfaces looking?"
else:
raise KeyboardInterrupt
with patch('prompt_toolkit.PromptSession.prompt_async', mock_prompt_async):
async def run():
# Run session
return await interface.run_session(
raw_bytes=raw_bytes,
node_info={"name": "test"},
on_ai_call=mock_ai_call,
blocks=blocks
)
asyncio.run(run())
# In range mode: it should have concatenated the valid blocks
# block[0] is raw_bytes[0:10] => b"show ip "
# block[1] is raw_bytes[25:35] => b" show run"
# block[2] is raw_bytes[35:45] => b" router#"
# Note: raw_bytes[10:25] (garbage) must be excluded!
assert captured_buffer is not None
assert "garbage_to_skip_here" not in captured_buffer
assert "show ip" in captured_buffer
assert "show run" in captured_buffer
def test_build_context_blocks_pager_scrolling_enter():
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "sixwind>"}
raw_bytes = (
b"sixwind> show configuration | less\r\n"
b"line 1 of output\nline 2 of output\n\r"
b"line 3 of output\nline 4 of output\n\r"
b"line 5 of output\n(END)\x1b[?1049l\x1b[?47l\r\nsixwind> \r\n"
b"sixwind> \r\n"
b"sixwind> \r\n"
b"sixwind> "
)
cmd_byte_positions = [
(0, None),
(36, None),
(70, None),
(105, None),
(153, None),
(164, None),
(175, None),
(186, None)
]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
valid_blocks = [b for b in blocks if "CURRENT CONTEXT" not in b[2]]
assert len(valid_blocks) == 1
assert "show configuration" in valid_blocks[0][2]
assert valid_blocks[0][0] == 36
assert valid_blocks[0][1] == 153
def test_build_context_blocks_pager_scrolling_space():
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "sixwind>"}
raw_bytes = (
b"sixwind> show configuration | less\r\n"
b"line 1 of output\nline 2 of output\n "
b"line 3 of output\nline 4 of output\n "
b"line 5 of output\n(END)\x1b[?1049l\x1b[?47l\r\n"
b"sixwind> \r\n"
b"sixwind> \r\n"
b"sixwind> \r\n"
b"sixwind> "
)
cmd_byte_positions = [
(0, None),
(36, None),
(144, None),
(155, None),
(166, None),
(177, None)
]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
valid_blocks = [b for b in blocks if "CURRENT CONTEXT" not in b[2]]
assert len(valid_blocks) == 1
assert "show configuration" in valid_blocks[0][2]
assert valid_blocks[0][0] == 36
assert valid_blocks[0][1] == 155
def test_build_context_blocks_pager_scrolling_6wind_escapes():
from connpy.services.ai_service import AIService
svc = AIService(None)
node_info = {"prompt": "6WIND-PE1>", "os": "6wind"}
raw_bytes = (
b"6WIND-PE1> show config running fullpath nodefault\r\n"
b"line 1\r\n"
b"line 2\r\n"
b":\x1b[K\r\x1b[K/ vrf main interface gre gre2 mtu 8400\r\n"
b":\x1b[K\x07\r\x1b[K\x1b[?1l\x1b>6WIND-PE1> \r\n"
b"6WIND-PE1> \r\n"
b"6WIND-PE1> "
)
cmd_byte_positions = [
(0, None),
(52, None),
(136, None),
(177, None),
(177, None),
(190, None),
(203, None)
]
blocks = svc.build_context_blocks(raw_bytes, cmd_byte_positions, node_info)
valid_blocks = [b for b in blocks if "CURRENT CONTEXT" not in b[2]]
assert len(valid_blocks) == 1
assert "show config running" in valid_blocks[0][2]
-56
View File
@@ -1,56 +0,0 @@
"""Tests for connpy.core_plugins.capture"""
import pytest
from unittest.mock import MagicMock, patch
from connpy.core_plugins.capture import Entrypoint
@pytest.fixture
def RemoteCapture():
return Entrypoint.get_remote_capture_class()
@pytest.fixture
def mock_connapp():
app = MagicMock()
app.services.nodes.list_nodes.return_value = ["test_node"]
app.services.nodes.get_node_details.return_value = {"host": "127.0.0.1", "protocol": "ssh"}
app.services.config_svc.get_settings().get.return_value = "/fake/ws"
mock_node = MagicMock()
mock_node.protocol = "ssh"
mock_node.unique = "test_node"
app.node.return_value = mock_node
return app
class TestRemoteCapture:
def test_init_node_not_found(self, mock_connapp, RemoteCapture):
# Attempt to capture a node not in inventory
mock_connapp.services.nodes.list_nodes.return_value = []
with pytest.raises(SystemExit) as exc:
RemoteCapture(mock_connapp, "test_node", "eth0")
assert exc.value.code == 2
def test_init_success(self, mock_connapp, RemoteCapture):
rc = RemoteCapture(mock_connapp, "test_node", "eth0")
assert rc.node_name == "test_node"
assert rc.interface == "eth0"
assert rc.wireshark_path == "/fake/ws"
def test_is_port_in_use(self, mock_connapp, RemoteCapture):
rc = RemoteCapture(mock_connapp, "test_node", "eth0")
with patch("socket.socket") as mock_socket:
mock_sock_instance = MagicMock()
mock_socket.return_value.__enter__.return_value = mock_sock_instance
mock_sock_instance.connect_ex.return_value = 0
assert rc._is_port_in_use(8080) is True
mock_sock_instance.connect_ex.return_value = 1
assert rc._is_port_in_use(8080) is False
def test_find_free_port(self, mock_connapp, RemoteCapture):
rc = RemoteCapture(mock_connapp, "test_node", "eth0")
with patch.object(RemoteCapture, "_is_port_in_use") as mock_is_in_use:
# First 2 ports in use, 3rd is free
mock_is_in_use.side_effect = [True, True, False]
port = rc._find_free_port(20000, 30000)
assert 20000 <= port <= 30000
assert mock_is_in_use.call_count == 3
-239
View File
@@ -1,239 +0,0 @@
import os
import pytest
import grpc
import argparse
from unittest.mock import MagicMock, patch
from connpy.connapp import connapp
from connpy.services.provider import ServiceProvider
from connpy.cli.user_handler import UserHandler
from connpy.cli.login_handler import LoginHandler
from connpy.grpc_layer.stubs import AuthClientInterceptor, AuthStub
@pytest.fixture
def mock_config():
config = MagicMock()
config.config = {"service_mode": "local", "remote_host": "localhost:8048"}
config.defaultdir = "/mock/default/dir"
return config
@pytest.fixture
def app_instance(mock_config):
with patch("connpy.services.provider.ServiceProvider") as mock_provider_cls:
mock_provider = MagicMock()
mock_provider.context = MagicMock()
mock_provider.nodes = MagicMock()
mock_provider.profiles = MagicMock()
mock_provider.config_svc = MagicMock()
mock_provider.plugins = MagicMock()
mock_provider.sync = MagicMock()
mock_provider.mode = "local"
mock_provider.remote_host = "localhost:8048"
mock_provider_cls.return_value = mock_provider
app = connapp(mock_config)
# Mock UserService on app services
app.services.users = MagicMock()
return app
class TestCLIMultiUserParsing:
def test_parser_contains_user_login_logout(self, app_instance):
parser, _ = app_instance.get_parser()
# Verify subcommands exist by finding the _SubParsersAction
subparsers_action = None
for action in parser._actions:
if isinstance(action, argparse._SubParsersAction):
subparsers_action = action
break
assert subparsers_action is not None
subcommands = subparsers_action.choices.keys()
assert "user" in subcommands
assert "login" in subcommands
assert "logout" in subcommands
def test_user_parser_arguments(self, app_instance):
parser, _ = app_instance.get_parser()
# Parse add user
args = parser.parse_args(["user", "--add", "newguy"])
assert args.add == ["newguy"]
assert args.func == app_instance._user.dispatch
# Parse delete user
args = parser.parse_args(["user", "--del", "oldguy"])
assert args.delete == ["oldguy"]
# Parse list users
args = parser.parse_args(["user", "--list"])
assert args.list is True
# Parse show user
args = parser.parse_args(["user", "--show", "someguy"])
assert args.show == ["someguy"]
# Parse regen-password
args = parser.parse_args(["user", "--regen-password", "someguy"])
assert args.regen_password == ["someguy"]
# Parse path
args = parser.parse_args(["user", "--add", "newguy", "--path", "/some/path"])
assert args.add == ["newguy"]
assert args.path == ["/some/path"]
def test_login_logout_parser_arguments(self, app_instance):
parser, _ = app_instance.get_parser()
args = parser.parse_args(["login", "someuser"])
assert args.username == "someuser"
assert args.status is False
assert args.func == app_instance._login.dispatch
args = parser.parse_args(["login", "--status"])
assert args.status is True
args = parser.parse_args(["login", "-s"])
assert args.status is True
args = parser.parse_args(["logout"])
assert args.func == app_instance._login.dispatch
class TestUserHandlerDispatch:
def test_user_handler_fails_in_remote_mode(self, app_instance):
app_instance.services.mode = "remote"
handler = UserHandler(app_instance)
args = MagicMock()
args.add = ["testuser"]
with pytest.raises(SystemExit) as excinfo:
handler.dispatch(args)
assert excinfo.value.code == 1
def test_user_handler_routes_add_correctly(self, app_instance):
app_instance.services.mode = "local"
handler = UserHandler(app_instance)
args = MagicMock()
args.add = ["newuser"]
args.delete = None
args.list = False
args.show = None
args.regen_password = None
with patch.object(handler, "add_user") as mock_add:
handler.dispatch(args)
assert args.action == "add"
assert args.username == "newuser"
mock_add.assert_called_once_with(args)
def test_user_handler_routes_list_correctly(self, app_instance):
app_instance.services.mode = "local"
handler = UserHandler(app_instance)
args = MagicMock()
args.add = None
args.delete = None
args.list = True
args.show = None
args.regen_password = None
with patch.object(handler, "list_users") as mock_list:
handler.dispatch(args)
assert args.action == "list"
mock_list.assert_called_once_with(args)
class TestAuthClientInterceptor:
def test_auth_client_interceptor_adds_bearer_token(self):
# Mock token provider
token_provider = MagicMock(return_value="my-super-secret-token")
interceptor = AuthClientInterceptor(token_provider)
# Mock ClientCallDetails using namedtuple
from collections import namedtuple
ClientCallDetails = namedtuple('ClientCallDetails', ['method', 'timeout', 'metadata', 'credentials', 'wait_for_ready', 'compression'])
mock_details = ClientCallDetails(
method="/connpy.NodeService/list_nodes",
timeout=10,
metadata=[],
credentials=None,
wait_for_ready=True,
compression=None
)
intercepted_details = interceptor._add_metadata(mock_details)
# Verify metadata was injected
metadata_dict = dict(intercepted_details.metadata)
assert "authorization" in metadata_dict
assert metadata_dict["authorization"] == "Bearer my-super-secret-token"
def test_auth_client_interceptor_no_token(self):
token_provider = MagicMock(return_value=None)
interceptor = AuthClientInterceptor(token_provider)
from collections import namedtuple
ClientCallDetails = namedtuple('ClientCallDetails', ['method', 'timeout', 'metadata', 'credentials', 'wait_for_ready', 'compression'])
mock_details = ClientCallDetails(
method="/connpy.NodeService/list_nodes",
timeout=10,
metadata=[],
credentials=None,
wait_for_ready=True,
compression=None
)
intercepted_details = interceptor._add_metadata(mock_details)
# Verify metadata remains empty
assert len(intercepted_details.metadata) == 0
class TestLoginHandlerStatus:
def test_status_no_token(self, app_instance):
handler = LoginHandler(app_instance)
with patch("os.path.exists", return_value=False):
with patch("connpy.printer.warning") as mock_warning:
handler.show_status()
mock_warning.assert_called_once_with("No active session found. You can log in using 'connpy login'.")
def test_status_invalid_token(self, app_instance):
handler = LoginHandler(app_instance)
with patch("os.path.exists", return_value=True):
with patch("builtins.open", mock_open(read_data="invalid-token")):
with patch("connpy.printer.error") as mock_error:
handler.show_status()
mock_error.assert_called_once_with("Invalid local session token format.")
def test_status_valid_token(self, app_instance):
handler = LoginHandler(app_instance)
# Mock token payload: {"sub": "testuser", "exp": 1780007003}
# Part 1 (header): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
# Part 2 (payload): eyJzdWIiOiJ0ZXN0dXNlciIsImV4cCI6MTc4MDAwNzAwM30
# Part 3 (sig): signature
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0dXNlciIsImV4cCI6MTc4MDAwNzAwM30.signature"
with patch("os.path.exists", return_value=True):
with patch("builtins.open", mock_open(read_data=token)):
with patch("connpy.printer.success") as mock_success:
with patch("connpy.printer.info") as mock_info:
# Patch time so exp is in the future
with patch("datetime.datetime") as mock_dt:
mock_dt.now.return_value.timestamp.return_value = 1780000000
# Mock fromtimestamp for expiration display
mock_dt.fromtimestamp.return_value.strftime.return_value = "2026-05-28 19:23:23 UTC"
handler.show_status()
mock_success.assert_called_once_with("Logged in as 'testuser'")
def mock_open(*args, **kwargs):
from unittest.mock import mock_open as unittest_mock_open
return unittest_mock_open(*args, **kwargs)
-136
View File
@@ -1,136 +0,0 @@
import pytest
from unittest.mock import patch, MagicMock, ANY
from connpy.connapp import connapp
import os
@pytest.fixture
def app(populated_config):
"""Returns an instance of connapp initialized with mock config."""
return connapp(populated_config)
def test_run_generate_ai_dispatch(app):
"""Test that connpy run --generate-ai parses and calls ai_generate."""
with patch("connpy.cli.run_handler.RunHandler.ai_generate") as mock_ai_gen:
app.start(["run", "--generate-ai", "new_playbook.yaml"])
mock_ai_gen.assert_called_once()
args = mock_ai_gen.call_args[0][0]
assert args.data == ["new_playbook.yaml"]
assert args.action == "generate_ai"
def test_run_preflight_ai_node(app):
"""Test that connpy run --preflight-ai calls predict_execution_results and exits."""
with patch("connpy.services.node_service.NodeService.list_nodes", return_value=["router1"]):
with patch("connpy.services.ai_service.AIService.predict_execution_results") as mock_predict:
with pytest.raises(SystemExit) as exc:
app.start(["run", "router1", "show version", "--preflight-ai"])
assert exc.value.code == 0
mock_predict.assert_called_once_with(["router1"], ["show version"], chunk_callback=ANY)
def test_run_analyze_node(app):
"""Test that connpy run --analyze calls analyze_execution_results after execution."""
mock_run = MagicMock(return_value={"router1": {"status": 0, "output": "success"}})
with patch("connpy.services.node_service.NodeService.list_nodes", return_value=["router1"]):
with patch("connpy.services.execution_service.ExecutionService.run_commands", mock_run):
with patch("connpy.services.ai_service.AIService.analyze_execution_results") as mock_analyze:
app.start(["run", "router1", "show version", "--analyze"])
mock_run.assert_called_once()
mock_analyze.assert_called_once_with(
{"router1": {"status": 0, "output": "success"}},
query="show version",
chunk_callback=ANY
)
def test_run_preflight_ai_playbook(app, tmp_path):
"""Test that running a playbook with --preflight-ai predicts results per task."""
playbook_path = tmp_path / "test_playbook.yaml"
playbook_content = """
tasks:
- name: test-task
action: run
nodes: "router1"
commands: ["show ip interface brief"]
output: stdout
"""
playbook_path.write_text(playbook_content)
with patch("connpy.services.node_service.NodeService.list_nodes", return_value=["router1"]):
with patch("connpy.services.ai_service.AIService.predict_execution_results") as mock_predict:
with pytest.raises(SystemExit) as exc:
app.start(["run", str(playbook_path), "--preflight-ai"])
assert exc.value.code == 0
mock_predict.assert_called_once_with(["router1"], ["show ip interface brief"], chunk_callback=ANY)
def test_run_analyze_playbook(app, tmp_path):
"""Test that running a playbook with --analyze triggers strategic analysis on all task outcomes."""
playbook_path = tmp_path / "test_playbook.yaml"
playbook_content = """
tasks:
- name: test-task
action: run
nodes: "router1"
commands: ["show ip interface brief"]
output: stdout
"""
playbook_path.write_text(playbook_content)
mock_run = MagicMock(return_value={"router1": {"status": 0, "output": "ok"}})
with patch("connpy.services.node_service.NodeService.list_nodes", return_value=["router1"]):
with patch("connpy.services.execution_service.ExecutionService.run_commands", mock_run):
with patch("connpy.services.ai_service.AIService.analyze_execution_results") as mock_analyze:
app.start(["run", str(playbook_path), "--analyze"])
mock_run.assert_called_once()
mock_analyze.assert_called_once_with(
{"router1": {"status": 0, "output": "ok"}},
query=f"Playbook: {str(playbook_path)}",
chunk_callback=ANY
)
def test_ai_generate_wizard_save(app, tmp_path):
"""Test that ai_generate wizard runs interactive chat loop, asks for validation and saves YAML."""
dest_yaml = tmp_path / "playbook.yaml"
mock_chat = MagicMock(return_value={
"response": "Here is your playbook.",
"chat_history": [],
"playbook_yaml": "tasks:\n - name: mytask"
})
app.services.ai.build_playbook_chat = mock_chat
# Mock rich.prompt.Prompt.ask to simulate User inputting prompt and then 'y' to save
with patch("rich.prompt.Prompt.ask", side_effect=["create a basic task", "y"]):
app.start(["run", "--generate-ai", str(dest_yaml)])
mock_chat.assert_called_once_with("create a basic task", chat_history=[], chunk_callback=ANY)
assert os.path.exists(dest_yaml)
with open(dest_yaml) as f:
content = f.read()
assert "tasks:" in content
def test_ai_generate_wizard_run(app, tmp_path):
"""Test that ai_generate wizard runs, saves the playbook and executes it when choosing 'run'."""
dest_yaml = tmp_path / "playbook_run.yaml"
mock_chat = MagicMock(return_value={
"response": "Here is your playbook.",
"chat_history": [],
"playbook_yaml": "tasks:\n - name: mytask\n action: run\n nodes: '*'\n commands: ['show version']\n output: stdout"
})
app.services.ai.build_playbook_chat = mock_chat
with patch("rich.prompt.Prompt.ask", side_effect=["create task", "run"]):
with patch("connpy.cli.run_handler.RunHandler.yaml_run") as mock_yaml_run:
app.start(["run", "--generate-ai", str(dest_yaml)])
mock_chat.assert_called_once_with("create task", chat_history=[], chunk_callback=ANY)
assert os.path.exists(dest_yaml)
with open(dest_yaml) as f:
content = f.read()
assert "tasks:" in content
mock_yaml_run.assert_called_once()
args = mock_yaml_run.call_args[0][0]
assert args.data == [str(dest_yaml)]
-67
View File
@@ -1,67 +0,0 @@
import pytest
from unittest.mock import MagicMock, patch
from connpy.cli.sso_handler import SSOHandler
def test_sso_handler_add_provider_with_allowed_domains():
# 1. Setup mock app structure
app_mock = MagicMock()
app_mock.services.mode = "local"
app_mock.config.config = {"sso": {"providers": {}}}
handler = SSOHandler(app_mock)
# Mock inquirer prompts
mock_answers = {
"jwks_url": "https://accounts.google.com/.well-known/jwks.json",
"secret": "my-secret-key",
"username_claim": "email",
"algorithms": "RS256, HS256",
"allowed_domains": "yyy.com, company.org"
}
args_mock = MagicMock()
args_mock.provider = "google"
with patch("inquirer.prompt", return_value=mock_answers):
handler.add_provider(args_mock)
# Verify update_setting was called with the correct data structure
app_mock.services.config_svc.update_setting.assert_called_once()
saved_key, saved_sso_config = app_mock.services.config_svc.update_setting.call_args[0]
assert saved_key == "sso"
assert "providers" in saved_sso_config
assert "google" in saved_sso_config["providers"]
google_config = saved_sso_config["providers"]["google"]
assert google_config["jwks_url"] == "https://accounts.google.com/.well-known/jwks.json"
assert google_config["secret"] == "my-secret-key"
assert google_config["username_claim"] == "email"
assert google_config["algorithms"] == ["RS256", "HS256"]
assert google_config["allowed_domains"] == ["yyy.com", "company.org"]
def test_sso_handler_add_provider_allowed_domains_empty():
app_mock = MagicMock()
app_mock.services.mode = "local"
app_mock.config.config = {"sso": {"providers": {}}}
handler = SSOHandler(app_mock)
mock_answers = {
"jwks_url": "https://accounts.google.com/.well-known/jwks.json",
"secret": "",
"username_claim": "sub",
"algorithms": "RS256",
"allowed_domains": " " # empty input
}
args_mock = MagicMock()
args_mock.provider = "google"
with patch("inquirer.prompt", return_value=mock_answers):
handler.add_provider(args_mock)
saved_key, saved_sso_config = app_mock.services.config_svc.update_setting.call_args[0]
google_config = saved_sso_config["providers"]["google"]
assert "allowed_domains" not in google_config
-245
View File
@@ -1,245 +0,0 @@
"""Tests for connpy.completion module."""
import os
import json
import pytest
from connpy.completion import load_txt_cache, get_cwd
# =========================================================================
# load_txt_cache tests
# =========================================================================
class TestLoadTxtCache:
def test_load_existing_cache(self, tmp_path):
"""Loads lines from a file correctly."""
cache_file = tmp_path / "cache.txt"
cache_file.write_text("node1\nnode2\nnode3@folder")
result = load_txt_cache(str(cache_file))
assert result == ["node1", "node2", "node3@folder"]
def test_load_nonexistent_cache(self, tmp_path):
"""Returns empty list if file is missing."""
result = load_txt_cache(str(tmp_path / "missing.txt"))
assert result == []
# =========================================================================
# get_cwd tests
# =========================================================================
class TestGetCwd:
def test_current_dir(self, tmp_path, monkeypatch):
"""Lists files in current directory."""
monkeypatch.chdir(tmp_path)
(tmp_path / "file1.txt").touch()
(tmp_path / "file2.py").touch()
subdir = tmp_path / "subdir"
subdir.mkdir()
result = get_cwd(["run", "run"])
# Should list files
assert any("file1.txt" in r for r in result)
assert any("subdir/" in r for r in result)
def test_specific_path(self, tmp_path, monkeypatch):
"""Lists files matching a partial path."""
monkeypatch.chdir(tmp_path)
(tmp_path / "script.yaml").touch()
(tmp_path / "script2.yaml").touch()
result = get_cwd(["run", "script"])
assert any("script" in r for r in result)
def test_folder_only(self, tmp_path, monkeypatch):
"""folderonly=True returns only directories."""
monkeypatch.chdir(tmp_path)
(tmp_path / "file.txt").touch()
subdir = tmp_path / "mydir"
subdir.mkdir()
result = get_cwd(["export", "export"], folderonly=True)
files_in_result = [r for r in result if "file.txt" in r]
assert len(files_in_result) == 0
dirs_in_result = [r for r in result if "mydir" in r]
assert len(dirs_in_result) > 0
# =========================================================================
# Tree completions tests
# =========================================================================
class TestTreeCompletions:
def test_config_auth_completions(self):
from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp")
# Test config completions
config_completions = resolve_completion(["config", ""], tree)
assert "--engineer-auth" in config_completions
assert "--architect-auth" in config_completions
# Resolve when --engineer-auth is chosen in config
auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree)
assert isinstance(auth_comp, list)
# Loop back check:
# e.g., connpy config --engineer-auth some_val
# should loop back and resolve to config options
loop_back_comp = resolve_completion(["config", "--engineer-auth", "some_val", ""], tree)
assert "--architect-auth" in loop_back_comp
assert "--engineer-auth" in loop_back_comp
def test_ai_auth_completions(self):
from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp")
# Test ai completions
ai_completions = resolve_completion(["ai", ""], tree)
assert "--engineer-auth" in ai_completions
assert "--architect-auth" in ai_completions
# Resolve after choosing option
auth_comp = resolve_completion(["ai", "--engineer-auth", ""], tree)
assert isinstance(auth_comp, list)
# Loop back check:
# e.g., connpy ai --engineer-auth some_val
# should loop back and resolve to ai options, excluding --engineer-auth
loop_back_comp = resolve_completion(["ai", "--engineer-auth", "some_val", ""], tree)
assert "--architect-auth" in loop_back_comp
assert "--engineer-auth" not in loop_back_comp
def test_sixwindmcp_plugin_completions(self):
from connpy.completion import resolve_completion, get_cwd
import importlib.util
# Load the testremote/remote_plugins/sixwindmcp.py plugin
plugin_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"testremote", "remote_plugins", "sixwindmcp.py"
)
spec = importlib.util.spec_from_file_location("sixwindmcp", plugin_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.get_cwd = get_cwd
plugin_node = module._connpy_tree()
assert "--set-path" in plugin_node
assert "--path" in plugin_node
assert "start" in plugin_node
tree = {"sixwindmcp": plugin_node}
# Test resolution when --set-path is chosen
res = resolve_completion(["sixwindmcp", "--set-path", ""], tree)
assert isinstance(res, list)
# Loop back check:
# e.g., connpy sixwindmcp --set-path /tmp start
# should loop back and resolve to plugin options
loop_back_comp = resolve_completion(["sixwindmcp", "--set-path", "/tmp", ""], tree)
assert "start" in loop_back_comp
assert "stop" in loop_back_comp
class TestUserCompletions:
def test_user_command_options(self):
from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp")
# Test options at the "user" level
user_completions = resolve_completion(["user", ""], tree)
assert "--add" in user_completions
assert "--del" in user_completions
assert "--rm" in user_completions
assert "--show" in user_completions
assert "--regen-password" in user_completions
assert "--list" in user_completions
assert "--ls" in user_completions
def test_user_action_completed_users(self, tmp_path):
from connpy.completion import _build_tree, resolve_completion
import yaml
# Create users directory and mock registry
users_dir = tmp_path / "users"
users_dir.mkdir()
registry_file = users_dir / "registry.yaml"
registry_data = {
"users": {
"fluzzi": {"password_hash": "hash1"},
"john": {"password_hash": "hash2"}
}
}
with open(registry_file, "w") as f:
yaml.dump(registry_data, f)
tree = _build_tree([], [], [], {}, str(tmp_path))
# Resolve after --del, --rm, --show, --regen-password
for action in ["--del", "--rm", "--show", "--regen-password"]:
completions = resolve_completion(["user", action, ""], tree)
assert "fluzzi" in completions
assert "john" in completions
# --add username completed options
add_completions = resolve_completion(["user", "--add", "newguy", ""], tree)
assert "--path" in add_completions
def test_login_logout_completions(self):
from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp")
# Test login option resolution
login_completions = resolve_completion(["login", ""], tree)
assert "--help" in login_completions
# Test logout option resolution
logout_completions = resolve_completion(["logout", ""], tree)
assert "--help" in logout_completions
class TestSsoCompletions:
def test_sso_command_options(self):
from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp")
# Test options at the "sso" level
sso_completions = resolve_completion(["sso", ""], tree)
assert "--add" in sso_completions
assert "--del" in sso_completions
assert "--rm" in sso_completions
assert "--show" in sso_completions
assert "--list" in sso_completions
assert "--ls" in sso_completions
def test_sso_action_completed_providers(self, tmp_path):
from connpy.completion import _build_tree, resolve_completion
import yaml
# Create mock config.yaml with SSO providers
config_file = tmp_path / "config.yaml"
config_data = {
"config": {
"sso": {
"providers": {
"google": {"username_claim": "email"},
"authelia": {"username_claim": "sub"}
}
}
}
}
with open(config_file, "w") as f:
yaml.dump(config_data, f)
tree = _build_tree([], [], [], {}, str(tmp_path))
# Resolve after --del, --rm, --show, --add
for action in ["--del", "--rm", "--show", "--add"]:
completions = resolve_completion(["sso", action, ""], tree)
assert "google" in completions
assert "authelia" in completions
-585
View File
@@ -1,585 +0,0 @@
"""Tests for connpy.configfile module."""
import json
import os
import re
import pytest
import yaml
from copy import deepcopy
class TestConfigfileInit:
def test_creates_default_config(self, tmp_config_dir):
"""Creates config.yaml with defaults when it doesn't exist."""
config_file = tmp_config_dir / "config.yaml"
config_file.unlink(missing_ok=True) # Remove existing
key_file = tmp_config_dir / ".osk"
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(key_file))
assert config_file.exists()
assert conf.config["case"] == False
assert conf.config["idletime"] == 30
assert "default" in conf.profiles
def test_creates_rsa_key(self, tmp_config_dir):
"""Generates RSA key when it doesn't exist."""
key_file = tmp_config_dir / ".osk"
key_file.unlink() # Remove existing
from connpy.configfile import configfile
conf = configfile(conf=str(tmp_config_dir / "config.yaml"), key=str(key_file))
assert key_file.exists()
assert conf.privatekey is not None
assert conf.publickey is not None
def test_loads_existing_config(self, config):
"""Loads correctly from existing config."""
assert config.config is not None
assert config.connections is not None
assert config.profiles is not None
def test_config_file_permissions(self, tmp_config_dir):
"""Config is created with 0o600 permissions."""
config_file = tmp_config_dir / "config.yaml"
config_file.unlink(missing_ok=True)
from connpy.configfile import configfile
configfile(conf=str(config_file), key=str(tmp_config_dir / ".osk"))
stat = os.stat(str(config_file))
assert oct(stat.st_mode & 0o777) == oct(0o600)
def test_custom_paths(self, tmp_path):
"""Accepts custom paths for conf and key."""
config_dir = tmp_path / "custom"
config_dir.mkdir()
(config_dir / "plugins").mkdir()
# Write .folder for the config dir
dot_folder = tmp_path / ".config" / "conn"
dot_folder.mkdir(parents=True, exist_ok=True)
(dot_folder / ".folder").write_text(str(config_dir))
(dot_folder / "plugins").mkdir(exist_ok=True)
conf_path = str(config_dir / "my_config.yaml")
key_path = str(config_dir / "my_key")
from connpy.configfile import configfile
conf = configfile(conf=conf_path, key=key_path)
assert conf.file == conf_path
assert conf.key == key_path
class TestEncryption:
def test_encrypt_password(self, config):
"""Encrypts and produces b'...' format."""
encrypted = config.encrypt("mysecret")
assert encrypted.startswith("b'") or encrypted.startswith('b"')
def test_encrypt_decrypt_roundtrip(self, config):
"""Encrypt then decrypt returns original."""
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import ast
original = "super_secret_password"
encrypted = config.encrypt(original)
# Decrypt
with open(config.key) as f:
key = RSA.import_key(f.read())
decryptor = PKCS1_OAEP.new(key)
decrypted = decryptor.decrypt(ast.literal_eval(encrypted)).decode("utf-8")
assert decrypted == original
class TestExplodeUnique:
def test_simple_node(self, config):
result = config._explode_unique("router1")
assert result == {"id": "router1"}
def test_node_with_folder(self, config):
result = config._explode_unique("r1@office")
assert result == {"id": "r1", "folder": "office"}
def test_node_with_subfolder(self, config):
result = config._explode_unique("r1@dc@office")
assert result == {"id": "r1", "folder": "office", "subfolder": "dc"}
def test_folder_only(self, config):
result = config._explode_unique("@office")
assert result == {"folder": "office"}
def test_subfolder_only(self, config):
result = config._explode_unique("@dc@office")
assert result == {"folder": "office", "subfolder": "dc"}
def test_too_deep(self, config):
result = config._explode_unique("a@b@c@d")
assert result == False
def test_empty_folder(self, config):
result = config._explode_unique("a@")
assert result == False
def test_empty_subfolder(self, config):
result = config._explode_unique("a@@office")
assert result == False
class TestCRUDNodes:
def test_add_node_root(self, config):
config._connections_add(
id="router1", host="10.0.0.1", protocol="ssh",
port="22", user="admin", password="pass", options="",
logs="", tags="", jumphost=""
)
assert "router1" in config.connections
assert config.connections["router1"]["host"] == "10.0.0.1"
def test_add_node_folder(self, config):
config._folder_add(folder="office")
config._connections_add(
id="server1", folder="office", host="10.0.1.1",
protocol="ssh", port="", user="root", password="pass",
options="", logs="", tags="", jumphost=""
)
assert "server1" in config.connections["office"]
def test_add_node_subfolder(self, config):
config._folder_add(folder="office")
config._folder_add(folder="office", subfolder="dc")
config._connections_add(
id="db1", folder="office", subfolder="dc", host="10.0.2.1",
protocol="ssh", port="", user="dbadmin", password="pass",
options="", logs="", tags="", jumphost=""
)
assert "db1" in config.connections["office"]["dc"]
def test_del_node_root(self, config):
config._connections_add(
id="router1", host="10.0.0.1", protocol="ssh",
port="", user="", password="", options="",
logs="", tags="", jumphost=""
)
config._connections_del(id="router1")
assert "router1" not in config.connections
def test_del_node_folder(self, config):
config._folder_add(folder="office")
config._connections_add(
id="server1", folder="office", host="10.0.1.1",
protocol="ssh", port="", user="", password="",
options="", logs="", tags="", jumphost=""
)
config._connections_del(id="server1", folder="office")
assert "server1" not in config.connections["office"]
def test_add_folder(self, config):
config._folder_add(folder="office")
assert "office" in config.connections
assert config.connections["office"]["type"] == "folder"
def test_add_subfolder(self, config):
config._folder_add(folder="office")
config._folder_add(folder="office", subfolder="dc")
assert "dc" in config.connections["office"]
assert config.connections["office"]["dc"]["type"] == "subfolder"
def test_del_folder(self, config):
config._folder_add(folder="office")
config._folder_del(folder="office")
assert "office" not in config.connections
def test_del_subfolder(self, config):
config._folder_add(folder="office")
config._folder_add(folder="office", subfolder="dc")
config._folder_del(folder="office", subfolder="dc")
assert "dc" not in config.connections["office"]
class TestCRUDProfiles:
def test_add_profile(self, config):
config._profiles_add(
id="myprofile", host="", protocol="telnet",
port="23", user="user1", password="pass1",
options="", logs="", tags="", jumphost=""
)
assert "myprofile" in config.profiles
assert config.profiles["myprofile"]["protocol"] == "telnet"
def test_del_profile(self, config):
config._profiles_add(
id="temp", host="", protocol="ssh", port="",
user="", password="", options="", logs="", tags="", jumphost=""
)
config._profiles_del(id="temp")
assert "temp" not in config.profiles
def test_default_profile_exists(self, config):
assert "default" in config.profiles
class TestGetItem:
def test_getitem_node(self, populated_config):
node = populated_config.getitem("router1")
assert node["host"] == "10.0.0.1"
assert "type" not in node # type is stripped
def test_getitem_folder(self, populated_config):
nodes = populated_config.getitem("@office")
# Should contain server1@office but NOT datacenter (subfolder)
assert "server1@office" in nodes
assert all("type" not in v for v in nodes.values())
def test_getitem_subfolder(self, populated_config):
nodes = populated_config.getitem("@datacenter@office")
assert "db1@datacenter@office" in nodes
def test_getitem_node_in_folder(self, populated_config):
node = populated_config.getitem("server1@office")
assert node["host"] == "10.0.1.1"
def test_getitem_node_in_subfolder(self, populated_config):
node = populated_config.getitem("db1@datacenter@office")
assert node["host"] == "10.0.2.1"
def test_getitem_with_profile_extraction(self, tmp_config_dir):
"""extract=True resolves @profile references."""
config_file = tmp_config_dir / "config.yaml"
data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {
"router1": {
"host": "10.0.0.1", "protocol": "ssh", "port": "",
"user": "@office-user", "password": "@office-user",
"options": "", "logs": "", "tags": "", "jumphost": "",
"type": "connection"
}
},
"profiles": {
"default": {"host": "", "protocol": "ssh", "port": "",
"user": "", "password": "", "options": "",
"logs": "", "tags": "", "jumphost": ""},
"office-user": {"host": "", "protocol": "ssh", "port": "",
"user": "officeadmin", "password": "officepass",
"options": "", "logs": "", "tags": "", "jumphost": ""}
}
}
config_file.write_text(yaml.dump(data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(tmp_config_dir / ".osk"))
node = conf.getitem("router1", extract=True)
assert node["user"] == "officeadmin"
assert node["password"] == "officepass"
def test_getitems_multiple(self, populated_config):
nodes = populated_config.getitems(["router1", "server1@office"])
assert "router1" in nodes
assert "server1@office" in nodes
def test_getitems_folder(self, populated_config):
nodes = populated_config.getitems(["@office"])
assert "server1@office" in nodes
class TestGetAll:
def test_getallnodes_no_filter(self, populated_config):
nodes = populated_config._getallnodes()
assert "router1" in nodes
assert "server1@office" in nodes
assert "db1@datacenter@office" in nodes
def test_getallnodes_string_filter(self, populated_config):
nodes = populated_config._getallnodes("router.*")
assert "router1" in nodes
assert "server1@office" not in nodes
def test_getallnodes_list_filter(self, populated_config):
nodes = populated_config._getallnodes(["router.*", "db.*"])
assert "router1" in nodes
assert "db1@datacenter@office" in nodes
assert "server1@office" not in nodes
def test_getallnodes_filter_invalid_type(self, populated_config):
with pytest.raises(SystemExit) as exc:
populated_config._getallnodes(123)
assert exc.value.code == 1
def test_getallfolders(self, populated_config):
folders = populated_config._getallfolders()
assert "@office" in folders
assert "@datacenter@office" in folders
def test_getallnodesfull(self, populated_config):
nodes = populated_config._getallnodesfull()
assert "router1" in nodes
assert nodes["router1"]["host"] == "10.0.0.1"
def test_getallnodesfull_with_filter(self, populated_config):
nodes = populated_config._getallnodesfull("router.*")
assert "router1" in nodes
assert "server1@office" not in nodes
def test_profileused(self, tmp_config_dir):
"""Detects nodes using a specific profile."""
config_file = tmp_config_dir / "config.yaml"
data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {
"router1": {
"host": "10.0.0.1", "protocol": "ssh", "port": "",
"user": "@myprofile", "password": "pass",
"options": "", "logs": "", "tags": "", "jumphost": "",
"type": "connection"
},
"router2": {
"host": "10.0.0.2", "protocol": "ssh", "port": "",
"user": "admin", "password": "pass",
"options": "", "logs": "", "tags": "", "jumphost": "",
"type": "connection"
}
},
"profiles": {
"default": {"host": "", "protocol": "ssh", "port": "",
"user": "", "password": "", "options": "",
"logs": "", "tags": "", "jumphost": ""},
"myprofile": {"host": "", "protocol": "ssh", "port": "",
"user": "profuser", "password": "profpass",
"options": "", "logs": "", "tags": "", "jumphost": ""}
}
}
config_file.write_text(yaml.dump(data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(tmp_config_dir / ".osk"))
used = conf._profileused("myprofile")
assert "router1" in used
assert "router2" not in used
def test_saveconfig(self, config):
"""Save and reload correctly."""
config._connections_add(
id="test_node", host="1.2.3.4", protocol="ssh",
port="", user="", password="", options="",
logs="", tags="", jumphost=""
)
result = config._saveconfig(config.file)
assert result == 0
# Reload and verify
from connpy.configfile import configfile
reloaded = configfile(conf=config.file, key=config.key)
assert "test_node" in reloaded.connections
class TestValidateConfig:
def test_valid_config(self, config):
data = {"config": {}, "connections": {}, "profiles": {}}
assert config._validate_config(data) == True
def test_none_data(self, config):
assert config._validate_config(None) == False
def test_string_data(self, config):
assert config._validate_config("not a dict") == False
def test_missing_key(self, config):
assert config._validate_config({"config": {}, "connections": {}}) == False
def test_empty_dict(self, config):
assert config._validate_config({}) == False
class TestCorruptionRecovery:
def test_corrupt_yaml_recovers_from_cache(self, tmp_config_dir):
"""If YAML is corrupt but cache is valid, recovers from cache."""
config_file = tmp_config_dir / "config.yaml"
key_file = tmp_config_dir / ".osk"
# Write valid config with router1
valid_data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {"router1": {"host": "10.0.0.1", "type": "connection", "protocol": "ssh", "port": "", "user": "", "password": "", "options": "", "logs": "", "tags": "", "jumphost": ""}},
"profiles": {"default": {"host": "", "protocol": "ssh", "port": "", "user": "", "password": "", "options": "", "logs": "", "tags": "", "jumphost": ""}}
}
config_file.write_text(yaml.dump(valid_data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(key_file))
# Save to populate cache at the real self.cachefile path
conf._saveconfig(conf.file)
cachefile_path = conf.cachefile
assert os.path.exists(cachefile_path)
# Now corrupt the YAML
config_file.write_text("")
import time; time.sleep(0.05) # Ensure YAML is newer than cache
# Reload - should recover from cache
conf2 = configfile(conf=str(config_file), key=str(key_file))
assert "router1" in conf2.connections
assert conf2.connections["router1"]["host"] == "10.0.0.1"
def test_corrupt_cache_uses_yaml(self, tmp_config_dir):
"""If cache is corrupt but YAML is valid, uses YAML."""
config_file = tmp_config_dir / "config.yaml"
key_file = tmp_config_dir / ".osk"
valid_data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {},
"profiles": {"default": {"host": "", "protocol": "ssh", "port": "", "user": "", "password": "", "options": "", "logs": "", "tags": "", "jumphost": ""}}
}
config_file.write_text(yaml.dump(valid_data, default_flow_style=False, sort_keys=False))
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(key_file))
cachefile_path = conf.cachefile
# Now corrupt the cache (valid JSON but invalid config structure)
from pathlib import Path
Path(cachefile_path).write_text(json.dumps({"garbage": True}))
# Make cache newer than YAML to force cache path
import time; time.sleep(0.05)
os.utime(cachefile_path, None)
conf2 = configfile(conf=str(config_file), key=str(key_file))
assert conf2.config["case"] == False
assert "default" in conf2.profiles
def test_both_corrupt_creates_default(self, tmp_config_dir):
"""If both YAML and cache are corrupt, creates fresh config."""
config_file = tmp_config_dir / "config.yaml"
key_file = tmp_config_dir / ".osk"
from connpy.configfile import configfile
conf = configfile(conf=str(config_file), key=str(key_file))
cachefile_path = conf.cachefile
# Corrupt YAML
config_file.write_text("")
# Corrupt cache
from pathlib import Path
Path(cachefile_path).write_text(json.dumps({"garbage": True}))
import time; time.sleep(0.05)
os.utime(str(config_file), None)
conf2 = configfile(conf=str(config_file), key=str(key_file))
# Should get defaults, not crash
assert conf2.config is not None
assert "default" in conf2.profiles
assert isinstance(conf2.connections, dict)
class TestAtomicSave:
def test_save_creates_no_leftover_tmp(self, config):
"""After successful save, no .tmp file remains."""
config._connections_add(
id="test123", host="1.2.3.4", protocol="ssh",
port="", user="", password="", options="",
logs="", tags="", jumphost=""
)
result = config._saveconfig(config.file)
assert result == 0
assert not os.path.exists(config.file + '.tmp')
def test_save_preserves_original_on_error(self, config):
"""If save fails, original config file is not corrupted."""
import unittest.mock as mock
config._connections_add(
id="original_node", host="10.0.0.1", protocol="ssh",
port="", user="", password="", options="",
logs="", tags="", jumphost=""
)
config._saveconfig(config.file)
# Now add another node and make yaml.dump fail
config._connections_add(
id="new_node", host="10.0.0.2", protocol="ssh",
port="", user="", password="", options="",
logs="", tags="", jumphost=""
)
with mock.patch('connpy.configfile.yaml.dump', side_effect=IOError("disk full")):
result = config._saveconfig(config.file)
assert result == 1
# Original file should still be valid with original_node
from connpy.configfile import configfile
reloaded = configfile(conf=config.file, key=config.key)
assert "original_node" in reloaded.connections
class TestMigrationSafety:
def test_migration_validates_legacy_data(self, tmp_path):
"""Migration skips invalid legacy JSON files."""
from unittest.mock import patch
config_dir = tmp_path / ".config" / "conn"
config_dir.mkdir(parents=True)
(config_dir / "plugins").mkdir()
# Write .folder
(config_dir / ".folder").write_text(str(config_dir))
# Generate RSA key
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
key_file = config_dir / ".osk"
key_file.write_bytes(key.export_key("PEM"))
os.chmod(str(key_file), 0o600)
# Write invalid JSON config (missing required keys)
legacy_file = config_dir / "config.json"
legacy_file.write_text(json.dumps({"garbage": True}))
with patch("os.path.expanduser", return_value=str(tmp_path)):
from connpy.configfile import configfile
conf = configfile(key=str(key_file))
# Legacy file should NOT have been moved to .backup
assert legacy_file.exists()
assert not (config_dir / "config.json.backup").exists()
def test_migration_verifies_written_yaml(self, tmp_path):
"""Migration succeeds when legacy JSON is valid."""
from unittest.mock import patch
config_dir = tmp_path / ".config" / "conn"
config_dir.mkdir(parents=True)
(config_dir / "plugins").mkdir()
# Write .folder
(config_dir / ".folder").write_text(str(config_dir))
# Generate RSA key
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
key_file = config_dir / ".osk"
key_file.write_bytes(key.export_key("PEM"))
os.chmod(str(key_file), 0o600)
valid_data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {"r1": {"host": "1.2.3.4", "type": "connection", "protocol": "ssh", "port": "", "user": "", "password": "", "options": "", "logs": "", "tags": "", "jumphost": ""}},
"profiles": {"default": {"host": "", "protocol": "ssh", "port": "", "user": "", "password": "", "options": "", "logs": "", "tags": "", "jumphost": ""}}
}
legacy_file = config_dir / "config.json"
legacy_file.write_text(json.dumps(valid_data))
with patch("os.path.expanduser", return_value=str(tmp_path)):
from connpy.configfile import configfile
conf = configfile(key=str(key_file))
# Migration should have succeeded: YAML exists, JSON backed up
yaml_file = config_dir / "config.yaml"
assert yaml_file.exists()
assert (config_dir / "config.json.backup").exists()
assert not legacy_file.exists()
assert "r1" in conf.connections
-326
View File
@@ -1,326 +0,0 @@
import pytest
from unittest.mock import patch, MagicMock
from connpy.connapp import connapp
import sys
import yaml
import os
@pytest.fixture
def app(populated_config):
"""Returns an instance of connapp initialized with the mock config."""
return connapp(populated_config)
def test_connapp_init(app, populated_config):
"""Test that connapp initializes correctly with config."""
assert app.config == populated_config
assert app.case == populated_config.config.get("case", False)
@patch("connpy.cli.node_handler.NodeHandler.dispatch")
def test_node_default(mock_func_node, app):
"""Test that default 'node' command correctly parses and calls _func_node."""
app.start(["node", "router1"])
mock_func_node.assert_called_once()
args = mock_func_node.call_args[0][0]
assert args.data == "router1"
assert args.action == "connect"
@patch("connpy.cli.node_handler.NodeHandler.dispatch")
def test_node_add(mock_func_node, app):
"""Test that 'node -a' command correctly parses."""
app.start(["node", "-a", "new_router"])
mock_func_node.assert_called_once()
args = mock_func_node.call_args[0][0]
assert args.data == "new_router"
assert args.action == "add"
@patch("connpy.services.node_service.NodeService.list_nodes")
@patch("connpy.services.node_service.NodeService.delete_node")
@patch("inquirer.prompt")
def test_node_del(mock_prompt, mock_delete_node, mock_list_nodes, app):
mock_list_nodes.return_value = ["router1"]
mock_prompt.return_value = {"delete": True}
app.start(["node", "-r", "router1"])
mock_delete_node.assert_called_once_with("router1", is_folder=False, save=True)
@patch("connpy.services.node_service.NodeService.list_nodes")
@patch("connpy.services.node_service.NodeService.get_node_details")
@patch("connpy.services.node_service.NodeService.update_node")
@patch("connpy.cli.forms.Forms.questions_edit")
@patch("connpy.cli.forms.Forms.questions_nodes")
def test_node_mod(mock_q_nodes, mock_q_edit, mock_update_node, mock_get_details, mock_list_nodes, app):
mock_list_nodes.return_value = ["router1"]
mock_get_details.return_value = {"host": "1.1.1.1", "port": 22}
mock_q_edit.return_value = {"host": True}
mock_q_nodes.return_value = {"host": "2.2.2.2", "port": 22}
app.start(["node", "-e", "router1"])
mock_update_node.assert_called_once()
@patch("connpy.printer.data")
def test_node_show(mock_data, app):
app.nodes_list = ["router1"]
app.config.getitem = MagicMock(return_value={"host": "1.1.1.1"})
app.start(["node", "-s", "router1"])
mock_data.assert_called()
@patch("connpy.services.profile_service.ProfileService.list_profiles")
@patch("connpy.connapp.printer.console.print")
def test_profile_list(mock_print, mock_list_profiles, app):
"""Test 'profile list' invokes profile service correctly."""
mock_list_profiles.return_value = ["default", "office-user"]
app.start(["list", "profiles"])
assert mock_list_profiles.call_count >= 2
@patch("connpy.services.node_service.NodeService.list_nodes")
def test_node_list(mock_list_nodes, app):
"""Test 'list nodes' invokes node service."""
mock_list_nodes.return_value = ["router1", "server1"]
app.start(["list", "nodes"])
# Should be called during init and during the list command
assert mock_list_nodes.call_count >= 2
@patch("connpy.services.system_service.SystemService.get_api_status")
def test_api_stop(mock_status, app):
mock_status.return_value = {"running": True, "pid": "1234"}
app.services.system.stop_api = MagicMock(return_value=True)
app.start(["api", "-x"])
app.services.system.stop_api.assert_called_once()
@patch("connpy.services.profile_service.ProfileService.list_profiles")
@patch("connpy.services.profile_service.ProfileService.add_profile")
@patch("connpy.cli.forms.Forms.questions_profiles")
def test_profile_add(mock_q_profiles, mock_add_profile, mock_list_profiles, app):
mock_list_profiles.return_value = ["default"]
mock_q_profiles.return_value = {"host": "test"}
app.start(["profile", "-a", "new_profile"])
mock_add_profile.assert_called_once_with("new_profile", {"host": "test"})
@patch("connpy.services.profile_service.ProfileService.get_profile")
@patch("connpy.services.profile_service.ProfileService.delete_profile")
@patch("inquirer.prompt")
def test_profile_del(mock_prompt, mock_delete_profile, mock_get_profile, app):
mock_get_profile.return_value = {"host": "test"}
mock_prompt.return_value = {"delete": True}
app.start(["profile", "-r", "test_profile"])
mock_delete_profile.assert_called_once_with("test_profile")
@patch("connpy.services.profile_service.ProfileService.get_profile")
@patch("connpy.services.profile_service.ProfileService.update_profile")
@patch("connpy.cli.forms.Forms.questions_edit")
@patch("connpy.cli.forms.Forms.questions_profiles")
def test_profile_mod(mock_q_profiles, mock_q_edit, mock_update_profile, mock_get_profile, app):
mock_get_profile.return_value = {"host": "test", "port": 22}
mock_q_edit.return_value = {"host": True}
mock_q_profiles.return_value = {"id": "test_profile", "host": "new_host", "port": 22}
app.start(["profile", "-e", "test_profile"])
mock_update_profile.assert_called_once_with("test_profile", {"id": "test_profile", "host": "new_host", "port": 22})
@patch("connpy.services.profile_service.ProfileService.get_profile")
@patch("connpy.printer.data")
def test_profile_show(mock_data, mock_get_profile, app):
mock_get_profile.return_value = {"host": "test"}
app.start(["profile", "-s", "test_profile"])
mock_data.assert_called()
@patch("connpy.services.node_service.NodeService.move_node")
def test_move(mock_move_node, app):
app.start(["move", "src_node", "dst_node"])
mock_move_node.assert_called_once_with("src_node", "dst_node", copy=False)
@patch("connpy.services.node_service.NodeService.move_node")
def test_copy(mock_move_node, app):
app.start(["copy", "src_node", "dst_node"])
mock_move_node.assert_called_once_with("src_node", "dst_node", copy=True)
@patch("connpy.cli.forms.Forms.questions_bulk")
@patch("connpy.services.node_service.NodeService.bulk_add")
def test_bulk(mock_bulk_add, mock_q_bulk, app):
mock_q_bulk.return_value = {"ids": "node1", "host": "host1", "location": ""}
mock_bulk_add.return_value = 1
app.start(["bulk"])
mock_bulk_add.assert_called_once()
@patch("connpy.services.import_export_service.ImportExportService.export_to_file")
def test_export(mock_export, app):
with pytest.raises(SystemExit):
app.start(["export", "file.yml", "@folder1"])
mock_export.assert_called_once_with("file.yml", folders=["@folder1"])
@patch("os.path.exists")
@patch("inquirer.prompt")
@patch("connpy.services.import_export_service.ImportExportService.import_from_file")
def test_import(mock_import, mock_prompt, mock_exists, app):
mock_exists.return_value = True
mock_prompt.return_value = {"import": True}
app.start(["import", "file.yml"])
mock_import.assert_called_once_with("file.yml")
@patch("connpy.services.ai_service.AIService.ask")
@patch("connpy.connapp.console.status")
def test_ai(mock_status, mock_ask, app):
mock_ask.return_value = {"response": "AI output", "usage": {"total": 10, "input": 5, "output": 5}}
app.start(["ai", "--engineer-api-key", "testkey", "how are you"])
mock_ask.assert_called_once()
@patch("connpy.services.execution_service.ExecutionService.run_commands")
def test_run(mock_run_commands, app):
app.start(["run", "router1", "command1", "command2"])
mock_run_commands.assert_called_once()
assert mock_run_commands.call_args[1]["nodes_filter"] == ["router1"]
assert mock_run_commands.call_args[1]["commands"] == ["command1 command2"]
@patch("os.path.exists")
@patch("shutil.copy2")
@patch("connpy.plugins.Plugins.verify_script")
def test_plugin_add(mock_verify, mock_copy, mock_exists, app):
def mock_exists_side_effect(path):
if "testplug.py" in path: return False
if "testplug.py.bkp" in path: return False
if "file.py" in path: return True
return True
mock_exists.side_effect = mock_exists_side_effect
mock_verify.return_value = None
app.commands = []
app.start(["plugin", "--add", "testplug", "file.py"])
mock_copy.assert_called()
@patch("connpy.services.config_service.ConfigService.update_setting")
def test_config(mock_update_setting, app):
app.start(["config", "--allow-uppercase", "true"])
mock_update_setting.assert_called_with("case", True)
@patch("connpy.services.system_service.SystemService.get_api_status")
def test_api_start(mock_status, app):
mock_status.return_value = {"running": False}
app.services.system.start_api = MagicMock()
app.start(["api", "-s", "8080"])
app.services.system.start_api.assert_called_once_with(port=8080)
@patch("connpy.services.system_service.SystemService.get_api_status")
def test_api_debug(mock_status, app):
mock_status.return_value = {"running": False}
app.services.system.debug_api = MagicMock()
app.start(["api", "-d", "8080"])
app.services.system.debug_api.assert_called_once_with(port=8080)
@patch("connpy.services.node_service.NodeService.list_folders")
def test_list_folders(mock_list_folders, app):
mock_list_folders.return_value = ["folder1"]
app.start(["list", "folders"])
# Called during init and during the list command
assert mock_list_folders.call_count >= 2
@patch("connpy.services.config_service.ConfigService.update_setting")
def test_config_various(mock_update_setting, app):
app.start(["config", "--fzf", "true"])
mock_update_setting.assert_called_with("fzf", True)
app.start(["config", "--keepalive", "60"])
mock_update_setting.assert_called_with("idletime", 60)
@patch("connpy.services.config_service.ConfigService.set_config_folder")
def test_config_folder(mock_set_config_folder, app):
app.start(["config", "--configfolder", "/new/path"])
mock_set_config_folder.assert_called_once_with("/new/path")
@patch("connpy.services.plugin_service.PluginService.list_plugins")
def test_plugin_list(mock_list_plugins, app):
mock_list_plugins.return_value = {"testplug": {"enabled": True}}
app.start(["plugin", "--list"])
mock_list_plugins.assert_called_once()
@patch("connpy.services.plugin_service.PluginService.delete_plugin")
def test_plugin_delete(mock_delete, app):
app.start(["plugin", "--del", "testplug"])
mock_delete.assert_called_once_with("testplug")
@patch("connpy.services.plugin_service.PluginService.enable_plugin")
def test_plugin_enable(mock_enable, app):
app.start(["plugin", "--enable", "testplug"])
mock_enable.assert_called_once_with("testplug")
@patch("connpy.services.plugin_service.PluginService.disable_plugin")
def test_plugin_disable(mock_disable, app):
app.start(["plugin", "--disable", "testplug"])
mock_disable.assert_called_once_with("testplug")
@patch("connpy.services.ai_service.AIService.list_sessions")
def test_ai_list(mock_list_sessions, app):
mock_list_sessions.return_value = ([{"id": "1", "title": "t", "created_at": "now", "model": "m"}], 1)
app.start(["ai", "--list"])
mock_list_sessions.assert_called_once()
def test_type_node_reserved_word(app):
app.commands = ["bulk", "ai", "run"]
with patch("sys.argv", ["connpy", "node", "-a", "bulk"]):
with pytest.raises(SystemExit) as exc:
app._type_node("bulk")
assert exc.value.code == 2
# In move/copy it also raises because destination cannot be reserved
with patch("sys.argv", ["connpy", "mv", "test1", "bulk"]):
with pytest.raises(SystemExit) as exc:
app._type_node("bulk")
assert exc.value.code == 2
@patch("connpy.services.config_service.ConfigService.update_setting")
@patch("connpy.services.config_service.ConfigService.get_settings")
def test_config_auth_inline_json(mock_get_settings, mock_update_setting, app):
mock_get_settings.return_value = {"ai": {}}
app.start(["config", "--engineer-auth", '{"vertex_project": "test-123"}'])
mock_update_setting.assert_called_once()
args, kwargs = mock_update_setting.call_args
assert args[0] == "ai"
assert args[1]["engineer_auth"] == {"vertex_project": "test-123"}
@patch("connpy.services.config_service.ConfigService.update_setting")
@patch("connpy.services.config_service.ConfigService.get_settings")
def test_config_auth_inline_yaml(mock_get_settings, mock_update_setting, app):
mock_get_settings.return_value = {"ai": {}}
app.start(["config", "--architect-auth", 'project: test-yaml'])
mock_update_setting.assert_called_once()
args, kwargs = mock_update_setting.call_args
assert args[0] == "ai"
assert args[1]["architect_auth"] == {"project": "test-yaml"}
@patch("connpy.services.config_service.ConfigService.update_setting")
@patch("connpy.services.config_service.ConfigService.get_settings")
def test_config_clear_auth(mock_get_settings, mock_update_setting, app):
mock_get_settings.return_value = {"ai": {"engineer_auth": {"project": "123"}, "engineer_api_key": "some-key"}}
app.start(["config", "--engineer-auth", "clear"])
args, kwargs = mock_update_setting.call_args
assert "engineer_auth" not in args[1]
app.start(["config", "--engineer-api-key", "none"])
args, kwargs = mock_update_setting.call_args
assert "engineer_api_key" not in args[1]
@patch("os.path.exists")
@patch("builtins.open")
@patch("connpy.services.config_service.ConfigService.update_setting")
@patch("connpy.services.config_service.ConfigService.get_settings")
def test_config_auth_file_path(mock_get_settings, mock_update_setting, mock_open, mock_exists, app):
mock_get_settings.return_value = {"ai": {}}
mock_exists.side_effect = lambda p: True if p == "/path/to/creds.json" else False
mock_file = MagicMock()
mock_file.read.return_value = '{"vertex_project": "file-project"}'
mock_open.return_value.__enter__.return_value = mock_file
app.start(["config", "--engineer-auth", "/path/to/creds.json"])
mock_update_setting.assert_called_once()
args, kwargs = mock_update_setting.call_args
assert args[0] == "ai"
assert args[1]["engineer_auth"] == {"vertex_project": "file-project"}
@patch("connpy.services.node_service.NodeService.list_nodes")
@patch("connpy.services.node_service.NodeService.connect_node")
def test_node_connect_exact_match_priority(mock_connect_node, mock_list_nodes, app):
"""Test that exact matches are prioritized over partial/regex matches when connecting."""
mock_list_nodes.return_value = ["pe1@ctx", "qro1pe1@ctx"]
app.start(["node", "pe1@ctx"])
mock_connect_node.assert_called_once_with("pe1@ctx", sftp=False, debug=False, logger=app._service_logger)
-489
View File
@@ -1,489 +0,0 @@
"""Tests for connpy.core module — node and nodes classes."""
import json
import os
import io
import re
import pytest
from unittest.mock import patch, MagicMock, PropertyMock
from copy import deepcopy
# =========================================================================
# node.__init__ tests
# =========================================================================
class TestNodeInit:
def test_basic_init(self):
"""Creates node with basic attributes."""
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="pass1", protocol="ssh")
assert n.unique == "router1"
assert n.host == "10.0.0.1"
assert n.user == "admin"
assert n.protocol == "ssh"
assert n.password == ["pass1"]
def test_default_protocol(self):
"""Default protocol is ssh."""
from connpy.core import node
n = node("router1", "10.0.0.1")
assert n.protocol == "ssh"
def test_password_as_list_of_profiles(self, populated_config):
"""Password list with @profile references resolves correctly."""
from connpy.core import node
n = node("router1", "10.0.0.1", password=["@office-user"],
config=populated_config)
assert n.password == ["officepass"]
def test_password_plain_string(self):
"""Plain string password is wrapped in a list."""
from connpy.core import node
n = node("router1", "10.0.0.1", password="mypass")
assert n.password == ["mypass"]
def test_node_with_profile(self, populated_config):
"""Resolves @profile references for user."""
from connpy.core import node
n = node("test1", "10.0.0.1", user="@office-user", password="plain",
config=populated_config)
assert n.user == "officeadmin"
def test_node_tags(self):
"""Tags are stored correctly."""
from connpy.core import node
tags = {"os": "cisco_ios", "prompt": r"Router#"}
n = node("router1", "10.0.0.1", tags=tags)
assert n.tags["os"] == "cisco_ios"
# =========================================================================
# Command generation tests
# =========================================================================
class TestCommandGeneration:
def _make_node(self, **kwargs):
from connpy.core import node
defaults = {
"unique": "test", "host": "10.0.0.1", "protocol": "ssh",
"user": "admin", "password": "", "port": "", "options": "",
"jumphost": "", "tags": "", "logs": ""
}
defaults.update(kwargs)
return node(defaults.pop("unique"), defaults.pop("host"), **defaults)
def test_ssh_cmd_basic(self):
n = self._make_node()
cmd = n._get_cmd()
assert "ssh" in cmd
assert "admin@10.0.0.1" in cmd
def test_ssh_cmd_port(self):
n = self._make_node(port="2222")
cmd = n._get_cmd()
assert "-p 2222" in cmd
def test_ssh_cmd_options(self):
n = self._make_node(options="-o StrictHostKeyChecking=no")
cmd = n._get_cmd()
assert "-o StrictHostKeyChecking=no" in cmd
def test_sftp_cmd_port(self):
n = self._make_node(protocol="sftp", port="2222")
cmd = n._get_cmd()
assert "-P 2222" in cmd # SFTP uses uppercase P
def test_telnet_cmd(self):
n = self._make_node(protocol="telnet", port="23")
cmd = n._get_cmd()
assert "telnet 10.0.0.1" in cmd
assert "23" in cmd
def test_ssm_cmd_basic(self):
n = self._make_node(protocol="ssm", host="i-12345")
cmd = n._get_cmd()
assert "aws ssm start-session" in cmd
assert "--target i-12345" in cmd
def test_ssm_cmd_tags(self):
n = self._make_node(protocol="ssm", host="i-12345", tags={"region": "us-west-2", "profile": "prod"})
cmd = n._get_cmd()
assert "--region us-west-2" in cmd
assert "--profile prod" in cmd
def test_ssm_cmd_options(self):
n = self._make_node(protocol="ssm", host="i-12345", options="--document-name AWS-StartInteractiveCommand")
cmd = n._get_cmd()
assert "--document-name AWS-StartInteractiveCommand" in cmd
def test_kubectl_cmd(self):
n = self._make_node(protocol="kubectl", host="my-pod", tags={"kube_command": "/bin/sh"})
cmd = n._get_cmd()
assert "kubectl exec" in cmd
assert "my-pod" in cmd
assert "/bin/sh" in cmd
def test_kubectl_cmd_default_command(self):
n = self._make_node(protocol="kubectl", host="my-pod")
cmd = n._get_cmd()
assert "/bin/bash" in cmd
def test_docker_cmd(self):
n = self._make_node(protocol="docker", host="my-container",
tags={"docker_command": "/bin/sh"})
cmd = n._get_cmd()
assert "docker" in cmd
assert "my-container" in cmd
assert "/bin/sh" in cmd
def test_invalid_protocol_raises(self):
n = self._make_node(protocol="invalid_proto")
with pytest.raises(SystemExit) as exc:
n._get_cmd()
assert exc.value.code == 1
def test_ssh_cmd_no_user(self):
n = self._make_node(user="")
cmd = n._get_cmd()
assert "10.0.0.1" in cmd
assert "@" not in cmd # No user@ prefix
# =========================================================================
# Password decryption tests
# =========================================================================
class TestPasswordDecryption:
def test_passtx_plaintext(self, config):
"""Plaintext passwords pass through unchanged."""
from connpy.core import node
n = node("test", "10.0.0.1", password="plainpass", config=config)
result = n._passtx(["plainpass"])
assert result == ["plainpass"]
def test_passtx_encrypted(self, config):
"""Encrypted passwords get decrypted."""
from connpy.core import node
encrypted = config.encrypt("mysecret")
n = node("test", "10.0.0.1", password=encrypted, config=config)
result = n._passtx([encrypted])
assert result == ["mysecret"]
def test_passtx_missing_key_raises(self):
"""Missing key file raises ValueError."""
from connpy.core import node
n = node("test", "10.0.0.1", password="pass")
# A password formatted as encrypted but no valid key
with pytest.raises((ValueError, Exception)):
n._passtx(["""b'corrupted_encrypted_data'"""], keyfile="/nonexistent")
# =========================================================================
# Log handling tests
# =========================================================================
class TestLogHandling:
def test_logfile_variable_substitution(self):
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", protocol="ssh", port="22",
logs="/logs/${unique}_${host}_${user}")
result = n._logfile()
assert result == "/logs/router1_10.0.0.1_admin"
def test_logfile_date_substitution(self):
from connpy.core import node
import datetime
n = node("router1", "10.0.0.1", logs="/logs/${date '%Y'}")
result = n._logfile()
assert datetime.datetime.now().strftime("%Y") in result
def test_logclean_removes_ansi(self):
from connpy.core import node
n = node("test", "10.0.0.1")
dirty = "\x1B[32mgreen text\x1B[0m"
clean = n._logclean(dirty, var=True)
assert "\x1B" not in clean
assert "green text" in clean
def test_logclean_removes_backspaces(self):
from connpy.core import node
n = node("test", "10.0.0.1")
dirty = "type\bo"
clean = n._logclean(dirty, var=True)
assert "\b" not in clean
# =========================================================================
# run() and test() with mock pexpect
# =========================================================================
class TestNodeRun:
def _make_connected_node(self, mock_pexpect_obj, **kwargs):
"""Create a node and mock its _connect to succeed."""
from connpy.core import node
defaults = {
"unique": "router1", "host": "10.0.0.1",
"protocol": "ssh", "user": "admin", "password": ""
}
defaults.update(kwargs)
n = node(defaults.pop("unique"), defaults.pop("host"), **defaults)
return n
def test_run_returns_output(self, mock_pexpect):
"""run() returns string output."""
child = mock_pexpect["child"]
pexp = mock_pexpect["pexpect"]
# Simulate: connect succeeds, command runs, prompt found
child.expect.return_value = 9 # prompt index for ssh
child.logfile_read = None
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
# Mock _connect to return True and set up child
with patch.object(n, '_connect', return_value=True):
n.child = child
log_buffer = io.BytesIO(b"show version\nRouter v1.0\nrouter#")
n.mylog = log_buffer
child.logfile_read = log_buffer
with patch.object(n, '_logclean', return_value="Router v1.0"):
output = n.run(["show version"])
assert n.status == 0
assert output == "Router v1.0"
def test_run_status_1_on_failure(self, mock_pexpect):
"""Status 1 when connection fails."""
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value="Connection failed code: 1\nrefused"):
output = n.run(["show version"])
assert n.status == 1
assert "refused" in output
def test_run_with_variables(self, mock_pexpect):
"""Variables get substituted in commands."""
child = mock_pexpect["child"]
child.expect.return_value = 9
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
sent_commands = []
child.sendline.side_effect = lambda cmd: sent_commands.append(cmd)
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"output")
with patch.object(n, '_logclean', return_value="output"):
n.run(["show ip route {subnet}"], vars={"subnet": "10.0.0.0/24"})
assert "show ip route 10.0.0.0/24" in sent_commands
def test_run_saves_to_folder(self, mock_pexpect, tmp_path):
"""folder param saves log file."""
child = mock_pexpect["child"]
child.expect.return_value = 9
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"log output")
with patch.object(n, '_logclean', return_value="log output"):
n.run(["show version"], folder=str(tmp_path))
log_files = list(tmp_path.glob("router1_*.txt"))
assert len(log_files) == 1
assert "log output" in log_files[0].read_text()
class TestNodeTest:
def test_test_returns_dict(self, mock_pexpect):
"""test() returns dict of results."""
child = mock_pexpect["child"]
child.expect.return_value = 0 # prompt found (index 0 in test expects)
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"1.1.1.1 is up")
with patch.object(n, '_logclean', return_value="1.1.1.1 is up"):
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
assert isinstance(result, dict)
assert result.get("1.1.1.1") == True
def test_test_expected_not_found(self, mock_pexpect):
"""Expected text not found returns False."""
child = mock_pexpect["child"]
child.expect.return_value = 0
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"some other output")
with patch.object(n, '_logclean', return_value="some other output"):
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
assert isinstance(result, dict)
assert result.get("1.1.1.1") == False
def test_test_expected_regex(self, mock_pexpect):
"""Regex in expected matches correctly."""
child = mock_pexpect["child"]
child.expect.return_value = 0
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"Debian version 12.5")
with patch.object(n, '_logclean', return_value="Debian version 12.5"):
result = n.test(["cat /etc/debian_version"], "version \\d+\\.\\d+")
assert isinstance(result, dict)
assert result.get("version \\d+\\.\\d+") == True
def test_test_expected_invalid_regex(self, mock_pexpect):
"""Malformed regex defaults to literal matching safely."""
child = mock_pexpect["child"]
child.expect.return_value = 0
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
# (invalid is a malformed regex (missing closing paren), but matches literally
n.mylog = io.BytesIO(b"some (invalid text")
with patch.object(n, '_logclean', return_value="some (invalid text"):
result = n.test(["echo"], "(invalid")
assert isinstance(result, dict)
assert result.get("(invalid") == True
def test_test_expected_with_vars(self, mock_pexpect):
"""Expected output formats variables properly."""
child = mock_pexpect["child"]
child.expect.return_value = 0
from connpy.core import node
n = node("router1", "10.0.0.1", user="admin", password="")
with patch.object(n, '_connect', return_value=True):
n.child = child
n.mylog = io.BytesIO(b"Debian version 12")
with patch.object(n, '_logclean', return_value="Debian version 12"):
result = n.test(["echo"], "version {version_num}", vars={"version_num": "12"})
assert isinstance(result, dict)
assert result.get("version 12") == True
# =========================================================================
# nodes (parallel) tests
# =========================================================================
class TestNodes:
def test_nodes_init(self):
"""Creates list of node objects."""
from connpy.core import nodes
nodes_dict = {
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
}
mynodes = nodes(nodes_dict)
assert len(mynodes.nodelist) == 2
assert hasattr(mynodes, "r1")
assert hasattr(mynodes, "r2")
def test_nodes_run_parallel(self):
"""run() executes on all nodes and returns dict."""
from connpy.core import nodes
nodes_dict = {
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
}
mynodes = nodes(nodes_dict)
# Mock run on each node — must set output AND status on the node
for n in mynodes.nodelist:
original_node = n # capture by value
def make_mock(node_ref):
def mock_run(commands, **kwargs):
node_ref.output = f"output from {node_ref.unique}"
node_ref.status = 0
return mock_run
n.run = make_mock(n)
result = mynodes.run(["show version"])
assert "r1" in result
assert "r2" in result
def test_nodes_splitlist(self):
"""_splitlist divides list correctly."""
from connpy.core import nodes
mynodes = nodes({"r1": {"host": "1.1.1.1", "user": "", "password": ""}})
chunks = list(mynodes._splitlist([1, 2, 3, 4, 5], 2))
assert chunks == [[1, 2], [3, 4], [5]]
def test_nodes_run_with_vars(self):
"""Variables per node and __global__ work."""
from connpy.core import nodes
nodes_dict = {
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
}
mynodes = nodes(nodes_dict)
captured_vars = {}
def mock_run(commands, vars=None, **kwargs):
captured_vars.update(vars or {})
mynodes.r1.output = "ok"
mynodes.r1.status = 0
mynodes.r1.run = mock_run
variables = {
"__global__": {"mask": "255.255.255.0"},
"r1": {"ip": "10.0.0.1"}
}
mynodes.run(["show ip"], vars=variables)
assert captured_vars.get("mask") == "255.255.255.0"
assert captured_vars.get("ip") == "10.0.0.1"
def test_nodes_on_complete_callback(self):
"""on_complete callback fires per node."""
from connpy.core import nodes
nodes_dict = {
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
}
mynodes = nodes(nodes_dict)
completed = []
def mock_run(commands, **kwargs):
mynodes.r1.output = "done"
mynodes.r1.status = 0
mynodes.r1.run = mock_run
def on_done(unique, output, status):
completed.append(unique)
mynodes.run(["show version"], on_complete=on_done)
assert "r1" in completed
-55
View File
@@ -1,55 +0,0 @@
import pytest
from unittest.mock import MagicMock, patch
from connpy.services.execution_service import ExecutionService
def test_run_commands_callback(populated_config):
"""Test that run_commands correctly passes on_node_complete to the executor."""
service = ExecutionService(populated_config)
# Mock the Nodes class in connpy.services.execution_service
with patch("connpy.services.execution_service.Nodes") as MockNodes:
mock_executor = MockNodes.return_value
mock_executor.run.return_value = {"router1": "output"}
callback = MagicMock()
service.run_commands(
nodes_filter="router1",
commands=["show version"],
on_node_complete=callback
)
# Verify executor.run was called with on_complete=callback
# Note: ExecutionService calls executor.run(..., on_complete=on_node_complete, ...)
MockNodes.return_value.run.assert_called_once()
args, kwargs = MockNodes.return_value.run.call_args
assert kwargs["on_complete"] == callback
def test_test_commands_callback_regression(populated_config):
"""
Test that test_commands correctly passes on_node_complete to the executor.
Regression: ExecutionService.test_commands currently ignores on_node_complete.
"""
service = ExecutionService(populated_config)
with patch("connpy.services.execution_service.Nodes") as MockNodes:
mock_executor = MockNodes.return_value
mock_executor.test.return_value = {"router1": {"PASS": True}}
callback = MagicMock()
service.test_commands(
nodes_filter="router1",
commands=["show version"],
expected=["12.4"],
on_node_complete=callback
)
# This is expected to FAIL because ExecutionService.test_commands
# doesn't pass on_complete to executor.test
MockNodes.return_value.test.assert_called_once()
args, kwargs = MockNodes.return_value.test.call_args
# We expect 'on_complete' to be in kwargs and equal to our callback
assert "on_complete" in kwargs, "on_complete parameter missing in call to executor.test"
assert kwargs["on_complete"] == callback
-136
View File
@@ -1,136 +0,0 @@
"""
Tests for gRPC auth serialization/deserialization (engineer_auth, architect_auth, provider auth).
These tests verify that:
1. to_struct/from_struct round-trips correctly for auth dicts.
2. AIStub.ask() correctly serializes engineer_auth and architect_auth into AskRequest.
3. AIServicer.ask() correctly deserializes them and passes them to the service.
4. AIStub.configure_provider() serializes auth into ProviderRequest.
5. AIServicer.configure_provider() deserializes auth and forwards it to the service.
"""
import pytest
from unittest.mock import MagicMock, patch, call
from connpy.grpc_layer import connpy_pb2
from connpy.grpc_layer.utils import to_struct, from_struct
# --- Unit: Struct round-trip ---
class TestStructRoundTrip:
def test_simple_dict(self):
d = {"api_key": "secret", "region": "us-east-1"}
assert from_struct(to_struct(d)) == d
def test_nested_dict(self):
d = {"vertex_project": "my-project", "vertex_location": "us-central1", "nested": {"key": "val"}}
assert from_struct(to_struct(d)) == d
def test_empty_dict(self):
assert from_struct(to_struct({})) == {}
def test_none_returns_empty(self):
assert from_struct(to_struct(None)) == {}
# --- Unit: AskRequest Struct fields ---
class TestAskRequestStructFields:
def test_engineer_auth_round_trip(self):
auth = {"vertex_project": "proj", "vertex_location": "us-central1"}
req = connpy_pb2.AskRequest(input_text="hi")
req.engineer_auth.CopyFrom(to_struct(auth))
assert from_struct(req.engineer_auth) == auth
def test_architect_auth_round_trip(self):
auth = {"api_key": "sk-abc", "base_url": "https://custom.api/v1"}
req = connpy_pb2.AskRequest(input_text="hi")
req.architect_auth.CopyFrom(to_struct(auth))
assert from_struct(req.architect_auth) == auth
def test_has_field_false_when_unset(self):
req = connpy_pb2.AskRequest(input_text="hi")
assert not req.HasField("engineer_auth")
assert not req.HasField("architect_auth")
def test_has_field_true_when_set(self):
req = connpy_pb2.AskRequest(input_text="hi")
req.engineer_auth.CopyFrom(to_struct({"k": "v"}))
assert req.HasField("engineer_auth")
# --- Unit: ProviderRequest Struct field ---
class TestProviderRequestStructField:
def test_auth_round_trip(self):
auth = {"vertex_project": "proj", "vertex_location": "eu-west1"}
req = connpy_pb2.ProviderRequest(provider="vertex", model="gemini-pro")
req.auth.CopyFrom(to_struct(auth))
assert from_struct(req.auth) == auth
def test_has_field_false_when_unset(self):
req = connpy_pb2.ProviderRequest(provider="openai", model="gpt-4o")
assert not req.HasField("auth")
def test_has_field_true_when_set(self):
req = connpy_pb2.ProviderRequest(provider="vertex")
req.auth.CopyFrom(to_struct({"vertex_project": "p"}))
assert req.HasField("auth")
# --- Integration: Server deserializes auth and passes to service ---
class TestAIServicerAuthDeserialization:
@pytest.fixture
def servicer(self, populated_config):
from connpy.grpc_layer.server import AIServicer
return AIServicer(populated_config)
def test_configure_provider_passes_auth_to_service(self, servicer):
auth = {"vertex_project": "my-proj", "vertex_location": "us-central1"}
req = connpy_pb2.ProviderRequest(provider="vertex", model="gemini/gemini-pro", api_key="")
req.auth.CopyFrom(to_struct(auth))
with patch.object(servicer.service, "configure_provider") as mock_cp:
mock_context = MagicMock()
servicer.configure_provider(req, mock_context)
mock_cp.assert_called_once_with("vertex", "gemini/gemini-pro", "", auth=auth)
def test_configure_provider_no_auth(self, servicer):
req = connpy_pb2.ProviderRequest(provider="openai", model="gpt-4o", api_key="sk-test")
with patch.object(servicer.service, "configure_provider") as mock_cp:
mock_context = MagicMock()
servicer.configure_provider(req, mock_context)
mock_cp.assert_called_once_with("openai", "gpt-4o", "sk-test", auth=None)
# --- Integration: Stub serializes auth into request ---
class TestAIStubAuthSerialization:
@pytest.fixture
def ai_stub(self):
from connpy.grpc_layer.stubs import AIStub
mock_channel = MagicMock()
stub = AIStub(mock_channel, "localhost:8048")
return stub
def test_configure_provider_with_auth_serializes_struct(self, ai_stub):
auth = {"vertex_project": "proj", "vertex_location": "us-central1"}
ai_stub.stub.configure_provider = MagicMock()
ai_stub.configure_provider("vertex", model="gemini/gemini-pro", auth=auth)
ai_stub.stub.configure_provider.assert_called_once()
sent_req = ai_stub.stub.configure_provider.call_args[0][0]
assert sent_req.provider == "vertex"
assert sent_req.model == "gemini/gemini-pro"
assert sent_req.HasField("auth")
assert from_struct(sent_req.auth) == auth
def test_configure_provider_without_auth_no_struct(self, ai_stub):
ai_stub.stub.configure_provider = MagicMock()
ai_stub.configure_provider("openai", model="gpt-4o", api_key="sk-x")
sent_req = ai_stub.stub.configure_provider.call_args[0][0]
assert not sent_req.HasField("auth")
-213
View File
@@ -1,213 +0,0 @@
import pytest
import grpc
import json
import os
import threading
from unittest.mock import MagicMock, patch
from concurrent import futures
from connpy.grpc_layer import server, connpy_pb2, connpy_pb2_grpc, stubs
from connpy.services.exceptions import ConnpyError
class MockContext:
def abort(self, code, details):
raise Exception(f"gRPC Abort: {code} - {details}")
# --- UNIT TESTS (with mocks) ---
class TestNodeServicerNaming:
@pytest.fixture
def servicer(self, populated_config):
return server.NodeServicer(populated_config)
@patch("connpy.core.node")
def test_interact_node_uses_passed_name(self, mock_node, servicer):
# Setup request with custom name
params = {"name": "custom-node-name@test", "host": "1.2.3.4", "protocol": "ssh"}
request = connpy_pb2.InteractRequest(
id="dynamic",
connection_params_json=json.dumps(params)
)
# Mock node to allow _connect
mock_node_instance = MagicMock()
mock_node_instance._connect.return_value = True
mock_node.return_value = mock_node_instance
# We only need the first iteration of the generator to check naming
gen = servicer.interact_node(iter([request]), MockContext())
next(gen) # Skip the success response
# Verify that node() was called with the custom name
mock_node.assert_called()
found = False
for call in mock_node.call_args_list:
if call.args[0] == "custom-node-name@test":
found = True
break
assert found
@patch("connpy.core.node")
def test_interact_node_fallback_naming(self, mock_node, servicer):
# Setup request without custom name but with host
params = {"host": "my-instance", "protocol": "ssm"}
request = connpy_pb2.InteractRequest(
id="dynamic",
connection_params_json=json.dumps(params)
)
mock_node_instance = MagicMock()
mock_node_instance._connect.return_value = True
mock_node.return_value = mock_node_instance
gen = servicer.interact_node(iter([request]), MockContext())
next(gen)
# Verify fallback name: dynamic-{host}@remote
found = False
for call in mock_node.call_args_list:
if call.args[0] == "dynamic-my-instance@remote":
found = True
break
assert found
class TestStubsMessageFormatting:
@patch("termios.tcsetattr")
@patch("termios.tcgetattr")
@patch("tty.setraw")
@patch("os.read")
@patch("select.select")
def test_connect_dynamic_msg_formatting_ssm(self, mock_select, mock_read, mock_setraw, mock_getattr, mock_setattr):
from connpy.grpc_layer.stubs import NodeStub
mock_getattr.return_value = [0, 0, 0, 0, 0, 0, [0] * 32]
mock_channel = MagicMock()
stub = NodeStub(mock_channel, "localhost:8048")
mock_resp = MagicMock()
mock_resp.success = True
mock_resp.stdout_data = b''
stub.stub.interact_node.return_value = iter([mock_resp])
with patch("connpy.printer.success") as mock_success:
with patch("sys.stdin.fileno", return_value=0):
mock_select.return_value = ([], [], [])
params = {"protocol": "ssm", "host": "i-12345", "name": "my-ssm-node@aws"}
with patch("select.select", side_effect=KeyboardInterrupt):
try:
stub.connect_dynamic(params)
except KeyboardInterrupt:
pass
mock_success.assert_called()
msg = mock_success.call_args[0][0]
assert "Connected to my-ssm-node@aws" in msg
assert "at i-12345" in msg
assert ":22" not in msg
assert "via: ssm" in msg
# --- INTEGRATION TESTS (Real Server/Stub Communication) ---
class TestGRPCIntegration:
@pytest.fixture
def grpc_server(self, populated_config):
"""Starts a local gRPC server for integration testing."""
srv = grpc.server(futures.ThreadPoolExecutor(max_workers=5))
# Register services
connpy_pb2_grpc.add_NodeServiceServicer_to_server(server.NodeServicer(populated_config), srv)
connpy_pb2_grpc.add_ProfileServiceServicer_to_server(server.ProfileServicer(populated_config), srv)
connpy_pb2_grpc.add_ConfigServiceServicer_to_server(server.ConfigServicer(populated_config), srv)
connpy_pb2_grpc.add_ExecutionServiceServicer_to_server(server.ExecutionServicer(populated_config), srv)
connpy_pb2_grpc.add_ImportExportServiceServicer_to_server(server.ImportExportServicer(populated_config), srv)
connpy_pb2_grpc.add_AIServiceServicer_to_server(server.AIServicer(populated_config), srv)
port = srv.add_insecure_port('127.0.0.1:0')
srv.start()
yield f"127.0.0.1:{port}"
srv.stop(0)
@pytest.fixture
def channel(self, grpc_server):
with grpc.insecure_channel(grpc_server) as channel:
yield channel
@pytest.fixture
def node_stub(self, channel):
return stubs.NodeStub(channel, "localhost")
@pytest.fixture
def profile_stub(self, channel):
return stubs.ProfileStub(channel, "localhost")
@pytest.fixture
def config_stub(self, channel):
return stubs.ConfigStub(channel, "localhost")
@pytest.fixture
def ai_stub(self, channel):
return stubs.AIStub(channel, "localhost")
def test_list_nodes_integration(self, node_stub):
nodes = node_stub.list_nodes()
assert "router1" in nodes
assert "server1@office" in nodes
def test_get_node_details_integration(self, node_stub):
details = node_stub.get_node_details("router1")
assert details["host"] == "10.0.0.1"
def test_node_not_found_integration(self, node_stub):
with pytest.raises(ConnpyError) as exc:
node_stub.get_node_details("non-existent")
assert "Node 'non-existent' not found." in str(exc.value)
def test_list_profiles_integration(self, profile_stub):
profiles = profile_stub.list_profiles()
assert "office-user" in profiles
def test_get_settings_integration(self, config_stub):
settings = config_stub.get_settings()
assert "idletime" in settings
def test_update_setting_integration(self, config_stub):
config_stub.update_setting("idletime", 99)
settings = config_stub.get_settings()
assert settings["idletime"] == 99
def test_list_mcp_servers_integration(self, ai_stub):
ai_stub.configure_mcp("test-mcp", url="http://localhost:8080", enabled=True)
servers = ai_stub.list_mcp_servers()
assert "test-mcp" in servers
assert servers["test-mcp"]["url"] == "http://localhost:8080"
def test_add_delete_node_integration(self, node_stub):
node_stub.add_node("integration-test-node", {"host": "9.9.9.9"})
assert "integration-test-node" in node_stub.list_nodes()
node_stub.delete_node("integration-test-node")
assert "integration-test-node" not in node_stub.list_nodes()
def test_import_yaml_integration(self, channel, node_stub):
import yaml
from connpy.grpc_layer import stubs
stub = stubs.ImportExportStub(channel, "localhost")
# ImportExportService expects a flat dict of nodes, not a full config structure
inventory = {
"imported-node": {"host": "8.8.8.8", "protocol": "ssh", "type": "connection"}
}
yaml_content = yaml.dump(inventory)
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
f.write(yaml_content)
temp_path = f.name
try:
stub.import_from_file(temp_path)
# Verify the node was imported and is visible via NodeStub
nodes = node_stub.list_nodes()
assert "imported-node" in nodes
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
-216
View File
@@ -1,216 +0,0 @@
"""Tests for connpy.hooks module — MethodHook and ClassHook."""
import pytest
from connpy.hooks import MethodHook, ClassHook
# =========================================================================
# MethodHook Tests
# =========================================================================
class TestMethodHook:
def test_basic_call(self):
"""Decorated function executes normally."""
@MethodHook
def add(a, b):
return a + b
assert add(2, 3) == 5
def test_pre_hook_modifies_args(self):
"""Pre-hook can modify arguments before execution."""
@MethodHook
def greet(name):
return f"Hello {name}"
def uppercase_hook(name):
return (name.upper(),), {}
greet.register_pre_hook(uppercase_hook)
assert greet("world") == "Hello WORLD"
def test_post_hook_modifies_result(self):
"""Post-hook can modify the return value."""
@MethodHook
def compute(x):
return x * 2
def double_result(*args, **kwargs):
return kwargs["result"] * 2
compute.register_post_hook(double_result)
assert compute(5) == 20 # 5*2=10, then 10*2=20
def test_multiple_pre_hooks_order(self):
"""Pre-hooks execute in registration order."""
calls = []
@MethodHook
def func(x):
return x
def hook1(x):
calls.append("hook1")
return (x,), {}
def hook2(x):
calls.append("hook2")
return (x,), {}
func.register_pre_hook(hook1)
func.register_pre_hook(hook2)
func(1)
assert calls == ["hook1", "hook2"]
def test_multiple_post_hooks_order(self):
"""Post-hooks execute in registration order."""
calls = []
@MethodHook
def func(x):
return x
def hook1(*args, **kwargs):
calls.append("hook1")
return kwargs["result"]
def hook2(*args, **kwargs):
calls.append("hook2")
return kwargs["result"]
func.register_post_hook(hook1)
func.register_post_hook(hook2)
func(1)
assert calls == ["hook1", "hook2"]
def test_pre_hook_exception_continues(self, capsys):
"""If a pre-hook raises, the function still executes."""
@MethodHook
def func(x):
return x + 1
def bad_hook(x):
raise RuntimeError("broken hook")
func.register_pre_hook(bad_hook)
# Should not raise — the hook error is printed but execution continues
result = func(5)
assert result == 6
def test_post_hook_exception_continues(self, capsys):
"""If a post-hook raises, the result is still returned."""
@MethodHook
def func(x):
return x + 1
def bad_hook(*args, **kwargs):
raise RuntimeError("broken post hook")
func.register_post_hook(bad_hook)
result = func(5)
assert result == 6
def test_method_hook_as_instance_method(self):
"""MethodHook works as a descriptor on a class."""
class MyClass:
@MethodHook
def double(self, x):
return x * 2
obj = MyClass()
assert obj.double(5) == 10
def test_method_hook_instance_hook_registration(self):
"""Can register hooks via instance method access."""
class MyClass:
@MethodHook
def process(self, x):
return x
def add_ten(*args, **kwargs):
return kwargs["result"] + 10
obj = MyClass()
obj.process.register_post_hook(add_ten)
assert obj.process(5) == 15
# =========================================================================
# ClassHook Tests
# =========================================================================
class TestClassHook:
def test_creates_instance(self):
"""ClassHook still creates instances normally."""
@ClassHook
class MyClass:
def __init__(self, value):
self.value = value
obj = MyClass(42)
assert obj.value == 42
def test_modify_future_instances(self):
"""modify() affects all future instances."""
@ClassHook
class MyClass:
def __init__(self):
self.x = 1
def set_x_to_99(instance):
instance.x = 99
MyClass.modify(set_x_to_99)
obj = MyClass()
assert obj.x == 99
def test_modify_does_not_affect_past(self):
"""modify() does not affect already-created instances."""
@ClassHook
class MyClass:
def __init__(self):
self.x = 1
old_obj = MyClass()
def set_x_to_99(instance):
instance.x = 99
MyClass.modify(set_x_to_99)
assert old_obj.x == 1 # Not affected
assert MyClass().x == 99 # New instance IS affected
def test_instance_modify(self):
"""instance.modify() only affects that specific instance."""
@ClassHook
class MyClass:
def __init__(self):
self.x = 1
obj1 = MyClass()
obj2 = MyClass()
obj1.modify(lambda inst: setattr(inst, 'x', 999))
assert obj1.x == 999
assert obj2.x == 1
def test_multiple_deferred_hooks(self):
"""Multiple modify() calls apply in order."""
@ClassHook
class MyClass:
def __init__(self):
self.log = []
MyClass.modify(lambda inst: inst.log.append("first"))
MyClass.modify(lambda inst: inst.log.append("second"))
obj = MyClass()
assert obj.log == ["first", "second"]
def test_getattr_delegation(self):
"""ClassHook delegates attribute access to the wrapped class."""
@ClassHook
class MyClass:
class_var = "hello"
def __init__(self):
pass
assert MyClass.class_var == "hello"
-360
View File
@@ -1,360 +0,0 @@
import os
import pytest
import grpc
from concurrent import futures
from google.protobuf.empty_pb2 import Empty
from connpy.grpc_layer import server, connpy_pb2, connpy_pb2_grpc, stubs
from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider
from connpy.configfile import configfile
@pytest.fixture
def test_config_dir(tmp_path):
"""Creates a temporary config directory for testing gRPC auth."""
config_dir = tmp_path / "conn_config"
config_dir.mkdir()
# Initialize basic config file inside it
from connpy.configfile import configfile
conf_file = os.path.join(str(config_dir), "config.yaml")
configfile(conf=conf_file)
return config_dir
@pytest.fixture
def registry(test_config_dir):
"""Initializes UserRegistry."""
return UserRegistry(str(test_config_dir))
@pytest.fixture
def auth_grpc_server(test_config_dir, registry):
"""Starts an authenticated local gRPC server for integration testing."""
srv = grpc.server(
futures.ThreadPoolExecutor(max_workers=5),
interceptors=[server.AuthInterceptor(registry)]
)
fallback_provider = ServiceProvider(configfile(conf=os.path.join(str(test_config_dir), "config.yaml")), mode="local")
# Register services
connpy_pb2_grpc.add_NodeServiceServicer_to_server(server.NodeServicer(fallback_provider, registry=registry), srv)
connpy_pb2_grpc.add_AuthServiceServicer_to_server(server.AuthServicer(registry), srv)
port = srv.add_insecure_port('127.0.0.1:0')
srv.start()
yield f"127.0.0.1:{port}"
srv.stop(0)
@pytest.fixture
def channel(auth_grpc_server):
with grpc.insecure_channel(auth_grpc_server) as channel:
yield channel
class TestGRPCAuthentication:
def test_backward_compatibility_no_users(self, channel, registry):
"""Verifies that if no users are registered, gRPC calls proceed without authentication."""
assert registry.has_users() is False
# Calling NodeService list_nodes should succeed without any authorization metadata
stub = connpy_pb2_grpc.NodeServiceStub(channel)
req = connpy_pb2.FilterRequest()
res = stub.list_nodes(req)
assert res is not None
def test_login_and_authenticated_calls(self, channel, registry):
"""Tests user creation, login to retrieve JWT, and using JWT to access protected endpoints."""
username = "alice"
password = "alicepassword"
# 1. Register a user in the registry
registry.user_service.create_user(username, password)
assert registry.has_users() is True
# 2. Try unauthenticated call - must fail with UNAUTHENTICATED
node_stub = connpy_pb2_grpc.NodeServiceStub(channel)
req = connpy_pb2.FilterRequest()
with pytest.raises(grpc.RpcError) as exc:
node_stub.list_nodes(req)
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
assert "Authorization token is missing" in exc.value.details()
# 3. Call login endpoint (open method) - must succeed
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginRequest(username=username, password=password)
login_res = auth_stub.login(login_req)
assert login_res.username == username
assert isinstance(login_res.token, str)
assert login_res.expires_at > 0
# 4. Make authenticated call using Bearer token - must succeed
metadata = [("authorization", f"Bearer {login_res.token}")]
res = node_stub.list_nodes(req, metadata=metadata)
assert res is not None
def test_login_invalid_credentials(self, channel, registry):
"""Verifies login fails and returns UNAUTHENTICATED for incorrect credentials."""
registry.user_service.create_user("bob", "bobpass")
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginRequest(username="bob", password="wrongpassword")
with pytest.raises(grpc.RpcError) as exc:
auth_stub.login(login_req)
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
assert "Invalid username or password" in exc.value.details()
def test_change_password(self, channel, registry):
"""Tests changing password via gRPC and verifying old password no longer works."""
username = "charlie"
registry.user_service.create_user(username, "oldpass")
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
# 1. Login with old password to get token
login_res = auth_stub.login(connpy_pb2.LoginRequest(username=username, password="oldpass"))
token = login_res.token
# 2. Change password via gRPC using the token
metadata = [("authorization", f"Bearer {token}")]
change_req = connpy_pb2.ChangePasswordRequest(old_password="oldpass", new_password="newpass")
auth_stub.change_password(change_req, metadata=metadata)
# 3. Logging in with old password must fail
with pytest.raises(grpc.RpcError) as exc:
auth_stub.login(connpy_pb2.LoginRequest(username=username, password="oldpass"))
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
# 4. Logging in with new password must succeed
login_res_new = auth_stub.login(connpy_pb2.LoginRequest(username=username, password="newpass"))
assert login_res_new.token is not None
def test_sso_login_success_and_auto_provision(self, channel, registry):
"""Tests that a valid SSO token successfully logs the user in and auto-provisions their account."""
import jwt
# 1. Setup SSO configuration in the registry's shared config
registry._shared_config.config["sso"] = {
"providers": {
"authelia": {
"secret": "sso-shared-secret",
"username_claim": "preferred_username",
"algorithms": ["HS256"]
}
}
}
# 2. Check that the user 'ssoalice' does not exist yet
assert not any(u["username"] == "ssoalice" for u in registry.user_service.list_users())
# 3. Generate a valid SSO token signed with Authelia's secret
sso_token = jwt.encode(
{"preferred_username": "ssoalice"},
"sso-shared-secret",
algorithm="HS256"
)
# 4. Call login_sso
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="ssoalice",
id_token=sso_token,
provider="authelia"
)
login_res = auth_stub.login_sso(login_req)
assert login_res.username == "ssoalice"
assert isinstance(login_res.token, str)
assert login_res.expires_at > 0
# 5. Verify user 'ssoalice' was auto-created/provisioned
assert any(u["username"] == "ssoalice" for u in registry.user_service.list_users())
# 6. Make an authenticated call to NodeService list_nodes with the returned token
node_stub = connpy_pb2_grpc.NodeServiceStub(channel)
req = connpy_pb2.FilterRequest()
metadata = [("authorization", f"Bearer {login_res.token}")]
res = node_stub.list_nodes(req, metadata=metadata)
assert res is not None
def test_sso_login_invalid_signature(self, channel, registry):
"""Verifies that an SSO token with an invalid signature fails with UNAUTHENTICATED."""
import jwt
registry._shared_config.config["sso"] = {
"providers": {
"authelia": {
"secret": "sso-shared-secret",
"username_claim": "sub",
"algorithms": ["HS256"]
}
}
}
# Token signed with a WRONG key
wrong_token = jwt.encode({"sub": "bob"}, "wrong-secret", algorithm="HS256")
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="bob",
id_token=wrong_token,
provider="authelia"
)
with pytest.raises(grpc.RpcError) as exc:
auth_stub.login_sso(login_req)
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
assert "SSO Token validation failed" in exc.value.details()
def test_sso_login_mismatched_username(self, channel, registry):
"""Verifies that if the requested username doesn't match the token claim, it fails."""
import jwt
registry._shared_config.config["sso"] = {
"providers": {
"authelia": {
"secret": "sso-shared-secret",
"username_claim": "sub",
"algorithms": ["HS256"]
}
}
}
token = jwt.encode({"sub": "charlie"}, "sso-shared-secret", algorithm="HS256")
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="different_user",
id_token=token,
provider="authelia"
)
with pytest.raises(grpc.RpcError) as exc:
auth_stub.login_sso(login_req)
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
assert "Mismatched username" in exc.value.details()
def test_sso_login_allowed_domains_success(self, channel, registry):
"""Verifies that SSO login succeeds if email matches allowed_domains."""
import jwt
registry._shared_config.config["sso"] = {
"providers": {
"google": {
"secret": "google-secret",
"username_claim": "sub",
"algorithms": ["HS256"],
"allowed_domains": ["yyy.com", "other.org"]
}
}
}
token = jwt.encode(
{"sub": "john", "email": "john@yyy.com"},
"google-secret",
algorithm="HS256"
)
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="john",
id_token=token,
provider="google"
)
login_res = auth_stub.login_sso(login_req)
assert login_res.username == "john"
def test_sso_login_allowed_domains_failed(self, channel, registry):
"""Verifies that SSO login fails if email does not match allowed_domains."""
import jwt
registry._shared_config.config["sso"] = {
"providers": {
"google": {
"secret": "google-secret",
"username_claim": "sub",
"algorithms": ["HS256"],
"allowed_domains": ["yyy.com"]
}
}
}
token = jwt.encode(
{"sub": "john", "email": "john@attacker.com"},
"google-secret",
algorithm="HS256"
)
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="john",
id_token=token,
provider="google"
)
with pytest.raises(grpc.RpcError) as exc:
auth_stub.login_sso(login_req)
assert exc.value.code() == grpc.StatusCode.UNAUTHENTICATED
assert "SSO user domain 'attacker.com' not allowed" in exc.value.details()
def test_sso_login_allowed_domains_fallback_to_username(self, channel, registry):
"""Verifies allowed_domains validation falls back to username claim if email is not present."""
import jwt
registry._shared_config.config["sso"] = {
"providers": {
"google": {
"secret": "google-secret",
"username_claim": "sub",
"algorithms": ["HS256"],
"allowed_domains": ["yyy.com"]
}
}
}
token = jwt.encode(
{"sub": "john@yyy.com"},
"google-secret",
algorithm="HS256"
)
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_req = connpy_pb2.LoginSSORequest(
username="john",
id_token=token,
provider="google"
)
login_res = auth_stub.login_sso(login_req)
assert login_res.username == "john"
def test_login_and_login_sso_expiration_time(self, channel, registry):
"""Verifies expires_at is set to 12 hours in both login and login_sso."""
import jwt
import datetime
# 1. Test standard login expiration
registry.user_service.create_user("exp_user", "password123")
auth_stub = connpy_pb2_grpc.AuthServiceStub(channel)
login_res = auth_stub.login(connpy_pb2.LoginRequest(username="exp_user", password="password123"))
now = int(datetime.datetime.now(datetime.timezone.utc).timestamp())
expected_expires_12h = now + 12 * 3600
# Allow a 10s buffer for execution lag
assert abs(login_res.expires_at - expected_expires_12h) < 10
# 2. Test SSO login expiration
registry._shared_config.config["sso"] = {
"providers": {
"authelia": {
"secret": "sso-secret",
"username_claim": "sub",
"algorithms": ["HS256"]
}
}
}
token = jwt.encode({"sub": "sso_exp_user"}, "sso-secret", algorithm="HS256")
login_sso_res = auth_stub.login_sso(connpy_pb2.LoginSSORequest(
username="sso_exp_user",
id_token=token,
provider="authelia"
))
assert abs(login_sso_res.expires_at - expected_expires_12h) < 10
-66
View File
@@ -1,66 +0,0 @@
import pytest
from connpy.services.node_service import NodeService
from connpy.services.exceptions import NodeNotFoundError, NodeAlreadyExistsError
def test_list_nodes_filtering_parity(populated_config):
"""
Test that list_nodes uses literal 'in' logic instead of re.search.
Regression: NodeService currently uses re.search in some versions,
but we want to ensure it uses literal 'in' for parity.
"""
service = NodeService(populated_config)
# If it uses 'in' logic, '1' should match all nodes containing '1'
# router1, server1@office, db1@datacenter@office
nodes = service.list_nodes(filter_str="1")
assert len(nodes) == 3
assert "router1" in nodes
assert "server1@office" in nodes
assert "db1@datacenter@office" in nodes
# Test regex-specific characters.
# NodeService should use re.search, so '^router' will match 'router1'.
nodes_regex = service.list_nodes(filter_str="^router")
assert "router1" in nodes_regex
def test_list_nodes_dynamic_formatting(populated_config):
"""
Test that list_nodes supports dynamic formatting for any node attribute.
Regression: NodeService currently has hardcoded support for name, location, host.
"""
service = NodeService(populated_config)
# Try to format using 'user' and 'protocol' which are NOT in the hardcoded list
# (name, location, host)
format_str = "{name} -> {user}@{host} ({protocol})"
# router1: host=10.0.0.1, user=admin, protocol=ssh
# Expected: "router1 -> admin@10.0.0.1 (ssh)"
formatted = service.list_nodes(filter_str="router1", format_str=format_str)
assert len(formatted) == 1
# This will FAIL if it only supports {name}, {location}, {host}
assert formatted[0] == "router1 -> admin@10.0.0.1 (ssh)"
def test_node_editing_parity(populated_config):
"""
Test that add_node improperly raises NodeAlreadyExistsError when used for editing.
Regression: connapp._mod calls add_node instead of update_node.
"""
service = NodeService(populated_config)
# router1 already exists in populated_config
# We confirm that calling add_node with an existing ID raises NodeAlreadyExistsError
# which is why connapp._mod (which calls add_node) is currently broken for editing.
with pytest.raises(NodeAlreadyExistsError):
service.add_node("router1", {"host": "1.1.1.1"})
def test_list_nodes_case_sensitivity(populated_config):
"""Test that filtering respects the case setting in config."""
service = NodeService(populated_config)
# Default case is False (case-insensitive)
nodes = service.list_nodes(filter_str="ROUTER")
assert "router1" in nodes
-67
View File
@@ -1,67 +0,0 @@
import os
import pytest
from connpy.grpc_layer.server import NodeServicer, _current_user
from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider
@pytest.fixture
def test_config_dir(tmp_path):
"""Creates a temporary config directory for testing user registry."""
config_dir = tmp_path / "conn_config"
config_dir.mkdir()
return config_dir
@pytest.fixture
def registry(test_config_dir):
"""Initializes UserRegistry pointing to a temporary directory."""
return UserRegistry(str(test_config_dir))
def test_dynamic_routing_isolation(test_config_dir, registry):
"""Verifies that NodeServicer routes list_nodes to the correct user configuration based on _current_user ContextVar."""
# Setup fallback provider
from connpy.configfile import configfile
conf_file = os.path.join(registry.user_service.config_dir, "config.yaml")
config = configfile(conf=conf_file)
fallback_provider = ServiceProvider(config, mode="local")
# Create servicer with fallback and registry
servicer = NodeServicer(fallback_provider, registry=registry)
# Register two users
u1 = "user1"
u2 = "user2"
registry.user_service.create_user(u1, "pass1")
registry.user_service.create_user(u2, "pass2")
p1 = registry.get_provider(u1)
p2 = registry.get_provider(u2)
# Add nodes to each user's provider
p1.nodes.add_node("node-for-user-1", {"host": "1.1.1.1"})
p2.nodes.add_node("node-for-user-2", {"host": "2.2.2.2"})
# Verify fallback is empty
fallback_res = servicer.list_nodes(type('Request', (), {'filter_str': None, 'format_str': None})(), None)
from connpy.grpc_layer.utils import from_value
assert "node-for-user-1" not in from_value(fallback_res.data)
assert "node-for-user-2" not in from_value(fallback_res.data)
# Set context to User 1
t1 = _current_user.set(u1)
try:
res1 = servicer.list_nodes(type('Request', (), {'filter_str': None, 'format_str': None})(), None)
nodes1 = from_value(res1.data)
assert "node-for-user-1" in nodes1
assert "node-for-user-2" not in nodes1
finally:
_current_user.reset(t1)
# Set context to User 2
t2 = _current_user.set(u2)
try:
res2 = servicer.list_nodes(type('Request', (), {'filter_str': None, 'format_str': None})(), None)
nodes2 = from_value(res2.data)
assert "node-for-user-2" in nodes2
assert "node-for-user-1" not in nodes2
finally:
_current_user.reset(t2)
-327
View File
@@ -1,327 +0,0 @@
"""Tests for connpy.plugins module."""
import os
import textwrap
import pytest
from connpy.plugins import Plugins
# ---------------------------------------------------------------------------
# Helper: write a plugin script to a file
# ---------------------------------------------------------------------------
def _write_plugin(path, code):
"""Write dedented code to a file."""
with open(path, "w") as f:
f.write(textwrap.dedent(code))
# =========================================================================
# verify_script tests
# =========================================================================
class TestVerifyScript:
def test_valid_parser_entrypoint(self, tmp_path):
p = tmp_path / "good.py"
_write_plugin(p, """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser()
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
""")
plugins = Plugins()
assert plugins.verify_script(str(p)) == False
def test_valid_preload_only(self, tmp_path):
p = tmp_path / "preload.py"
_write_plugin(p, """\
class Preload:
def __init__(self, connapp):
pass
""")
plugins = Plugins()
assert plugins.verify_script(str(p)) == False
def test_valid_all_three(self, tmp_path):
p = tmp_path / "all.py"
_write_plugin(p, """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser()
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
class Preload:
def __init__(self, connapp):
pass
""")
plugins = Plugins()
assert plugins.verify_script(str(p)) == False
def test_parser_without_entrypoint(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser()
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result # Should be a truthy error string
assert "Entrypoint" in result
def test_entrypoint_without_parser(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "Parser" in result
def test_no_valid_class(self, tmp_path):
p = tmp_path / "empty.py"
_write_plugin(p, """\
def some_function():
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "No valid class" in result
def test_parser_missing_self_parser(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
class Parser:
def __init__(self):
self.something = "not parser"
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "self.parser" in result
def test_entrypoint_wrong_args(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser()
class Entrypoint:
def __init__(self, args):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "Entrypoint" in result
def test_preload_wrong_args(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
class Preload:
def __init__(self, connapp, extra):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "Preload" in result
def test_disallowed_top_level(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
MY_GLOBAL = "not allowed"
class Preload:
def __init__(self, connapp):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "not allowed" in result.lower() or "Plugin can only have" in result
def test_syntax_error(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
def broken(
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "Syntax error" in result
def test_if_name_main_allowed(self, tmp_path):
p = tmp_path / "good.py"
_write_plugin(p, """\
class Preload:
def __init__(self, connapp):
pass
if __name__ == "__main__":
print("standalone")
""")
plugins = Plugins()
assert plugins.verify_script(str(p)) == False
def test_other_if_not_allowed(self, tmp_path):
p = tmp_path / "bad.py"
_write_plugin(p, """\
import sys
if sys.platform == "linux":
pass
class Preload:
def __init__(self, connapp):
pass
""")
plugins = Plugins()
result = plugins.verify_script(str(p))
assert result
assert "__name__" in result
# =========================================================================
# Import and loading tests
# =========================================================================
class TestPluginLoading:
def test_import_from_path(self, tmp_path):
p = tmp_path / "mymod.py"
_write_plugin(p, """\
MY_VAR = 42
""")
plugins = Plugins()
module = plugins._import_from_path(str(p))
assert module.MY_VAR == 42
def test_import_plugins_to_argparse(self, tmp_path):
"""Valid plugins get loaded into argparse."""
import argparse
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
_write_plugin(plugin_dir / "myplugin.py", """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser(description="My plugin")
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
""")
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
plugins = Plugins()
plugins._import_plugins_to_argparse(str(plugin_dir), subparsers)
assert "myplugin" in plugins.plugins
assert "myplugin" in plugins.plugin_parsers
def test_plugin_name_collision(self, tmp_path):
"""Plugin with same name as existing subcommand is skipped."""
import argparse
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
_write_plugin(plugin_dir / "existcmd.py", """\
import argparse
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser()
class Entrypoint:
def __init__(self, args, parser, connapp):
pass
""")
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.add_parser("existcmd") # Already taken
plugins = Plugins()
plugins._import_plugins_to_argparse(str(plugin_dir), subparsers)
assert "existcmd" not in plugins.plugins
def test_preload_registration(self, tmp_path):
"""Preload class gets registered in preloads dict."""
import argparse
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
_write_plugin(plugin_dir / "preloader.py", """\
class Preload:
def __init__(self, connapp):
pass
""")
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
plugins = Plugins()
plugins._import_plugins_to_argparse(str(plugin_dir), subparsers)
assert "preloader" in plugins.preloads
def test_invalid_plugin_skipped(self, tmp_path, capsys):
"""Invalid plugin is skipped with error message."""
import argparse
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
_write_plugin(plugin_dir / "badplugin.py", """\
MY_GLOBAL = "bad"
""")
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
plugins = Plugins()
plugins._import_plugins_to_argparse(str(plugin_dir), subparsers)
assert "badplugin" not in plugins.plugins
captured = capsys.readouterr()
assert "Failed to load plugin" in captured.err or "Failed to load plugin" in captured.out
def test_empty_directory(self, tmp_path):
"""Empty directory doesn't cause errors."""
import argparse
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
plugins = Plugins()
plugins._import_plugins_to_argparse(str(plugin_dir), subparsers)
assert len(plugins.plugins) == 0
-104
View File
@@ -1,104 +0,0 @@
"""Tests for connpy.printer module."""
import sys
from io import StringIO
from connpy import printer
class TestPrinter:
def test_info_output(self, capsys):
printer.info("hello world")
captured = capsys.readouterr()
assert "[i] hello world" in captured.out
def test_success_output(self, capsys):
printer.success("done")
captured = capsys.readouterr()
assert "[✓] done" in captured.out
def test_warning_output(self, capsys):
printer.warning("careful")
captured = capsys.readouterr()
assert "[!] careful" in captured.out
def test_error_output(self, capsys):
printer.error("failed")
captured = capsys.readouterr()
assert "[✗] failed" in captured.err
def test_debug_output(self, capsys):
printer.debug("debug info")
captured = capsys.readouterr()
assert "[d] debug info" in captured.out
def test_start_output(self, capsys):
printer.start("starting")
captured = capsys.readouterr()
assert "[+] starting" in captured.out
def test_custom_output(self, capsys):
printer.custom("TAG", "custom message")
captured = capsys.readouterr()
assert "[TAG] custom message" in captured.out
def test_multiline_indentation(self, capsys):
printer.info("line1\nline2\nline3")
captured = capsys.readouterr()
lines = captured.out.strip().split("\n")
assert lines[0] == "[i] line1"
# Second line should be indented by len("[i] ") = 4 chars
assert lines[1].startswith(" line2")
assert lines[2].startswith(" line3")
def test_data_output(self, capsys):
printer.data("my title", "key: value")
captured = capsys.readouterr()
# Rich output is formatted with ansi escape sequences or box drawing chars
# Just check that title and content appear in the output stream
assert "my title" in captured.out
assert "key" in captured.out
def test_node_panel_pass(self, capsys):
printer.node_panel("node1", "output line\n", 0)
captured = capsys.readouterr()
assert "node1" in captured.out
assert "PASS" in captured.out
assert "output line" in captured.out
def test_node_panel_fail(self, capsys):
printer.node_panel("node2", "error line\n", 1)
captured = capsys.readouterr()
assert "node2" in captured.out
assert "FAIL" in captured.out
assert "error line" in captured.out
def test_test_panel(self, capsys):
printer.test_panel("node1", "output", 0, {"check1": True, "check2": False})
captured = capsys.readouterr()
assert "node1" in captured.out
assert "check1" in captured.out
assert "check2" in captured.out
def test_test_summary(self, capsys):
results = {"node1": {"test1": True}, "node2": {"test2": False}}
printer.test_summary(results)
captured = capsys.readouterr()
assert "node1" in captured.out
assert "node2" in captured.out
assert "test1" in captured.out
assert "test2" in captured.out
def test_header_output(self, capsys):
printer.header("My Header")
captured = capsys.readouterr()
assert "My Header" in captured.out
def test_kv_output(self, capsys):
printer.kv("mykeystring", "myvaluestring")
captured = capsys.readouterr()
assert "mykeystring" in captured.out
assert "myvaluestring" in captured.out
def test_confirm_action(self, capsys):
printer.confirm_action("router1", "delete")
captured = capsys.readouterr()
assert "[i] delete: router1" in captured.out
-65
View File
@@ -1,65 +0,0 @@
import threading
import io
import time
import sys
import pytest
from connpy import printer
def test_printer_thread_isolation():
"""Verify that printer output is isolated per thread when using set_thread_stream."""
num_threads = 5
iterations = 20
results = {}
def worker(thread_id):
# Create a private buffer for this thread
buf = io.StringIO()
printer.set_thread_stream(buf)
# Ensure we have a clean console for this thread
# In a real gRPC request, this happens automatically as it's a new thread
printer.set_thread_console(None)
# Each thread prints its own ID
expected_msg = f"Thread-{thread_id}"
for _ in range(iterations):
printer.info(expected_msg)
time.sleep(0.01)
results[thread_id] = buf.getvalue()
printer.set_thread_stream(None)
threads = []
for i in range(num_threads):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
# Validation
for thread_id, output in results.items():
expected_msg = f"Thread-{thread_id}"
assert expected_msg in output
# Ensure no leaks
for other_id in range(num_threads):
if other_id == thread_id: continue
assert f"Thread-{other_id}" not in output
def test_printer_manual_stream():
"""Verify that setting a thread stream correctly captures printer output in the current thread."""
buf = io.StringIO()
# We must clear the thread-local console to force it to pick up the new sys.stdout proxy
printer.set_thread_console(None)
printer.set_thread_stream(buf)
printer.info("Captured-Message")
output = buf.getvalue()
printer.set_thread_stream(None)
printer.set_thread_console(None)
assert "Captured-Message" in output
-83
View File
@@ -1,83 +0,0 @@
import pytest
from connpy.services.profile_service import ProfileService
from connpy.services.exceptions import ProfileNotFoundError, ProfileAlreadyExistsError
def test_profile_crud(populated_config):
"""Test basic CRUD operations for profiles."""
service = ProfileService(populated_config)
# List
profiles = service.list_profiles()
assert "default" in profiles
assert "office-user" in profiles
# Get
office = service.get_profile("office-user")
assert office["user"] == "officeadmin"
# Add
new_data = {
"user": "newadmin",
"password": "newpassword"
}
service.add_profile("new-profile", new_data)
assert "new-profile" in service.list_profiles()
assert service.get_profile("new-profile")["user"] == "newadmin"
# Update
update_data = {
"user": "updatedadmin"
}
service.update_profile("new-profile", update_data)
assert service.get_profile("new-profile")["user"] == "updatedadmin"
# Delete
service.delete_profile("new-profile")
assert "new-profile" not in service.list_profiles()
def test_profile_inheritance_parity(populated_config):
"""
Test that profiles can inherit from other profiles.
Regression: ProfileService currently doesn't resolve inheritance within profiles.
"""
service = ProfileService(populated_config)
# Create a profile that inherits from 'office-user'
# 'office-user' has user='officeadmin', password='officepass'
inherited_data = {
"user": "@office-user",
"options": "-v"
}
service.add_profile("inherited-profile", inherited_data)
# When we get the profile, we expect it to be resolved if inheritance is supported
# This is a common pattern in connpy for nodes, but should it work for profiles?
# The task mentions "profile CRUD and inheritance parity".
profile = service.get_profile("inherited-profile")
# If inheritance is resolved, user should be 'officeadmin'
# This is expected to FAIL if ProfileService just returns the raw dict.
assert profile["user"] == "officeadmin"
assert profile["password"] == "officepass"
assert profile["options"] == "-v"
def test_delete_default_profile_fails(populated_config):
"""Test that deleting the 'default' profile is prohibited."""
service = ProfileService(populated_config)
from connpy.services.exceptions import InvalidConfigurationError
with pytest.raises(InvalidConfigurationError, match="Cannot delete the 'default' profile"):
service.delete_profile("default")
def test_delete_used_profile_fails(populated_config):
"""Test that deleting a profile used by nodes is prohibited."""
service = ProfileService(populated_config)
from connpy.services.exceptions import InvalidConfigurationError
# In populated_config, we need to make sure a node uses a profile
# Let's add a node that uses 'office-user'
populated_config._connections_add(id="testnode", host="1.1.1.1", user="@office-user")
with pytest.raises(InvalidConfigurationError, match="is used by nodes"):
service.delete_profile("office-user")
-42
View File
@@ -1,42 +0,0 @@
import pytest
from unittest.mock import patch, MagicMock
from connpy.services.provider import ServiceProvider
def test_service_provider_local_mode():
config_mock = MagicMock()
with patch("connpy.services.provider.NodeService", create=True) as MockNodeService, \
patch("connpy.services.provider.ProfileService", create=True), \
patch("connpy.services.provider.ConfigService", create=True), \
patch("connpy.services.provider.PluginService", create=True), \
patch("connpy.services.provider.AIService", create=True), \
patch("connpy.services.provider.SystemService", create=True), \
patch("connpy.services.provider.ExecutionService", create=True), \
patch("connpy.services.provider.ImportExportService", create=True):
provider = ServiceProvider(config_mock, mode="local")
assert provider.mode == "local"
assert provider.config == config_mock
# Verify that an attribute was created
assert provider.nodes is not None
def test_service_provider_remote_mode():
config_mock = MagicMock()
with patch("connpy.services.provider.ConfigService", create=True) as MockConfigService, \
patch("grpc.insecure_channel", create=True) as MockChannel:
provider = ServiceProvider(config_mock, mode="remote", remote_host="localhost:50051")
# Verify ConfigService is initialized locally
assert provider.config_svc is not None
# Verify grpc channel was created
MockChannel.assert_called_once_with("localhost:50051")
# Verify a stub was assigned
assert provider.nodes is not None
def test_service_provider_unknown_mode():
config_mock = MagicMock()
with pytest.raises(ValueError, match="Unknown service mode: invalid_mode"):
ServiceProvider(config_mock, mode="invalid_mode")
-198
View File
@@ -1,198 +0,0 @@
import os
import shutil
import pytest
from connpy.configfile import configfile
from connpy.services.plugin_service import PluginService
from connpy.services.exceptions import InvalidConfigurationError
@pytest.fixture
def temp_plugins_env(tmp_path):
"""Creates a temporary isolated environment for core, shared, and user plugins."""
base_dir = tmp_path / "plugins_test_env"
base_dir.mkdir()
# Paths for shared config and user config folders
shared_dir = base_dir / "shared"
user_dir = base_dir / "user"
shared_dir.mkdir()
user_dir.mkdir()
# Create plugins subdirectories
(shared_dir / "plugins").mkdir()
(user_dir / "plugins").mkdir()
# Mock core_plugins path by creating a sibling folder
core_dir = base_dir / "core_plugins"
core_dir.mkdir()
# Config file paths
shared_path = os.path.join(shared_dir, "config.yaml")
user_path = os.path.join(user_dir, "config.yaml")
# Write empty config templates
import yaml
empty_conf = {"config": {}, "connections": {}, "profiles": {}}
with open(shared_path, "w") as f:
yaml.safe_dump(empty_conf, f)
with open(user_path, "w") as f:
yaml.safe_dump(empty_conf, f)
return {
"shared_dir": shared_dir,
"user_dir": user_dir,
"core_dir": core_dir,
"shared_path": shared_path,
"user_path": user_path
}
def test_plugin_resolution_priority_merge(temp_plugins_env, monkeypatch):
"""Test that list_plugins correctly merges core, shared, and user plugins with overrides."""
env = temp_plugins_env
# 1. Create a core plugin: 'coreplug'
core_file = env["core_dir"] / "coreplug.py"
with open(core_file, "w") as f:
f.write("# core plugin content")
# 2. Create a shared plugin: 'sharedplug'
shared_file = env["shared_dir"] / "plugins" / "sharedplug.py"
with open(shared_file, "w") as f:
f.write("# shared plugin content")
# 3. Create a user plugin: 'userplug'
user_file = env["user_dir"] / "plugins" / "userplug.py"
with open(user_file, "w") as f:
f.write("# user plugin content")
# 4. Create an override plugin: 'overrideplug' in all three directories
with open(env["core_dir"] / "overrideplug.py", "w") as f:
f.write("# core override version")
with open(env["shared_dir"] / "plugins" / "overrideplug.py", "w") as f:
f.write("# shared override version")
with open(env["user_dir"] / "plugins" / "overrideplug.py", "w") as f:
f.write("# user override version")
# Initialize configs
shared_cfg = configfile(conf=env["shared_path"])
user_cfg = configfile(conf=env["user_path"], shared_config=shared_cfg)
# Initialize service
plugin_svc = PluginService(user_cfg)
# Monkeypatch the core plugins folder path inside list_plugins
# in order to use our mock core folder instead of the real one.
# Note: real path is computed via __file__, so we'll mock the internal core path
monkeypatch.setattr(
"os.path.realpath",
lambda path: os.path.join(str(env["core_dir"]), "dummy")
)
plugins_list = plugin_svc.list_plugins()
# Verify all plugins are registered
assert "coreplug" in plugins_list
assert "sharedplug" in plugins_list
assert "userplug" in plugins_list
assert "overrideplug" in plugins_list
# Verify status is Active (enabled=True)
assert plugins_list["coreplug"]["enabled"] is True
assert plugins_list["sharedplug"]["enabled"] is True
assert plugins_list["userplug"]["enabled"] is True
assert plugins_list["overrideplug"]["enabled"] is True
# Verify hashes differ matching user overrides
import hashlib
user_override_hash = hashlib.md5(b"# user override version").hexdigest()
assert plugins_list["overrideplug"]["hash"] == user_override_hash
def test_get_plugin_source_override(temp_plugins_env, monkeypatch):
"""Test that get_plugin_source resolves the highest priority plugin version."""
env = temp_plugins_env
# Create override in shared and user
with open(env["shared_dir"] / "plugins" / "myplug.py", "w") as f:
f.write("shared content")
with open(env["user_dir"] / "plugins" / "myplug.py", "w") as f:
f.write("user override")
shared_cfg = configfile(conf=env["shared_path"])
user_cfg = configfile(conf=env["user_path"], shared_config=shared_cfg)
plugin_svc = PluginService(user_cfg)
# Fetch source
source = plugin_svc.get_plugin_source("myplug")
assert source == "user override"
def test_delete_plugin_restrictions(temp_plugins_env):
"""Test that deleting shared plugins is rejected, but deleting user overrides works."""
env = temp_plugins_env
# Create shared plugin
with open(env["shared_dir"] / "plugins" / "globalplug.py", "w") as f:
f.write("global content")
# Create user plugin override
with open(env["user_dir"] / "plugins" / "globalplug.py", "w") as f:
f.write("user content")
shared_cfg = configfile(conf=env["shared_path"])
user_cfg = configfile(conf=env["user_path"], shared_config=shared_cfg)
plugin_svc = PluginService(user_cfg)
# 1. Delete plugin (should delete the user override first)
plugin_svc.delete_plugin("globalplug")
# Verify user override is gone, but shared plugin remains
assert not os.path.exists(env["user_dir"] / "plugins" / "globalplug.py")
assert os.path.exists(env["shared_dir"] / "plugins" / "globalplug.py")
# 2. Try to delete again (now only exists in shared/global folder)
with pytest.raises(InvalidConfigurationError) as exc:
plugin_svc.delete_plugin("globalplug")
assert "Global and core plugins are read-only" in str(exc.value)
# Verify shared plugin is still present
assert os.path.exists(env["shared_dir"] / "plugins" / "globalplug.py")
def test_shadow_disable_and_enable_mechanisms(temp_plugins_env):
"""Test that disabling a shared plugin creates a shadow backup file and enabling it removes it."""
env = temp_plugins_env
# Create a shared plugin
with open(env["shared_dir"] / "plugins" / "sharedplug.py", "w") as f:
f.write("shared content")
shared_cfg = configfile(conf=env["shared_path"])
user_cfg = configfile(conf=env["user_path"], shared_config=shared_cfg)
plugin_svc = PluginService(user_cfg)
# Ensure it's active initially
list_initial = plugin_svc.list_plugins()
assert list_initial["sharedplug"]["enabled"] is True
# 1. Disable the shared plugin (should shadow-disable it in user dir)
res = plugin_svc.disable_plugin("sharedplug")
assert res is True
# Verify shadow bkp file exists in user plugins and has 0 bytes
shadow_bkp = env["user_dir"] / "plugins" / "sharedplug.py.bkp"
assert os.path.exists(shadow_bkp)
assert os.path.getsize(shadow_bkp) == 0
# Verify list_plugins lists it as disabled
list_disabled = plugin_svc.list_plugins()
assert list_disabled["sharedplug"]["enabled"] is False
# 2. Re-enable the shadow-disabled plugin (should delete the user shadow file)
res_enable = plugin_svc.enable_plugin("sharedplug")
assert res_enable is True
# Verify shadow file is deleted
assert not os.path.exists(shadow_bkp)
# Verify list_plugins lists it as active again
list_active = plugin_svc.list_plugins()
assert list_active["sharedplug"]["enabled"] is True
-296
View File
@@ -1,296 +0,0 @@
import pytest
import json
from unittest.mock import patch, MagicMock
from connpy.ai import PlaybookBuilderAgent
from connpy.services.ai_service import AIService
# =========================================================================
# PlaybookBuilderAgent validation tests
# =========================================================================
def test_validate_playbook_valid(ai_config):
"""Verifies that a valid canonical tasks[] playbook passes validation."""
agent = PlaybookBuilderAgent(ai_config)
valid_yaml = """
tasks:
- name: "Apply standard config"
action: "run"
nodes: "router1"
commands:
- "conf t"
- "end"
output: "stdout"
- name: "Verify connectivity"
action: "test"
nodes: "router1"
commands:
- "ping 10.0.0.1"
expected: "!"
output: "stdout"
"""
res = agent.validate_playbook(valid_yaml)
assert res["valid"] is True
assert "valid" in res["message"].lower()
def test_validate_playbook_invalid_yaml(ai_config):
"""Verifies that syntax errors in YAML are caught and reported."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
tasks:
- name: "Broken task"
action: "run
nodes: "router1"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "syntax error" in res["error"].lower()
def test_validate_playbook_missing_tasks_key(ai_config):
"""Verifies that a playbook without tasks root key is invalid."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
not_tasks:
- name: "Apply standard config"
action: "run"
nodes: "router1"
commands:
- "conf t"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "missing mandatory root 'tasks' key" in res["error"].lower()
def test_validate_playbook_missing_mandatory_fields(ai_config):
"""Verifies that missing name, action, nodes, commands, or output triggers a validation failure."""
agent = PlaybookBuilderAgent(ai_config)
# Missing nodes
invalid_yaml = """
tasks:
- name: "Apply standard config"
action: "run"
commands:
- "conf t"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "missing mandatory fields" in res["error"].lower()
assert "nodes" in res["error"]
def test_validate_playbook_invalid_action(ai_config):
"""Verifies that an unsupported action type is caught."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
tasks:
- name: "Apply standard config"
action: "delete_everything"
nodes: "router1"
commands:
- "conf t"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "invalid action" in res["error"].lower()
def test_validate_playbook_missing_expected_in_test(ai_config):
"""Verifies that action 'test' requires the expected field."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
tasks:
- name: "Apply standard config"
action: "test"
nodes: "router1"
commands:
- "ping 10.0.0.1"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "missing the mandatory 'expected' key" in res["error"].lower()
def test_validate_playbook_invalid_nodes_type(ai_config):
"""Verifies that nodes of invalid type (e.g. integer) is caught."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
tasks:
- name: "Apply config"
action: "run"
nodes: 12345
commands:
- "conf t"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "nodes' must be a string (regex) or a list of strings (regexes)" in res["error"]
def test_validate_playbook_invalid_nodes_list_item(ai_config):
"""Verifies that nodes list containing non-string items is caught."""
agent = PlaybookBuilderAgent(ai_config)
invalid_yaml = """
tasks:
- name: "Apply config"
action: "run"
nodes:
- "router1"
- 9999
commands:
- "conf t"
output: "stdout"
"""
res = agent.validate_playbook(invalid_yaml)
assert res["valid"] is False
assert "list contains a non-string value" in res["error"]
# =========================================================================
# AIService new methods delegation tests
# =========================================================================
def test_build_playbook_chat_delegation(ai_config):
"""Verifies that build_playbook_chat instantiates PlaybookBuilderAgent and delegates ask."""
service = AIService(ai_config)
with patch("connpy.ai.PlaybookBuilderAgent") as MockAgentClass:
mock_agent = MockAgentClass.return_value
mock_agent.ask.return_value = {"response": "Mock response", "chat_history": []}
history = [{"role": "user", "content": "build playbook"}]
res = service.build_playbook_chat("help me", chat_history=history)
MockAgentClass.assert_called_once_with(ai_config)
mock_agent.ask.assert_called_once_with("help me", chat_history=history, status=None, chunk_callback=None)
assert res["response"] == "Mock response"
def test_analyze_execution_results_delegation(ai_config):
"""Verifies that analyze_execution_results formats prompt with @architect and delegates to self.ask."""
service = AIService(ai_config)
service.ask = MagicMock()
results = {"router1": {"output": "success", "status": 0}}
service.analyze_execution_results(results, query="diagnose border")
service.ask.assert_called_once()
args, kwargs = service.ask.call_args
prompt = args[0]
assert prompt.startswith("@architect:")
assert "diagnose border" in prompt
assert "Results Data:" in prompt
assert "router1" in prompt
assert kwargs.get("one_shot") is True
def test_predict_execution_results_delegation(ai_config):
"""Verifies that predict_execution_results formats prompt with @engineer and delegates to self.ask."""
service = AIService(ai_config)
service.ask = MagicMock()
nodes = ["router1", "router2"]
commands = ["conf t", "interface lo0"]
service.predict_execution_results(nodes, commands)
service.ask.assert_called_once()
args, kwargs = service.ask.call_args
prompt = args[0]
assert prompt.startswith("@engineer:")
assert "Preflight Simulation Agent" in prompt
assert "router1, router2" in prompt
assert "conf t" in prompt
assert "interface lo0" in prompt
# =========================================================================
# gRPC Integration Tests for AIService
# =========================================================================
import grpc
from concurrent import futures
from connpy.grpc_layer import server, connpy_pb2, connpy_pb2_grpc, stubs
class TestGRPCAIIntegration:
@pytest.fixture
def grpc_server(self, populated_config):
"""Starts a local gRPC server for IA integration testing."""
srv = grpc.server(futures.ThreadPoolExecutor(max_workers=5))
connpy_pb2_grpc.add_AIServiceServicer_to_server(server.ServerServicer(populated_config).ai if hasattr(server, 'ServerServicer') else server.AIServicer(populated_config), srv)
port = srv.add_insecure_port('127.0.0.1:0')
srv.start()
yield f"127.0.0.1:{port}"
srv.stop(0)
@pytest.fixture
def channel(self, grpc_server):
with grpc.insecure_channel(grpc_server) as channel:
yield channel
@pytest.fixture
def ai_stub(self, channel):
return stubs.AIStub(channel, "localhost")
def test_build_playbook_chat_grpc(self, ai_stub, populated_config):
"""Verifies that build_playbook_chat gRPC stream functions correctly."""
# Mock PlaybookBuilderAgent.ask to simulate agent response stream
def mock_ask(user_input, chat_history=None, status=None, debug=False, chunk_callback=None):
if chunk_callback:
chunk_callback("Generated Tasks:\n- name: config")
return {"response": "Done", "playbook_yaml": "tasks:\n- name: config"}
with patch("connpy.ai.PlaybookBuilderAgent.ask", side_effect=mock_ask):
chunks = []
def callback(chunk):
chunks.append(chunk)
res = ai_stub.build_playbook_chat("make playbook", chunk_callback=callback)
assert "tasks:" in res["playbook_yaml"]
assert len(chunks) > 0
assert "Generated Tasks:" in chunks[0]
def test_analyze_execution_results_grpc(self, ai_stub, populated_config):
"""Verifies that analyze_execution_results gRPC stream functions correctly."""
# Mock AIService.ask to simulate response stream
def mock_ask(prompt, status=None, debug=False, chunk_callback=None, **kwargs):
if chunk_callback:
chunk_callback("Results are optimal.")
return {"response": "Done"}
with patch.object(AIService, "ask", side_effect=mock_ask):
chunks = []
def callback(chunk):
chunks.append(chunk)
res = ai_stub.analyze_execution_results({"r1": "ok"}, query="test query", chunk_callback=callback)
assert res is not None
assert len(chunks) > 0
assert "optimal" in chunks[0]
def test_predict_execution_results_grpc(self, ai_stub, populated_config):
"""Verifies that predict_execution_results gRPC stream functions correctly."""
# Mock AIService.ask to simulate response stream
def mock_ask(prompt, status=None, debug=False, chunk_callback=None, **kwargs):
if chunk_callback:
chunk_callback("Commands are safe.")
return {"response": "Done"}
with patch.object(AIService, "ask", side_effect=mock_ask):
chunks = []
def callback(chunk):
chunks.append(chunk)
res = ai_stub.predict_execution_results(["r1"], ["show version"], chunk_callback=callback)
assert res is not None
assert len(chunks) > 0
assert "safe" in chunks[0]
-217
View File
@@ -1,217 +0,0 @@
import os
import time
import pytest
import yaml
from connpy.configfile import configfile
from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider
@pytest.fixture
def temp_config_dir(tmp_path):
"""Creates a temporary config directory for testing."""
config_dir = tmp_path / "conn_shared_test"
config_dir.mkdir()
return config_dir
def test_shared_ai_deep_merge(temp_config_dir):
"""Test get_effective_setting deep merge logic for 'ai' settings."""
shared_dir = os.path.join(temp_config_dir, "shared")
user_dir = os.path.join(temp_config_dir, "user")
os.makedirs(shared_dir, exist_ok=True)
os.makedirs(user_dir, exist_ok=True)
shared_path = os.path.join(shared_dir, "config.yaml")
user_path = os.path.join(user_dir, "config.yaml")
# Write shared configuration
shared_data = {
"config": {
"theme": "dark",
"case": False,
"ai": {
"engineer_model": "shared-eng-model",
"architect_model": "shared-arch-model",
"engineer_api_key": "shared-key",
"mcp_servers": {
"global-server": {
"url": "http://global-server/sse",
"enabled": True
},
"override-server": {
"url": "http://override-shared/sse",
"enabled": True
}
}
}
},
"connections": {},
"profiles": {}
}
with open(shared_path, "w") as f:
yaml.safe_dump(shared_data, f)
# Write user configuration with overrides
user_data = {
"config": {
"case": True,
"ai": {
"engineer_model": "user-custom-eng-model",
"mcp_servers": {
"override-server": {
"enabled": False
},
"user-server": {
"url": "http://user-server/sse",
"enabled": True
}
}
}
},
"connections": {},
"profiles": {}
}
with open(user_path, "w") as f:
yaml.safe_dump(user_data, f)
# Initialize configfile instances
shared_config = configfile(conf=shared_path)
user_config = configfile(conf=user_path, shared_config=shared_config)
# Verify non-inheritable settings (theme, case)
assert user_config.get_effective_setting("case") is True
assert user_config.get_effective_setting("theme") is None # Should NOT inherit "theme"
# Verify AI setting deep merge
effective_ai = user_config.get_effective_setting("ai")
# Model override
assert effective_ai.get("engineer_model") == "user-custom-eng-model"
# Model inheritance
assert effective_ai.get("architect_model") == "shared-arch-model"
# API key inheritance
assert effective_ai.get("engineer_api_key") == "shared-key"
# MCP Servers merge
mcp = effective_ai.get("mcp_servers", {})
# Inherited server
assert "global-server" in mcp
assert mcp["global-server"]["url"] == "http://global-server/sse"
assert mcp["global-server"]["enabled"] is True
# Merged & overridden server
assert "override-server" in mcp
assert mcp["override-server"]["url"] == "http://override-shared/sse" # inherited
assert mcp["override-server"]["enabled"] is False # overridden
# User-only server
assert "user-server" in mcp
assert mcp["user-server"]["url"] == "http://user-server/sse"
def test_registry_injection_and_hot_reload(temp_config_dir):
"""Test that UserRegistry correctly injects shared config and hot-reloads it when it changes on disk."""
registry = UserRegistry(str(temp_config_dir))
# Define paths
shared_path = os.path.join(temp_config_dir, "config.yaml")
# 1. Create a global config file
global_data = {
"config": {
"ai": {
"engineer_api_key": "global-initial-key",
"engineer_model": "global-model"
}
},
"connections": {},
"profiles": {}
}
with open(shared_path, "w") as f:
yaml.safe_dump(global_data, f)
# Re-init registry to pick up the newly created shared config file
registry = UserRegistry(str(temp_config_dir))
# Register user
username = "testuser"
registry.user_service.create_user(username, "testpassword")
# Check initial injection
provider = registry.get_provider(username)
ai_settings = provider.config.get_effective_setting("ai")
assert ai_settings.get("engineer_api_key") == "global-initial-key"
assert ai_settings.get("engineer_model") == "global-model"
# 2. Modify global config on disk
global_data["config"]["ai"]["engineer_api_key"] = "global-updated-key"
# Sleep briefly to ensure mtime change is detectable
time.sleep(0.1)
with open(shared_path, "w") as f:
yaml.safe_dump(global_data, f)
# Set the mtime forward explicitly to avoid filesystem resolution limits
new_mtime = os.path.getmtime(shared_path) + 10.0
os.utime(shared_path, (new_mtime, new_mtime))
# Retrieve provider again - should trigger hot-reload of shared config
provider2 = registry.get_provider(username)
ai_settings_updated = provider2.config.get_effective_setting("ai")
assert ai_settings_updated.get("engineer_api_key") == "global-updated-key"
assert ai_settings_updated.get("engineer_model") == "global-model"
def test_shared_ai_credential_isolation(temp_config_dir):
"""Test that setting user engineer/architect credentials discards corresponding shared credentials."""
shared_dir = os.path.join(temp_config_dir, "shared_isolation")
user_dir = os.path.join(temp_config_dir, "user_isolation")
os.makedirs(shared_dir, exist_ok=True)
os.makedirs(user_dir, exist_ok=True)
shared_path = os.path.join(shared_dir, "config.yaml")
user_path = os.path.join(user_dir, "config.yaml")
# Shared has both api_key and auth
shared_data = {
"config": {
"ai": {
"engineer_api_key": "global-initial-key",
"engineer_auth": {"vertex_project": "shared-project", "api_key": "shared-auth-key"},
"architect_api_key": "global-arch-key",
"architect_auth": {"project": "arch-project"}
}
},
"connections": {},
"profiles": {}
}
with open(shared_path, "w") as f:
yaml.safe_dump(shared_data, f)
# User configures ONLY engineer_api_key (expects engineer_auth to be discarded)
# and ONLY architect_auth (expects architect_api_key to be discarded)
user_data = {
"config": {
"ai": {
"engineer_api_key": "user-custom-key",
"architect_auth": {"project": "user-project", "api_key": "user-auth-key"}
}
},
"connections": {},
"profiles": {}
}
with open(user_path, "w") as f:
yaml.safe_dump(user_data, f)
shared_config = configfile(conf=shared_path)
user_config = configfile(conf=user_path, shared_config=shared_config)
effective_ai = user_config.get_effective_setting("ai")
# 1. Engineer: local api_key is present, so shared engineer_auth must be completely discarded
assert effective_ai.get("engineer_api_key") == "user-custom-key"
assert "engineer_auth" not in effective_ai
# 2. Architect: local auth is present, so shared architect_api_key must be completely discarded
assert effective_ai.get("architect_auth") == {"project": "user-project", "api_key": "user-auth-key"}
assert "architect_api_key" not in effective_ai
-103
View File
@@ -1,103 +0,0 @@
"""Tests for connpy.services.sync_service"""
import pytest
import os
from unittest.mock import MagicMock, patch
from connpy.services.sync_service import SyncService
@pytest.fixture
def mock_config():
config = MagicMock()
config.defaultdir = "/fake/dir"
config.file = "/fake/dir/config.yaml"
config.key = "/fake/dir/.osk"
config.cachefile = "/fake/dir/.cache"
config.fzf_cachefile = "/fake/dir/.fzf_cache"
config.config = {"sync": True, "sync_remote": False}
return config
class TestSyncService:
def test_init(self, mock_config):
s = SyncService(mock_config)
assert s.sync_enabled is True
assert s.token_file == os.path.join("/fake/dir", "gtoken.json")
@patch("connpy.services.sync_service.os.path.exists")
@patch("connpy.services.sync_service.Credentials")
def test_get_credentials_success(self, MockCreds, mock_exists, mock_config):
mock_exists.return_value = True
mock_cred_instance = MagicMock()
mock_cred_instance.valid = True
MockCreds.from_authorized_user_file.return_value = mock_cred_instance
s = SyncService(mock_config)
creds = s.get_credentials()
assert creds == mock_cred_instance
@patch("connpy.services.sync_service.os.path.exists")
def test_get_credentials_not_found(self, mock_exists, mock_config):
mock_exists.return_value = False
s = SyncService(mock_config)
assert s.get_credentials() is None
@patch("connpy.services.sync_service.zipfile.ZipFile")
@patch("connpy.services.sync_service.os.path.exists")
@patch("connpy.services.sync_service.os.path.basename")
def test_compress_and_upload_local(self, mock_basename, mock_exists, MockZipFile, mock_config):
mock_basename.return_value = "config.yaml"
mock_exists.return_value = True
s = SyncService(mock_config)
# Mocking list_backups and upload_file to avoid real API calls
s.list_backups = MagicMock(return_value=[])
s.upload_file = MagicMock(return_value=True)
zip_mock = MagicMock()
MockZipFile.return_value.__enter__.return_value = zip_mock
s.compress_and_upload()
# Verify zip was created with local config and key
zip_mock.write.assert_any_call(s.config.file, "config.yaml")
zip_mock.write.assert_any_call(s.config.key, ".osk")
@patch("connpy.services.sync_service.zipfile.ZipFile")
@patch("connpy.services.sync_service.os.path.exists")
@patch("connpy.services.sync_service.os.path.dirname")
@patch("connpy.services.sync_service.os.remove")
def test_perform_restore(self, mock_remove, mock_dirname, mock_exists, MockZipFile, mock_config):
mock_dirname.return_value = "/fake/dir"
# Mock exists to return True for key and zip, but False for caches during the cleanup phase
def exists_side_effect(path):
if ".cache" in path or ".fzf_cache" in path:
return False
return True
mock_exists.side_effect = exists_side_effect
s = SyncService(mock_config)
zip_mock = MagicMock()
zip_mock.namelist.return_value = ["config.yaml", ".osk"]
MockZipFile.return_value.__enter__.return_value = zip_mock
with patch("connpy.services.sync_service.yaml.safe_load") as mock_load:
mock_load.return_value = {"connections": {}, "profiles": {}, "config": {}}
assert s.perform_restore("/fake/zip.zip") is True
zip_mock.extract.assert_any_call(".osk", "/fake/dir")
@patch.object(SyncService, "get_credentials")
@patch("connpy.services.sync_service.build")
def test_list_backups(self, mock_build, mock_get_credentials, mock_config):
mock_get_credentials.return_value = MagicMock()
mock_service = MagicMock()
mock_build.return_value = mock_service
mock_service.files().list().execute.return_value = {
"files": [
{"id": "1", "name": "backup1.zip", "appProperties": {"timestamp": "1000", "date": "2024"}}
]
}
s = SyncService(mock_config)
files = s.list_backups()
assert len(files) == 1
assert files[0]["id"] == "1"
assert files[0]["timestamp"] == "1000"
-134
View File
@@ -1,134 +0,0 @@
import os
import pytest
from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider
@pytest.fixture
def test_config_dir(tmp_path):
"""Creates a temporary config directory for testing user registry."""
config_dir = tmp_path / "conn_config"
config_dir.mkdir()
return config_dir
@pytest.fixture
def registry(test_config_dir):
"""Initializes UserRegistry pointing to a temporary directory."""
return UserRegistry(str(test_config_dir))
class TestUserRegistry:
def test_has_users_empty(self, registry):
"""Verifies has_users is False when no users exist."""
assert registry.has_users() is False
def test_get_provider_returns_service_provider(self, registry):
"""Tests that get_provider lazy-loads a valid ServiceProvider instance."""
username = "alice"
registry.user_service.create_user(username, "password")
assert registry.has_users() is True
provider = registry.get_provider(username)
assert isinstance(provider, ServiceProvider)
assert provider.mode == "local"
def test_get_provider_cached(self, registry):
"""Verifies that subsequent calls return the cached singleton instance."""
username = "bob"
registry.user_service.create_user(username, "password")
p1 = registry.get_provider(username)
p2 = registry.get_provider(username)
assert p1 is p2 # must be exact same object reference
def test_two_users_isolated(self, registry):
"""Ensures different users get completely separate ServiceProviders and configs."""
u1 = "user1"
u2 = "user2"
registry.user_service.create_user(u1, "pass1")
registry.user_service.create_user(u2, "pass2")
p1 = registry.get_provider(u1)
p2 = registry.get_provider(u2)
assert p1 is not p2
assert p1.config is not p2.config
# Add a node for user1 and verify user2 is unaffected
p1.nodes.add_node("node1", {"host": "1.1.1.1"})
assert "node1" in p1.nodes.list_nodes()
assert "node1" not in p2.nodes.list_nodes()
def test_evict_clears_cache(self, registry):
"""Verifies that eviction deletes the cached provider from memory."""
username = "evictuser"
registry.user_service.create_user(username, "pass")
p1 = registry.get_provider(username)
assert username in registry._providers
registry.evict(username)
assert username not in registry._providers
# Calling get_provider again spawns a new instance
p2 = registry.get_provider(username)
assert p1 is not p2
def test_provider_hot_reload_on_external_change(self, registry):
"""Verifies that UserRegistry hot-reloads the provider if config.yaml is updated externally."""
username = "charlie"
registry.user_service.create_user(username, "password")
# Initial load (no nodes)
p1 = registry.get_provider(username)
assert len(p1.nodes.list_nodes()) == 0
# Resolve config.yaml file path
conf_file = os.path.join(registry.server_config_dir, "users", username, "config.yaml")
# Modify the config file physically on disk by appending a node
from connpy.configfile import configfile
cfg = configfile(conf=conf_file)
cfg._connections_add(id="testnode", host="8.8.8.8")
cfg._saveconfig(cfg.file)
# Artificially increase mtime to force reload
mtime = os.path.getmtime(conf_file)
os.utime(conf_file, (mtime + 5.0, mtime + 5.0))
# Fetch provider again
p2 = registry.get_provider(username)
# Verify it hot-reloaded and the new node is immediately visible
assert p1 is not p2
assert "testnode" in p2.nodes.list_nodes()
def test_provider_hot_reload_fails_on_corrupt_file_keeps_old_provider(self, registry):
"""Verifies that UserRegistry keeps serving the old provider if disk config is corrupt."""
username = "danny"
registry.user_service.create_user(username, "password")
# Initial load
p1 = registry.get_provider(username)
p1.nodes.add_node("nodeA", {"host": "2.2.2.2"})
assert "nodeA" in p1.nodes.list_nodes()
# Resolve config.yaml path
conf_file = os.path.join(registry.server_config_dir, "users", username, "config.yaml")
# Write corrupted content directly to config.yaml
with open(conf_file, "w") as f:
f.write("corrupt yaml content ::: invalid syntax :::")
# Artificially increase mtime to force reload attempt
mtime = os.path.getmtime(conf_file)
os.utime(conf_file, (mtime + 5.0, mtime + 5.0))
# Fetching provider again should fallback to old_provider instead of failing completely
p2 = registry.get_provider(username)
# Verify fallback
assert p1 is p2
assert "nodeA" in p2.nodes.list_nodes()
-217
View File
@@ -1,217 +0,0 @@
import os
import shutil
import pytest
import datetime
import jwt
import yaml
from pathlib import Path
from connpy.services.user_service import UserService
@pytest.fixture
def test_config_dir(tmp_path):
"""Creates a temporary config directory for testing user registry."""
config_dir = tmp_path / "conn_config"
config_dir.mkdir()
return config_dir
@pytest.fixture
def user_service(test_config_dir):
"""Initializes UserService pointing to a temporary directory."""
return UserService(str(test_config_dir))
class TestUserService:
def test_no_users(self, user_service):
"""Verifies that a new registry is empty by default."""
users = user_service.list_users()
assert users == []
def test_create_user_default(self, user_service):
"""Tests Mode A: fresh user config and key creation."""
username = "testuser"
res = user_service.create_user(username, "mypassword")
assert res["username"] == username
assert res["config_path"] is None
assert "created" in res
# Verify folder, config.yaml and .osk key are created
user_dir = os.path.join(user_service.users_dir, username)
assert os.path.isdir(user_dir)
assert os.path.isdir(os.path.join(user_dir, "plugins"))
assert os.path.isdir(os.path.join(user_dir, "ai_sessions"))
assert os.path.isfile(os.path.join(user_dir, "config.yaml"))
assert os.path.isfile(os.path.join(user_dir, ".osk"))
def test_create_user_custom_path(self, user_service, tmp_path):
"""Tests Mode B: using an existing valid config path."""
# Setup existing custom config directory
custom_dir = tmp_path / "custom_user_conn"
custom_dir.mkdir()
config_file = custom_dir / "config.yaml"
# Write basic config.yaml
config_data = {
"config": {"case": False, "idletime": 30, "fzf": False},
"connections": {},
"profiles": {}
}
with open(config_file, "w") as f:
yaml.dump(config_data, f)
res = user_service.create_user("fluzzi", "fluzzipass", config_path=str(custom_dir))
assert res["username"] == "fluzzi"
assert res["config_path"] == str(custom_dir)
# Verify no directory is created under the server's user folder
user_dir = os.path.join(user_service.users_dir, "fluzzi")
assert not os.path.exists(user_dir)
def test_create_user_custom_path_auto_init(self, user_service, tmp_path):
"""Ensures create_user automatically initializes a missing directory and default config.yaml."""
custom_dir = tmp_path / "new_custom_config"
# Test creation where the directory does not exist yet
res = user_service.create_user("john", "pass", config_path=str(custom_dir))
assert res["username"] == "john"
assert res["config_path"] == str(custom_dir)
# Verify custom path and subdirs/configs were created
assert os.path.isdir(custom_dir)
assert os.path.exists(os.path.join(custom_dir, "config.yaml"))
assert os.path.isdir(os.path.join(custom_dir, "plugins"))
assert os.path.isdir(os.path.join(custom_dir, "ai_sessions"))
def test_create_duplicate_user(self, user_service):
"""Ensures duplicate usernames are rejected."""
user_service.create_user("dupuser", "password")
with pytest.raises(ValueError, match="already exists"):
user_service.create_user("dupuser", "anotherpass")
def test_delete_user_default(self, user_service):
"""Tests Mode A: deleting a server-managed user cleans up directories."""
username = "deluser"
user_service.create_user(username, "password")
user_dir = os.path.join(user_service.users_dir, username)
assert os.path.isdir(user_dir)
user_service.delete_user(username)
# Directory should be cleaned up
assert not os.path.exists(user_dir)
# Registry should be updated
assert len(user_service.list_users()) == 0
def test_delete_user_custom_path(self, user_service, tmp_path):
"""Tests Mode B: deleting a custom-path user leaves files untouched."""
custom_dir = tmp_path / "fluzzi_custom"
custom_dir.mkdir()
config_file = custom_dir / "config.yaml"
with open(config_file, "w") as f:
yaml.dump({"config": {}, "connections": {}, "profiles": {}}, f)
username = "fluzzi"
user_service.create_user(username, "pass", config_path=str(custom_dir))
user_service.delete_user(username)
# Registry cleared
assert len(user_service.list_users()) == 0
# Files remain untouched
assert os.path.isdir(str(custom_dir))
assert os.path.isfile(str(config_file))
def test_list_users(self, user_service):
"""Tests listing all registered users with their metadata."""
user_service.create_user("user1", "pass1")
user_service.create_user("user2", "pass2")
users = user_service.list_users()
assert len(users) == 2
usernames = [u["username"] for u in users]
assert "user1" in usernames
assert "user2" in usernames
def test_get_user(self, user_service):
"""Tests retrieving a single user's configuration metadata."""
user_service.create_user("user1", "pass1")
user = user_service.get_user("user1")
assert user["username"] == "user1"
assert user["config_path"] is None
assert "created" in user
with pytest.raises(ValueError, match="not found"):
user_service.get_user("nonexistent")
def test_authenticate_valid(self, user_service):
"""Verifies successful authentication."""
user_service.create_user("john", "my-secure-password")
assert user_service.authenticate("john", "my-secure-password") is True
def test_authenticate_invalid(self, user_service):
"""Verifies unsuccessful authentication on incorrect or missing credentials."""
user_service.create_user("john", "my-secure-password")
assert user_service.authenticate("john", "wrong-password") is False
assert user_service.authenticate("nonexistent", "my-secure-password") is False
def test_jwt_roundtrip(self, user_service):
"""Tests generating a JWT token and verifying it back to the username."""
username = "jwttester"
user_service.create_user(username, "pass")
token = user_service.generate_jwt(username)
assert isinstance(token, str)
verified_user = user_service.verify_jwt(token)
assert verified_user == username
def test_jwt_expired(self, user_service):
"""Tests that expired JWT tokens are rejected and return None."""
username = "jwttester"
user_service.create_user(username, "pass")
# Manually generate an expired token by setting exp to the past
registry = user_service._load_registry()
expired_time = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=10)
payload = {
"sub": username,
"exp": expired_time
}
token = jwt.encode(payload, registry["jwt_secret"], algorithm="HS256")
if isinstance(token, bytes):
token = token.decode("utf-8")
verified_user = user_service.verify_jwt(token)
assert verified_user is None
def test_change_password(self, user_service):
"""Tests changing password for a user."""
username = "passchanger"
user_service.create_user(username, "oldpass")
# Old credentials authenticate
assert user_service.authenticate(username, "oldpass") is True
# Change password
user_service.change_password(username, "oldpass", "newpass")
# Old password fails, new password works
assert user_service.authenticate(username, "oldpass") is False
assert user_service.authenticate(username, "newpass") is True
# Change with invalid old password should fail
with pytest.raises(ValueError, match="Invalid credentials"):
user_service.change_password(username, "wrongold", "evennewer")
def test_admin_change_password(self, user_service):
"""Tests administrative password change (no old password required)."""
username = "adminpasschanger"
user_service.create_user(username, "oldpass")
# Admin changes password directly
user_service.admin_change_password(username, "newpass")
# Verify credentials
assert user_service.authenticate(username, "oldpass") is False
assert user_service.authenticate(username, "newpass") is True
-32
View File
@@ -1,32 +0,0 @@
import pytest
from connpy.utils import log_cleaner
def test_log_cleaner_empty():
assert log_cleaner("") == ""
assert log_cleaner(None) == ""
def test_log_cleaner_plain_text():
assert log_cleaner("hello world") == "hello world"
def test_log_cleaner_ansi_colors():
# \x1b[31m is red, \x1b[0m is reset
assert log_cleaner("\x1b[31mhello\x1b[0m world") == "hello world"
def test_log_cleaner_osc_window_title():
# Set window title OSC: \x1b]0;my title\x07 followed by prompt
sample = "\x1b]0;fluzzi32@norman: ~\x07fluzzi32@norman:~$"
assert log_cleaner(sample) == "fluzzi32@norman:~$"
def test_log_cleaner_osc_with_st_terminator():
# OSC can also be terminated by \x1b\\ (ST)
sample = "\x1b]0;some title\x1b\\my_prompt>"
assert log_cleaner(sample) == "my_prompt>"
def test_log_cleaner_mixed_ansi_and_osc():
sample = "\x1b]0;title\x07\x1b[32muser@host\x1b[0m:\x1b[34m/path\x1b[0m$ "
assert log_cleaner(sample) == "user@host:/path$"
def test_log_cleaner_carriage_return_and_backspace():
# Test that standard control sequences like \r and \b still work as expected
assert log_cleaner("hello\rworld") == "world"
assert log_cleaner("hell\bo") == "helo"
-203
View File
@@ -1,203 +0,0 @@
import asyncio
import os
import sys
import termios
import tty
import signal
import struct
import fcntl
class LocalStream:
"""
Asynchronous stream wrapper for local stdin/stdout.
Handles terminal raw mode, async I/O, and SIGWINCH signals.
"""
def __init__(self):
self.stdin_fd = sys.stdin.fileno()
self.stdout_fd = sys.stdout.fileno()
self.original_tty_settings = None
self.resize_callback = None
self._reader_queue = asyncio.Queue()
self._loop = None
def setup(self, resize_callback=None):
self._loop = asyncio.get_running_loop()
self.resize_callback = resize_callback
# Save original terminal settings
try:
self.original_tty_settings = termios.tcgetattr(self.stdin_fd)
tty.setraw(self.stdin_fd)
except termios.error:
# Not a TTY, maybe piped or redirected
pass
# Set stdin non-blocking
flags = fcntl.fcntl(self.stdin_fd, fcntl.F_GETFL)
fcntl.fcntl(self.stdin_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
# Setup read callback
self._loop.add_reader(self.stdin_fd, self._read_ready)
# Register SIGWINCH
if resize_callback:
try:
self._loop.add_signal_handler(signal.SIGWINCH, self._handle_winch)
except (NotImplementedError, RuntimeError):
# signal handling not supported on some loops (e.g., Windows Proactor)
pass
def stop_reading(self):
"""Temporarily stop reading from stdin."""
if self._loop and self.stdin_fd is not None:
try:
self._loop.remove_reader(self.stdin_fd)
except Exception:
pass
def start_reading(self):
"""Resume reading from stdin."""
if self._loop and self.stdin_fd is not None:
try:
# Ensure we don't add it twice
self._loop.remove_reader(self.stdin_fd)
except Exception:
pass
self._loop.add_reader(self.stdin_fd, self._read_ready)
def teardown(self):
if self._loop:
try:
self._loop.remove_reader(self.stdin_fd)
except Exception:
pass
if self.resize_callback:
try:
self._loop.remove_signal_handler(signal.SIGWINCH)
except Exception:
pass
# Restore terminal settings
if self.original_tty_settings is not None:
try:
termios.tcsetattr(self.stdin_fd, termios.TCSADRAIN, self.original_tty_settings)
except termios.error:
pass
# Restore blocking mode for stdin
try:
flags = fcntl.fcntl(self.stdin_fd, fcntl.F_GETFL)
fcntl.fcntl(self.stdin_fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
except Exception:
pass
def _read_ready(self):
try:
# Read whatever is available
data = os.read(self.stdin_fd, 4096)
if data:
self._reader_queue.put_nowait(data)
else:
self._reader_queue.put_nowait(b'') # EOF
except BlockingIOError:
pass
except OSError:
self._reader_queue.put_nowait(b'') # EOF on error
async def read(self) -> bytes:
"""Asynchronously read bytes from stdin."""
return await self._reader_queue.get()
async def write(self, data: bytes):
"""Asynchronously write bytes to stdout."""
if not data:
return
try:
os.write(self.stdout_fd, data)
except OSError:
pass
def _handle_winch(self):
if self.resize_callback:
try:
# Use ioctl to get the current window size
s = struct.pack("HHHH", 0, 0, 0, 0)
a = fcntl.ioctl(self.stdout_fd, termios.TIOCGWINSZ, s)
rows, cols, _, _ = struct.unpack("HHHH", a)
# We schedule the callback safely inside the asyncio loop
# instead of running it raw in the signal handler
self._loop.call_soon(self.resize_callback, rows, cols)
except Exception:
pass
import threading
class RemoteStream:
"""
Asynchronous stream wrapper for gRPC remote connections.
Bridges the blocking gRPC iterators with the async _async_interact_loop.
"""
def __init__(self, request_iterator, response_queue):
self.request_iterator = request_iterator
self.response_queue = response_queue
self.running = True
self._reader_queue = asyncio.Queue()
self.copilot_queue = asyncio.Queue()
self.resize_callback = None
self._loop = None
self.t = None
def setup(self, resize_callback=None):
self._loop = asyncio.get_running_loop()
self.resize_callback = resize_callback
def read_requests():
try:
for req in self.request_iterator:
if not self.running:
break
if req.cols > 0 and req.rows > 0:
if self.resize_callback:
self._loop.call_soon_threadsafe(self.resize_callback, req.rows, req.cols)
# Copilot dispatching
copilot_msg = {}
if getattr(req, "copilot_question", ""):
copilot_msg.update({
"question": req.copilot_question,
"context_buffer": getattr(req, "copilot_context_buffer", ""),
"node_info_json": getattr(req, "copilot_node_info_json", "")
})
if getattr(req, "copilot_action", ""):
copilot_msg["action"] = req.copilot_action
if copilot_msg:
self._loop.call_soon_threadsafe(self.copilot_queue.put_nowait, copilot_msg)
if req.stdin_data:
self._loop.call_soon_threadsafe(self._reader_queue.put_nowait, req.stdin_data)
except Exception:
pass
finally:
if self._loop and not self._loop.is_closed():
try:
self._loop.call_soon_threadsafe(self._reader_queue.put_nowait, b'')
except RuntimeError:
pass
self.t = threading.Thread(target=read_requests, daemon=True)
self.t.start()
def teardown(self):
self.running = False
self.response_queue.put(None) # Signal EOF
async def read(self) -> bytes:
"""Asynchronously read bytes from the gRPC iterator queue."""
return await self._reader_queue.get()
async def write(self, data: bytes):
"""Asynchronously write bytes to the gRPC response queue."""
if data:
self.response_queue.put(data)
-70
View File
@@ -1,70 +0,0 @@
import re
def log_cleaner(data: str) -> str:
"""
Stateless utility to remove ANSI sequences and process cursor movements.
"""
if not data:
return ""
# Remove OSC (Operating System Command) sequences (e.g., set window title \x1b]0;...\x07)
data = re.sub(r'\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)', '', data)
lines = data.split('\n')
cleaned_lines = []
# Regex to capture: ANSI sequences, control characters (\r, \b, etc), and plain text chunks
token_re = re.compile(r'(\x1B(?:[\x30-\x5A\x5C-\x7E]|\[[0-?]*[ -/ ]*[@-~])|\r|\b|\x7f|[\x00-\x1F]|[^\x1B\r\b\x7f\x00-\x1F]+)')
for line in lines:
buffer = []
cursor = 0
for token in token_re.findall(line):
if token == '\r':
cursor = 0
elif token in ('\b', '\x7f'):
if cursor > 0:
cursor -= 1
elif token.startswith('\x1B[') and len(token) >= 3:
# Parse CSI: \x1B[ <params> <final_char>
final = token[-1]
param_str = token[2:-1]
n = int(param_str) if param_str.isdigit() else 1
if final == 'D': # CUB Cursor Back
cursor = max(0, cursor - n)
elif final == 'C': # CUF Cursor Forward
cursor = min(len(buffer), cursor + n)
elif final == 'K': # EL Erase in Line
if n == 0 or param_str == '': # Clear to end
buffer = buffer[:cursor]
elif n == 1: # Clear to start
buffer[:cursor] = [' '] * cursor
elif n == 2: # Clear entire line
buffer = []
cursor = 0
elif final == 'G': # CHA Cursor Horizontal Absolute (1-indexed)
cursor = max(0, n - 1)
# Pad buffer if cursor is beyond current length
if cursor > len(buffer):
buffer.extend([' '] * (cursor - len(buffer)))
elif final == 'P': # DCH Delete Characters
del buffer[cursor:cursor + n]
elif final == '@': # ICH Insert Characters
buffer[cursor:cursor] = [' '] * n
# All other CSI sequences are silently discarded
elif token.startswith('\x1B'):
continue
elif len(token) == 1 and ord(token) < 32:
continue
else:
for char in token:
if cursor == len(buffer):
buffer.append(char)
else:
buffer[cursor] = char
cursor += 1
cleaned_lines.append("".join(buffer))
return "\n".join(cleaned_lines).replace('\n\n', '\n').strip()

Some files were not shown because too many files have changed in this diff Show More