Skip to content

Commit 8c4af60

Browse files
committed
feat: init base work of redirect
1 parent 0563580 commit 8c4af60

10 files changed

Lines changed: 248 additions & 6 deletions

File tree

.compose/authelia/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
notification.txt
2-
db.sqlite3
2+
db.sqlite3
3+
private.pem
4+
public.pem
5+
configuration.yml

.compose/authelia/configuration.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ identity_providers:
7878
jwks:
7979
- key_id: "kube_login"
8080
key: |
81-
81+
{{ secret "/config/private.pem" | nindent 10 }}
82+
8283
8384
8485

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Taskfile.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ tasks:
1919
env:
2020
KUBECONFIG: ./kubeconfig.local.yaml
2121
OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:5081/api/default"
22-
OTEL_EXPORTER_OTLP_HEADERS: "Authorization=Basic cm9vdEBleGFtcGxlLmNvbTpBaU9hZHozNHp6RHdWSHkw,organization=default,stream-name=default"
23-
REDIS_URL: redis://:{{ .REDIS_PASSWORD }}@localhost:6379/prorxyauthk8s
22+
OTEL_EXPORTER_OTLP_HEADERS: "Authorization=Basic cm9vdEBleGFtcGxlLmNvbTpDb21wbGV4cGFzcyMxMjM=,organization=default,stream-name=default"
23+
REDIS_URL: redis://:{{ .REDIS_PASSWORD }}@localhost:6379
2424
silent: true
2525
cmds:
2626
- echo "Running the backend"
@@ -49,6 +49,14 @@ tasks:
4949
fi
5050
- command -v docker && PUID="$(id -u)" PGID="$(id -g)" docker compose up || true
5151
- command -v podman && PUID="$(id -u)" PGID="$(id -g)" podman compose up || true
52+
init:service:
53+
desc: "Initialize the service task"
54+
silent: true
55+
cmds:
56+
- echo "Initializing the service task"
57+
- echo "Generating self-signed certificates for local development"
58+
- docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd)/.compose/authelia":/keys authelia/authelia:latest authelia crypto pair rsa generate --directory /keys
59+
5260
recu:
5361
desc: "Run all gen jobs"
5462
silent: true

libs/api/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ utoipa-actix-web = { workspace = true }
99
actix-web = { workspace = true }
1010
tracing = { workspace = true }
1111
deadpool-redis = { workspace = true }
12+
reqwest = { workspace = true }
13+
serde = { workspace = true }
1214

1315
common = { path = "../common" }
16+
crd = { path = "../crd" }
1417

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

libs/api/src/cluster/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod base;
2+
pub mod redirect;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use actix_web::{delete, get, patch, post, put, web, HttpRequest, Responder};
2+
use common::State;
3+
use redirect::redirect;
4+
use tracing::instrument;
5+
6+
pub mod redirect;
7+
8+
// https://kubernetes.io/docs/reference/using-api/api-concepts/#api-verbs
9+
10+
/// Cluster redirect
11+
///
12+
/// Redirect to the cluster if exists
13+
#[utoipa::path(
14+
tag = "clusters",
15+
responses(
16+
(status = 200, description = "ATM nothing real"),
17+
(status = 500, description = "Internal server error."),
18+
)
19+
)]
20+
#[get("/{ns}/{cluster}/{path:.*}")]
21+
#[instrument(name = "get_redirect", skip(data))]
22+
pub async fn get_redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
23+
redirect(req, data).await
24+
}
25+
26+
/// Cluster redirect
27+
///
28+
/// Redirect to the cluster if exists
29+
#[utoipa::path(
30+
tag = "clusters",
31+
responses(
32+
(status = 200, description = "ATM nothing real"),
33+
(status = 500, description = "Internal server error."),
34+
)
35+
)]
36+
#[post("/{ns}/{cluster}/{path:.*}")]
37+
#[instrument(name = "post_redirect", skip(data))]
38+
pub async fn post_redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
39+
redirect(req, data).await
40+
}
41+
42+
/// Cluster redirect
43+
///
44+
/// Redirect to the cluster if exists
45+
#[utoipa::path(
46+
tag = "clusters",
47+
responses(
48+
(status = 200, description = "ATM nothing real"),
49+
(status = 500, description = "Internal server error."),
50+
)
51+
)]
52+
#[put("/{ns}/{cluster}/{path:.*}")]
53+
#[instrument(name = "put_redirect", skip(data))]
54+
pub async fn put_redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
55+
redirect(req, data).await
56+
}
57+
58+
/// Cluster redirect
59+
///
60+
/// Redirect to the cluster if exists
61+
#[utoipa::path(
62+
tag = "clusters",
63+
responses(
64+
(status = 200, description = "ATM nothing real"),
65+
(status = 500, description = "Internal server error."),
66+
)
67+
)]
68+
#[patch("/{ns}/{cluster}/{path:.*}")]
69+
#[instrument(name = "patch_redirect", skip(data))]
70+
pub async fn patch_redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
71+
redirect(req, data).await
72+
}
73+
74+
/// Cluster redirect
75+
///
76+
/// Redirect to the cluster if exists
77+
#[utoipa::path(
78+
tag = "clusters",
79+
responses(
80+
(status = 200, description = "ATM nothing real"),
81+
(status = 500, description = "Internal server error."),
82+
)
83+
)]
84+
#[delete("/{ns}/{cluster}/{path:.*}")]
85+
#[instrument(name = "delete_redirect", skip(data))]
86+
pub async fn delete_redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
87+
redirect(req, data).await
88+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use actix_web::{web, HttpRequest, HttpResponse, Responder};
2+
use common::State;
3+
use crd::ProxyKubeApi;
4+
use deadpool_redis::redis::AsyncTypedCommands;
5+
use tracing::{error, instrument};
6+
7+
#[instrument(name = "main_redirect", skip(data))]
8+
pub async fn redirect(req: HttpRequest, data: web::Data<State>) -> impl Responder {
9+
let ns: String = req.match_info().get("ns").unwrap().parse().unwrap();
10+
let cluster: String = req.match_info().get("cluster").unwrap().parse().unwrap();
11+
let path: String = req.match_info().get("path").unwrap().parse().unwrap();
12+
let mut conn = match data.get_redis_conn().await {
13+
Ok(conn) => conn,
14+
Err(e) => return HttpResponse::ServiceUnavailable().body(e.to_string()),
15+
};
16+
let proxy_json = match conn.get(format!("proxyk8sauth:{}/{}", ns, cluster)).await {
17+
Ok(Some(proxy)) => proxy,
18+
Ok(None) => return HttpResponse::NotFound().finish(),
19+
Err(e) => return HttpResponse::ServiceUnavailable().body(e.to_string()),
20+
};
21+
22+
let proxy = match ProxyKubeApi::from_json(&proxy_json) {
23+
Some(proxy) => proxy,
24+
None => {
25+
error!("Couldn't parse object");
26+
return HttpResponse::NotFound().finish();
27+
}
28+
};
29+
30+
let url_to_call = match proxy
31+
.spec
32+
.service
33+
.url_to_call(data.client.clone(), "default".to_string())
34+
.await
35+
{
36+
Ok(url) => url,
37+
Err(err) => {
38+
error!(err);
39+
return HttpResponse::NotFound().finish();
40+
}
41+
};
42+
// https://github.com/actix/examples/blob/master/http-proxy/src/main.rs#L56
43+
HttpResponse::Ok().body(format!("{}/{}", url_to_call, path))
44+
}

libs/api/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::{api_doc::ApiDoc, base::health, cluster::base::base_cluster};
1+
use crate::{
2+
api_doc::ApiDoc,
3+
base::health,
4+
cluster::{base::base_cluster, redirect},
5+
};
26
use actix_web::App;
37
use utoipa::{openapi::OpenApi as OpenApiType, OpenApi};
48
use utoipa_actix_web::{scope, service_config::ServiceConfig, AppExt};
@@ -15,7 +19,12 @@ pub fn init_api() -> impl FnOnce(&mut ServiceConfig) {
1519

1620
pub fn init_cluster_api() -> impl FnOnce(&mut ServiceConfig) {
1721
|cfg: &mut ServiceConfig| {
18-
cfg.service(base_cluster);
22+
cfg.service(base_cluster)
23+
.service(redirect::get_redirect)
24+
.service(redirect::post_redirect)
25+
.service(redirect::put_redirect)
26+
.service(redirect::patch_redirect)
27+
.service(redirect::delete_redirect);
1928
}
2029
}
2130

swagger.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,88 @@
4444
}
4545
}
4646
}
47+
},
48+
"/clusters/{ns}/{cluster}/{path}": {
49+
"get": {
50+
"tags": [
51+
"clusters"
52+
],
53+
"summary": "Cluster redirect",
54+
"description": "Redirect to the cluster if exists",
55+
"operationId": "get_redirect",
56+
"responses": {
57+
"200": {
58+
"description": "ATM nothing real"
59+
},
60+
"500": {
61+
"description": "Internal server error."
62+
}
63+
}
64+
},
65+
"put": {
66+
"tags": [
67+
"clusters"
68+
],
69+
"summary": "Cluster redirect",
70+
"description": "Redirect to the cluster if exists",
71+
"operationId": "put_redirect",
72+
"responses": {
73+
"200": {
74+
"description": "ATM nothing real"
75+
},
76+
"500": {
77+
"description": "Internal server error."
78+
}
79+
}
80+
},
81+
"post": {
82+
"tags": [
83+
"clusters"
84+
],
85+
"summary": "Cluster redirect",
86+
"description": "Redirect to the cluster if exists",
87+
"operationId": "post_redirect",
88+
"responses": {
89+
"200": {
90+
"description": "ATM nothing real"
91+
},
92+
"500": {
93+
"description": "Internal server error."
94+
}
95+
}
96+
},
97+
"delete": {
98+
"tags": [
99+
"clusters"
100+
],
101+
"summary": "Cluster redirect",
102+
"description": "Redirect to the cluster if exists",
103+
"operationId": "delete_redirect",
104+
"responses": {
105+
"200": {
106+
"description": "ATM nothing real"
107+
},
108+
"500": {
109+
"description": "Internal server error."
110+
}
111+
}
112+
},
113+
"patch": {
114+
"tags": [
115+
"clusters"
116+
],
117+
"summary": "Cluster redirect",
118+
"description": "Redirect to the cluster if exists",
119+
"operationId": "patch_redirect",
120+
"responses": {
121+
"200": {
122+
"description": "ATM nothing real"
123+
},
124+
"500": {
125+
"description": "Internal server error."
126+
}
127+
}
128+
}
47129
}
48130
},
49131
"components": {},

0 commit comments

Comments
 (0)