Skip to content

Commit e904761

Browse files
authored
chore: Replace abandoned bincode with postcard (#1608)
Bincode was abandoned. The usecase is very small (only for the auth caching). Replace it with a similar `postcard`. On the way ensure that the cache file is setting reasonable file permissions.
1 parent 9f6a1bc commit e904761

File tree

4 files changed

+62
-56
lines changed

4 files changed

+62
-56
lines changed

Cargo.lock

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

deny.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ ignore = [
7373
#"RUSTSEC-0000-0000",
7474
"RUSTSEC-2021-0127", # serde_cbor as optional transitive dep: https://github.com/mozilla/authenticator-rs/issues/327
7575
"RUSTSEC-2024-0436",
76+
# ratatui is going to be updated soon
77+
"RUSTSEC-2026-0002",
7678
#{ id = "RUSTSEC-0000-0000", reason = "you can specify a reason the advisory is ignored" },
7779
#"[email protected]", # you can also ignore yanked crate versions if you wish
7880
#{ crate = "[email protected]", reason = "you can specify why you are ignoring the yanked crate" },

openstack_sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ passkey = ["keystone_ng", "dep:webauthn-authenticator-rs", "dep:webauthn-rs-prot
4646
[dependencies]
4747
async-trait = {workspace = true}
4848
base64 = { workspace = true }
49-
bincode = { version = "^2.0", default-features = false, features = ["serde", "std"] }
5049
bytes = {workspace = true}
5150
chrono = { workspace= true }
5251
config = { workspace = true, features = ["yaml"] }
@@ -63,6 +62,7 @@ hyper-util = { version = "^0.1", features = ["full"] }
6362
itertools = { workspace = true }
6463
json-patch = { workspace = true }
6564
open.workspace = true
65+
postcard = { version = "1.1", default-features = false, features = ["use-std"] }
6666
regex = { workspace = true }
6767
reqwest = { workspace = true, features = ["gzip", "deflate", "form", "http2",
6868
"socks", "system-proxy"] }

openstack_sdk/src/state.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,15 @@ use serde::{Deserialize, Serialize};
2222
use std::collections::HashMap;
2323
use std::fs::{DirBuilder, File};
2424
use std::io::prelude::*;
25+
use std::os::unix::fs::PermissionsExt;
2526
use std::path::PathBuf;
26-
//use thiserror::Error;
2727
use tracing::{debug, info, trace, warn};
2828

2929
use crate::auth::{
3030
authtoken::{AuthToken, AuthTokenScope},
3131
AuthState,
3232
};
3333

34-
// /// Errors which may occur when creating connection state data.
35-
// #[derive(Debug, Error)]
36-
// #[non_exhaustive]
37-
// pub enum StateError {
38-
// #[error("failed to deserialize config: {}", source)]
39-
// Parse {
40-
// /// The source of the error.
41-
// #[from]
42-
// source: config::ConfigError,
43-
// },
44-
// #[error("IO error: {}", source)]
45-
// IO {
46-
// /// The source of the error.
47-
// #[from]
48-
// source: std::io::Error,
49-
// },
50-
// }
51-
5234
/// A HashMap of Scope to Token
5335
#[derive(Clone, Default, Deserialize, Serialize, Debug)]
5436
pub(crate) struct ScopeAuths(HashMap<AuthTokenScope, AuthToken>);
@@ -246,37 +228,34 @@ impl State {
246228
Ok(mut file) => {
247229
let mut contents = vec![];
248230
match file.read_to_end(&mut contents) {
249-
Ok(_) => match bincode::serde::decode_from_slice(
250-
&contents,
251-
bincode::config::legacy(),
252-
) {
253-
Ok::<(ScopeAuths, usize), _>((mut auth, _)) => {
231+
Ok(_) => match postcard::from_bytes::<ScopeAuths>(&contents) {
232+
Ok(mut auth) => {
254233
auth.filter_invalid_auths();
255234
trace!("Cached Auth info: {:?}", auth);
256235
Some(auth)
257236
}
258237
Err(x) => {
259238
info!(
260-
"Corrupted cache file {}: {:?}. Removing ",
239+
"Corrupted cache file `{}`: {:?}. Removing ",
261240
fname.display(),
262241
x
263242
);
264243
let _ = std::fs::remove_file(fname);
265244
None
266245
}
267246
},
268-
_ => {
247+
Err(e) => {
269248
// Not able to read file, maybe it is corrupted. There is nothing user can
270249
// or is expected to do about it, but it make sense to make user aware of.
271-
info!("Error reading file {}", fname.display());
250+
info!("Error reading file `{}`: {:?}", fname.display(), e);
272251
None
273252
}
274253
}
275254
}
276-
_ => {
255+
Err(e) => {
277256
// Not able to open file, maybe it is missing. There is nothing user can or is
278257
// expected to do about it.
279-
debug!("Error opening file {}", fname.display());
258+
debug!("Error opening file `{}`: {:?}", fname.display(), e);
280259
None
281260
}
282261
}
@@ -291,17 +270,25 @@ impl State {
291270

292271
let _ = state.0.insert(scope.clone(), data.clone());
293272

294-
match bincode::serde::encode_to_vec(&state, bincode::config::legacy()) {
295-
Ok(ser_data) => match File::create(fname.as_path()) {
296-
Ok(mut file) => {
297-
let _ = file.write_all(&ser_data);
273+
match File::create(fname.as_path()) {
274+
Ok(mut file) => {
275+
match file.metadata() {
276+
Ok(metadata) => {
277+
let mut permissions = metadata.permissions();
278+
permissions.set_mode(0o600);
279+
let _ = file.set_permissions(permissions);
280+
}
281+
Err(_) => {
282+
warn!("Cannot set permissions for the cache file");
283+
return;
284+
}
298285
}
299-
_ => {
300-
warn!("Error writing state file");
286+
if let Err(e) = postcard::to_io(&state, &mut file) {
287+
warn!("Error serializing state: {:?}", e);
301288
}
302-
},
303-
Err(e) => {
304-
warn!("Error serializing state, {:?}", e);
289+
}
290+
_ => {
291+
warn!("Error writing state file");
305292
}
306293
}
307294
}

0 commit comments

Comments
 (0)