Skip to content

upgrade(library_config): Fetch config path from registry #935

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions library-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ bench = false
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9.34"
anyhow = "1.0"
known-folders = "1.2.0"

[target.'cfg(windows)'.dependencies]
winreg = "0.55"

[dev-dependencies]
tempfile = { version = "3.3" }
54 changes: 49 additions & 5 deletions library-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use std::borrow::Cow;
use std::{borrow::Cow, path::PathBuf};
use std::cell::OnceCell;
use std::collections::HashMap;
use std::ops::Deref;
Expand Down Expand Up @@ -484,6 +484,40 @@ impl Target {
}
}

#[cfg(windows)]
fn get_programdata_for_product(product: &str) -> PathBuf {
use winreg::enums::*;
use winreg::RegKey;
let keyname = format!("SOFTWARE\\Datadog\\{}", product);
let hk_local_machine = RegKey::predef(HKEY_LOCAL_MACHINE);
match hk_local_machine.open_subkey_with_flags(&keyname, KEY_READ) {
Ok(key) => {
match key.get_value::<String, _>("ConfigRoot") {
Ok(val) => PathBuf::from(val),
Err(_) => {
get_default_programdata();
}
}
}
Err(_) => {
get_default_programdata();
}
}
}

#[cfg(windows)]
fn get_default_programdata() -> PathBuf {
use known_folders::{get_known_folder_path, KnownFolder};
let mut path = get_known_folder_path(KnownFolder::ProgramData);
path.push("Datadog");
path
}

#[cfg(not(windows))]
fn get_programdata_for_product(_product: &str) -> PathBuf {
PathBuf::from("C:\\ProgramData\\Datadog")
}

impl Configurator {
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
pub const FLEET_STABLE_CONFIGURATION_PATH: &'static str =
Expand All @@ -493,19 +527,29 @@ impl Configurator {
pub const LOCAL_STABLE_CONFIGURATION_PATH: &'static str =
Self::local_stable_configuration_path(Target::current());

pub const fn local_stable_configuration_path(target: Target) -> &'static str {
pub fn local_stable_configuration_path(target: Target) -> Cow<'static, str> {
match target {
Target::Linux => "/etc/datadog-agent/application_monitoring.yaml",
Target::Macos => "/opt/datadog-agent/etc/application_monitoring.yaml",
Target::Windows => "C:\\ProgramData\\Datadog\\application_monitoring.yaml",
// Target::Windows => "C:\\ProgramData\\Datadog\\application_monitoring.yaml",
Target::Windows => {
let mut path = get_programdata_for_product("Datadog Agent");
path.push("application_monitoring.yaml");
Box::leak(path.into_boxed_path()).to_str().unwrap()
}
}
}

pub const fn fleet_stable_configuration_path(target: Target) -> &'static str {
pub fn fleet_stable_configuration_path(target: Target) -> &'static str {
match target {
Target::Linux => "/etc/datadog-agent/managed/datadog-agent/stable/application_monitoring.yaml",
Target::Macos => "/opt/datadog-agent/etc/stable/application_monitoring.yaml",
Target::Windows => "C:\\ProgramData\\Datadog\\managed\\datadog-agent\\stable\\application_monitoring.yaml",
// Target::Windows => "C:\\ProgramData\\Datadog\\managed\\datadog-agent\\stable\\application_monitoring.yaml",
Target::Windows => {
let mut path = get_programdata_for_product("Datadog Agent");
path.push("configs\\datadog_agent\\stable\\application_monitoring.yaml");
Box::leak(path.into_boxed_path()).to_str().unwrap()
}
}
}

Expand Down
Loading