From d8da04ede74d3ab4014c39a784a2937cfa44bbc8 Mon Sep 17 00:00:00 2001 From: David Swan Date: Wed, 29 Jul 2020 11:21:32 +0100 Subject: [PATCH] (IAC-732) - implement `Run only when user is logged on --- REFERENCE.md | 4 ++ lib/puppet/type/scheduled_task.rb | 6 ++- .../puppetlabs/scheduled_task/task.rb | 6 ++- spec/acceptance/should_create_task_spec.rb | 47 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index b365601e..a9e0dbcf 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -66,6 +66,10 @@ Please also note that Puppet must be running as a privileged user in order to manage `scheduled_task` resources. Running as an unprivileged user will result in 'access denied' errors. +If a user is specified without an accompanying password the +task will be created with `Run only when user is logged on` +specified. + Default value: system ##### `compatibility` diff --git a/lib/puppet/type/scheduled_task.rb b/lib/puppet/type/scheduled_task.rb index 287405c5..a0fa0000 100644 --- a/lib/puppet/type/scheduled_task.rb +++ b/lib/puppet/type/scheduled_task.rb @@ -75,7 +75,11 @@ Please also note that Puppet must be running as a privileged user in order to manage `scheduled_task` resources. Running as an - unprivileged user will result in 'access denied' errors." + unprivileged user will result in 'access denied' errors. + + If a user is specified without an accompanying password the + task will be created with `Run only when user is logged on` + specified." defaultto :system diff --git a/lib/puppet_x/puppetlabs/scheduled_task/task.rb b/lib/puppet_x/puppetlabs/scheduled_task/task.rb index 7a06c499..00b2ec0a 100644 --- a/lib/puppet_x/puppetlabs/scheduled_task/task.rb +++ b/lib/puppet_x/puppetlabs/scheduled_task/task.rb @@ -349,7 +349,11 @@ def set_account_information(user, password) @definition.Principal.LogonType = TASK_LOGON_TYPE::TASK_LOGON_SERVICE_ACCOUNT else @definition.Principal.UserId = user - @definition.Principal.LogonType = TASK_LOGON_TYPE::TASK_LOGON_PASSWORD + @definition.Principal.LogonType = if @task_password + TASK_LOGON_TYPE::TASK_LOGON_PASSWORD + else + TASK_LOGON_TYPE::TASK_LOGON_INTERACTIVE_TOKEN + end end true diff --git a/spec/acceptance/should_create_task_spec.rb b/spec/acceptance/should_create_task_spec.rb index 239f72fb..5ae89f37 100644 --- a/spec/acceptance/should_create_task_spec.rb +++ b/spec/acceptance/should_create_task_spec.rb @@ -123,6 +123,53 @@ end end + it 'creates a task with a username: taskscheduler_api2' do + pp = <<-MANIFEST + scheduled_task {'#{taskname}': + ensure => present, + command => 'c:\\\\windows\\\\system32\\\\notepad.exe', + arguments => "foo bar baz", + working_dir => 'c:\\\\windows', + user => '#{username}', + trigger => { + schedule => daily, + start_time => '12:00', + }, + } + MANIFEST + apply_manifest(pp, catch_failures: true) + + # Verify the task exists + query_cmd = "schtasks.exe /query /v /fo list /tn #{taskname}" + run_shell(query_cmd) do |result| + expect(result.stdout).to match(%r{#{username}}) + end + end + + it 'creates a task with a username: win32_taskscheduler' do + pp = <<-MANIFEST + scheduled_task {'#{taskname}': + ensure => present, + command => 'c:\\\\windows\\\\system32\\\\notepad.exe', + arguments => "foo bar baz", + working_dir => 'c:\\\\windows', + user => '#{username}', + trigger => { + schedule => daily, + start_time => '12:00', + }, + provider => 'win32_taskscheduler' + } + MANIFEST + apply_manifest(pp, catch_failures: true) + + # Verify the task exists + query_cmd = "schtasks.exe /query /v /fo list /tn #{taskname}" + run_shell(query_cmd) do |result| + expect(result.stdout).to match(%r{#{username}}) + end + end + it "updates a task's credentials: win32_taskscheduler" do pp = <<-MANIFEST scheduled_task {'#{taskname}':