Skip to content

Commit

Permalink
CLI now sends machine host + username to show in mirrord operator sta…
Browse files Browse the repository at this point in the history
…tus (metalbear-co#2197)

* CLI now sends machine host + username to show in mirrord operator status

* ..

* fmt
  • Loading branch information
aviramha authored Jan 24, 2024
1 parent d40c80d commit 803fc38
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions changelog.d/+better-name-in-operator.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CLI now sends machine host + username to show in mirrord operator status
(not sent to our cloud!)
8 changes: 4 additions & 4 deletions mirrord/auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ edition.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
default = ["client"]
client = [
"dep:gethostname",
"dep:home",
"dep:fs4",
"dep:k8s-openapi",
"dep:kube",
"dep:serde_yaml",
"dep:tokio"
"dep:tokio",
"dep:whoami"
]

[dependencies]
chrono = "0.4"
gethostname = { version = "0.4", optional = true }
whoami = { version = "1", optional = true }
home = { version = "0.5", optional = true }
pem = "2"
fs4 = { version = "0.6", features = ["tokio-async"], optional = true }
Expand Down
27 changes: 22 additions & 5 deletions mirrord/auth/src/credential_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ pub struct CredentialStore {
credentials: HashMap<String, Credentials>,
}

/// Information about user gathered from the local system to be shared with the operator
/// for better status reporting.
#[derive(Default, Debug)]
pub struct UserIdentity {
/// User's name
pub name: Option<String>,
/// User's hostname
pub hostname: Option<String>,
}

impl UserIdentity {
pub fn load() -> Self {
Self {
// next release of whoami (v2) will have fallible types
// so keep this Option for then :)
name: Some(whoami::realname()),
hostname: Some(whoami::hostname()),
}
}
}

impl CredentialStore {
/// Load contents of store from file
async fn load<R: AsyncRead + Unpin>(source: &mut R) -> Result<Self> {
Expand Down Expand Up @@ -84,11 +105,7 @@ impl CredentialStore {
};

if !credentials.is_ready() {
let common_name = self
.common_name
.clone()
.or_else(|| gethostname::gethostname().into_string().ok())
.unwrap_or_default();
let common_name = self.common_name.clone().unwrap_or_else(whoami::hostname);

credentials
.get_client_certificate::<R>(client.clone(), &common_name)
Expand Down
2 changes: 1 addition & 1 deletion mirrord/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde_json.workspace = true
thiserror.workspace = true
tracing.workspace = true
serde_yaml = "0.9"
toml = "0.7"
toml = "0.8"
schemars = { version = "0.8.11" }
bimap.workspace = true
nom = "7.1"
Expand Down
14 changes: 13 additions & 1 deletion mirrord/operator/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use http::request::Request;
use kube::{api::PostParams, Api, Client, Resource};
use mirrord_analytics::{AnalyticsHash, AnalyticsOperatorProperties, AnalyticsReporter};
use mirrord_auth::{
certificate::Certificate, credential_store::CredentialStoreSync, credentials::LicenseValidity,
certificate::Certificate,
credential_store::{CredentialStoreSync, UserIdentity},
credentials::LicenseValidity,
};
use mirrord_config::{
feature::network::incoming::ConcurrentSteal,
Expand Down Expand Up @@ -492,11 +494,21 @@ impl OperatorApi {
self.check_no_port_locks(target).await?;
}

let UserIdentity { name, hostname } = UserIdentity::load();

let request = {
let mut builder = Request::builder()
.uri(self.connect_url(&session_info))
.header("x-session-id", session_info.metadata.session_id.to_string());

if let Some(name) = name {
builder = builder.header("x-client-name", name);
};

if let Some(hostname) = hostname {
builder = builder.header("x-client-hostname", hostname);
};

match session_info.metadata.client_credentials() {
Ok(Some(credentials)) => {
builder = builder.header("x-client-der", credentials);
Expand Down

0 comments on commit 803fc38

Please sign in to comment.