-
-
Notifications
You must be signed in to change notification settings - Fork 33
Implement posture checks #871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ef0b63d
nix flake update
j-chmielewski 552c5f6
initial work on posture checks on old UI
j-chmielewski 01c97f1
nix flake update
j-chmielewski 9bdd8cf
posture check for non-mfa locations
j-chmielewski a41bb1e
posture checks during MFA connection flow
j-chmielewski d8013be
cargo update; extend glib trivyignore
j-chmielewski b0a1a93
fix clippy issue
j-chmielewski 465ce52
fix typing
j-chmielewski 3740ff6
update protos, restructure protos module
j-chmielewski 5cae43b
fix proto imports; only windows gets postures from service
j-chmielewski a7286ff
remove console.logs
j-chmielewski e713b03
Merge branch 'dev' into posture-checks
j-chmielewski 33989ba
allow dead code
j-chmielewski 43ceab6
fix location id; better error message
j-chmielewski 9bac643
whitelist new tauri commands
j-chmielewski e862913
fix windows compilation
j-chmielewski 9117a18
Merge branch 'dev' into posture-checks
j-chmielewski 2b74ac2
import Id
j-chmielewski b93294a
remove unused command argument
j-chmielewski eb9c4d5
post_with_headers helper
j-chmielewski 0f9fc2f
fix windows proto import
j-chmielewski b334470
update proto submodule
j-chmielewski cd82d72
move non-mfa posture connection fully to backend
j-chmielewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ in | |
| trunk | ||
| sqlx-cli | ||
| vtsls | ||
| trivy | ||
| ]; | ||
|
|
||
| shellHook = with pkgs; '' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| pub mod inspector; | ||
| pub mod models; | ||
| pub mod periodic; | ||
| pub mod posture; | ||
| pub mod provisioning; | ||
| pub mod service_locations; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| use std::time::Duration; | ||
|
|
||
| use reqwest::{Client, StatusCode}; | ||
| use serde::Deserialize; | ||
| use tauri::AppHandle; | ||
|
|
||
| use crate::{ | ||
| database::{ | ||
| models::{instance::Instance, location::Location, wireguard_keys::WireguardKeys}, | ||
| DB_POOL, | ||
| }, | ||
| error::Error, | ||
| service::proto::defguard::enterprise::posture::v2::{ | ||
| DevicePostureCheckRequest, DevicePostureCheckResponse, DevicePostureData, | ||
| }, | ||
| tray::{configure_tray_icon, reload_tray_menu}, | ||
| utils::{construct_platform_header, handle_connection_for_location}, | ||
| CLIENT_PLATFORM_HEADER, CLIENT_VERSION_HEADER, PKG_VERSION, | ||
| }; | ||
|
|
||
| const HTTP_TIMEOUT: Duration = Duration::from_secs(10); | ||
| const POSTURE_ENDPOINT: &str = "/api/v1/posture/connect"; | ||
|
|
||
| /// Collects device posture data, sends it to the proxy, and on success establishes | ||
| /// the WireGuard tunnel using the returned preshared key. | ||
| pub async fn connect_with_posture_check( | ||
| location_id: crate::database::models::Id, | ||
|
j-chmielewski marked this conversation as resolved.
Outdated
|
||
| handle: &AppHandle, | ||
| ) -> Result<(), Error> { | ||
| let location = Location::find_by_id(&*DB_POOL, location_id) | ||
| .await? | ||
| .ok_or(Error::NotFound)?; | ||
|
|
||
| let instance = Instance::find_by_id(&*DB_POOL, location.instance_id) | ||
| .await? | ||
| .ok_or(Error::NotFound)?; | ||
|
|
||
| let keys = WireguardKeys::find_by_instance_id(&*DB_POOL, location.instance_id) | ||
| .await? | ||
| .ok_or_else(|| { | ||
| Error::ResourceNotFound(format!( | ||
| "WireGuard keys not found for instance {}", | ||
| location.instance_id | ||
| )) | ||
| })?; | ||
|
|
||
| let posture_data = DevicePostureData::new(); | ||
|
|
||
| let request = DevicePostureCheckRequest { | ||
| location_id: location.network_id, | ||
| pubkey: keys.pubkey, | ||
| device_posture_data: Some(posture_data), | ||
| }; | ||
|
|
||
| let proxy_url = tauri::Url::parse(&instance.proxy_url) | ||
|
j-chmielewski marked this conversation as resolved.
|
||
| .map_err(|e| Error::InternalError(format!("Invalid proxy URL: {e}")))? | ||
| .join(POSTURE_ENDPOINT) | ||
| .map_err(|e| Error::InternalError(format!("Failed to build posture URL: {e}")))?; | ||
|
|
||
| debug!("Sending posture check request to {proxy_url}"); | ||
| let response = Client::new() | ||
| .post(proxy_url) | ||
| .json(&request) | ||
| .header(CLIENT_VERSION_HEADER, PKG_VERSION) | ||
| .header(CLIENT_PLATFORM_HEADER, construct_platform_header()) | ||
| .timeout(HTTP_TIMEOUT) | ||
| .send() | ||
| .await | ||
| .map_err(|e| Error::HttpError(e.to_string()))?; | ||
|
|
||
| match response.status() { | ||
| StatusCode::OK => { | ||
| let body: DevicePostureCheckResponse = response | ||
| .json() | ||
| .await | ||
| .map_err(|e| Error::HttpError(e.to_string()))?; | ||
| debug!("Posture check approved for location {location_id}, connecting..."); | ||
| handle_connection_for_location(&location, Some(body.preshared_key), handle).await?; | ||
|
j-chmielewski marked this conversation as resolved.
Outdated
|
||
| reload_tray_menu(handle).await; | ||
| configure_tray_icon(handle).await?; | ||
| info!("Connected to location {location} after posture check"); | ||
| Ok(()) | ||
| } | ||
| StatusCode::FORBIDDEN => { | ||
| #[derive(Deserialize)] | ||
| struct PostureRejection { | ||
| error: String, | ||
| } | ||
| let body: PostureRejection = response | ||
| .json() | ||
| .await | ||
| .map_err(|e| Error::HttpError(e.to_string()))?; | ||
| error!( | ||
| "Posture check rejected for location {location_id}: {}", | ||
| body.error | ||
| ); | ||
| Err(Error::PostureCheckFailed(body.error)) | ||
| } | ||
| status => Err(Error::HttpError(format!( | ||
| "Unexpected proxy response: {status}" | ||
| ))), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.