summaryrefslogtreecommitdiff
path: root/tests/unittests/cmd/test_query.py
blob: 207078fa0b85f655e2eecfbcca3249207647faf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# This file is part of cloud-init. See LICENSE file for license information.

import errno
import gzip
import json
import os
from collections import namedtuple
from io import BytesIO
from textwrap import dedent

import pytest

from cloudinit.cmd import query
from cloudinit.helpers import Paths
from cloudinit.sources import (
    INSTANCE_JSON_FILE,
    INSTANCE_JSON_SENSITIVE_FILE,
    REDACT_SENSITIVE_VALUE,
)
from cloudinit.util import b64e, write_file
from tests.unittests.helpers import mock

M_PATH = "cloudinit.cmd.query."


def _gzip_data(data):
    with BytesIO() as iobuf:
        with gzip.GzipFile(mode="wb", fileobj=iobuf) as gzfp:
            gzfp.write(data)
        return iobuf.getvalue()


@mock.patch(M_PATH + "addLogHandlerCLI", lambda *args: "")
class TestQuery:

    Args = namedtuple(
        "Args",
        "debug dump_all format instance_data list_keys user_data vendor_data"
        " varname",
    )

    def _setup_paths(self, tmpdir, ud_val=None, vd_val=None):
        """Write userdata and vendordata into a tmpdir.

        Return:
            4-tuple : (paths, run_dir_path, userdata_path, vendordata_path)
        """
        if ud_val:
            user_data = tmpdir.join("user-data")
            write_file(user_data.strpath, ud_val)
        else:
            user_data = None
        if vd_val:
            vendor_data = tmpdir.join("vendor-data")
            write_file(vendor_data.strpath, vd_val)
        else:
            vendor_data = None
        run_dir = tmpdir.join("run_dir")
        run_dir.ensure_dir()

        cloud_dir = tmpdir.join("cloud_dir")
        cloud_dir.ensure_dir()

        return (
            Paths(
                {"cloud_dir": cloud_dir.strpath, "run_dir": run_dir.strpath}
            ),
            run_dir,
            user_data,
            vendor_data,
        )

    def test_handle_args_error_on_missing_param(self, caplog, capsys):
        """Error when missing required parameters and print usage."""
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=None,
        )
        with mock.patch(
            M_PATH + "addLogHandlerCLI", return_value=""
        ) as m_cli_log:
            assert 1 == query.handle_args("anyname", args)
        expected_error = (
            "Expected one of the options: --all, --format, --list-keys"
            " or varname\n"
        )
        assert expected_error in caplog.text
        out, _err = capsys.readouterr()
        assert "usage: query" in out
        assert 1 == m_cli_log.call_count

    @pytest.mark.parametrize(
        "inst_data,varname,expected_error",
        (
            (
                '{"v1": {"key-2": "value-2"}}',
                "v1.absent_leaf",
                "instance-data 'v1' has no 'absent_leaf'\n",
            ),
            (
                '{"v1": {"key-2": "value-2"}}',
                "absent_key",
                "Undefined instance-data key 'absent_key'\n",
            ),
        ),
    )
    def test_handle_args_error_on_invalid_varname_paths(
        self, inst_data, varname, expected_error, caplog, tmpdir
    ):
        """Error when varname is not a valid instance-data variable path."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(inst_data)
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=varname,
        )
        paths, _, _, _ = self._setup_paths(tmpdir)
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            with mock.patch(M_PATH + "addLogHandlerCLI", return_value=""):
                with mock.patch(M_PATH + "load_userdata") as m_lud:
                    m_lud.return_value = "ud"
                    assert 1 == query.handle_args("anyname", args)
        assert expected_error in caplog.text

    def test_handle_args_error_on_missing_instance_data(self, caplog, tmpdir):
        """When instance_data file path does not exist, log an error."""
        absent_fn = tmpdir.join("absent")
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=absent_fn.strpath,
            list_keys=False,
            user_data="ud",
            vendor_data="vd",
            varname=None,
        )
        assert 1 == query.handle_args("anyname", args)

        msg = "Missing instance-data file: %s" % absent_fn
        assert msg in caplog.text

    def test_handle_args_error_when_no_read_permission_instance_data(
        self, caplog, tmpdir
    ):
        """When instance_data file is unreadable, log an error."""
        noread_fn = tmpdir.join("unreadable")
        noread_fn.write("thou shall not pass")
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=noread_fn.strpath,
            list_keys=False,
            user_data="ud",
            vendor_data="vd",
            varname=None,
        )
        with mock.patch(M_PATH + "util.load_file") as m_load:
            m_load.side_effect = OSError(errno.EACCES, "Not allowed")
            assert 1 == query.handle_args("anyname", args)
        msg = "No read permission on '%s'. Try sudo" % noread_fn
        assert msg in caplog.text

    @pytest.mark.parametrize(
        "exception",
        [
            (OSError(errno.EACCES, "Not allowed"),),
            (OSError(errno.ENOENT, "Not allowed"),),
            (IOError,),
        ],
    )
    def test_handle_args_error_when_no_read_permission_init_cfg(
        self, exception, capsys
    ):
        """query.handle_status_args exists with 1 and no sys-output."""
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=None,
        )
        with mock.patch(
            M_PATH + "read_cfg_paths",
            side_effect=exception,
        ) as m_read_cfg_paths:
            query.handle_args("anyname", args)
        assert m_read_cfg_paths.call_count == 1
        out, err = capsys.readouterr()
        assert not out
        assert not err

    def test_handle_args_defaults_instance_data(self, caplog, tmpdir):
        """When no instance_data argument, default to configured run_dir."""
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=None,
        )
        paths, run_dir, _, _ = self._setup_paths(tmpdir)
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            assert 1 == query.handle_args("anyname", args)
        json_file = run_dir.join(INSTANCE_JSON_FILE)
        msg = "Missing instance-data file: %s" % json_file.strpath
        assert msg in caplog.text

    def test_handle_args_root_fallsback_to_instance_data(self, caplog, tmpdir):
        """When no instance_data argument, root falls back to redacted json."""
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=None,
        )
        paths, run_dir, _, _ = self._setup_paths(tmpdir)
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            with mock.patch("os.getuid") as m_getuid:
                m_getuid.return_value = 0
                assert 1 == query.handle_args("anyname", args)
        json_file = run_dir.join(INSTANCE_JSON_FILE)
        sensitive_file = run_dir.join(INSTANCE_JSON_SENSITIVE_FILE)
        msg = "Missing root-readable %s. Using redacted %s instead." % (
            sensitive_file.strpath,
            json_file.strpath,
        )
        assert msg in caplog.text

    @pytest.mark.parametrize(
        "ud_src,ud_expected,vd_src,vd_expected",
        (
            ("hi mom", "hi mom", "hi pops", "hi pops"),
            ("ud".encode("utf-8"), "ud", "vd".encode("utf-8"), "vd"),
            (_gzip_data(b"ud"), "ud", _gzip_data(b"vd"), "vd"),
            (_gzip_data("ud".encode("utf-8")), "ud", _gzip_data(b"vd"), "vd"),
        ),
    )
    def test_handle_args_root_processes_user_data(
        self, ud_src, ud_expected, vd_src, vd_expected, capsys, tmpdir
    ):
        """Support reading multiple user-data file content types"""
        paths, run_dir, user_data, vendor_data = self._setup_paths(
            tmpdir, ud_val=ud_src, vd_val=vd_src
        )
        sensitive_file = run_dir.join(INSTANCE_JSON_SENSITIVE_FILE)
        sensitive_file.write('{"my-var": "it worked"}')
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=user_data.strpath,
            vendor_data=vendor_data.strpath,
            varname=None,
        )
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            with mock.patch("os.getuid") as m_getuid:
                m_getuid.return_value = 0
                assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        cmd_output = json.loads(out)
        assert "it worked" == cmd_output["my-var"]
        if ud_expected == "ci-b64:":
            ud_expected = "ci-b64:{}".format(b64e(ud_src))
        if vd_expected == "ci-b64:":
            vd_expected = "ci-b64:{}".format(b64e(vd_src))
        assert ud_expected == cmd_output["userdata"]
        assert vd_expected == cmd_output["vendordata"]

    def test_handle_args_user_vendor_data_defaults_to_instance_link(
        self, capsys, tmpdir
    ):
        """When no instance_data argument, root uses sensitive json."""
        paths, run_dir, _, _ = self._setup_paths(tmpdir)
        sensitive_file = run_dir.join(INSTANCE_JSON_SENSITIVE_FILE)
        sensitive_file.write('{"my-var": "it worked"}')

        ud_path = os.path.join(paths.instance_link, "user-data.txt")
        write_file(ud_path, "instance_link_ud")
        vd_path = os.path.join(paths.instance_link, "vendor-data.txt")
        write_file(vd_path, "instance_link_vd")

        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=None,
            vendor_data=None,
            varname=None,
        )
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            with mock.patch("os.getuid", return_value=0):
                assert 0 == query.handle_args("anyname", args)
        expected = (
            '{\n "my-var": "it worked",\n '
            '"userdata": "instance_link_ud",\n '
            '"vendordata": "instance_link_vd"\n}\n'
        )
        out, _ = capsys.readouterr()
        assert expected == out

    def test_handle_args_root_uses_instance_sensitive_data(
        self, capsys, tmpdir
    ):
        """When no instance_data argument, root uses sensitive json."""
        paths, run_dir, user_data, vendor_data = self._setup_paths(
            tmpdir, ud_val="ud", vd_val="vd"
        )
        sensitive_file = run_dir.join(INSTANCE_JSON_SENSITIVE_FILE)
        sensitive_file.write('{"my-var": "it worked"}')
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=None,
            list_keys=False,
            user_data=user_data.strpath,
            vendor_data=vendor_data.strpath,
            varname=None,
        )
        with mock.patch(M_PATH + "read_cfg_paths") as m_paths:
            m_paths.return_value = paths
            with mock.patch("os.getuid") as m_getuid:
                m_getuid.return_value = 0
                assert 0 == query.handle_args("anyname", args)
        expected = (
            '{\n "my-var": "it worked",\n '
            '"userdata": "ud",\n "vendordata": "vd"\n}\n'
        )
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_dumps_all_instance_data(self, capsys, tmpdir):
        """When --all is specified query will dump all instance data vars."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write('{"my-var": "it worked"}')
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=False,
            user_data="ud",
            vendor_data="vd",
            varname=None,
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        expected = (
            '{\n "my-var": "it worked",\n "userdata": "<%s> file:ud",\n'
            ' "vendordata": "<%s> file:vd"\n}\n'
            % (REDACT_SENSITIVE_VALUE, REDACT_SENSITIVE_VALUE)
        )
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_returns_top_level_varname(self, capsys, tmpdir):
        """When the argument varname is passed, report its value."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write('{"my-var": "it worked"}')
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=False,
            user_data="ud",
            vendor_data="vd",
            varname="my_var",
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        assert "it worked\n" == out

    @pytest.mark.parametrize(
        "inst_data,varname,expected",
        (
            (
                '{"v1": {"key-2": "value-2"}, "my-var": "it worked"}',
                "v1.key_2",
                "value-2\n",
            ),
            # Assert no jinja underscore-delimited aliases are reported on CLI
            (
                '{"v1": {"something-hyphenated": {"no.underscores":"x",'
                ' "no-alias": "y"}}, "my-var": "it worked"}',
                "v1.something_hyphenated",
                '{\n "no-alias": "y",\n "no.underscores": "x"\n}\n',
            ),
        ),
    )
    def test_handle_args_returns_nested_varname(
        self, inst_data, varname, expected, capsys, tmpdir
    ):
        """If user_data file is a jinja template render instance-data vars."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(inst_data)
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=instance_data.strpath,
            user_data="ud",
            vendor_data="vd",
            list_keys=False,
            varname=varname,
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_returns_standardized_vars_to_top_level_aliases(
        self, capsys, tmpdir
    ):
        """Any standardized vars under v# are promoted as top-level aliases."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(
            '{"v1": {"v1_1": "val1.1"}, "v2": {"v2_2": "val2.2"},'
            ' "top": "gun"}'
        )
        expected = dedent(
            """\
            {
             "top": "gun",
             "userdata": "<redacted for non-root user> file:ud",
             "v1": {
              "v1_1": "val1.1"
             },
             "v1_1": "val1.1",
             "v2": {
              "v2_2": "val2.2"
             },
             "v2_2": "val2.2",
             "vendordata": "<redacted for non-root user> file:vd"
            }
        """
        )
        args = self.Args(
            debug=False,
            dump_all=True,
            format=None,
            instance_data=instance_data.strpath,
            user_data="ud",
            vendor_data="vd",
            list_keys=False,
            varname=None,
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_list_keys_sorts_top_level_keys_when_no_varname(
        self, capsys, tmpdir
    ):
        """Sort all top-level keys when only --list-keys provided."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(
            '{"v1": {"v1_1": "val1.1"}, "v2": {"v2_2": "val2.2"},'
            ' "top": "gun"}'
        )
        expected = "top\nuserdata\nv1\nv1_1\nv2\nv2_2\nvendordata\n"
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=True,
            user_data="ud",
            vendor_data="vd",
            varname=None,
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_list_keys_sorts_nested_keys_when_varname(
        self, capsys, tmpdir
    ):
        """Sort all nested keys of varname object when --list-keys provided."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(
            '{"v1": {"v1_1": "val1.1", "v1_2": "val1.2"}, "v2":'
            + ' {"v2_2": "val2.2"}, "top": "gun"}'
        )
        expected = "v1_1\nv1_2\n"
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=True,
            user_data="ud",
            vendor_data="vd",
            varname="v1",
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 0 == query.handle_args("anyname", args)
        out, _err = capsys.readouterr()
        assert expected == out

    def test_handle_args_list_keys_errors_when_varname_is_not_a_dict(
        self, caplog, tmpdir
    ):
        """Raise an error when --list-keys and varname specify a non-list."""
        instance_data = tmpdir.join("instance-data")
        instance_data.write(
            '{"v1": {"v1_1": "val1.1", "v1_2": "val1.2"}, "v2": '
            + '{"v2_2": "val2.2"}, "top": "gun"}'
        )
        expected_error = "--list-keys provided but 'top' is not a dict"
        args = self.Args(
            debug=False,
            dump_all=False,
            format=None,
            instance_data=instance_data.strpath,
            list_keys=True,
            user_data="ud",
            vendor_data="vd",
            varname="top",
        )
        with mock.patch("os.getuid") as m_getuid:
            m_getuid.return_value = 100
            assert 1 == query.handle_args("anyname", args)
        assert expected_error in caplog.text


# vi: ts=4 expandtab