Skip to content

Commit

Permalink
Fix error behaviour for the client used by rhay
Browse files Browse the repository at this point in the history
  • Loading branch information
sebt3 committed Jun 10, 2024
1 parent bf6cb8a commit 3321fb6
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kuberest"
version = "0.9.1"
version = "0.9.2"
authors = ["Sébastien Huss <[email protected]>"]
edition = "2021"
default-run = "controller"
Expand Down
4 changes: 2 additions & 2 deletions charts/kuberest/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: kuberest
description: Allow to Control remote REST api endpoints from the confort of your cluster
type: application
version: "0.9.1"
appVersion: "0.9.1"
version: "0.9.2"
appVersion: "0.9.2"
8 changes: 4 additions & 4 deletions deploy/operator/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
labels:
app: kuberest
app.kubernetes.io/name: kuberest
app.kubernetes.io/version: "0.9.1"
app.kubernetes.io/version: "0.9.2"
namespace: default
automountServiceAccountToken: true
---
Expand Down Expand Up @@ -54,7 +54,7 @@ metadata:
labels:
app: kuberest
app.kubernetes.io/name: kuberest
app.kubernetes.io/version: "0.9.1"
app.kubernetes.io/version: "0.9.2"
spec:
type: ClusterIP
ports:
Expand All @@ -74,7 +74,7 @@ metadata:
labels:
app: kuberest
app.kubernetes.io/name: kuberest
app.kubernetes.io/version: "0.9.1"
app.kubernetes.io/version: "0.9.2"
spec:
replicas: 1
selector:
Expand All @@ -92,7 +92,7 @@ spec:
{}
containers:
- name: kuberest
image: sebt3/kuberest:0.9.1
image: sebt3/kuberest:0.9.2
imagePullPolicy: IfNotPresent
securityContext:
{}
Expand Down
17 changes: 0 additions & 17 deletions examples/authentik/openid-gitea.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,6 @@ spec:
provider: "{{ write.oauth.gitea.pk }}"
meta_launch_url: https://gitea.your-company.com
outputs:
- kind: ConfigMap
metadata:
name: project-list
data:
config.yaml: |-
name: gitea-app
authorization_flow: "{{ read.flow.authorization.pk }}"
authentication_flow: "{{ read.flow.default.pk }}"
client_id: "{{ input.uuid }}"
property_mappings:
- "{{ json_query "results[0].pk" read.scopes.email }}"
- "{{ json_query "results[0].pk" read.scopes.openid }}"
- "{{ json_query "results[0].pk" read.scopes.profile }}"
client_type: "confidential"
sub_mode: "user_username"
signing_key: "{{ json_query "results[0].pk" read.keypair.ak }}"
redirect_uris: "https://gitea.your-company.com/user/oauth2/authentik/callback"
- kind: Secret
metadata:
name: gitea-openid
Expand Down
4 changes: 2 additions & 2 deletions src/handlebarshandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ handlebars_helper!(base64_encode: |arg:Value| STANDARD.encode(arg.as_str().unwra
warn!("handlebars::base64_encode received a non-string parameter: {:}",arg);
""
}).to_string()));
handlebars_helper!(header_basic: |username:Value,password:Value| STANDARD.encode(format!("Basic {}:{}",username.as_str().unwrap_or_else(|| {
handlebars_helper!(header_basic: |username:Value,password:Value| format!("Basic {}",STANDARD.encode(format!("{}:{}",username.as_str().unwrap_or_else(|| {
warn!("handlebars::header_basic received a non-string username: {:}",username);
""
}),password.as_str().unwrap_or_else(|| {
warn!("handlebars::header_basic received a non-string password: {:}",password);
""
}))));
})))));
handlebars_helper!(gen_password: |len:u32| Passwords::new().generate(len, 6, 2, 2));
handlebars_helper!(gen_password_alphanum: |len:u32| Passwords::new().generate(len, 8, 2, 0));

Expand Down
179 changes: 109 additions & 70 deletions src/httphandler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::str::FromStr;

use crate::{Error, Error::*};
use actix_web::Result;
use base64::{engine::general_purpose::STANDARD, Engine as _};
use reqwest::{Client, Response};
use rhai::{Dynamic, Map};
Expand Down Expand Up @@ -161,22 +164,29 @@ impl RestClient {

pub fn rhai_get(&mut self, path: String) -> Map {
let mut ret = Map::new();
let result = self.http_get(path.as_str()).unwrap();
ret.insert(
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
match self.http_get(path.as_str()) {
Ok(result) =>{
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
},Err(e) =>{
let mut res = Map::new();
res.insert("error".to_string().into(), Dynamic::from_str(&format!("{:}",e)).unwrap());
res
}
}
}

pub fn http_patch(&mut self, path: &str, body: &str) -> Result<Response, reqwest::Error> {
Expand Down Expand Up @@ -230,22 +240,30 @@ impl RestClient {
serde_json::to_string(&val).unwrap()
};
let mut ret = Map::new();
let result = self.http_patch(path.as_str(), &body).unwrap();
ret.insert(
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
match self.http_patch(path.as_str(), &body) {
Ok(result) =>{
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
},Err(e) =>{
let mut res = Map::new();
res.insert("error".to_string().into(), Dynamic::from_str(&format!("{:}",e)).unwrap());
res
}

}
}

pub fn http_put(&mut self, path: &str, body: &str) -> Result<Response, reqwest::Error> {
Expand Down Expand Up @@ -299,22 +317,29 @@ impl RestClient {
serde_json::to_string(&val).unwrap()
};
let mut ret = Map::new();
let result = self.http_put(path.as_str(), &body).unwrap();
ret.insert(
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
match self.http_put(path.as_str(), &body) {
Ok(result) =>{
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
},Err(e) =>{
let mut res = Map::new();
res.insert("error".to_string().into(), Dynamic::from_str(&format!("{:}",e)).unwrap());
res
}
}
}

pub fn http_post(&mut self, path: &str, body: &str) -> Result<Response, reqwest::Error> {
Expand Down Expand Up @@ -368,22 +393,29 @@ impl RestClient {
serde_json::to_string(&val).unwrap()
};
let mut ret = Map::new();
let result = self.http_post(path.as_str(), &body).unwrap();
ret.insert(
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
match self.http_post(path.as_str(), &body) {
Ok(result) =>{
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
},Err(e) =>{
let mut res = Map::new();
res.insert("error".to_string().into(), Dynamic::from_str(&format!("{:}",e)).unwrap());
res
}
}
}

pub fn http_delete(&mut self, path: &str) -> Result<Response, reqwest::Error> {
Expand Down Expand Up @@ -429,22 +461,29 @@ impl RestClient {

pub fn rhai_delete(&mut self, path: String) -> Map {
let mut ret = Map::new();
let result = self.http_delete(path.as_str()).unwrap();
ret.insert(
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
match self.http_delete(path.as_str()) {
Ok(result) =>{
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
"code".to_string().into(),
Dynamic::from_int(result.status().as_u16().to_string().parse::<i64>().unwrap()),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let text = result.text().await.unwrap();
ret.insert(
"json".to_string().into(),
serde_json::from_str(&text).unwrap_or(Dynamic::from(json!({}))),
);
ret.insert("body".to_string().into(), Dynamic::from(text));
ret.into()
})
})
},Err(e) =>{
let mut res = Map::new();
res.insert("error".to_string().into(), Dynamic::from_str(&format!("{:}",e)).unwrap());
res
}
}
}

pub fn obj_read(&mut self, method: ReadMethod, path: &str, key: &str) -> Result<Value, Error> {
Expand Down

0 comments on commit 3321fb6

Please sign in to comment.