Skip to content

Commit

Permalink
Start util module
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Jul 15, 2019
1 parent 9a35c8f commit 18becfc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
29 changes: 1 addition & 28 deletions keymaker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,17 @@
from io import open

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

try:
from shutil import which
except ImportError:
def which(name):
if os.path.exists("/usr/local/bin/" + name):
return "/usr/local/bin/" + name
elif os.path.exists("/usr/bin/" + name):
return "/usr/bin/" + name

import boto3
from botocore.exceptions import ClientError

from .iam.policies import trust_policy_template, keymaker_instance_role_policy, keymaker_instance_assume_role_statement
from .util import which, ARN, from_bytes

USING_PYTHON2 = True if sys.version_info < (3, 0) else False

logger = logging.getLogger(__name__)

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


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

default_iam_linux_group_prefix = "keymaker_"
default_iam_linux_user_suffix = ""

Expand Down Expand Up @@ -156,17 +140,6 @@ def get_authorized_keys(args):
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"""
uid_bytes = hashlib.sha256(aws_key_id.encode()).digest()[-2:]
Expand Down
29 changes: 29 additions & 0 deletions keymaker/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
from collections import namedtuple

try:
from shutil import which
except ImportError:
def which(name):
if os.path.exists("/usr/local/bin/" + name):
return "/usr/local/bin/" + name
elif os.path.exists("/usr/bin/" + name):
return "/usr/bin/" + name

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


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

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
2 changes: 1 addition & 1 deletion keymaker/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.0"
__version__ = '1.0.9'

0 comments on commit 18becfc

Please # to comment.