lebe-dev / zabbix-api-rs

Zabbix API for Rust
MIT License
4 stars 2 forks source link

login demo code in README.md has "unexpected error" #1

Closed tjyang closed 9 months ago

tjyang commented 9 months ago

@lebe-dev Thanks for creating this framework, I am interested to learn use it.

1 directory, 3 files $ $ cargo install --path . -f

- error message, I am expected to see error message saying that remote certificate not valid.

$ which test01 ~/.cargo/bin/test01 $ test01 thread 'main' panicked at src/main.rs:15:9: unexpected error note: run with RUST_BACKTRACE=1 environment variable to display a backtrace $

- Cargo.toml 

$ cat Cargo.toml [package] name = "test01" version = "0.1.0" edition = "2021"

See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies] log = "0.4.20" reqwest = { version = "0.11.23", features = ["blocking", "json"] } zabbix-api = "0.1.0" $

- main.rs with masked private info.

$ cat src/main.rs use reqwest::blocking::Client; use zabbix_api::client::v6::ZabbixApiV6Client; use zabbix_api::client::ZabbixApiClient; use log::error;

fn main() { let http_client = Client::new();

let client = ZabbixApiV6Client::new(http_client, "https://zbbix03.test.com/zabbix/api_jsonrpc.php");

match client.get_auth_session("Admin", "zabbix") { Ok(session) => println!("session: {session}"), Err(e) => { error!("error: {}", e); panic!("unexpected error") } } $

- user curl to verify login

$ more ~/zbxc-login.sh curl --request POST \ --insecure \ --url 'https://zabbix03.test.com/zabbix/api_jsonrpc.php' \ --header 'Content-Type: application/json-rpc' \ --data '{"jsonrpc":"2.0","method":"user.login","params":{"username":"Admin","password":"zabbix"},"id":1}' echo $

- remote zabbix was able return bearer string after using --insecure to ignore certificate check

$ ~/zbxc-login.sh {"jsonrpc":"2.0","result":"6b47c2a57d09b187f511d40369xxxxxx","id":1} $

lebe-dev commented 9 months ago

Hello, thanks for report. Replace:

error!("error: {}", e);

with:

println!("error: {}", e);

And send output here.

tjyang commented 9 months ago
- I am guessing it was due to check of remote server certificate invalided issue.

curl --request POST \ --insecure \ --url 'https://zabbix03.test.com/zabbix/api_jsonrpc.php' \ --header 'Content-Type: application/json-rpc' \ --data '{"jsonrpc":"2.0","method":"user.login","params":{"username":"Admin","password":"zabbix"},"id":1}' echo


- See R1, Can you add  arg/setting like curl's --insecure to skip certificate check ? 

R1: https://github.com/seanmonstar/reqwest/issues/182
lebe-dev commented 9 months ago

You can build http client in such way:

let http_client = ClientBuilder::new().danger_accept_invalid_certs(true).build()

Then use in ZabbixApiV6Client::new(...).

tjyang commented 9 months ago

//use reqwest::blocking::Client; use reqwest::ClientBuilder; use zabbix_api::client::v6::ZabbixApiV6Client; use zabbix_api::client::ZabbixApiClient;

fn main() { // let http_client = Client::new(); let http_client = ClientBuilder::new().danger_accept_invalid_certs(true).build();

let client = ZabbixApiV6Client::new(http_client, "https://1.1.1.1/zabbix/api_jsonrpc.php");

match client.get_auth_session("Admin", "zabbix") { Ok(session) => println!("session: {session}"), Err(e) => { //error!("error: {}", e); println!("error: {}", e); panic!("unexpected error") } } }

- error message

$ cargo build

Compiling reqwest v0.11.23 Compiling zabbix-api v0.1.0 Compiling test01 v0.1.0 (xxxx/zbxc) error[E0308]: mismatched types --> src/main.rs:12:41 | 12 | let client = ZabbixApiV6Client::new(http_client, "https://100.64.5.212/zabbix/api_jsonrpc.php"); | ---------------------- ^^^^^^^^^^^ expected `Client`, found `Result` | | | arguments to this function are incorrect | = note: expected struct `reqwest::blocking::Client` found enum `Result` note: associated function defined here --> /home/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/zabbix-api-0.1.0/src/client/v6/mod.rs:29:12 | 29 | pub fn new(client: Client, api_endpoint_url: &str) -> ZabbixApiV6Client { | ^^^ For more information about this error, try `rustc --explain E0308`. error: could not compile `test01` (bin "test01") due to previous error error: failed to compile `test01 v0.1.0 (/home/me/locos/zbxc)`, intermediate artifacts can be found at `/home/me/locos/zbxc/target`. To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path. make: *** [Makefile:108: install] Error 101 $ ```
tjyang commented 9 months ago

@lebe-dev , can you pls delete above spam ?

lebe-dev commented 9 months ago

ClientBulder's build method returns Result<Client, Error>, not Client. So, you have to handle this or use .unwrap().

tjyang commented 9 months ago
lebe-dev commented 9 months ago

You should use another ClientBuilder -> reqwest::blocking::ClientBuilder.

My crate doesn't support asynchronous code.

tjyang commented 9 months ago

@lebe-dev Thanks for the guidance along the way. it is time for me to use vscode/rust and really understand the rust code in details.