Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] salt.module.win_task.info wrong execution_time_limit returned when set to False #66347

Open
JanCle opened this issue Apr 11, 2024 · 1 comment
Labels
Bug broken, incorrect, or confusing behavior

Comments

@JanCle
Copy link

JanCle commented Apr 11, 2024

Description

When creating a Task without an Execution Time Limit with execution_time_limit = False,
the ExecutionTimeLimit is set to "PT0S" as this is the way to disable it.

salt/salt/modules/win_task.py

Lines 1123 to 1125 in 97e9395

if execution_time_limit is not None:
if execution_time_limit is False:
task_definition.Settings.ExecutionTimeLimit = "PT0S"

This value is not correctly evaluted when running task.info, as the used duration-mapping has no value for PT0S.

duration = {
"Immediately": "PT0M",
"Indefinitely": "",
"Do not wait": "PT0M",
"15 seconds": "PT15S",
"30 seconds": "PT30S",
"1 minute": "PT1M",
"5 minutes": "PT5M",
"10 minutes": "PT10M",
"15 minutes": "PT15M",
"30 minutes": "PT30M",
"1 hour": "PT1H",
"2 hours": "PT2H",
"4 hours": "PT4H",
"8 hours": "PT8H",
"12 hours": "PT12H",
"1 day": ["P1D", "PT24H"],
"3 days": ["P3D", "PT72H"],
"30 days": "P30D",
"90 days": "P90D",
"180 days": "P180D",
"365 days": "P365D",
}

Since the function "_reverse_lookup" sets value_index = -1 from the getgo, task.info will always return "365 days" in this case.

def _reverse_lookup(dictionary, value):
"""
Lookup the key in a dictionary by its value. Will return the first match.
:param dict dictionary: The dictionary to search
:param str value: The value to search for.
:return: Returns the first key to match the value
:rtype: str
"""
value_index = -1
for idx, dict_value in enumerate(dictionary.values()):
if isinstance(dict_value, list):
if value in dict_value:
value_index = idx
break
elif value == dict_value:
value_index = idx
break
return list(dictionary)[value_index]

Steps to Reproduce the behavior

Run following state against a Windows-minion.

run_some_script:
  module.run:
    - task.create_task:
      - name: run_some_script
      - action_type: Execute
      - arguments: -ExecutionPolicy Bypass -File .\some_script.ps1
      - cmd: PowerShell.exe
      - ac_only: True
      - execution_time_limit: False
      - run_if_idle: False
      - run_if_network: False
      - start_when_available: False
      - allow_demand_start: True
      - delete_after: False
      - force_stop: True
      - multiple_instances: No New Instance
      - restart_interval: False
      - stop_if_on_batteries: True
      - wake_to_run: False
      - force: True

Run following command if state was successful:

salt windows-minion task.info run_some_script

Expected behavior

The returned value should be "False" or "Indefenitely".

Versions Report

salt --versions-report

from master-of-masters:

Salt Version:
          Salt: 3006.4

Python Version:
        Python: 3.10.13 (main, Oct  4 2023, 21:54:22) [GCC 11.2.0]

Dependency Versions:
          cffi: 1.14.6
      cherrypy: unknown
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.2
       libgit2: Not Installed
  looseversion: 1.0.2
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 22.0
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.9.8
        pygit2: Not Installed
  python-gnupg: 0.4.8
        PyYAML: 6.0.1
         PyZMQ: 23.2.0
        relenv: 0.13.12
         smmap: Not Installed
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.4

System Versions:
          dist: ubuntu 22.04.3 jammy
        locale: utf-8
       machine: x86_64
       release: 5.15.0-101-generic
        system: Linux
       version: Ubuntu 22.04.3 jammy

from minion:

    Salt Version:
              Salt: 3006.4

    Python Version:
            Python: 3.10.13 (heads/main:7ee24e6, Sep  6 2023, 02:19:49) [MSC v.1936 64 bit (AMD64)]

    Dependency Versions:
              cffi: 1.14.6
          cherrypy: 18.6.1
          dateutil: 2.8.1
         docker-py: Not Installed
             gitdb: 4.0.7
         gitpython: Not Installed
            Jinja2: 3.1.2
           libgit2: Not Installed
      looseversion: 1.0.2
          M2Crypto: Not Installed
              Mako: Not Installed
           msgpack: 1.0.2
      msgpack-pure: Not Installed
      mysql-python: Not Installed
         packaging: 22.0
         pycparser: 2.21
          pycrypto: Not Installed
      pycryptodome: 3.10.1
            pygit2: Not Installed
      python-gnupg: 0.4.8
            PyYAML: 6.0.1
             PyZMQ: 25.0.2
            relenv: 0.13.12
             smmap: 4.0.0
           timelib: 0.2.4
           Tornado: 4.5.3
               ZMQ: 4.3.4

    System Versions:
              dist:
            locale: cp1252
           machine: AMD64
           release: 2022Server
            system: Windows
           version: 2022Server 10.0.20348 SP0 Multiprocessor Free

Additional context

A simple fix would be to add an option to the duration-mapping with "PT0S", preferabley for the Key "Indefenitely".
Optimally, instead of setting False to disable and to prevent confusion, setting to Indefenitely should be set to PT0S rather than False.

@JanCle JanCle added Bug broken, incorrect, or confusing behavior needs-triage labels Apr 11, 2024
Copy link

welcome bot commented Apr 11, 2024

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior
Projects
None yet
Development

No branches or pull requests

2 participants