Skip to content

Commit 87003fa

Browse files
committed
api: fix request methods
The API did not match what is actually possible with the kernel API. Instead, just offer two simple methods which do what is needed. Also remove the old test which doesn't match what the API looks like anymore.
1 parent 05b8ddb commit 87003fa

File tree

1 file changed

+29
-138
lines changed

1 file changed

+29
-138
lines changed

src/api.rs

Lines changed: 29 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ fn into_serial(res: libc::c_long) -> KeyringSerial {
6666
KeyringSerial::new(res as i32).unwrap()
6767
}
6868

69+
/// Request a key from the kernel.
6970
fn request_impl<K: KeyType>(
7071
description: &str,
7172
info: Option<&str>,
@@ -122,31 +123,24 @@ impl Keyring {
122123

123124
/// Requests a keyring with the given description by searching the thread, process, and session
124125
/// keyrings.
125-
pub fn request<D>(description: D) -> Result<Self>
126+
///
127+
/// If it is not found, the `info` string (if provided) will be handed off to
128+
/// `/sbin/request-key` to generate the key.
129+
///
130+
/// If `target` is given, the found keyring will be linked into it. If `target` is not given
131+
/// and a new key is constructed due to the request, it will be linked into the default
132+
/// keyring (see `Keyring::set_default`).
133+
pub fn request<'a, D, I, IS, T>(description: D, info: I, target: T) -> Result<Self>
126134
where
127135
D: AsRef<str>,
136+
I: Into<Option<IS>>,
137+
IS: AsRef<str>,
138+
T: Into<Option<TargetKeyring<'a>>>,
128139
{
129140
check_call_keyring(request_impl::<keytypes::Keyring>(
130141
description.as_ref(),
131-
None,
132-
None,
133-
))
134-
}
135-
136-
/// Requests a keyring with the given description by searching the thread, process, and session
137-
/// keyrings.
138-
///
139-
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
140-
/// the key.
141-
pub fn request_with_fallback<D, I>(description: D, info: I) -> Result<Self>
142-
where
143-
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
144-
I: AsRef<str>,
145-
{
146-
check_call_keyring(request_impl::<keytypes::Keyring>(
147-
&description.borrow().description(),
148-
Some(info.as_ref()),
149-
None,
142+
info.into().as_ref().copied(),
143+
target.into().map(TargetKeyring::serial),
150144
))
151145
}
152146

@@ -371,74 +365,6 @@ impl Keyring {
371365
check_call_keyring(self.add_key_impl::<keytypes::Keyring>(description.borrow(), &()))
372366
}
373367

374-
/// Requests a key with the given description by searching the thread, process, and session
375-
/// keyrings.
376-
///
377-
/// If it is found, it is attached to the keyring.
378-
pub fn request_key<K, D>(&self, description: D) -> Result<Key>
379-
where
380-
K: KeyType,
381-
D: Borrow<K::Description>,
382-
{
383-
check_call_key(request_impl::<K>(
384-
&description.borrow().description(),
385-
None,
386-
Some(self.id),
387-
))
388-
}
389-
390-
/// Requests a keyring with the given description by searching the thread, process, and session
391-
/// keyrings.
392-
///
393-
/// If it is found, it is attached to the keyring.
394-
pub fn request_keyring<D>(&self, description: D) -> Result<Self>
395-
where
396-
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
397-
{
398-
check_call_keyring(request_impl::<keytypes::Keyring>(
399-
&description.borrow().description(),
400-
None,
401-
Some(self.id),
402-
))
403-
}
404-
405-
/// Requests a key with the given description by searching the thread, process, and session
406-
/// keyrings.
407-
///
408-
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
409-
/// the key. If found, it will be attached to the current keyring. Requires `write` permission
410-
/// to the keyring.
411-
pub fn request_key_with_fallback<K, D, I>(&self, description: D, info: I) -> Result<Key>
412-
where
413-
K: KeyType,
414-
D: Borrow<K::Description>,
415-
I: AsRef<str>,
416-
{
417-
check_call_key(request_impl::<K>(
418-
&description.borrow().description(),
419-
Some(info.as_ref()),
420-
Some(self.id),
421-
))
422-
}
423-
424-
/// Requests a keyring with the given description by searching the thread, process, and session
425-
/// keyrings.
426-
///
427-
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
428-
/// the key. If found, it will be attached to the current keyring. Requires `write` permission
429-
/// to the keyring.
430-
pub fn request_keyring_with_fallback<D, I>(&self, description: D, info: I) -> Result<Self>
431-
where
432-
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
433-
I: AsRef<str>,
434-
{
435-
check_call_keyring(request_impl::<keytypes::Keyring>(
436-
&description.borrow().description(),
437-
Some(info.as_ref()),
438-
Some(self.id),
439-
))
440-
}
441-
442368
/// Revokes the keyring.
443369
///
444370
/// Requires `write` permission on the keyring.
@@ -561,35 +487,27 @@ impl Key {
561487
self.id
562488
}
563489

564-
/// Requests a key with the given description by searching the thread, process, and session
565-
/// keyrings.
566-
pub fn request<K, D>(description: D) -> Result<Self>
567-
where
568-
K: KeyType,
569-
D: Borrow<K::Description>,
570-
{
571-
check_call_key(request_impl::<K>(
572-
&description.borrow().description(),
573-
None,
574-
None,
575-
))
576-
}
577-
578-
/// Requests a key with the given description by searching the thread, process, and session
579-
/// keyrings.
490+
/// Requests a key with the given type and description by searching the thread, process, and
491+
/// session keyrings.
580492
///
581-
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
582-
/// the key.
583-
pub fn request_with_fallback<K, D, I>(description: D, info: I) -> Result<Self>
493+
/// If it is not found, the `info` string (if provided) will be handed off to
494+
/// `/sbin/request-key` to generate the key.
495+
///
496+
/// If `target` is given, the found keyring will be linked into it. If `target` is not given
497+
/// and a new key is constructed due to the request, it will be linked into the default
498+
/// keyring (see `Keyring::set_default`).
499+
pub fn request<'a, K, D, I, IS, T>(description: D, info: I, target: T) -> Result<Self>
584500
where
585501
K: KeyType,
586502
D: Borrow<K::Description>,
587-
I: AsRef<str>,
503+
I: Into<Option<IS>>,
504+
IS: AsRef<str>,
505+
T: Into<Option<TargetKeyring<'a>>>,
588506
{
589-
check_call_key(request_impl::<keytypes::Keyring>(
507+
check_call_key(request_impl::<K>(
590508
&description.borrow().description(),
591-
Some(info.as_ref()),
592-
None,
509+
info.into().as_ref().copied(),
510+
target.into().map(TargetKeyring::serial),
593511
))
594512
}
595513

@@ -911,30 +829,3 @@ impl KeyManager {
911829
})
912830
}
913831
}
914-
915-
#[cfg(test)]
916-
mod tests {
917-
use super::*;
918-
use crate::tests::utils;
919-
920-
#[test]
921-
fn test_request_key() {
922-
let mut keyring = utils::new_test_keyring();
923-
924-
// Create the key.
925-
let description = "test:rust-keyutils:request_key";
926-
let payload = "payload";
927-
let key = keyring
928-
.add_key::<keytypes::User, _, _>(description, payload.as_bytes())
929-
.unwrap();
930-
931-
let found_key = keyring
932-
.request_key::<keytypes::User, _>(description)
933-
.unwrap();
934-
assert_eq!(found_key, key);
935-
936-
// Clean up.
937-
keyring.unlink_key(&key).unwrap();
938-
keyring.invalidate().unwrap();
939-
}
940-
}

0 commit comments

Comments
 (0)