mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Update small utility (#31026)
Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
This commit is contained in:
parent
b78826ac18
commit
b4b51dbd8a
1 changed files with 13 additions and 4 deletions
|
|
@ -3,6 +3,9 @@
|
|||
'''
|
||||
Create n new policies, where n is the integer first argument passed to the script.
|
||||
|
||||
Optional second positional argument defines which team to create policies on, global/all teams
|
||||
policies if omitted.
|
||||
|
||||
Assumes the script can see the `TOKEN` (API token) and `SERVER_URL` environment variables.
|
||||
'''
|
||||
|
||||
|
|
@ -10,7 +13,7 @@ import subprocess, os, sys, string, random
|
|||
|
||||
POLICIES_ENDPOINT="/api/latest/fleet/policies"
|
||||
|
||||
def main(n: int):
|
||||
def main(n: int, team_id: int):
|
||||
token, server_url = os.environ.get("TOKEN"), os.environ.get("SERVER_URL")
|
||||
if not token or not server_url:
|
||||
raise Exception("Make sure you have set TOKEN and SERVER_URL as environment variables.")
|
||||
|
|
@ -18,8 +21,9 @@ def main(n: int):
|
|||
name='Policy ' + ''.join(random.choices(string.ascii_lowercase, k=5))
|
||||
critical=random.choice(["true", "false"])
|
||||
data = f'{{"name": "{name}", "query": "SELECT 1 FROM osquery_info;", "description": "Selects 1", "resolution": "Resolution steps", "platform": "{random.choice(["windows", "linux", "darwin"])}", "critical": {critical}}}'
|
||||
path_suffix = POLICIES_ENDPOINT if team_id == None else f"/api/latest/fleet/teams/{team_id}/policies"
|
||||
process = subprocess.Popen(
|
||||
["curl", "-X", "POST", "-k", "-s", "-H", f"Authorization: Bearer {token}", f"{server_url}{POLICIES_ENDPOINT}", "-d", f"{data}", "--insecure"], stdout=subprocess.PIPE
|
||||
["curl", "-X", "POST", "-k", "-s", "-H", f"Authorization: Bearer {token}", f"{server_url}{path_suffix}", "-d", f"{data}", "--insecure"], stdout=subprocess.PIPE
|
||||
)
|
||||
(out, err) = process.communicate()
|
||||
print(out, "\n")
|
||||
|
|
@ -29,6 +33,11 @@ def main(n: int):
|
|||
if __name__ == "__main__":
|
||||
try:
|
||||
n = int(sys.argv[1])
|
||||
except:
|
||||
except(IndexError):
|
||||
raise Exception("Enter the number of policies to create as a single integer argument.")
|
||||
main(n)
|
||||
try:
|
||||
team_id = int(sys.argv[2])
|
||||
except(IndexError):
|
||||
team_id = None
|
||||
|
||||
main(n, team_id)
|
||||
Loading…
Reference in a new issue