#!/usr/bin/env python
#
# Copyright (C) 2018 Layered Insight - All Rights Reserved
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential

from __future__ import print_function
import argparse
import sys
from pprint import pprint
import layint_runtime_api
from layint_runtime_api.rest import ApiException
from vyper import v
from li_utils.envtools import env_setup

# Parse CLI
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Verbose logging", action="store_true")
parser.add_argument("--policy-name", help="Name of policy to delete rule from", required=True)
parser.add_argument("--name", help="Name label for rules to delete", required=True)
# TBD add support for rule matching...
#parser.add_argument("--type", help="rule type", choices=["file", "write", "execution", "listener", "network", "syscall", "syscallgroup", "java"], default="file")
#parser.add_argument("--program", help="Program to match for rule, absolute path highly recommended.", default="*")
#parser.add_argument("--action", help="Action for the rule", choices=["allow", "deny"], default="allow")
#parser.add_argument("--syscall", help="Syscall to match for the rule", default="")
#parser.add_argument("--arg1", help="Argument1 to match for the rule", default="")
#parser.add_argument("--arg2", help="Argument2 to match for the rule", default="")
#parser.add_argument("--arg3", help="Argument3 to match for the rule", default="")
args = parser.parse_args()

env_setup()

layint_runtime_api.configuration.api_key['Authorization'] = v.get("api_key")
policy_api = layint_runtime_api.PolicyApi()
policy_api.api_client.host = v.get("api_host")

try:
    policy = policy_api.get_policy_by_name(args.policy_name)
except ApiException as e:
    print("Unable to load policy named %s:%s\n" % (args.policyname, e))
    sys.exit()


if policy.user_id is None:
    print("Policy user_id is empty %s\n" % (policy.user_id))
    sys.exit()

if policy.group_id is None:
    print("Policy group_id is empty %s\n" % (policy.group_id))
    sys.exit()

#print("Policy user_id is %s\n" % (policy.user_id))
#print("Policy group_id is %s\n" % (policy.group_id))

# TBD prototype rule matching
#rule = layint_runtime_api.PolicyRule()
#rule.name = args.name
# TODO we need to export a form of GetXSyscallNumbers (where X if rule type) from common/syscall.go
#  and set rule_type automatically to avoid bugs or unexpected behavior
#rule.rule_type = args.type
#rule.syscall = args.syscall
#rule.program = args.program
#rule.action = args.action
#rule.arg1 = args.arg1
#rule.arg2 = args.arg2
#rule.arg3 = args.arg3


orig_rules = policy.rules
policy.rules = []
for orule in orig_rules:
    if not orule.name == args.name:
        policy.rules.append(orule)

# clobber all rules if empty?
#if not args.name:
#    policy.rules = None

# update new Policy definition
if args.verbose:
    print("Creating Policy %s on API host %s..." % (args.policy_name, v.get("api_host")))

try:
    api_response = policy_api.update_policy(policy.id, policy=policy)
    if args.verbose:
        pprint(api_response)
except ApiException as e:
    print("Exception when calling PolicyApi->update_policy: %s\n" % e)
    sys.exit()

if args.verbose:
    print("Policy updated, ID " + str(policy.id))
