Skip to content

Commit f65f013

Browse files
committed
Add Unix Peer Credential auth support
This commit adds support for authentication using Unix Peer Credentials. This involves the library automatically populating the `Auth` field of every request with a serialized version of the UID. Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
1 parent dbd449a commit f65f013

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/auth.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum AuthenticationData {
1515
/// The `Secret` struct can be imported from
1616
/// `parsec_client::core::secrecy::Secret`.
1717
AppIdentity(Secret<String>),
18+
/// Used for authentication via
19+
UnixPeerCredentials,
1820
}
1921

2022
impl AuthenticationData {
@@ -23,6 +25,7 @@ impl AuthenticationData {
2325
match self {
2426
AuthenticationData::None => AuthType::NoAuth,
2527
AuthenticationData::AppIdentity(_) => AuthType::Direct,
28+
AuthenticationData::UnixPeerCredentials => AuthType::UnixPeerCredentials,
2629
}
2730
}
2831
}
@@ -34,6 +37,10 @@ impl From<&AuthenticationData> for RequestAuth {
3437
AuthenticationData::AppIdentity(name) => {
3538
RequestAuth::new(name.expose_secret().bytes().collect())
3639
}
40+
AuthenticationData::UnixPeerCredentials => {
41+
let current_uid = users::get_current_uid();
42+
RequestAuth::new(current_uid.to_le_bytes().to_vec())
43+
}
3744
}
3845
}
3946
}

src/core/testing/core_tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,25 @@ fn auth_value_test() {
774774
);
775775
}
776776

777+
#[test]
778+
fn peer_credential_auth_test() {
779+
let mut client: TestBasicClient = Default::default();
780+
client.set_auth_data(AuthenticationData::UnixPeerCredentials);
781+
client.set_mock_read(&get_response_bytes_from_result(
782+
NativeResult::PsaDestroyKey(operations::psa_destroy_key::Result {}),
783+
));
784+
let key_name = String::from("key-name");
785+
client
786+
.psa_destroy_key(key_name)
787+
.expect("Failed to call destroy key");
788+
789+
let req = get_req_from_bytes(client.get_mock_write());
790+
assert_eq!(
791+
&users::get_current_uid().to_le_bytes().to_vec(),
792+
req.auth.buffer.expose_secret()
793+
);
794+
}
795+
777796
#[test]
778797
fn failing_ipc_test() {
779798
let mut client: TestBasicClient = Default::default();

0 commit comments

Comments
 (0)