Last active
January 16, 2023 00:16
-
-
Save adammatthews/933da0c7f6fc0f53ef2df8e06c7bc8d3 to your computer and use it in GitHub Desktop.
Okta Network Zone - Auto Set your Home IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
## Script to update an Okta Network Zome named "Home Allowed" with your home ISP's public IP address. | |
## Ensure there is only 1 IP range in this Network Zone. | |
## | |
## Created by: Adam Matthews | |
## Date: 11th Jan 2023 | |
# | |
import requests | |
import json | |
policyName = "Home Allowed" # Change to match your policy name | |
orgURL = "https://<<yourorgtenant>>.oktapreview.com" | |
apiToken = "<<yourapitoken>>" | |
url = orgURL+"/api/v1/zones" | |
payload={} | |
headers = { | |
'Authorization': 'SSWS '+apiToken, | |
'Content-Type': 'application/json' | |
} | |
def get(): | |
endpoint = 'https://ipinfo.io/json' | |
response = requests.get(endpoint, verify = True) | |
if response.status_code != 200: | |
return 'Status:', response.status_code, 'Problem with the request. Exiting.' | |
exit() | |
data = response.json() | |
return data['ip'] | |
#get my ip | |
my_ip = get() | |
response = requests.request("GET", url, headers=headers, data=payload) | |
jsonObject = json.loads(response.text) | |
ipRange = (my_ip+"-"+my_ip) # build single range for your hope IP | |
for key in jsonObject: | |
if key['name'] == policyName: | |
if key['gateways'][0]['value'] == ipRange : | |
print("No Change") | |
else: # IP has changes, we need to update Okta. | |
key['gateways'][0]['value'] = ipRange | |
url = orgURL+"/api/v1/zones/"+key['id'] | |
payload = json.dumps(key) | |
response = requests.request("PUT", url, headers=headers, data=payload) | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment