<commit_before>from __future__ import absolute_import, division, print_function, unicode_literals

from io import open

import os, sys, json, time, logging, subprocess, pwd, hashlib
from collections import namedtuple

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)

import boto3
from botocore.exceptions import ClientError

class ARN(namedtuple("ARN", "partition service region account resource")):
    def __str__(self):
        return ":".join(["arn"] + list(self))

ARN.__new__.__defaults__ = ("aws", "", "", "", "")

def parse_arn(arn):
    return ARN(*arn.split(":", 5)[1:])

def configure(args):
    print("Will configure", args)

def get_authorized_keys(args):
    iam = boto3.client("iam")
    try:
        for key_desc in iam.list_ssh_public_keys(UserName=args.user)["SSHPublicKeys"]:
            key = iam.get_ssh_public_key(UserName=args.user, SSHPublicKeyId=key_desc["SSHPublicKeyId"], Encoding="SSH")
            if key["SSHPublicKey"]["Status"] == "Active":
                print(key["SSHPublicKey"]["SSHPublicKeyBody"])
    except Exception as e:
        err_exit("Error while retrieving IAM SSH keys for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)

def aws_to_unix_id(i):
    return 2000 + (int.from_bytes(hashlib.sha256(i.encode()).digest()[-2:], byteorder=sys.byteorder) // 2)

def get_uid(args):
    iam = boto3.resource("iam")
    try:
        user_id = iam.User(args.user).user_id
        uid = aws_to_unix_id(user_id)
        print(uid)
    except Exception as e:
        err_exit("Error while retrieving UID for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)

def get_groups(args):
    iam_linux_group_prefix = "keymaker_"
    iam = boto3.resource("iam")
    try:
        for group in iam.User(args.user).groups.all():
            if group.name.startswith(iam_linux_group_prefix):
                gid = aws_to_unix_id(group.group_id)
                print(group.name[len(iam_linux_group_prefix):])
    except Exception as e:
        err_exit("Error while retrieving UID for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)<commit_msg>fix to permit python 2/3 to get UID from AWS key ID<commit_after>from __future__ import absolute_import, division, print_function, unicode_literals

from io import open

import os
import sys
import json
import time
import logging
import subprocess
import pwd
import hashlib
import codecs
from collections import namedtuple

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)

import boto3
from botocore.exceptions import ClientError

class ARN(namedtuple("ARN", "partition service region account resource")):
    def __str__(self):
        return ":".join(["arn"] + list(self))

ARN.__new__.__defaults__ = ("aws", "", "", "", "")

def parse_arn(arn):
    return ARN(*arn.split(":", 5)[1:])

def configure(args):
    print("Will configure", args)

def get_authorized_keys(args):
    iam = boto3.client("iam")
    try:
        for key_desc in iam.list_ssh_public_keys(UserName=args.user)["SSHPublicKeys"]:
            key = iam.get_ssh_public_key(UserName=args.user, SSHPublicKeyId=key_desc["SSHPublicKeyId"], Encoding="SSH")
            if key["SSHPublicKey"]["Status"] == "Active":
                print(key["SSHPublicKey"]["SSHPublicKeyBody"])
    except Exception as e:
        err_exit("Error while retrieving IAM SSH keys for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)

def from_bytes(data, big_endian=False):
    """Used on Python 2 to handle int.from_bytes"""
    if isinstance(data, str):
        data = bytearray(data)
    if big_endian:
        data = reversed(data)
    num = 0
    for offset, byte in enumerate(data):
        num += byte << (offset * 8)
    return num

def aws_to_unix_id(aws_key_id):
    """Converts a AWS Key ID into a UID"""
    if int(sys.version[0]) == 3:
        return 2000 + (
            int.from_bytes(hashlib.sha256(aws_key_id.encode()).digest()[-2:],
            byteorder=sys.byteorder) // 2)
    else:
        return 2000 + int(
            from_bytes(hashlib.sha256(aws_key_id.encode()).digest()[-2:]) // 2)

def get_uid(args):
    iam = boto3.resource("iam")
    try:
        user_id = iam.User(args.user).user_id
        uid = aws_to_unix_id(user_id)
        print(uid)
    except Exception as e:
        err_exit("Error while retrieving UID for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)

def get_groups(args):
    iam_linux_group_prefix = "keymaker_"
    iam = boto3.resource("iam")
    try:
        for group in iam.User(args.user).groups.all():
            if group.name.startswith(iam_linux_group_prefix):
                gid = aws_to_unix_id(group.group_id)
                print(group.name[len(iam_linux_group_prefix):])
    except Exception as e:
        err_exit("Error while retrieving UID for {u}: {e}".format(u=args.user, e=str(e)), code=os.errno.EINVAL)
{'fraction_non_alphanumeric': 0.13794506612410987, 'fraction_numerical': 0.009359104781281791, 'mean_word_length': 3.602996254681648, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1029692', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1646, 'n_words': 364}