Skip to main content

mssf_core/runtime/
config.rs

1// ------------------------------------------------------------
2// Copyright (c) Microsoft Corporation.  All rights reserved.
3// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
4// ------------------------------------------------------------
5
6use crate::{WString, error::ErrorCode, strings::StringResult};
7use mssf_com::{
8    FabricRuntime::{IFabricConfigurationPackage, IFabricConfigurationPackage2},
9    FabricTypes::{
10        FABRIC_CONFIGURATION_PARAMETER, FABRIC_CONFIGURATION_PARAMETER_EX1,
11        FABRIC_CONFIGURATION_SECTION,
12    },
13};
14use windows_core::Interface;
15
16#[derive(Debug, Clone)]
17pub struct ConfigurationPackage {
18    com: IFabricConfigurationPackage2,
19}
20
21pub struct ConfigurationPackageDesc {
22    pub name: WString,
23    pub service_manifest_name: WString,
24    pub service_manifest_version: WString,
25    pub version: WString,
26}
27
28pub struct ConfigurationSettings {
29    pub sections: Vec<ConfigurationSection>,
30}
31
32impl From<IFabricConfigurationPackage2> for ConfigurationPackage {
33    fn from(com: IFabricConfigurationPackage2) -> Self {
34        Self { com }
35    }
36}
37
38impl From<IFabricConfigurationPackage> for ConfigurationPackage {
39    fn from(value: IFabricConfigurationPackage) -> Self {
40        let com = value.cast::<IFabricConfigurationPackage2>().unwrap();
41        Self::from(com)
42    }
43}
44
45impl From<ConfigurationPackage> for IFabricConfigurationPackage2 {
46    fn from(value: ConfigurationPackage) -> Self {
47        value.com
48    }
49}
50
51impl ConfigurationPackage {
52    pub fn get_description(&self) -> ConfigurationPackageDesc {
53        let raw = unsafe { self.com.get_Description().as_ref().unwrap() };
54
55        ConfigurationPackageDesc {
56            name: WString::from(raw.Name),
57            service_manifest_name: WString::from(raw.ServiceManifestName),
58            service_manifest_version: WString::from(raw.ServiceManifestVersion),
59            version: WString::from(raw.Version),
60        }
61    }
62
63    pub fn get_settings(&self) -> ConfigurationSettings {
64        let sections = unsafe { self.com.get_Settings().as_ref() }
65            .map(|list| {
66                unsafe { list.Sections.as_ref() }
67                    .map(|l| crate::iter::vec_from_raw_com(l.Count as usize, l.Items))
68                    .unwrap_or_default()
69            })
70            .unwrap_or_default();
71        ConfigurationSettings { sections }
72    }
73
74    pub fn get_path(&self) -> WString {
75        let raw = unsafe { self.com.get_Path() };
76        WString::from(raw)
77    }
78
79    pub fn get_section(&self, section_name: &WString) -> crate::Result<ConfigurationSection> {
80        let raw = unsafe { self.com.GetSection(section_name.as_pcwstr()) }?;
81        let raw_ref = unsafe { raw.as_ref() };
82        match raw_ref {
83            Some(c) => Ok(ConfigurationSection::from(c)),
84            None => Err(ErrorCode::E_POINTER.into()),
85        }
86    }
87
88    pub fn get_value(
89        &self,
90        section_name: &WString,
91        parameter_name: &WString,
92    ) -> crate::Result<(WString, bool)> {
93        let mut is_encrypted: u8 = Default::default();
94        let raw = unsafe {
95            self.com.GetValue(
96                section_name.as_pcwstr(),
97                parameter_name.as_pcwstr(),
98                std::ptr::addr_of_mut!(is_encrypted),
99            )
100        }?;
101        Ok((WString::from(raw), is_encrypted != 0))
102    }
103
104    pub fn decrypt_value(&self, encryptedvalue: &WString) -> crate::Result<WString> {
105        let s = unsafe { self.com.DecryptValue(encryptedvalue.as_pcwstr()) }?;
106        Ok(StringResult::from(&s).into_inner())
107    }
108}
109
110pub struct ConfigurationSection {
111    pub name: WString,
112    pub parameters: Vec<ConfigurationParameter>,
113}
114
115impl From<&FABRIC_CONFIGURATION_SECTION> for ConfigurationSection {
116    fn from(value: &FABRIC_CONFIGURATION_SECTION) -> Self {
117        let parameters = unsafe { value.Parameters.as_ref() }
118            .map(|list| crate::iter::vec_from_raw_com(list.Count as usize, list.Items))
119            .unwrap_or_default();
120        Self {
121            name: WString::from(value.Name),
122            parameters,
123        }
124    }
125}
126
127#[derive(Debug)]
128pub struct ConfigurationParameter {
129    pub is_encrypted: bool,
130    pub must_overrride: bool,
131    pub name: WString,
132    pub value: WString,
133    pub r#type: WString,
134}
135
136impl From<&FABRIC_CONFIGURATION_PARAMETER> for ConfigurationParameter {
137    fn from(value: &FABRIC_CONFIGURATION_PARAMETER) -> Self {
138        let raw1 = unsafe {
139            (value.Reserved as *const FABRIC_CONFIGURATION_PARAMETER_EX1)
140                .as_ref()
141                .unwrap()
142        };
143        Self {
144            name: WString::from(value.Name),
145            is_encrypted: value.IsEncrypted,
146            must_overrride: value.MustOverride,
147            value: WString::from(value.Value),
148            r#type: WString::from(raw1.Type),
149        }
150    }
151}