【问题标题】:Getting System.IO.IOException on File.Encrypt with an impersonated user使用模拟用户在 File.Encrypt 上获取 System.IO.IOException
【发布时间】:2017-03-15 13:13:06
【问题描述】:

我正在尝试以编程方式加密文件夹(使用 Windows EFS)。以下 powershell 代码在通过 ISE powershell 控制台运行时可以正常工作。

$obj = New-Object -TypeName System.IO.FileInfo 'D:\Temp'      
$obj.Encrypt()

但是,通过带有 test-kitchen 的厨师食谱在模拟用户下运行此程序会产生以下错误

powershell 的配方包装器:

ruby_block 'Enable encryption on folder' do
  block do
 command = <<-EOH
   # Encrypt the folder
   $obj = New-Object -TypeName System.IO.FileInfo 'D:\\Temp'      
   $obj.Encrypt()
   EOH
   powershell_out!(command, { user: username, password: pwd,
                           domain: domain})
  end
end

导致以下堆栈跟踪:

PSMessageDetails      : 
Exception             : System.Management.Automation.MethodInvocationException:
                     Exception calling "Encrypt" with "0" argument(s): 
                    "The parameter is incorrect.
                    " ---> System.IO.IOException: The parameter is 
                    incorrect.

                       at System.IO.__Error.WinIOError(Int32 errorCode, 
                    String maybeFullPath)
                       at System.IO.File.Encrypt(String path)
                       at CallSite.Target(Closure , CallSite , Object )
                       --- End of inner exception stack trace ---
                       at System.Management.Automation.ExceptionHandlingOps
                    .CheckActionPreference(FunctionContext funcContext, 
                    Exception exception)
                       at System.Management.Automation.Interpreter.ActionCa
                    llInstruction`2.Run(InterpretedFrame frame)
                       at System.Management.Automation.Interpreter.EnterTry
                    CatchFinallyInstruction.Run(InterpretedFrame frame)
                       at System.Management.Automation.Interpreter.EnterTry
                    CatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : IOException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 5
PipelineIterationInfo : {}

注意:被模拟的用户是管理员组的一部分,并且对 D:\Temp 具有完全控制权。另一个关键观察结果是,如果我在通过 test-kitchen 运行 chef 之前以该用户身份执行交互式登录(RDP 会话),则上面的配方成功且没有任何问题,并且文件夹在本地加密,就像在控制台中交互式运行 powershell 一样。

异常似乎表明 System.IO.File.Encrypt 的路径参数未在内部某处设置/传递,但我不知道为什么这一次会起作用而不是另一次起作用。我确实尝试通过让配方以该用户身份对 localhost 运行 Invoke-Command 来创建用户配置文件,并且它确实创建了配置文件,例如(C:\users\... 被创建),但是 Encrypt() 调用仍然出错,并出现与上述相同的异常。 这似乎不是厨师或测试厨房的问题,而是由于加密文件系统错综复杂而在 powershell/windows 方面遗漏了一些东西,非常感谢任何帮助。

谢谢

【问题讨论】:

  • 这是在 Win2k12R2、powershell 4.0、chef-client 12.13.37 上
  • 这可能应该在此处删除并作为问题提交到我们的 GH 回购 :)
  • 我不确定在未加载用户配置文件时是否可以访问 EFS 文件。 According to technet, 这是存储底层加密密钥的地方。这可以解释为什么它在用户登录时起作用。
  • @coderanger 你认为这是厨师客户内部的问题吗?还是 mixlib-shellout?
  • @HarryJohnston 是的,这就是它的样子。我的印象是,厨师的脱壳会促进这一点,所以也许正如 coderanger 建议的那样,我应该将其移至相应厨师的 github 存储库。但是,我也愿意接受任何增加上述 powershell 的建议,以便它可以执行先决条件的用户配置文件加载。

标签: windows powershell chef-infra test-kitchen


【解决方案1】:

很遗憾,我不相信您可以通过远程会话加密文件。如果您要在本机 Windows Powershell 远程处理会话上尝试相同的操作,您将获得 UnauthorizedAccessException。如果您使用的是 Test-Kitchen,请尝试使用 Elevated winrm 传输。

示例 yaml:

platforms: - name: eval-win2012r2-standard os_type: windows transport: name: winrm elevated: true

这会模拟本地登录会话,并且可能会解决此错误。

【讨论】:

  • 谢谢马特,不幸的是elevated 没有帮助,仍然是同样的例外。但是,问题似乎与模拟有关,如果从 powershell_out 中删除“运行方式”凭据哈希 { user: username, password: pwd, domain: domain},则加密成功。
猜你喜欢
  • 2011-07-06
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多