Compare commits

...

2 Commits

3 changed files with 48 additions and 27 deletions

View File

@ -6,12 +6,15 @@ use axum::Json;
use serde::{Deserialize,Serialize};
use crate::*;
use crate::PowerDnsOidcTsigkeyError;
use url::Url;
pub async fn list_keys(
State(state): State<Arc<AppState>>,
) -> Result<Json<Vec<TsigKey>>, PowerDnsOidcTsigkeyError> {
let req = state.http_client.get::<String>((config_cell.get().unwrap().powerdns.url.to_string() + "/servers/localhost/tsigkeys").into())
.header("X-API-Key", config_cell.get().unwrap().powerdns.api_token.clone());
let req = state.http_client.get::<String>(
get_url(state.config.powerdns.url.clone(), "localhost".to_owned(), format!("tsigkeys"))
)
.header("X-API-Key", state.config.powerdns.api_token.clone());
let response = req
.send()
.await?;
@ -30,9 +33,11 @@ pub async fn list_key(
) -> PowerDnsOidcTsigkeyResult<Json<TsigKey>> {
let key: TsigKey = parse_json::<PowerDnsTsigKey>(
state.http_client.get::<String>(
(config_cell.get().unwrap().powerdns.url.to_string() + format!("/servers/localhost/tsigkeys/{}", key_id).as_str()).into()
get_url(state.config.powerdns.url.clone(),
"localhost".to_owned(),
format!("tsigkeys/{}", key_id))
)
.header("X-API-Key", config_cell.get().unwrap().powerdns.api_token.clone())
.header("X-API-Key", state.config.powerdns.api_token.clone())
.send()
.await?
)
@ -40,11 +45,27 @@ pub async fn list_key(
.into();
Ok(axum::Json(key))
}
//
//pub async fn create_key(
// State(state): State<Arc<AppState>>
//) -> PowerDnsOidcTsigkeyResult<Json<TsigKey>> {}
//
pub async fn create_key(
State(state): State<Arc<AppState>>,
body: String,
) -> PowerDnsOidcTsigkeyResult<Json<TsigKey>> {
let key: TsigKey = parse_json::<PowerDnsTsigKey>(
state.http_client.post(
get_url(state.config.powerdns.url.clone(), "localhost".to_owned(), "tsigkeys".to_owned())
)
.header("X-API-Key", state.config.powerdns.api_token.clone())
.header("Content-Type", "application/json")
.body(body)
.send()
.await?
)
.await?
.into();
Ok(axum::Json(key))
}
//pub async fn modify_key(
// Path(key_id): Path<String>,
// State(state): State<Arc<AppState>>
@ -57,6 +78,9 @@ pub async fn list_key(
//
//}
fn get_url(powerdns_url: Url, server: String, endpoint: String) -> String {
format!("{}/servers/{}/{}", powerdns_url.to_string(), server, endpoint).as_str().into()
}
#[derive(Serialize, Debug)]
pub struct TsigKeyList {

View File

@ -7,10 +7,8 @@ use std::{
fmt::Display,
};
use tokio::sync::OnceCell;
use axum::{
routing::{get},
routing::{get, post},
Router,
http::StatusCode,
response::IntoResponse,
@ -125,24 +123,23 @@ async fn parse_json<Out: DeserializeOwned>(res: ReqwestResponse) -> PowerDnsOidc
#[derive(Clone, Debug)]
pub struct AppState {
http_client: Client,
config: PowerDnsOidcTsigkeyConfig,
}
static config_cell: OnceCell<PowerDnsOidcTsigkeyConfig> = OnceCell::const_new();
#[tokio::main]
async fn main() {
match settings::PowerDnsOidcTsigkeyConfig::load("config.yaml") {
Ok(config) => {
config_cell.set(config).unwrap();
run().await;
println!("Configuration loaded!");
run(config).await;
},
Err(e) => println!("Failed to load config.yaml: {:?}", e),
};
}
async fn run() {
let addr: SocketAddr = (config_cell.get().unwrap().server.bind_address, config_cell.get().unwrap().server.port).into();
let state = AppState { http_client: reqwest::Client::new() };
async fn run(config: PowerDnsOidcTsigkeyConfig) {
let addr: SocketAddr = (config.server.bind_address, config.server.port).into();
let state = AppState { http_client: reqwest::Client::new(), config: config.clone() };
// let router = create_router(state);
let auth: JwtAuthorizer = JwtAuthorizer::from_oidc(&config_cell.get().unwrap().oidc.issuer.clone().to_string())
.validation(Validation::new()
@ -154,7 +151,7 @@ async fn run() {
let router = Router::new()
.route("/api/v1/tsigkeys", get(api::list_keys))
// .route("/api/v1/tsigkeys/create", post(api::create_key))
.route("/api/v1/tsigkeys/create", post(api::create_key))
.route("/api/v1/tsigkeys/:keyid", get(api::list_key))
// put(api::create_key).delete(api::delete_key).get(api::list_key))
.layer(auth.layer().await.unwrap())

View File

@ -4,7 +4,7 @@ use url::Url;
use serde::{Deserialize};
use config::{Config, ConfigError, Environment, File};
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct PowerDnsOidcTsigkeyConfig {
/// OIDC Provider
pub oidc: OidcConfig,
@ -16,7 +16,7 @@ pub struct PowerDnsOidcTsigkeyConfig {
pub server: ServerConfig,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct PowerDnsConfig {
/// URL where PowerDNS API can be reached
pub url: Url,
@ -24,13 +24,13 @@ pub struct PowerDnsConfig {
pub api_token: String,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct LogConfig {
/// The log level
pub level: String,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct ServerConfig {
/// IpAddress to listen on
pub bind_address: IpAddr,
@ -40,12 +40,12 @@ pub struct ServerConfig {
pub tls: ServerTlsConfig,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct ServerTlsConfig {
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct OidcConfig {
pub issuer: Url,
pub client_id: String,
@ -55,7 +55,7 @@ pub struct OidcConfig {
pub validation: OidcValidationConfig,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct OidcValidationConfig {
pub issuer: Vec<Url>,
pub audience: Vec<String>,