~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
14458.1.1 by Robert Collins
Working sketch.
16
from twisted.scripts.twistd import ServerOptions
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
17
from twisted.web.xmlrpc import Proxy
18
19
from zope.interface import implements
20
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
21
from lp.services.config import config
14600.2.6 by Curtis Hovey
Moved daemons to lp.services.
22
from lp.services.daemons import readyservice
14565.2.15 by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts.
23
from lp.services.scripts import execute_zcml_for_scripts
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
24
12392.7.21 by Julian Edwards
move code out of the tac file and into twistedftp.py
25
from lp.poppy import get_poppy_root
12919.6.13 by Ian Booth
Move job instantiation to separate class
26
from lp.poppy.twistedconfigreset import GPGHandlerConfigResetJob
12392.7.13 by Julian Edwards
Move the new ftp code to its own file
27
from lp.poppy.twistedftp import (
12392.7.21 by Julian Edwards
move code out of the tac file and into twistedftp.py
28
    FTPServiceFactory,
12392.7.13 by Julian Edwards
Move the new ftp code to its own file
29
    )
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
30
from lp.poppy.twistedsftp import SFTPServer
31
from lp.services.sshserver.auth import (
32
    LaunchpadAvatar, PublicKeyFromLaunchpadChecker)
33
from lp.services.sshserver.service import SSHService
11057.4.3 by Jonathan Lange
Use the DoNothing adapter.
34
from lp.services.sshserver.session import DoNothingSession
14458.1.1 by Robert Collins
Working sketch.
35
from lp.services.twistedsupport.loggingsupport import set_up_oops_reporting
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
36
37
38
def make_portal():
39
    """Create and return a `Portal` for the SSH service.
40
41
    This portal accepts SSH credentials and returns our customized SSH
42
    avatars (see `LaunchpadAvatar`).
43
    """
44
    authentication_proxy = Proxy(
45
        config.poppy.authentication_endpoint)
46
    portal = Portal(Realm(authentication_proxy))
47
    portal.registerChecker(
48
        PublicKeyFromLaunchpadChecker(authentication_proxy))
49
    return portal
50
51
52
class Realm:
53
    implements(IRealm)
54
55
    def __init__(self, authentication_proxy):
56
        self.authentication_proxy = authentication_proxy
57
58
    def requestAvatar(self, avatar_id, mind, *interfaces):
59
        # Fetch the user's details from the authserver
60
        deferred = mind.lookupUserDetails(
61
            self.authentication_proxy, avatar_id)
62
63
        # Once all those details are retrieved, we can construct the avatar.
64
        def got_user_dict(user_dict):
65
            avatar = LaunchpadAvatar(user_dict)
66
            return interfaces[0], avatar, avatar.logout
67
68
        return deferred.addCallback(got_user_dict)
69
70
71
def poppy_sftp_adapter(avatar):
72
    return SFTPServer(avatar, get_poppy_root())
73
74
14458.1.1 by Robert Collins
Working sketch.
75
# Force python logging to all go to the Twisted log.msg interface. The default
76
# - output on stderr - will not be watched by anyone.
77
from twisted.python import log
78
stream = log.StdioOnnaStick()
79
logging.basicConfig(stream=stream, level=logging.INFO)
12392.7.27 by Julian Edwards
make logging work, and fix the sleep in the tests to look for a special log message instead
80
81
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
82
components.registerAdapter(
83
    poppy_sftp_adapter, LaunchpadAvatar, filetransfer.ISFTPServer)
84
11057.4.3 by Jonathan Lange
Use the DoNothing adapter.
85
components.registerAdapter(DoNothingSession, LaunchpadAvatar, ISession)
86
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
87
12392.7.17 by Julian Edwards
get the port from the config
88
# ftpport defaults to 2121 in schema-lazr.conf
12392.7.22 by Julian Edwards
ftp_port not ftpport
89
ftpservice = FTPServiceFactory.makeFTPService(port=config.poppy.ftp_port)
12392.7.1 by Julian Edwards
first crappy stab at ftp service inside poppy
90
12392.7.16 by Julian Edwards
use the config's timeout
91
# Construct an Application that has the Poppy SSH server,
92
# and the Poppy FTP server.
14458.1.1 by Robert Collins
Working sketch.
93
options = ServerOptions()
94
options.parseOptions()
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
95
application = service.Application('poppy-sftp')
14458.1.1 by Robert Collins
Working sketch.
96
observer = set_up_oops_reporting(
97
    'poppy-sftp', 'poppy', options.get('logfile'))
98
application.addComponent(observer, ignoreClass=1)
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
99
12392.7.1 by Julian Edwards
first crappy stab at ftp service inside poppy
100
ftpservice.setServiceParent(application)
101
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
102
def timeout_decorator(factory):
103
    """Add idle timeouts to a factory."""
104
    return TimeoutFactory(factory, timeoutPeriod=config.poppy.idle_timeout)
105
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
106
svc = SSHService(
107
    portal=make_portal(),
108
    private_key_path=config.poppy.host_key_private,
109
    public_key_path=config.poppy.host_key_public,
110
    oops_configuration='poppy',
111
    main_log='poppy',
112
    access_log='poppy.access',
113
    access_log_path=config.poppy.access_log,
114
    strport=config.poppy.port,
12221.16.19 by Andrew Bennetts
Update poppy-sftp.tac for change to SSHService constructor.
115
    factory_decorator=timeout_decorator,
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
116
    banner=config.poppy.banner)
117
svc.setServiceParent(application)
118
12392.8.1 by Julian Edwards
first stab at rejecting unsigned changes files - requires a patch to Twisted to return the error code properly
119
# We need Zope for looking up the GPG utilities.
120
execute_zcml_for_scripts()
121
12919.6.12 by Ian Booth
Move gpghandler job to poppy-sftp.tac
122
# Set up the GPGHandler job
12919.6.14 by Ian Booth
Set up service properly using twisted
123
GPGHandlerConfigResetJob().setServiceParent(application)
12919.6.12 by Ian Booth
Move gpghandler job to poppy-sftp.tac
124
10918.2.3 by Steve Kowalik
Merge in the code for poppy-sftp
125
# Service that announces when the daemon is ready
11765.1.1 by Robert Collins
Split out the launchpad-buildd needed component from tachandler.py.
126
readyservice.ReadyService().setServiceParent(application)