Welcome to mirror list, hosted at ThFree Co, Russian Federation.

challenges.py « certbot_nginx « certbot-nginx - github.com/certbot/certbot.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aff1c57db2e989a549b3aff87437c092c14a2204 (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
"""Classes that perform challenges for Nginx"""

import logging
import os

import six

from acme import challenges

from certbot import errors
from certbot.plugins import common

from certbot_nginx import obj
from certbot_nginx import nginxparser


logger = logging.getLogger(__name__)

class NginxChallengePerformer(common.ChallengePerformer):
    """Additional helper methods for Nginx challenge performers."""

    def perform(self):
        """Perform all added challenges.

        :returns: challenge respones
        :rtype: `list` of `acme.challenges.KeyAuthorizationChallengeResponse`


        """
        raise NotImplementedError() # pragma: no cover

    def _make_server_block(self, achall, addrs):
        """Creates a server block for a challenge.

        :param achall: Annotated HTTP-01 challenge
        :type achall:
            :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge`

        :param list addrs: addresses of challenged domain
            :class:`list` of type :class:`~nginx.obj.Addr`

        :returns: server block for the challenge host
        :rtype: list

        """
        raise NotImplementedError() # pragma: no cover

    @property
    def _challenge_conf(self):
        """Location of the challenge config file"""
        raise NotImplementedError() # pragma: no cover

    def _listen_addresses(self, default_addr, ipv6_addr, port):
        """Finds addresses for each challenge block to listen on.

        :param string default_addr: default listen directive argument for ipv4
        :param string ipv6_addr: default listen directive argument for ipv6
        :param int port: port to check for ipv6 usage

        :returns: list of lists of :class:`certbot_nginx.obj.Addr` to apply
        :rtype: list

        """
        addresses = []
        ipv6, ipv6only = self.configurator.ipv6_info(port)

        for achall in self.achalls:
            vhost = self.configurator.choose_vhost(achall.domain, create_if_no_match=True)

            if vhost is not None and vhost.addrs:
                non_ssl_addrs = (addr for addr in vhost.addrs if not addr.ssl)
                addresses.append(list(non_ssl_addrs))
            else:
                if ipv6:
                    # If IPv6 is active in Nginx configuration
                    if not ipv6only:
                        # If ipv6only=on is not already present in the config
                        ipv6_addr = ipv6_addr + " ipv6only=on"
                    addresses.append([obj.Addr.fromstring(default_addr),
                                      obj.Addr.fromstring(ipv6_addr)])
                    logger.info(("Using default addresses %s and %s for authentication."),
                                default_addr,
                                ipv6_addr)
                else:
                    addresses.append([obj.Addr.fromstring(default_addr)])
                    logger.info("Using default address %s for authentication.",
                                default_addr)
        return addresses

    def _mod_config(self, ll_addrs):
        """Modifies Nginx config to include challenge server blocks.

        :param list ll_addrs: list of lists of
            :class:`certbot_nginx.obj.Addr` to apply

        :raises .MisconfigurationError:
            Unable to find a suitable block in which to include
            authenticator hosts.

        """
        # Add the 'include' statement for the challenges if it doesn't exist
        # already in the main config
        included = False
        include_directive = ['\n', 'include', ' ', self._challenge_conf]
        root = self.configurator.parser.config_root

        bucket_directive = ['\n', 'server_names_hash_bucket_size', ' ', '128']

        main = self.configurator.parser.parsed[root]
        for line in main:
            if line[0] == ['http']:
                body = line[1]
                found_bucket = False
                posn = 0
                for inner_line in body:
                    if inner_line[0] == bucket_directive[1]:
                        if int(inner_line[1]) < int(bucket_directive[3]):
                            body[posn] = bucket_directive
                        found_bucket = True
                    posn += 1
                if not found_bucket:
                    body.insert(0, bucket_directive)
                if include_directive not in body:
                    body.insert(0, include_directive)
                included = True
                break
        if not included:
            raise errors.MisconfigurationError(
                'Certbot could not find a block to include '
                'challenges in %s.' % root)
        config = [self._make_server_block(pair[0], pair[1])
                  for pair in six.moves.zip(self.achalls, ll_addrs)]
        config = nginxparser.UnspacedList(config)

        self.configurator.reverter.register_file_creation(
            True, self._challenge_conf)

        with open(self._challenge_conf, "w") as new_conf:
            nginxparser.dump(config, new_conf)

    def _make_base_block(self, addrs):
        """Creates a baseline server block that listens and logs for a challenge.

        :param list addrs: addresses of challenged domain
            :class:`list` of type :class:`~nginx.obj.Addr`

        :returns: baseline server block for the challenge host,
             without the server directive
        :rtype: list

        """
        block = [['listen', ' ', addr.to_string(include_default=False)] for addr in addrs]

        block.extend([# access and error logs necessary for
                      # integration testing (non-root)
                      ['access_log', ' ', os.path.join(
                          self.configurator.config.work_dir, 'access.log')],
                      ['error_log', ' ', os.path.join(
                          self.configurator.config.work_dir, 'error.log')]
                      ])
        return block

class NginxHttp01(NginxChallengePerformer):
    """HTTP-01 authenticator for Nginx

    :ivar configurator: NginxConfigurator object
    :type configurator: :class:`~nginx.configurator.NginxConfigurator`

    :ivar list achalls: Annotated
        class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge`
        challenges

    :param list indices: Meant to hold indices of challenges in a
        larger array. NginxHttp01 is capable of solving many challenges
        at once which causes an indexing issue within NginxConfigurator
        who must return all responses in order.  Imagine NginxConfigurator
        maintaining state about where all of the http-01 Challenges,
        TLS-SNI-01 Challenges belong in the response array.  This is an
        optional utility.

    :param str challenge_conf: location of the challenge config file

    """

    def __init__(self, configurator):
        super(NginxHttp01, self).__init__(configurator)
        self.challenge_conf = os.path.join(
            configurator.config.config_dir, "le_http_01_cert_challenge.conf")

    @property
    def _challenge_conf(self):
        """Location of the challenge config file"""
        return self.challenge_conf

    def perform(self):
        """Perform a challenge on Nginx.

        :returns: list of :class:`certbot.acme.challenges.HTTP01Response`
        :rtype: list

        """
        if not self.achalls:
            return []

        default_addr = "%s" % self.configurator.config.http01_port
        ipv6_addr = "[::]:{0}".format(
                        self.configurator.config.http01_port)

        addresses = self._listen_addresses(default_addr, ipv6_addr,
            self.configurator.config.http01_port)

        responses = [x.response(x.account_key) for x in self.achalls]

        # Set up the configuration
        self._mod_config(addresses)

        # Save reversible changes
        self.configurator.save("HTTP Challenge", True)

        return responses

    def _get_validation_path(self, achall):
        return os.sep + os.path.join(challenges.HTTP01.URI_ROOT_PATH, achall.chall.encode("token"))

    def _make_server_block(self, achall, addrs):
        """Creates a server block for a challenge.

        :param achall: Annotated HTTP-01 challenge
        :type achall:
            :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge`

        :param list addrs: addresses of challenged domain
            :class:`list` of type :class:`~nginx.obj.Addr`

        :returns: server block for the challenge host
        :rtype: list

        """
        block = self._make_base_block(addrs)
        validation = achall.validation(achall.account_key)
        validation_path = self._get_validation_path(achall)

        block.extend([['server_name', ' ', achall.domain],
                      [['location', ' ', '=', ' ', validation_path],
                        [['default_type', ' ', 'text/plain'],
                         ['return', ' ', '200', ' ', validation]]]])
        return [['server'], block]


class NginxTlsSni01(common.TLSSNI01, NginxChallengePerformer):
    """TLS-SNI-01 authenticator for Nginx

    :ivar configurator: NginxConfigurator object
    :type configurator: :class:`~nginx.configurator.NginxConfigurator`

    :ivar list achalls: Annotated
        class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge`
        challenges

    :param list indices: Meant to hold indices of challenges in a
        larger array. NginxTlsSni01 is capable of solving many challenges
        at once which causes an indexing issue within NginxConfigurator
        who must return all responses in order.  Imagine NginxConfigurator
        maintaining state about where all of the http-01 Challenges,
        TLS-SNI-01 Challenges belong in the response array.  This is an
        optional utility.

    :param str challenge_conf: location of the challenge config file

    """

    @property
    def _challenge_conf(self):
        """Location of the challenge config file"""
        return self.challenge_conf

    def perform(self):
        """Perform a challenge on Nginx.

        :returns: list of :class:`certbot.acme.challenges.TLSSNI01Response`
        :rtype: list

        """
        if not self.achalls:
            return []

        default_addr = "{0} ssl".format(
            self.configurator.config.tls_sni_01_port)
        ipv6_addr = "[::]:{0} ssl".format(
            self.configurator.config.tls_sni_01_port)
        addresses = self._listen_addresses(default_addr, ipv6_addr,
            self.configurator.config.tls_sni_01_port)

        # Create challenge certs
        responses = [self._setup_challenge_cert(x) for x in self.achalls]

        # Set up the configuration
        self._mod_config(addresses)

        # Save reversible changes
        self.configurator.save("SNI Challenge", True)

        return responses

    def _make_server_block(self, achall, addrs):
        """Creates a server block for a challenge.

        :param achall: Annotated TLS-SNI-01 challenge
        :type achall:
            :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge`

        :param list addrs: addresses of challenged domain
            :class:`list` of type :class:`~nginx.obj.Addr`

        :returns: server block for the challenge host
        :rtype: list

        """
        document_root = os.path.join(
            self.configurator.config.work_dir, "tls_sni_01_page")

        block = self._make_base_block(addrs)

        block.extend([['server_name', ' ',
                       achall.response(achall.account_key).z_domain.decode('ascii')],
                      ['ssl_certificate', ' ', self.get_cert_path(achall)],
                      ['ssl_certificate_key', ' ', self.get_key_path(achall)],
                      ['include', ' ', self.configurator.mod_ssl_conf],
                      [['location', ' ', '/'], [['root', ' ', document_root]]]])
        return [['server'], block]