Blame scripts/check_tag_perm.py

Thomas Oulevey 31974a
#!/usr/bin/env python
Thomas Oulevey 31974a
Thomas Oulevey 31974a
# Copyright (c) 2015, Thomas Oulevey <thomas.oulevey@cern.ch>
Thomas Oulevey 31974a
# All rights reserved.
Thomas Oulevey 31974a
#
Thomas Oulevey 31974a
# Redistribution and use in source and binary forms, with or without
Thomas Oulevey 31974a
# modification, are permitted provided that the following conditions are met:
Thomas Oulevey 31974a
#
Thomas Oulevey 31974a
# 1. Redistributions of source code must retain the above copyright notice, this
Thomas Oulevey 31974a
#   list of conditions and the following disclaimer.
Thomas Oulevey 31974a
# 2. Redistributions in binary form must reproduce the above copyright notice,
Thomas Oulevey 31974a
#   this list of conditions and the following disclaimer in the documentation
Thomas Oulevey 31974a
#   and/or other materials provided with the distribution.
Thomas Oulevey 31974a
#
Thomas Oulevey 31974a
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Thomas Oulevey 31974a
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Thomas Oulevey 31974a
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Thomas Oulevey 31974a
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Thomas Oulevey 31974a
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Thomas Oulevey 31974a
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Thomas Oulevey 31974a
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Thomas Oulevey 31974a
# INTERRUPTION) HOWEVER CAUSED AND  ON ANY THEORY OF LIABILITY, WHETHER
Thomas Oulevey 31974a
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Thomas Oulevey 31974a
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
Thomas Oulevey 31974a
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Thomas Oulevey 31974a
Thomas Oulevey 31974a
# This script checks if all tags have been assigned permissions and if not enforces them.
Thomas Oulevey 31974a
Thomas Oulevey 31974a
import koji
Thomas Oulevey 31974a
import os.path
Thomas Oulevey 31974a
import sys
Thomas Oulevey 31974a
from collections import defaultdict
Thomas Oulevey 31974a
Thomas Oulevey 31974a
KOJI_URL = 'http://localhost/kojihub'
Thomas Oulevey 31974a
CLIENT_CERT = os.path.expanduser('/etc/pki/koji/koji-admin.pem')
Thomas Oulevey 31974a
CLIENTCA_CERT = os.path.expanduser('/etc/pki/koji/koji_ca_cert.crt')
Thomas Oulevey 31974a
SERVERCA_CERT = os.path.expanduser('/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt')
Thomas Oulevey 31974a
USER = 'koji'
Thomas Oulevey 31974a
SYSTEM_TAGS = ['buildsys', 'bananas', 'infrastructure', 'oranges']
Thomas Oulevey 31974a
Thomas Oulevey 31974a
def get_all_tags():
Thomas Oulevey 31974a
    tags = [(x['name'], x['id'], x['perm']) for x in kojiclient.listTags()]
Thomas Oulevey 31974a
    return [ t for t in tags if t[0].split('-')[0][:-1] not in SYSTEM_TAGS ]
Thomas Oulevey 31974a
Thomas Oulevey 31974a
Thomas Oulevey 31974a
def fix_tag_permission(tags):
Thomas Oulevey 31974a
    for tag in tags:
Thomas Oulevey 31974a
        if not tag[0].endswith('-build') and tag[2] == None:
Thomas Oulevey 31974a
            perm_sig = 'build-' + tag[0].split('-')[0][:-1]
Thomas Oulevey 31974a
            print 'Updating %s with permission %s...'% (tag[0],perm_sig)
Thomas Oulevey 31974a
            kojiclient.editTag2(tag[0],perm=perm_sig)
Thomas Oulevey 31974a
Thomas Oulevey 31974a
if __name__ == '__main__':
Thomas Oulevey 31974a
    try:
Thomas Oulevey 31974a
        kojiclient = koji.ClientSession(KOJI_URL)
Thomas Oulevey 31974a
        kojiclient.ssl_login(CLIENT_CERT, CLIENTCA_CERT, SERVERCA_CERT)
Thomas Oulevey 31974a
    except:
Thomas Oulevey 31974a
        print "Could not connect to koji API"
Thomas Oulevey 31974a
        sys.exit(2)
Thomas Oulevey 31974a
    
Thomas Oulevey 31974a
    fix_tag_permission(get_all_tags())
Thomas Oulevey 31974a
    sys.exit(0)