Skip to content

Commit

Permalink
Parsed 0.2, work on serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Dec 17, 2024
1 parent 585b2e5 commit 6f67e1e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
38 changes: 12 additions & 26 deletions src/Microsoft.Management.Configuration/ConfigurationSetParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,37 +150,28 @@ namespace winrt::Microsoft::Management::Configuration::implementation
};

// Converts the string representation of SecurityContext to the enum
SecurityContext ParseSecurityContext(winrt::hstring securityContext)
bool TryParseSecurityContext(winrt::hstring securityContext, SecurityContext& context)
{
std::wstring securityContextLower = ToLower(securityContext);

if (securityContextLower == L"elevated")
{
return Security::IntegrityLevel::High;
context = SecurityContext::Elevated;
}
else if (securityContextLower == L"restricted")
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (m_enableRestrictedIntegrityLevel)
{
return Security::IntegrityLevel::Medium;
}
else
#endif
{
// Not supporting elevated callers downgrading at the moment.
THROW_WIN32(ERROR_NOT_SUPPORTED);

// Technically this means the default level of the user token, so if UAC is disabled it would be the only integrity level (aka current).
// return Security::IntegrityLevel::Medium;
}
context = SecurityContext::Restricted;
}
else if (securityContextLower == L"current")
{
return m_currentIntegrityLevel;
context = SecurityContext::Current;
}
else
{
return false;
}

THROW_WIN32(ERROR_NOT_SUPPORTED);
return true;
}
}

Expand Down Expand Up @@ -607,18 +598,13 @@ namespace winrt::Microsoft::Management::Configuration::implementation
auto securityContext = unit->Metadata().TryLookup(securityContextDirectiveFieldName);
if (securityContext)
{
auto securityContextProperty = securityContext.try_as<IPropertyValue>();
if (securityContextProperty && securityContextProperty.Type() == PropertyType::String)
auto securityContextProperty = securityContext.try_as<Windows::Foundation::IPropertyValue>();
if (securityContextProperty && securityContextProperty.Type() == Windows::Foundation::PropertyType::String)
{
return SecurityContextToIntegrityLevel(securityContextProperty.GetString());
TryParseSecurityContext(securityContextProperty.GetString(), computedContext);
}
}

unit->EnvironmentInternal().Context(computedContext);
}

// Gets the integrity level that the given unit should be run at
Security::IntegrityLevel GetIntegrityLevelForUnit(const ConfigurationUnit& unit)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ namespace winrt::Microsoft::Management::Configuration::implementation
}
}

void ConfigurationSetSerializer::WriteYamlValueSet(AppInstaller::YAML::Emitter& emitter, const Windows::Foundation::Collections::ValueSet& valueSet, std::initializer_list<ConfigurationField> exclusions)
void ConfigurationSetSerializer::WriteYamlValueSet(AppInstaller::YAML::Emitter& emitter, const Windows::Foundation::Collections::ValueSet& valueSet, const std::vector<ValueSetOverride>& overrides)
{
// Create a sorted list of the field names to exclude
std::vector<winrt::hstring> exclusionStrings;
for (ConfigurationField field : exclusions)
for (const ValueSetOverride& override : overrides)
{
exclusionStrings.emplace_back(GetConfigurationFieldNameHString(field));
exclusionStrings.emplace_back(GetConfigurationFieldNameHString(override.Field));
}
std::sort(exclusionStrings.begin(), exclusionStrings.end());

Expand All @@ -97,6 +97,16 @@ namespace winrt::Microsoft::Management::Configuration::implementation
}
}

for (const ValueSetOverride & override : overrides)
{
if (override.Value != nullptr)
{
std::string_view keyName = GetConfigurationFieldName(override.Field);
emitter << Key << keyName << Value;
WriteYamlValue(emitter, override.Value);
}
}

emitter << EndMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ namespace winrt::Microsoft::Management::Configuration::implementation
protected:
ConfigurationSetSerializer() = default;

void WriteYamlValueSet(AppInstaller::YAML::Emitter& emitter, const Windows::Foundation::Collections::ValueSet& valueSet, std::initializer_list<ConfigurationField> exclusions = {});
// A value to override in a value set.
// Prevents the value from the actual value set from being written.
// If provided, writes the given value instead.
struct ValueSetOverride
{
ConfigurationField Field;
Windows::Foundation::IInspectable Value;
};

void WriteYamlValueSet(AppInstaller::YAML::Emitter& emitter, const Windows::Foundation::Collections::ValueSet& valueSet, const std::vector<ValueSetOverride>& overrides = {});
void WriteYamlValueSetIfNotEmpty(AppInstaller::YAML::Emitter& emitter, ConfigurationField key, const Windows::Foundation::Collections::ValueSet& valueSet);
void WriteYamlValueSetAsArray(AppInstaller::YAML::Emitter& emitter, const Windows::Foundation::Collections::ValueSet& valueSetArray);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ namespace winrt::Microsoft::Management::Configuration::implementation

void ConfigurationSetSerializer_0_2::WriteResourceDirectives(AppInstaller::YAML::Emitter& emitter, const ConfigurationUnit& unit)
{
SecurityContext securityContext = unit.Environment().Context();

emitter << Key << GetConfigurationFieldName(ConfigurationField::Directives);
WriteYamlValueSet(emitter, unit.Metadata(), { ConfigurationField::ModuleDirective });
WriteYamlValueSet(emitter, unit.Metadata(),
{ { ConfigurationField::ModuleDirective },
{ ConfigurationField::SecurityContextDirective, (securityContext != SecurityContext::Current ? PropertyValue::CreateString(L"") : nullptr)}});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ namespace winrt::Microsoft::Management::Configuration::implementation
std::string_view GetConfigurationFieldName(ConfigurationField fieldName);

winrt::hstring GetConfigurationFieldNameHString(ConfigurationField fieldName);


}

0 comments on commit 6f67e1e

Please sign in to comment.