This file is indexed.

/usr/share/pyshared/os_collect_config/tests/test_ec2.py is in python-os-collect-config 0.1.15-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import fixtures
import requests
import testtools
from testtools import matchers
import urlparse
import uuid

from os_collect_config import collect
from os_collect_config import ec2
from os_collect_config import exc


META_DATA = {'local-ipv4': '192.0.2.1',
             'reservation-id': str(uuid.uuid1()),
             'local-hostname': 'foo',
             'ami-launch-index': '0',
             'public-hostname': 'foo',
             'hostname': 'foo',
             'ami-id': str(uuid.uuid1()),
             'instance-action': 'none',
             'public-ipv4': '192.0.2.1',
             'instance-type': 'flavor.small',
             'placement/': 'availability-zone',
             'placement/availability-zone': 'foo-az',
             'mpi/': 'foo-keypair',
             'mpi/foo-keypair': '192.0.2.1 slots=1',
             'block-device-mapping/': "ami\nroot\nephemeral0",
             'block-device-mapping/ami': 'vda',
             'block-device-mapping/root': '/dev/vda',
             'block-device-mapping/ephemeral0': '/dev/vdb',
             'public-keys/': '0=foo-keypair',
             'public-keys/0': 'openssh-key',
             'public-keys/0/': 'openssh-key',
             'public-keys/0/openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC',
             'instance-id': str(uuid.uuid1())}


class FakeResponse(dict):
    def __init__(self, text):
        self.text = text

    def raise_for_status(self):
        pass


class FakeRequests(object):
    exceptions = requests.exceptions

    class Session(object):
        def get(self, url):
            url = urlparse.urlparse(url)

            if url.path == '/latest/meta-data/':
                # Remove keys which have anything after /
                ks = [x for x in META_DATA.keys() if (
                    '/' not in x or not len(x.split('/')[1]))]
                return FakeResponse("\n".join(ks))

            path = url.path
            path = path.replace('/latest/meta-data/', '')
            return FakeResponse(META_DATA[path])


class FakeFailRequests(object):
    exceptions = requests.exceptions

    class Session(object):
        def get(self, url):
            raise requests.exceptions.HTTPError(403, 'Forbidden')


class TestEc2(testtools.TestCase):
    def setUp(self):
        super(TestEc2, self).setUp()
        self.log = self.useFixture(fixtures.FakeLogger())

    def test_collect_ec2(self):
        collect.setup_conf()
        ec2_md = ec2.Collector(requests_impl=FakeRequests).collect()
        self.assertThat(ec2_md, matchers.IsInstance(list))
        self.assertEqual('ec2', ec2_md[0][0])
        ec2_md = ec2_md[0][1]

        for k in ('public-ipv4', 'instance-id', 'hostname'):
            self.assertIn(k, ec2_md)
            self.assertEqual(ec2_md[k], META_DATA[k])

        self.assertEqual(ec2_md['block-device-mapping']['ami'], 'vda')

        # SSH keys are special cases
        self.assertEqual(
            {'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}},
            ec2_md['public-keys'])
        self.assertEqual('', self.log.output)

    def test_collect_ec2_fail(self):
        collect.setup_conf()
        collect_ec2 = ec2.Collector(requests_impl=FakeFailRequests)
        self.assertRaises(exc.Ec2MetadataNotAvailable, collect_ec2.collect)
        self.assertIn('Forbidden', self.log.output)