From ffab94ee3ec017963d70d0064565cff73ca1ab9d Mon Sep 17 00:00:00 2001 From: JohnMcPMS Date: Thu, 20 Apr 2023 13:20:06 -0700 Subject: [PATCH] Make entitlement best effort --- src/AppInstallerCLICore/Resources.h | 4 - .../Workflows/MSStoreInstallerHandler.cpp | 75 +++++++++++-------- .../Shared/Strings/en-us/winget.resw | 12 --- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/AppInstallerCLICore/Resources.h b/src/AppInstallerCLICore/Resources.h index 771e42568e..4de9e958e2 100644 --- a/src/AppInstallerCLICore/Resources.h +++ b/src/AppInstallerCLICore/Resources.h @@ -264,10 +264,6 @@ namespace AppInstaller::CLI::Resource WINGET_DEFINE_RESOURCE_STRINGID(MsixArgumentDescription); WINGET_DEFINE_RESOURCE_STRINGID(MsixSignatureHashFailed); WINGET_DEFINE_RESOURCE_STRINGID(MSStoreAppBlocked); - WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallGetEntitlementNetworkError); - WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallGetEntitlementNoStoreAccount); - WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallGetEntitlementServerError); - WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallGetEntitlementSuccess); WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallOrUpdateFailed); WINGET_DEFINE_RESOURCE_STRINGID(MSStoreInstallTryGetEntitlement); WINGET_DEFINE_RESOURCE_STRINGID(MSStoreStoreClientBlocked); diff --git a/src/AppInstallerCLICore/Workflows/MSStoreInstallerHandler.cpp b/src/AppInstallerCLICore/Workflows/MSStoreInstallerHandler.cpp index 965c207721..ee7a403464 100644 --- a/src/AppInstallerCLICore/Workflows/MSStoreInstallerHandler.cpp +++ b/src/AppInstallerCLICore/Workflows/MSStoreInstallerHandler.cpp @@ -79,48 +79,69 @@ namespace AppInstaller::CLI::Workflow return errorCode; } - bool GetFreeEntitlement(Execution::Context& context, const std::wstring& productId) + // The type of entitlement we were able to acquire/ensure. + enum class EntitlementType + { + None, + User, + Device, + }; + + EntitlementType EnsureFreeEntitlement(Execution::Context& context, const std::wstring& productId) { AppInstallManager installManager; + AICLI_LOG(CLI, Error, << "Getting entitlement for ProductId: " << Utility::ConvertToUTF8(productId)); + // Verifying/Acquiring product ownership context.Reporter.Info() << Resource::String::MSStoreInstallTryGetEntitlement << std::endl; - GetEntitlementResult result{ nullptr }; + GetEntitlementResult entitlementResult{ nullptr }; + EntitlementType result = EntitlementType::None; if (Manifest::ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope)) == Manifest::ScopeEnum::Machine) { - AICLI_LOG(CLI, Info, << "Get device entitlement."); - result = installManager.GetFreeDeviceEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); + AICLI_LOG(CLI, Info, << "Get device entitlement (machine scope install)."); + result = EntitlementType::Device; + entitlementResult = installManager.GetFreeDeviceEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); } else { AICLI_LOG(CLI, Info, << "Get user entitlement."); - result = installManager.GetFreeUserEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); - if (result.Status() == GetEntitlementStatus::NoStoreAccount) + result = EntitlementType::User; + entitlementResult = installManager.GetFreeUserEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); + + if (entitlementResult.Status() == GetEntitlementStatus::NoStoreAccount) { - AICLI_LOG(CLI, Info, << "Get device entitlement."); - result = installManager.GetFreeDeviceEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); + AICLI_LOG(CLI, Info, << "Get device entitlement (no store account)."); + result = EntitlementType::Device; + entitlementResult = installManager.GetFreeDeviceEntitlementAsync(productId, winrt::hstring(), winrt::hstring()).get(); } } - if (result.Status() == GetEntitlementStatus::Succeeded) + if (entitlementResult.Status() == GetEntitlementStatus::Succeeded) { - context.Reporter.Info() << Resource::String::MSStoreInstallGetEntitlementSuccess << std::endl; AICLI_LOG(CLI, Info, << "Get entitlement succeeded."); } - else if (result.Status() == GetEntitlementStatus::NetworkError) - { - context.Reporter.Info() << Resource::String::MSStoreInstallGetEntitlementNetworkError << std::endl; - AICLI_LOG(CLI, Error, << "Get entitlement failed. Network error."); - } - else if (result.Status() == GetEntitlementStatus::ServerError) + else { - context.Reporter.Info() << Resource::String::MSStoreInstallGetEntitlementServerError << std::endl; - AICLI_LOG(CLI, Error, << "Get entitlement failed Server error. ProductId: " << Utility::ConvertToUTF8(productId)); + result = EntitlementType::None; + + if (entitlementResult.Status() == GetEntitlementStatus::NetworkError) + { + AICLI_LOG(CLI, Error, << "Get entitlement failed. Network error."); + } + else if (entitlementResult.Status() == GetEntitlementStatus::ServerError) + { + AICLI_LOG(CLI, Error, << "Get entitlement failed. Server error."); + } + else + { + AICLI_LOG(CLI, Error, << "Get entitlement failed. Unknown status: " << static_cast(entitlementResult.Status())); + } } - return result.Status() == GetEntitlementStatus::Succeeded; + return result; } } @@ -135,11 +156,8 @@ namespace AppInstaller::CLI::Workflow { auto productId = Utility::ConvertToUTF16(context.Get()->ProductId); - // Verifying/Acquiring product ownership - if (!GetFreeEntitlement(context, productId)) - { - AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_MSSTORE_INSTALL_FAILED); - } + // Best effort verifying/acquiring product ownership. + std::ignore = EnsureFreeEntitlement(context, productId); AppInstallManager installManager; AppInstallOptions installOptions; @@ -184,7 +202,7 @@ namespace AppInstaller::CLI::Workflow else { auto errorCodeString = GetErrorCodeString(errorCode); - context.Reporter.Info() << Resource::String::MSStoreInstallOrUpdateFailed(errorCodeString) << std::endl; + context.Reporter.Error() << Resource::String::MSStoreInstallOrUpdateFailed(errorCodeString) << std::endl; context.Add(errorCode); AICLI_LOG(CLI, Error, << "MSStore install failed. ProductId: " << Utility::ConvertToUTF8(productId) << " HResult: " << errorCodeString); AICLI_TERMINATE_CONTEXT(errorCode); @@ -195,11 +213,8 @@ namespace AppInstaller::CLI::Workflow { auto productId = Utility::ConvertToUTF16(context.Get()->ProductId); - // Verifying/Acquiring product ownership - if (!GetFreeEntitlement(context, productId)) - { - AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_MSSTORE_INSTALL_FAILED); - } + // Best effort verifying/acquiring product ownership. + std::ignore = EnsureFreeEntitlement(context, productId); AppInstallManager installManager; AppUpdateOptions updateOptions; diff --git a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw index 853c4340eb..078b7a72dc 100644 --- a/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw +++ b/src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw @@ -386,18 +386,6 @@ They can be configured through the settings file 'winget settings'. Failed to install or upgrade Microsoft Store package. Error code: {0} {Locked="{0}"} Error message displayed when a Microsoft Store application package fails to install or upgrade. {0} is a placeholder replaced by an error code. - - Verifying/Requesting package acquisition failed: network error - - - Verifying/Requesting package acquisition failed: no store account found - - - Verifying/Requesting package acquisition failed: server error - - - Verifying/Requesting package acquisition success - Failed to install or upgrade Microsoft Store package because Microsoft Store client is blocked by policy