Skip to content

Commit

Permalink
Work on 0.3 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Dec 18, 2024
1 parent 6f67e1e commit 766494a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 35 deletions.
25 changes: 0 additions & 25 deletions src/Microsoft.Management.Configuration/ConfigurationSetParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,6 @@ namespace winrt::Microsoft::Management::Configuration::implementation
hstring Module;
hstring Resource;
};

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

if (securityContextLower == L"elevated")
{
context = SecurityContext::Elevated;
}
else if (securityContextLower == L"restricted")
{
context = SecurityContext::Restricted;
}
else if (securityContextLower == L"current")
{
context = SecurityContext::Current;
}
else
{
return false;
}

return true;
}
}

std::unique_ptr<ConfigurationSetParser> ConfigurationSetParser::Create(std::string_view input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ namespace winrt::Microsoft::Management::Configuration::implementation
auto result = make_self<implementation::ConfigurationSet>();

CHECK_ERROR(ParseValueSet(m_document, ConfigurationField::Metadata, false, result->Metadata()));

m_setEnvironment = make_self<implementation::ConfigurationEnvironment>();
CHECK_ERROR(ExtractEnvironmentFromMetadata(result->Metadata(), *m_setEnvironment));

CHECK_ERROR(ParseParameters(result));
CHECK_ERROR(ParseValueSet(m_document, ConfigurationField::Variables, false, result->Variables()));

std::vector<Configuration::ConfigurationUnit> units;
CHECK_ERROR(ParseConfigurationUnitsFromField(m_document, ConfigurationField::Resources, units));
CHECK_ERROR(ParseConfigurationUnitsFromField(m_document, ConfigurationField::Resources, *m_setEnvironment, units));
result->Units(std::move(units));

result->SchemaVersion(GetSchemaVersion());
Expand Down Expand Up @@ -170,24 +174,25 @@ namespace winrt::Microsoft::Management::Configuration::implementation
}
}

void ConfigurationSetParser_0_3::ParseConfigurationUnitsFromField(const Node& document, ConfigurationField field, std::vector<Configuration::ConfigurationUnit>& result)
void ConfigurationSetParser_0_3::ParseConfigurationUnitsFromField(const Node& document, ConfigurationField field, const ConfigurationEnvironment& defaultEnvironment, std::vector<Configuration::ConfigurationUnit>& result)
{
ParseSequence(document, field, false, Node::Type::Mapping, [&](const Node& item)
{
auto configurationUnit = make_self<ConfigurationUnit>();
ParseConfigurationUnit(configurationUnit.get(), item);
ParseConfigurationUnit(configurationUnit.get(), item, defaultEnvironment);
result.emplace_back(*configurationUnit);
});
}

void ConfigurationSetParser_0_3::ParseConfigurationUnit(ConfigurationUnit* unit, const Node& unitNode)
void ConfigurationSetParser_0_3::ParseConfigurationUnit(ConfigurationUnit* unit, const Node& unitNode, const ConfigurationEnvironment& defaultEnvironment)
{
// Set unknown intent as the new schema doesn't express it directly
unit->Intent(ConfigurationUnitIntent::Unknown);

CHECK_ERROR(GetStringValueForUnit(unitNode, ConfigurationField::Name, true, unit, &ConfigurationUnit::Identifier));
CHECK_ERROR(GetStringValueForUnit(unitNode, ConfigurationField::Type, true, unit, &ConfigurationUnit::Type));
CHECK_ERROR(ParseValueSet(unitNode, ConfigurationField::Metadata, false, unit->Metadata()));
CHECK_ERROR(ExtractEnvironmentForUnit(unit, defaultEnvironment));
CHECK_ERROR(ValidateType(unit, unitNode, ConfigurationField::Type, false, true));
CHECK_ERROR(GetStringArrayForUnit(unitNode, ConfigurationField::DependsOn, false, unit, &ConfigurationUnit::Dependencies));

Expand All @@ -204,7 +209,7 @@ namespace winrt::Microsoft::Management::Configuration::implementation
if (propertiesNode)
{
std::vector<Configuration::ConfigurationUnit> units;
CHECK_ERROR(ParseConfigurationUnitsFromField(propertiesNode, ConfigurationField::Resources, units));
CHECK_ERROR(ParseConfigurationUnitsFromField(propertiesNode, ConfigurationField::Resources, unit->EnvironmentInternal(), units));
unit->Units(std::move(units));
}
}
Expand All @@ -228,6 +233,20 @@ namespace winrt::Microsoft::Management::Configuration::implementation
return false;
}

void ConfigurationSetParser_0_3::ExtractEnvironmentFromMetadata(const Windows::Foundation::Collections::ValueSet& metadata, ConfigurationEnvironment& targetEnvironment, const ConfigurationEnvironment* defaultEnvironment)
{

}

void ConfigurationSetParser_0_3::ExtractEnvironmentForUnit(ConfigurationUnit* unit, const ConfigurationEnvironment& defaultEnvironment)
{
// Get unnested the security context

Check failure on line 243 in src/Microsoft.Management.Configuration/ConfigurationSetParser_0_3.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`unnested` is not a recognized word. (unrecognized-spelling)
ExtractSecurityContext(unit, defaultEnvironment.Context());

// Get nested environment
ExtractEnvironmentFromMetadata(unit->Metadata(), unit->EnvironmentInternal(), &defaultEnvironment);
}

std::optional<std::pair<Windows::Foundation::PropertyType, bool>> ParseWindowsFoundationPropertyType(std::string_view value)
{
if (value == "string")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT License.
#pragma once
#include "ConfigurationSetParser.h"
#include <ConfigurationParameter.h>
#include "ConfigurationParameter.h"
#include <winrt/Microsoft.Management.Configuration.h>

#include <winget/Yaml.h>
#include <optional>
Expand Down Expand Up @@ -52,12 +53,20 @@ namespace winrt::Microsoft::Management::Configuration::implementation
ConfigurationParameter* parameter,
void(ConfigurationParameter::* propertyFunction)(const Windows::Foundation::IInspectable& value));

void ParseConfigurationUnitsFromField(const AppInstaller::YAML::Node& document, ConfigurationField field, std::vector<Configuration::ConfigurationUnit>& result);
virtual void ParseConfigurationUnit(ConfigurationUnit* unit, const AppInstaller::YAML::Node& unitNode);
void ParseConfigurationUnitsFromField(const AppInstaller::YAML::Node& document, ConfigurationField field, const ConfigurationEnvironment& defaultEnvironment, std::vector<Configuration::ConfigurationUnit>& result);
virtual void ParseConfigurationUnit(ConfigurationUnit* unit, const AppInstaller::YAML::Node& unitNode, const ConfigurationEnvironment& defaultEnvironment);
// Determines if the given unit should be converted to a group.
bool ShouldConvertToGroup(ConfigurationUnit* unit);

// Extracts the environment configuration from the given metadata.
// This only examines the winget subnode.
void ExtractEnvironmentFromMetadata(const Windows::Foundation::Collections::ValueSet& metadata, ConfigurationEnvironment& targetEnvironment, const ConfigurationEnvironment* defaultEnvironment = nullptr);

// Extracts the environment for a unit.
void ExtractEnvironmentForUnit(ConfigurationUnit* unit, const ConfigurationEnvironment& defaultEnvironment);

AppInstaller::YAML::Node m_document;
com_ptr<ConfigurationEnvironment> m_setEnvironment;
};

std::optional<std::pair<Windows::Foundation::PropertyType, bool>> ParseWindowsFoundationPropertyType(std::string_view value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ namespace winrt::Microsoft::Management::Configuration::implementation
emitter << Key << GetConfigurationFieldName(ConfigurationField::Directives);
WriteYamlValueSet(emitter, unit.Metadata(),
{ { ConfigurationField::ModuleDirective },
{ ConfigurationField::SecurityContextDirective, (securityContext != SecurityContext::Current ? PropertyValue::CreateString(L"") : nullptr)}});
{ ConfigurationField::SecurityContextDirective, (securityContext != SecurityContext::Current ? PropertyValue::CreateString(ToWString(securityContext)) : nullptr)} });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,59 @@ namespace winrt::Microsoft::Management::Configuration::implementation
{
return hstring{ AppInstaller::Utility::ConvertToUTF16(GetConfigurationFieldName(fieldName)) };
}

bool TryParseSecurityContext(winrt::hstring value, SecurityContext& result)
{
std::wstring securityContextLower = AppInstaller::Utility::ToLower(value);

if (securityContextLower == L"elevated")
{
result = SecurityContext::Elevated;
}
else if (securityContextLower == L"restricted")
{
result = SecurityContext::Restricted;
}
else if (securityContextLower == L"current")
{
result = SecurityContext::Current;
}
else
{
return false;
}

return true;
}

SecurityContext ParseSecurityContext(hstring value)
{
SecurityContext result = SecurityContext::Current;
THROW_HR_IF(E_INVALIDARG, !TryParseSecurityContext(value, result));
return result;
}

std::string_view ToString(SecurityContext value)
{
switch (value)
{
case SecurityContext::Current: return "current";
case SecurityContext::Restricted: return "restricted";
case SecurityContext::Elevated: return "elevated";
}

THROW_HR(E_INVALIDARG);
}

std::wstring_view ToWString(SecurityContext value)
{
switch (value)
{
case SecurityContext::Current: return L"current";
case SecurityContext::Restricted: return L"restricted";
case SecurityContext::Elevated: return L"elevated";
}

THROW_HR(E_INVALIDARG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.
#pragma once
#include <string_view>
#include <winrt/base.h>
#include <winrt/Microsoft.Management.Configuration.h>

namespace winrt::Microsoft::Management::Configuration::implementation
{
Expand Down Expand Up @@ -46,5 +46,16 @@ namespace winrt::Microsoft::Management::Configuration::implementation

winrt::hstring GetConfigurationFieldNameHString(ConfigurationField fieldName);

// Attempts to parse a security context from a string.
// Returns true if successul; false otherwise.

Check failure on line 50 in src/Microsoft.Management.Configuration/ConfigurationSetUtilities.h

View workflow job for this annotation

GitHub Actions / Check Spelling

`successul` is not a recognized word. (unrecognized-spelling)
bool TryParseSecurityContext(hstring value, SecurityContext& result);

// Parses a security context from a string.
SecurityContext ParseSecurityContext(hstring value);

// Gets the string representation of a security context.
std::string_view ToString(SecurityContext value);

// Gets the string representation of a security context.
std::wstring_view ToWString(SecurityContext value);
}

0 comments on commit 766494a

Please sign in to comment.