Debian Codegraph @

lib/ansible/plugins/become/runas.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright: (c) 2018, Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 from __future__ import annotations
5
6 DOCUMENTATION = """
7 name: runas
8 short_description: Run As user
9 description:
10 - This become plugin allows your remote/login user to execute commands as another user via the windows runas facility.
11 author: ansible (@core)
12 version_added: "2.8"
13 options:
14 become_user:
15 description: User you 'become' to execute the task
16 ini:
17 - section: privilege_escalation
18 key: become_user
19 - section: runas_become_plugin
20 key: user
21 vars:
22 - name: ansible_become_user
23 - name: ansible_runas_user
24 env:
25 - name: ANSIBLE_BECOME_USER
26 - name: ANSIBLE_RUNAS_USER
27 keyword:
28 - name: become_user
29 required: True
30 become_flags:
31 description: Options to pass to runas, a space delimited list of k=v pairs
32 default: ''
33 ini:
34 - section: privilege_escalation
35 key: become_flags
36 - section: runas_become_plugin
37 key: flags
38 vars:
39 - name: ansible_become_flags
40 - name: ansible_runas_flags
41 env:
42 - name: ANSIBLE_BECOME_FLAGS
43 - name: ANSIBLE_RUNAS_FLAGS
44 keyword:
45 - name: become_flags
46 become_pass:
47 description: password
48 type: str
49 secret: true
50 ini:
51 - section: runas_become_plugin
52 key: password
53 vars:
54 - name: ansible_become_password
55 - name: ansible_become_pass
56 - name: ansible_runas_pass
57 env:
58 - name: ANSIBLE_BECOME_PASS
59 - name: ANSIBLE_RUNAS_PASS
60 notes:
61 - runas is really implemented in the powershell module handler and as such can only be used with winrm connections.
62 - This plugin ignores the 'become_exe' setting as it uses an API and not an executable.
63 - The Secondary Logon service (seclogon) must be running to use runas
64 """
65
66 from ansible.errors import AnsibleError
67 from ansible.parsing.splitter import split_args
68 from ansible.plugins.become import BecomeBase
69
70
71 class BecomeModule(BecomeBase):
72
73 name = 'runas'
74
75 def build_become_command(self, cmd, shell):
76 # this is a noop, the 'real' runas is implemented
77 # inside the windows powershell execution subsystem
78 return cmd
79
80 def _build_powershell_wrapper_action(self) -> tuple[str, dict[str, object], dict[str, object]]:
81 # See ansible.executor.powershell.become_wrapper.ps1 for the
82 # parameter names
83 params = {
84 'BecomeUser': self.get_option('become_user'),
85 }
86 secure_params = {}
87
88 password = self.get_option('become_pass')
89 if password:
90 secure_params['BecomePassword'] = password
91
92 flags = self.get_option('become_flags')
93 if flags:
94 split_flags = split_args(flags)
95 for flag in split_flags:
96 if '=' not in flag:
97 raise ValueError(f"become_flags entry '{flag}' is in an invalid format, must be a key=value pair")
98
99 k, v = flag.split('=', 1)
100
101 param_name, param_value = self._parse_flag(k, v)
102 params[param_name] = param_value
103
104 return 'become_wrapper.ps1', params, secure_params
105
106 def _parse_flag(self, name: str, value: str) -> tuple[str, str]:
107 logon_types = {
108 'interactive': 'Interactive',
109 'network': 'Network',
110 'batch': 'Batch',
111 'service': 'Service',
112 'unlock': 'Unlock',
113 'network_cleartext': 'NetworkCleartext',
114 'new_credentials': 'NewCredentials',
115 }
116 logon_flags = {
117 'none': 'None',
118 'with_profile': 'WithProfile',
119 'netcredentials_only': 'NetCredentialsOnly',
120 }
121
122 match name.lower():
123 case 'logon_type':
124 param_name = 'LogonType'
125 if param_value := logon_types.get(value.lower(), None):
126 return param_name, param_value
127 else:
128 raise AnsibleError(f"become_flags logon_type value '{value}' is not valid, valid values are: {', '.join(logon_types.keys())}")
129
130 case 'logon_flags':
131 param_name = 'LogonFlags'
132 flags = value.split(',')
133
134 param_values: list[str] = []
135 for flag in flags:
136 if not flag:
137 continue
138
139 if flag_value := logon_flags.get(flag.lower(), None):
140 param_values.append(flag_value)
141 else:
142 raise AnsibleError(f"become_flags logon_flags value '{flag}' is not valid, valid values are: {', '.join(logon_flags.keys())}")
143
144 return param_name, ", ".join(param_values)
145
146 case _:
147 raise AnsibleError(f"become_flags key '{name}' is not a valid runas flag, must be 'logon_type' or 'logon_flags'")