示例#1
0
def post_new_ssh_key(username):
    """ covered by 1_users / `User can add an ssh key` """
    json_data = get_json_data()
    if False:  # TODO: ensure that username exists
        return jsonify({"error": "username does not exist"}), 404

    pkey_contents = json_data.get("pkey_contents")

    if not pkey_contents:
        return jsonify({"error": "No key was given!"}), 400

    gw = GitoliteWrapper(GITOLITE_ADMIN_PATH)

    try:
        pretty_key_hex = gw.add_pkey_to_user(username, pkey_contents)
        return jsonify(key_added=pretty_key_hex)
    except InvalidKeyException as e:
        return jsonify({"error": "Public key was invalid format", "exception": str(e)}), 400
    except KeyAlreadyExistsError as e:
        return jsonify({"error": "Public key already exists", "exception": str(e)}), 400
    except UserDoesNotExistError as e:
        return jsonify({"error": "username not found!", "exception": str(e)}), 404
    parser.add_argument("-pyo", "--pyolite_location", help="the folder pyolite should bind to")
    args = parser.parse_args()

    password = "******"

    dbw = DatabaseWrapper(args.pyolite_location, port=args.mongo_port, repositories_root="/var/lib/jenkins/jobs/acceptance-tests/workspace/temp")
    gw = GitoliteWrapper(args.pyolite_location)

    # give one user a REAL pubkey, so git can interact with server
    # note: this is the contents of /home/git/.ssh/test_key.pub and test_key2.pub
    test_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAObvHN/F6EX4L9/ExE4LdezTD1Dx/+fX0wZdropEyx63VxBn8HpgwVcjYq5fINAUAqtNoNpIgyXpp8RhoFv488qaCL9V7qwSf1Bnv5uaZ5lxIpdfEFXhu4JsgtdphMangwvSf2ADLASFdB99Sjxo/nxmjHsvyPZXuvYMNdmKn9ZBQyimUNpERlL4/ECVRoebnL4lD/+rzreacTiZA6KvPn8f7Xh8JNHwm9ndRCxflc8fTH+0VgkC2kpQVkWOoXqhQUQpX8fYxrmCamDG4oRAKJpF2SLkRv1OAv+jY58e2fxAUqFK/u2k6XFPn9Sxmz8pZsvp8OHAuVeSdECyoKw/J git@gitsubmit"
    test_key2 = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8DylxzTAc4y/AG/RCONTiZx2+2hfAgfuncXwZylUse6JkHKIIFbjkXqoAkmT0hsugMHb6IpnINcc41vXXOPTwjLrgMaO7r/eK6gs6rqUiVaJ3oVuJK9qw1PwPo/4O/eCQ3mtoDSm+U27/Q5D391jAuqngmMtYxQAUqS+Hv4/ORNbCNXl/b4UQYldUz3N2giNPCIXA78CCDwz4V/jOq6zejnt+DLoiBCTnCQWCIANX+Ij5pvqBP2pDtO9RnFVHEK1O2jXUHGiLq9AcLvDDYGnATwnXbXtPn5Ub0tLFcanO4DW93WTljhCS7TyzUq/MyAGwNmwi7OSVqpNYcHvhUg37 git@gitsubmit"

    # give the users some pkeys
    bogus_key = create_bogus_key()
    gw.add_pkey_to_user("student1", bogus_key["pubkey_contents"])
    bogus_key2 = create_bogus_key()
    gw.add_pkey_to_user("student2", bogus_key2["pubkey_contents"])
    bogus_key3 = create_bogus_key()
    gw.add_pkey_to_user("teacher1", bogus_key3["pubkey_contents"])
    bogus_key4 = create_bogus_key()
    gw.add_pkey_to_user("teacher2", bogus_key4["pubkey_contents"])
    bogus_key4 = create_bogus_key()
    gw.add_pkey_to_user("spencer", bogus_key4["pubkey_contents"])
    gw.add_pkey_to_user("teacher_usable", test_key)
    gw.add_pkey_to_user("student_usable", test_key2)

    # populate the databases with some data
    dbw.create_user("student1", "*****@*****.**", password, "Student", "One")
    dbw.create_user("student2", "*****@*****.**", password, "Student", "Two")
    dbw.create_user("teacher1", "*****@*****.**", password, "Teacher", "One")