|
| 1 | +import yaml |
| 2 | +import os |
| 3 | +import strongdm |
| 4 | +from okta.framework.ApiClient import ApiClient |
| 5 | +from okta.framework.Utils import Utils |
| 6 | +from okta.models.user.User import User |
| 7 | +from okta.models.usergroup.UserGroup import UserGroup |
| 8 | + |
| 9 | +def load_matchers(): |
| 10 | + f = open('matchers.yml') |
| 11 | + data = yaml.load(f, Loader = yaml.Loader) |
| 12 | + return data |
| 13 | + |
| 14 | +class OktaUser: |
| 15 | + def __init__(self, login, first_name, last_name, groups): |
| 16 | + self.login = login |
| 17 | + self.first_name = first_name |
| 18 | + self.last_name = last_name |
| 19 | + self.groups = groups |
| 20 | + def __repr__(self): |
| 21 | + return "%s %s"%(self.login,self.groups) |
| 22 | + |
| 23 | +def load_okta_users(): |
| 24 | + ret = [] |
| 25 | + apiClient = ApiClient(pathname='/api/v1/users', base_url=os.getenv('OKTA_CLIENT_ORGURL'), api_token=os.getenv('OKTA_CLIENT_TOKEN')) |
| 26 | + params = { |
| 27 | + 'search': "profile.department eq \"Engineering\" and (status eq \"ACTIVE\")" |
| 28 | + } |
| 29 | + response = ApiClient.get_path(apiClient, '/', params=params) |
| 30 | + users = Utils.deserialize(response.text, User) |
| 31 | + for u in users: |
| 32 | + response = ApiClient.get_path(apiClient, '/{}/groups'.format(u.id)) |
| 33 | + userGroups = Utils.deserialize(response.text, UserGroup) |
| 34 | + groups = [] |
| 35 | + for ug in userGroups: |
| 36 | + groups.append(ug.profile.name) |
| 37 | + oktaUser = OktaUser(u.profile.login, u.profile.firstName, u.profile.lastName, groups) |
| 38 | + ret.append(oktaUser) |
| 39 | + return ret |
| 40 | + |
| 41 | +def main(): |
| 42 | + try: |
| 43 | + okta_sync() |
| 44 | + except Exception as ex: |
| 45 | + print("okta sync failed:"+str(ex)) |
| 46 | + |
| 47 | +def okta_sync(): |
| 48 | + SDM_API_ACCESS_KEY = os.getenv('SDM_API_ACCESS_KEY') |
| 49 | + SDM_API_SECRET_KEY = os.getenv('SDM_API_SECRET_KEY') |
| 50 | + OKTA_CLIENT_TOKEN = os.getenv('OKTA_CLIENT_TOKEN') |
| 51 | + OKTA_CLIENT_ORGURL = os.getenv('OKTA_CLIENT_ORGURL') |
| 52 | + |
| 53 | + if SDM_API_ACCESS_KEY is None or SDM_API_SECRET_KEY is None \ |
| 54 | + or OKTA_CLIENT_TOKEN is None or OKTA_CLIENT_ORGURL is None: |
| 55 | + print("SDM_API_ACCESS_KEY, SDM_API_SECRET_KEY, OKTA_CLIENT_TOKEN, and OKTA_CLIENT_ORGURL must be set") |
| 56 | + return |
| 57 | + |
| 58 | + matchers = load_matchers() |
| 59 | + okta_users = load_okta_users() |
| 60 | + |
| 61 | + client = strongdm.Client(SDM_API_ACCESS_KEY, SDM_API_SECRET_KEY) |
| 62 | + |
| 63 | + accounts = {o.email:o.id for o in client.accounts.list("")} |
| 64 | + permissions = [v for v in client.account_grants.list("")] |
| 65 | + |
| 66 | + # define current state |
| 67 | + current = {} |
| 68 | + for p in permissions: |
| 69 | + if p.account_id not in current: |
| 70 | + current[p.account_id] = set() |
| 71 | + current[p.account_id].add((p.resource_id,p.id)) |
| 72 | + |
| 73 | + # define desired state |
| 74 | + desired = {} |
| 75 | + overlapping = 0 |
| 76 | + for group in matchers["groups"]: |
| 77 | + for resourceQuery in group["resources"]: |
| 78 | + for res in client.resources.list(resourceQuery): |
| 79 | + for u in okta_users: |
| 80 | + if group["name"] in u.groups: |
| 81 | + if u.login not in accounts: |
| 82 | + continue |
| 83 | + overlapping+=1 |
| 84 | + aid = accounts[u.login] |
| 85 | + if aid not in desired: |
| 86 | + desired[aid] = set() |
| 87 | + desired[aid].add(res.id) |
| 88 | + |
| 89 | + # revoke things |
| 90 | + revocations = 0 |
| 91 | + for aid,curRes in current.items(): |
| 92 | + desRes = desired.get(aid,set()) |
| 93 | + for rid in curRes: |
| 94 | + if rid[0] not in desRes: |
| 95 | + revocations+=1 |
| 96 | + client.account_grants.delete(rid[1]) |
| 97 | + |
| 98 | + # grant things |
| 99 | + grants = 0 |
| 100 | + for aid,desRes in desired.items(): |
| 101 | + curRes = current.get(aid,set()) |
| 102 | + for rid in desRes: |
| 103 | + for cr in curRes: |
| 104 | + if rid != cr[0]: |
| 105 | + grants+=1 |
| 106 | + client.account_grants.create(strongdm.AccountGrant(resource_id=rid, account_id=aid)) |
| 107 | + |
| 108 | + print("{} Okta users, {} strongDM users, {} overlapping users, {} grants, {} revocations".format(\ |
| 109 | + len(okta_users),len(accounts), overlapping, grants, revocations)) |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
0 commit comments