~launchpad-pqm/launchpad/devel

10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
# This is a Twisted application config file.  To run, use:
5
#     twistd -noy sftp.tac
6
# or similar.  Refer to the twistd(1) man page for details.
7
12392.7.27 by Julian Edwards
make logging work, and fix the sleep in the tests to look for a special log message instead
8
import logging
9
12392.7.21 by Julian Edwards
move code out of the tac file and into twistedftp.py
10
from twisted.application import service
11057.4.3 by Jonathan Lange
Use the DoNothing adapter.
11
from twisted.conch.interfaces import ISession
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
12
from twisted.conch.ssh import filetransfer
13
from twisted.cred.portal import IRealm, Portal
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
14
from twisted.protocols.policies import TimeoutFactory
12392.7.13 by Julian Edwards
Move the new ftp code to its own file
15
from twisted.python import components
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
16
from twisted.web.xmlrpc import Proxy
17
18
from zope.interface import implements
19
20
from canonical.config import config
11765.1.1 by Robert Collins
Split out the launchpad-buildd needed component from tachandler.py.
21
from canonical.launchpad.daemons import readyservice
12392.8.1 by Julian Edwards
first stab at rejecting unsigned changes files - requires a patch to Twisted to return the error code properly
22
from canonical.launchpad.scripts import execute_zcml_for_scripts
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
23
12392.7.21 by Julian Edwards
move code out of the tac file and into twistedftp.py
24
from lp.poppy import get_poppy_root
12392.7.13 by Julian Edwards
Move the new ftp code to its own file
25
from lp.poppy.twistedftp import (
12392.7.21 by Julian Edwards
move code out of the tac file and into twistedftp.py
26
    FTPServiceFactory,
12392.7.13 by Julian Edwards
Move the new ftp code to its own file
27
    )
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
28
from lp.poppy.twistedsftp import SFTPServer
29
from lp.services.sshserver.auth import (
30
    LaunchpadAvatar, PublicKeyFromLaunchpadChecker)
31
from lp.services.sshserver.service import SSHService
11057.4.3 by Jonathan Lange
Use the DoNothing adapter.
32
from lp.services.sshserver.session import DoNothingSession
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
33
34
35
def make_portal():
36
    """Create and return a `Portal` for the SSH service.
37
38
    This portal accepts SSH credentials and returns our customized SSH
39
    avatars (see `LaunchpadAvatar`).
40
    """
41
    authentication_proxy = Proxy(
42
        config.poppy.authentication_endpoint)
43
    portal = Portal(Realm(authentication_proxy))
44
    portal.registerChecker(
45
        PublicKeyFromLaunchpadChecker(authentication_proxy))
46
    return portal
47
48
49
class Realm:
50
    implements(IRealm)
51
52
    def __init__(self, authentication_proxy):
53
        self.authentication_proxy = authentication_proxy
54
55
    def requestAvatar(self, avatar_id, mind, *interfaces):
56
        # Fetch the user's details from the authserver
57
        deferred = mind.lookupUserDetails(
58
            self.authentication_proxy, avatar_id)
59
60
        # Once all those details are retrieved, we can construct the avatar.
61
        def got_user_dict(user_dict):
62
            avatar = LaunchpadAvatar(user_dict)
63
            return interfaces[0], avatar, avatar.logout
64
65
        return deferred.addCallback(got_user_dict)
66
67
68
def poppy_sftp_adapter(avatar):
69
    return SFTPServer(avatar, get_poppy_root())
70
71
12392.7.27 by Julian Edwards
make logging work, and fix the sleep in the tests to look for a special log message instead
72
# Connect Python logging to Twisted's logging.
73
from lp.services.twistedsupport.loggingsupport import set_up_tacfile_logging
74
set_up_tacfile_logging("poppy-sftp", logging.INFO)
75
76
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
77
components.registerAdapter(
78
    poppy_sftp_adapter, LaunchpadAvatar, filetransfer.ISFTPServer)
79
11057.4.3 by Jonathan Lange
Use the DoNothing adapter.
80
components.registerAdapter(DoNothingSession, LaunchpadAvatar, ISession)
81
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
82
12392.7.17 by Julian Edwards
get the port from the config
83
# ftpport defaults to 2121 in schema-lazr.conf
12392.7.22 by Julian Edwards
ftp_port not ftpport
84
ftpservice = FTPServiceFactory.makeFTPService(port=config.poppy.ftp_port)
12392.7.1 by Julian Edwards
first crappy stab at ftp service inside poppy
85
12392.7.16 by Julian Edwards
use the config's timeout
86
# Construct an Application that has the Poppy SSH server,
87
# and the Poppy FTP server.
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
88
application = service.Application('poppy-sftp')
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
89
12392.7.1 by Julian Edwards
first crappy stab at ftp service inside poppy
90
ftpservice.setServiceParent(application)
91
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
92
def timeout_decorator(factory):
93
    """Add idle timeouts to a factory."""
94
    return TimeoutFactory(factory, timeoutPeriod=config.poppy.idle_timeout)
95
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
96
svc = SSHService(
97
    portal=make_portal(),
98
    private_key_path=config.poppy.host_key_private,
99
    public_key_path=config.poppy.host_key_public,
100
    oops_configuration='poppy',
101
    main_log='poppy',
102
    access_log='poppy.access',
103
    access_log_path=config.poppy.access_log,
104
    strport=config.poppy.port,
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
105
    factory_decorator=timeout_decorator,
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
106
    banner=config.poppy.banner)
107
svc.setServiceParent(application)
108
12392.8.1 by Julian Edwards
first stab at rejecting unsigned changes files - requires a patch to Twisted to return the error code properly
109
# We need Zope for looking up the GPG utilities.
110
execute_zcml_for_scripts()
111
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
112
# Service that announces when the daemon is ready
11765.1.1 by Robert Collins
Split out the launchpad-buildd needed component from tachandler.py.
113
readyservice.ReadyService().setServiceParent(application)