aboutsummaryrefslogtreecommitdiff
path: root/gitolite-tools/gitolite-groups
blob: a6c800869b8fd9f4bd4f071fa16f39b3b6969bd6 (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
#!/usr/bin/env python
# Copyright (C) 2013, 2014 Linaro Ltd.
#
# 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/>.

import base64
import httplib
import json
import ldap
import os
import sys
import urllib

from ConfigParser import ConfigParser

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import linaro_ldap

DEFAULT_CONFIG_FILE = "gitolite-tools.conf"

CONFIG = ConfigParser()
CONFIG.read(os.path.join(os.path.dirname(sys.argv[0]), DEFAULT_CONFIG_FILE))


def get_crowd_user(user):
    '''Crowd requires our LDAP's CN attribute as its "username" parameter'''
    with linaro_ldap.ldap_client(linaro_ldap.build_config()) as client:
        search_filter = "(uid={0})".format(user)
        base_dn = 'ou=accounts,dc=linaro,dc=org'
        result = client.search_s(
            base_dn, ldap.SCOPE_SUBTREE, search_filter, attrlist=['cn'])
        if result:
            try:
                return result[0][1]['cn'][0]
            except KeyError:
                sys.stderr.write(
                    "gitolite-groups: ERROR: User {0} does not have an email "
                    "address.\n".format(user))


def get_groups(user):
    crowd_usr = CONFIG.get("crowd", "crowd_name")
    crowd_pwd = CONFIG.get("crowd", "crowd_pwd")

    user = get_crowd_user(user)

    user_teams = ""
    crowd_error = False
    if user:
        params = {"username": user}
        auth = base64.encodestring('{0}:{1}'.format(crowd_usr, crowd_pwd))
        headers = {
            "Authorization": "Basic {0}".format(auth),
            "Accept": "application/json"
        }
        url = "/user/group/nested?{0}".format(
            urllib.urlencode(params))

        c = httplib.HTTPSConnection("login.linaro.org", 8443)
        c.request("GET", "/crowd/rest/usermanagement/1{0}".format(url),
                  headers=headers)

        try:
            resp = c.getresponse()

            if resp.status != 200:
                sys.stderr.write("gitolite-groups: ERROR: Non-successful "
                                 "response from Crowd: %s\n" % resp.status)
                crowd_error = True
            else:
                data = json.load(resp)
                names = [x["name"] for x in data["groups"]]
                user_teams = " ".join(names)
        except IOError, e:
            sys.stderr.write("gitolite-groups: ERROR: No connection to "
                             "Crowd server.\n")
            sys.stderr.write(e)
            crowd_error = True

    if crowd_error:
        sys.stderr.write("gitolite-groups: Warning: Group memberships "
                         "unavailble, access to some repositories may "
                         "be blocked.\n")

    return user_teams


if __name__ == "__main__":
    if len(sys.argv) == 1:
        sys.stderr.write("No user name provided to the program.\n")
        sys.exit(1)
    else:
        print(get_groups(sys.argv[1]))