summaryrefslogtreecommitdiff
path: root/src/leap/soledad/common/couch/state.py
blob: 5614b32f6a7eebc9a3cc6cc8a915091738402a42 (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
# -*- coding: utf-8 -*-
# state.py
# Copyright (C) 2015,2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Server state using CouchDatabase as backend.
"""
import re
import os
import treq

from six.moves.urllib.parse import urljoin
from twisted.internet import defer
from urlparse import urlsplit

from twisted.internet import reactor

from leap.soledad.common.log import getLogger
from leap.soledad.common.couch import CouchDatabase
from leap.soledad.common.couch import CONFIG_DOC_ID
from leap.soledad.common.couch import SCHEMA_VERSION
from leap.soledad.common.couch import SCHEMA_VERSION_KEY
from leap.soledad.common.command import exec_validated_cmd
from leap.soledad.common.l2db.remote.server_state import ServerState
from leap.soledad.common.l2db.errors import Unauthorized
from leap.soledad.common.errors import WrongCouchSchemaVersionError
from leap.soledad.common.errors import MissingCouchConfigDocumentError


logger = getLogger(__name__)


#
# Database schema version verification
#

@defer.inlineCallbacks
def _check_db_schema_version(url, db, auth, agent=None):
    """
    Check if the schema version is up to date for a given database.

    :param url: the server base URL.
    :type url: str
    :param db: the database name.
    :type db: str
    :param auth: a tuple with (username, password) for acessing CouchDB.
    :type auth: tuple(str, str)
    :param agent: an optional agent for doing requests, used in tests.
    :type agent: twisted.web.client.Agent

    :raise MissingCouchConfigDocumentError: raised when a database is not empty
                                            but has no config document in it.

    :raise WrongCouchSchemaVersionError: raised when a config document was
                                         found but the schema version is
                                         different from what is expected.
    """
    # if there are documents, ensure that a config doc exists
    db_url = urljoin(url, '%s/' % db)
    config_doc_url = urljoin(db_url, CONFIG_DOC_ID)
    res = yield treq.get(config_doc_url, auth=auth, agent=agent)

    if res.code != 200 and res.code != 404:
        raise Exception("Unexpected HTTP response code: %d" % res.code)

    elif res.code == 404:
        res = yield treq.get(urljoin(db_url, '_all_docs'), auth=auth,
                             params={'limit': 1}, agent=agent)
        docs = yield res.json()
        if docs['total_rows'] != 0:
            logger.error(
                "Missing couch config document in database %s" % db)
            raise MissingCouchConfigDocumentError(db)

    elif res.code == 200:
        config_doc = yield res.json()
        if config_doc[SCHEMA_VERSION_KEY] != SCHEMA_VERSION:
            logger.error(
                "Unsupported database schema in database %s" % db)
            raise WrongCouchSchemaVersionError(db)


def _stop(failure, reactor):
    logger.error("Failure while checking schema versions: %r - %s"
                 % (failure, failure.message))
    reactor.addSystemEventTrigger('after', 'shutdown', os._exit, 1)
    reactor.stop()


@defer.inlineCallbacks
def check_schema_versions(couch_url, agent=None, reactor=reactor):
    """
    Check that all user databases use the correct couch schema.

    :param couch_url: The URL for the couch database.
    :type couch_url: str
    :param agent: an optional agent for doing requests, used in tests.
    :type agent: twisted.web.client.Agent
    :param reactor: an optional reactor for stopping in case of errors, used
                    in tests.
    :type reactor: twisted.internet.base.ReactorBase
    """
    url = urlsplit(couch_url)
    auth = (url.username, url.password) if url.username else None
    url = "%s://%s:%d" % (url.scheme, url.hostname, url.port)
    res = yield treq.get(urljoin(url, '_all_dbs'), auth=auth, agent=agent)
    dbs = yield res.json()
    deferreds = []
    semaphore = defer.DeferredSemaphore(20)
    for db in dbs:
        if not db.startswith('user-'):
            continue
        d = semaphore.run(_check_db_schema_version, url, db, auth, agent=agent)
        d.addErrback(_stop, reactor=reactor)
        deferreds.append(d)
    d = defer.gatherResults(deferreds, consumeErrors=True)
    yield d


#
# CouchDB Server state
#

def is_db_name_valid(name):
    """
    Validate a user database using a regular expression.

    :param name: database name.
    :type name: str

    :return: boolean for name vailidity
    :rtype: bool
    """
    db_name_regex = "^user-[a-f0-9]+$"
    return re.match(db_name_regex, name) is not None


class CouchServerState(ServerState):

    """
    Inteface of the WSGI server with the CouchDB backend.
    """

    def __init__(self, couch_url, create_cmd=None):
        """
        Initialize the couch server state.

        :param couch_url: The URL for the couch database.
        :type couch_url: str
        :param create_cmd: Command to be executed for user db creation. It will
                           receive a properly sanitized parameter with user db
                           name and should access CouchDB with necessary
                           privileges, which server lacks for security reasons.
        :type create_cmd: str
        """
        self.couch_url = couch_url
        self.create_cmd = create_cmd

    def open_database(self, dbname):
        """
        Open a couch database.

        :param dbname: The name of the database to open.
        :type dbname: str

        :return: The SoledadBackend object.
        :rtype: SoledadBackend
        """
        url = urljoin(self.couch_url, dbname)
        db = CouchDatabase.open_database(url, create=False)
        return db

    def ensure_database(self, dbname):
        """
        Ensure couch database exists.

        :param dbname: The name of the database to ensure.
        :type dbname: str

        :raise Unauthorized: If disabled or other error was raised.

        :return: The SoledadBackend object and its replica_uid.
        :rtype: (SoledadBackend, str)
        """
        if not self.create_cmd:
            raise Unauthorized()
        else:
            code, out = exec_validated_cmd(self.create_cmd, dbname,
                                           validator=is_db_name_valid)
            if code is not 0:
                logger.error("""
                    Error while creating database (%s) with (%s) command.
                    Output: %s
                    Exit code: %d
                    """ % (dbname, self.create_cmd, out, code))
                raise Unauthorized()
        db = self.open_database(dbname)
        return db, db.replica_uid

    def delete_database(self, dbname):
        """
        Delete couch database.

        :param dbname: The name of the database to delete.
        :type dbname: str

        :raise Unauthorized: Always, because Soledad server is not allowed to
                             delete databases.
        """
        raise Unauthorized()