mirror of
https://github.com/house-of-vanity/khm.git
synced 2025-07-07 15:24:07 +00:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
3bedadf394 | |||
a0f83db19a | |||
c01eb48451 | |||
c3575b013f | |||
b4c2fae778 | |||
61552a3d70 |
24
.github/workflows/main.yml
vendored
24
.github/workflows/main.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
build_target: x86_64-unknown-linux-musl
|
||||
@ -23,6 +23,9 @@ jobs:
|
||||
- os: windows-latest
|
||||
build_target: x86_64-pc-windows-msvc
|
||||
platform_name: windows-amd64
|
||||
- os: macos-latest
|
||||
build_target: aarch64-apple-darwin
|
||||
platform_name: macos-arm64
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
@ -67,8 +70,12 @@ jobs:
|
||||
with:
|
||||
args: cargo build --target ${{ matrix.build_target }} --release
|
||||
|
||||
- name: Build MacOS
|
||||
if: matrix.os == 'macos-latest'
|
||||
run: cargo build --target ${{ matrix.build_target }} --release
|
||||
|
||||
- name: Build Windows
|
||||
if: matrix.os != 'ubuntu-latest'
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: cargo build --target ${{ matrix.build_target }} --release
|
||||
|
||||
- name: Upload artifact
|
||||
@ -87,16 +94,13 @@ jobs:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Create Release
|
||||
- uses: ncipollo/release-action@v1
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: Release ${{ github.ref }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
allowUpdates: true
|
||||
#artifacts: "release.tar.gz,foo/*.txt"
|
||||
|
||||
upload:
|
||||
name: Upload Release Assets
|
||||
@ -104,12 +108,14 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
platform_name: linux-amd64
|
||||
- os: windows-latest
|
||||
platform_name: windows-amd64
|
||||
- os: macos-latest
|
||||
platform_name: macos-arm64
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
|
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -1,6 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "actix-codec"
|
||||
@ -1053,9 +1053,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "khm"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"base64 0.21.7",
|
||||
"chrono",
|
||||
"clap",
|
||||
"env_logger",
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "khm"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
authors = ["AB <ab@hexor.cy>", "ChatGPT-4o"]
|
||||
|
||||
@ -11,6 +11,7 @@ serde_json = "1.0"
|
||||
env_logger = "0.11.3"
|
||||
log = "0.4"
|
||||
regex = "1.10.5"
|
||||
base64 = "0.21"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
|
26
README.MD
26
README.MD
@ -24,21 +24,23 @@ khm --server --ip 127.0.0.1 --port 8080 --db-host 127.0.0.1 --db-name khm --db-u
|
||||
To run the application in client mode, use the following command:
|
||||
|
||||
```bash
|
||||
khm --host http://khm.example.com:8080 --known-hosts ~/.ssh/known_hosts --in-place
|
||||
khm --host http://khm.example.com:8080/<FLOW_NAME>/ --known-hosts ~/.ssh/known_hosts --in-place
|
||||
```
|
||||
|
||||
### Arguments
|
||||
- `--server`: Run in server mode (default: false).
|
||||
- `--ip`: IP address to bind the server or client to (default: 127.0.0.1).
|
||||
- `--port`: Port to bind the server or client to (default: 8080).
|
||||
- `--db-host`: Hostname or IP address of the PostgreSQL database (default: 127.0.0.1).
|
||||
- `--db-name`: Name of the PostgreSQL database (default: khm).
|
||||
- `--db-user`: Username for the PostgreSQL database (required in server mode).
|
||||
- `--db-password`: Password for the PostgreSQL database (required in server mode).
|
||||
- `--host`: Host address of the server to connect to in client mode (required in client mode).
|
||||
- `--known-hosts`: Path to the `known_hosts` file (default: ~/.ssh/known_hosts).
|
||||
- `--in-place`: Update the `known_hosts` file with keys from the server after sending keys (default: false).
|
||||
- `--flows`: Comma-separated list of flows to manage (default: default).
|
||||
Options:
|
||||
- `--server` Run in server mode
|
||||
- `--in-place` Server mode: Sync the known_hosts file with keys from the server
|
||||
- `--flows <FLOWS>...` Server mode: Comma-separated list of flows to manage [default: default]
|
||||
- `-i, --ip <IP>` Server mode: IP address to bind the server to [default: 127.0.0.1]
|
||||
- `-p, --port <PORT>` Server mode: Port to bind the server to [default: 8080]
|
||||
- `--db-host <DB_HOST>` Server mode: Hostname or IP address of the PostgreSQL database [default: 127.0.0.1]
|
||||
- `--db-name <DB_NAME>` Server mode: Name of the PostgreSQL database [default: khm]
|
||||
- `--db-user <DB_USER>` Server mode: Username for the PostgreSQL database
|
||||
- `--db-password <DB_PASSWORD>` Server mode: Password for the PostgreSQL database
|
||||
- `--host <HOST>` Client mode: Full host address of the server to connect to. Like `https://khm.example.com/<FLOW_NAME>`
|
||||
- `--known-hosts <KNOWN_HOSTS>` Client mode: Path to the known_hosts file [default: ~/.ssh/known_hosts]
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use log::{error, info};
|
||||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
@ -48,10 +50,39 @@ fn write_known_hosts(file_path: &str, keys: &[SshKey]) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_keys_to_server(host: &str, keys: Vec<SshKey>) -> Result<(), reqwest::Error> {
|
||||
async fn send_keys_to_server(
|
||||
host: &str,
|
||||
keys: Vec<SshKey>,
|
||||
auth_string: &str,
|
||||
) -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let url = format!("{}/keys", host);
|
||||
let response = client.post(&url).json(&keys).send().await?;
|
||||
info!("URL: {} ", url);
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
|
||||
if !auth_string.is_empty() {
|
||||
let parts: Vec<&str> = auth_string.splitn(2, ':').collect();
|
||||
if parts.len() == 2 {
|
||||
let username = parts[0];
|
||||
let password = parts[1];
|
||||
|
||||
let auth_header_value = format!("{}:{}", username, password);
|
||||
let encoded_auth = general_purpose::STANDARD.encode(auth_header_value);
|
||||
let auth_header = format!("Basic {}", encoded_auth);
|
||||
|
||||
headers.insert(AUTHORIZATION, HeaderValue::from_str(&auth_header).unwrap());
|
||||
} else {
|
||||
error!("Invalid auth string format. Expected 'username:password'");
|
||||
}
|
||||
}
|
||||
|
||||
let response = client
|
||||
.post(&url)
|
||||
.headers(headers)
|
||||
.json(&keys)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
info!("Keys successfully sent to server.");
|
||||
@ -65,22 +96,38 @@ async fn send_keys_to_server(host: &str, keys: Vec<SshKey>) -> Result<(), reqwes
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_keys_from_server(host: &str) -> Result<Vec<SshKey>, reqwest::Error> {
|
||||
async fn get_keys_from_server(
|
||||
host: &str,
|
||||
auth_string: &str,
|
||||
) -> Result<Vec<SshKey>, reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let url = format!("{}/keys", host);
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let keys: Vec<SshKey> = response.json().await?;
|
||||
info!("Received {} keys from server", keys.len());
|
||||
Ok(keys)
|
||||
} else {
|
||||
error!(
|
||||
"Failed to get keys from server. Status: {}",
|
||||
response.status()
|
||||
);
|
||||
Ok(vec![])
|
||||
let mut headers = HeaderMap::new();
|
||||
|
||||
if !auth_string.is_empty() {
|
||||
let parts: Vec<&str> = auth_string.splitn(2, ':').collect();
|
||||
if parts.len() == 2 {
|
||||
let username = parts[0];
|
||||
let password = parts[1];
|
||||
|
||||
let auth_header_value = format!("{}:{}", username, password);
|
||||
let encoded_auth = general_purpose::STANDARD.encode(auth_header_value);
|
||||
let auth_header = format!("Basic {}", encoded_auth);
|
||||
|
||||
headers.insert(AUTHORIZATION, HeaderValue::from_str(&auth_header).unwrap());
|
||||
} else {
|
||||
error!("Invalid auth string format. Expected 'username:password'");
|
||||
}
|
||||
}
|
||||
|
||||
let response = client.get(&url).headers(headers).send().await?;
|
||||
|
||||
let response = response.error_for_status()?;
|
||||
|
||||
let keys: Vec<SshKey> = response.json().await?;
|
||||
info!("Received {} keys from server", keys.len());
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
pub async fn run_client(args: crate::Args) -> std::io::Result<()> {
|
||||
@ -89,13 +136,13 @@ pub async fn run_client(args: crate::Args) -> std::io::Result<()> {
|
||||
|
||||
let host = args.host.expect("host is required in client mode");
|
||||
info!("Client mode: Sending keys to server at {}", host);
|
||||
send_keys_to_server(&host, keys)
|
||||
send_keys_to_server(&host, keys, &args.basic_auth)
|
||||
.await
|
||||
.expect("Failed to send keys to server");
|
||||
|
||||
if args.in_place {
|
||||
info!("Client mode: In-place update is enabled. Fetching keys from server.");
|
||||
let server_keys = get_keys_from_server(&host)
|
||||
let server_keys = get_keys_from_server(&host, &args.basic_auth)
|
||||
.await
|
||||
.expect("Failed to get keys from server");
|
||||
|
||||
|
10
src/main.rs
10
src/main.rs
@ -19,8 +19,8 @@ use log::{error, info};
|
||||
Running in server mode:\n\
|
||||
khm --server --ip 0.0.0.0 --port 1337 --db-host psql.psql.svc --db-name khm --db-user admin --db-password <SECRET> --flows work,home\n\
|
||||
\n\
|
||||
Running in client mode to send diff and sync ~/.ssh/known_hosts with remote flow in place:\n\
|
||||
khm --host https://khm.example.com/default/keys --known-hosts ~/.ssh/known_hosts --in-place\n\
|
||||
Running in client mode to send diff and sync ~/.ssh/known_hosts with remote flow `work` in place:\n\
|
||||
khm --host https://khm.example.com/work --known-hosts ~/.ssh/known_hosts --in-place\n\
|
||||
\n\
|
||||
"
|
||||
)]
|
||||
@ -94,7 +94,7 @@ struct Args {
|
||||
#[arg(
|
||||
long,
|
||||
required_if_eq("server", "false"),
|
||||
help = "Client mode: Full host address of the server to connect to. Like https://khm.example.com/flow_name/keys"
|
||||
help = "Client mode: Full host address of the server to connect to. Like https://khm.example.com/<FLOW_NAME>"
|
||||
)]
|
||||
host: Option<String>,
|
||||
|
||||
@ -105,6 +105,10 @@ struct Args {
|
||||
help = "Client mode: Path to the known_hosts file"
|
||||
)]
|
||||
known_hosts: String,
|
||||
|
||||
/// Basic auth string for client mode. Format: user:pass
|
||||
#[arg(long, default_value = "", help = "Client mode: Basic Auth credentials")]
|
||||
basic_auth: String,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
Reference in New Issue
Block a user