diff --git a/tools/api/fleet/policies/create_n_policies b/tools/api/fleet/policies/create_n_policies index df6843b5fa..ce83eb7a13 100644 --- a/tools/api/fleet/policies/create_n_policies +++ b/tools/api/fleet/policies/create_n_policies @@ -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) \ No newline at end of file + try: + team_id = int(sys.argv[2]) + except(IndexError): + team_id = None + + main(n, team_id) \ No newline at end of file