Skip to content

Commit e7c8989

Browse files
authored
Merge pull request #85 from hug-dev/new-spiffe
Add SPIFFE authentication via the new crate
2 parents ce0b7fc + 2a0a200 commit e7c8989

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ derivative = "2.1.1"
1919
zeroize = "1.1.0"
2020
users = "0.10.0"
2121
url = "2.2.0"
22+
spiffe = { version = "0.1.1", optional = true }
2223

2324
[dev-dependencies]
2425
mockstream = "0.0.3"
2526

2627
[features]
2728
default = []
29+
spiffe-auth = ["spiffe"]
2830
testing = ["parsec-interface/testing"]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
This repository contains a Rust client for consuming the API provided by the [Parsec service](https://github.com/parallaxsecond/parsec).
1010
The low-level functionality that this library uses for IPC is implemented in the [interface crate](https://github.com/parallaxsecond/parsec-interface-rs).
1111

12-
Check out the `spiffe` branch for JWT SVID authentication feature.
12+
When using the JWT-SVID authentication method, the client will expect the `SPIFFE_ENDPOINT_SOCKET` environment variable to contain the path of the Workload API endpoint.
13+
See the [SPIFFE Workload Endpoint](https://github.com/spiffe/spiffe/blob/master/standards/SPIFFE_Workload_Endpoint.md#4-locating-the-endpoint) for more information.
1314

1415
## Locating the Parsec endpoint
1516

src/auth.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub enum Authentication {
2121
/// Used for authentication via Peer Credentials provided by Unix
2222
/// operating systems for Domain Socket connections.
2323
UnixPeerCredentials,
24+
/// Authentication using JWT SVID tokens. The will fetch its JWT-SVID and pass it in the
25+
/// Authentication field. The socket endpoint is found through the SPIFFE_ENDPOINT_SOCKET
26+
/// environment variable.
27+
#[cfg(feature = "spiffe-auth")]
28+
JwtSvid,
2429
}
2530

2631
impl Authentication {
@@ -30,6 +35,8 @@ impl Authentication {
3035
Authentication::None => AuthType::NoAuth,
3136
Authentication::Direct(_) => AuthType::Direct,
3237
Authentication::UnixPeerCredentials => AuthType::UnixPeerCredentials,
38+
#[cfg(feature = "spiffe-auth")]
39+
Authentication::JwtSvid => AuthType::JwtSvid,
3340
}
3441
}
3542
}
@@ -45,6 +52,20 @@ impl TryFrom<&Authentication> for RequestAuth {
4552
let current_uid = users::get_current_uid();
4653
Ok(RequestAuth::new(current_uid.to_le_bytes().to_vec()))
4754
}
55+
#[cfg(feature = "spiffe-auth")]
56+
Authentication::JwtSvid => {
57+
use crate::error::ClientErrorKind;
58+
use log::error;
59+
use spiffe::workload_api::client::WorkloadApiClient;
60+
61+
let client = WorkloadApiClient::default().unwrap();
62+
let token = client.fetch_jwt_token(&["parsec"], None).map_err(|e| {
63+
error!("Error while fetching the JWT-SVID ({}).", e);
64+
Error::Client(ClientErrorKind::Spiffe(e))
65+
})?;
66+
67+
Ok(RequestAuth::new(token.as_bytes().into()))
68+
}
4869
}
4970
}
5071
}
@@ -57,6 +78,8 @@ impl PartialEq for Authentication {
5778
(Authentication::Direct(app_name), Authentication::Direct(other_app_name)) => {
5879
app_name == other_app_name
5980
}
81+
#[cfg(feature = "spiffe-auth")]
82+
(Authentication::JwtSvid, Authentication::JwtSvid) => true,
6083
_ => false,
6184
}
6285
}

src/core/basic_client.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ impl BasicClient {
263263
AuthType::UnixPeerCredentials => {
264264
self.auth_data = Authentication::UnixPeerCredentials
265265
}
266+
#[cfg(feature = "spiffe-auth")]
267+
AuthType::JwtSvid => self.auth_data = Authentication::JwtSvid,
266268
auth => {
267269
warn!(
268270
"Authenticator of type \"{:?}\" not supported by this client library",

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub enum ClientErrorKind {
4848
InvalidSocketAddress,
4949
/// The socket URL is invalid
5050
InvalidSocketUrl,
51+
/// Error while using the SPIFFE Workload API
52+
#[cfg(feature = "spiffe-auth")]
53+
Spiffe(spiffe::workload_api::client::ClientError),
5154
}
5255

5356
impl From<ClientErrorKind> for Error {
@@ -80,6 +83,8 @@ impl fmt::Display for ClientErrorKind {
8083
ClientErrorKind::NotFound => write!(f, "one of the resources required in the operation was not found"),
8184
ClientErrorKind::InvalidSocketAddress => write!(f, "the socket address provided in the URL is not valid"),
8285
ClientErrorKind::InvalidSocketUrl => write!(f, "the socket URL is invalid"),
86+
#[cfg(feature = "spiffe-auth")]
87+
ClientErrorKind::Spiffe(error) => error.fmt(f),
8388
}
8489
}
8590
}

tests/ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set -euf -o pipefail
1313
################
1414
RUST_BACKTRACE=1 cargo build
1515
RUST_BACKTRACE=1 cargo build --features testing
16+
RUST_BACKTRACE=1 cargo build --features spiffe-auth
1617
RUST_BACKTRACE=1 cargo build --no-default-features
1718

1819
#################

0 commit comments

Comments
 (0)