From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9FE976CF15 for ; Wed, 31 Mar 2021 12:22:26 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9726DDE55 for ; Wed, 31 Mar 2021 12:22:26 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 07C50DDCE for ; Wed, 31 Mar 2021 12:22:24 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 53A994599C for ; Wed, 31 Mar 2021 12:22:23 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 31 Mar 2021 12:21:46 +0200 Message-Id: <20210331102202.14767-5-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210331102202.14767-1-s.reiter@proxmox.com> References: <20210331102202.14767-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.019 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH v3 proxmox-backup 04/20] vsock_client: support authorization header X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 Mar 2021 10:22:26 -0000 Pass in an optional auth tag, which will be passed as an Authorization header on every subsequent call. Signed-off-by: Stefan Reiter --- new in v3 src/client/vsock_client.rs | 64 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/client/vsock_client.rs b/src/client/vsock_client.rs index a7740ac2..5dd9eb4b 100644 --- a/src/client/vsock_client.rs +++ b/src/client/vsock_client.rs @@ -137,22 +137,28 @@ pub struct VsockClient { client: Client, cid: i32, port: u16, + auth: Option, } impl VsockClient { - pub fn new(cid: i32, port: u16) -> Self { + pub fn new(cid: i32, port: u16, auth: Option) -> Self { let conn = VsockConnector {}; let client = Client::builder().build::<_, Body>(conn); - Self { client, cid, port } + Self { + client, + cid, + port, + auth, + } } pub async fn get(&self, path: &str, data: Option) -> Result { - let req = Self::request_builder(self.cid, self.port, "GET", path, data)?; + let req = self.request_builder("GET", path, data)?; self.api_request(req).await } pub async fn post(&self, path: &str, data: Option) -> Result { - let req = Self::request_builder(self.cid, self.port, "POST", path, data)?; + let req = self.request_builder("POST", path, data)?; self.api_request(req).await } @@ -162,7 +168,7 @@ impl VsockClient { data: Option, output: &mut (dyn AsyncWrite + Send + Unpin), ) -> Result<(), Error> { - let req = Self::request_builder(self.cid, self.port, "GET", path, data)?; + let req = self.request_builder("GET", path, data)?; let client = self.client.clone(); @@ -210,47 +216,43 @@ impl VsockClient { .await } - pub fn request_builder( - cid: i32, - port: u16, + fn request_builder( + &self, method: &str, path: &str, data: Option, ) -> Result, Error> { let path = path.trim_matches('/'); - let url: Uri = format!("vsock://{}:{}/{}", cid, port, path).parse()?; + let url: Uri = format!("vsock://{}:{}/{}", self.cid, self.port, path).parse()?; + + let make_builder = |content_type: &str, url: &Uri| { + let mut builder = Request::builder() + .method(method) + .uri(url) + .header(hyper::header::CONTENT_TYPE, content_type); + if let Some(auth) = &self.auth { + builder = builder.header(hyper::header::AUTHORIZATION, auth); + } + builder + }; if let Some(data) = data { if method == "POST" { - let request = Request::builder() - .method(method) - .uri(url) - .header(hyper::header::CONTENT_TYPE, "application/json") - .body(Body::from(data.to_string()))?; + let builder = make_builder("application/json", &url); + let request = builder.body(Body::from(data.to_string()))?; return Ok(request); } else { let query = tools::json_object_to_query(data)?; - let url: Uri = format!("vsock://{}:{}/{}?{}", cid, port, path, query).parse()?; - let request = Request::builder() - .method(method) - .uri(url) - .header( - hyper::header::CONTENT_TYPE, - "application/x-www-form-urlencoded", - ) - .body(Body::empty())?; + let url: Uri = + format!("vsock://{}:{}/{}?{}", self.cid, self.port, path, query).parse()?; + let builder = make_builder("application/x-www-form-urlencoded", &url); + let request = builder.body(Body::empty())?; return Ok(request); } } - let request = Request::builder() - .method(method) - .uri(url) - .header( - hyper::header::CONTENT_TYPE, - "application/x-www-form-urlencoded", - ) - .body(Body::empty())?; + let builder = make_builder("application/x-www-form-urlencoded", &url); + let request = builder.body(Body::empty())?; Ok(request) } -- 2.20.1