Skip to content

Commit

Permalink
Merge branch 'tkt_61_sacar_ws_de_agentes' into 'dev'
Browse files Browse the repository at this point in the history
Sacar ws de agentes

Closes #61

See merge request faradaysec/faraday-cli!64
  • Loading branch information
Nicolas Rebagliati committed Jul 22, 2022
2 parents a3c78cc + 931313d commit 22235e2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG/current/remove_ws_from_agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove workspace from get/list agent and add it to run agent
29 changes: 19 additions & 10 deletions faraday_cli/api_client/faraday_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,27 +282,36 @@ def get_workspace_credentials(self, workspace_name: str):
return response.body

@handle_errors
def get_workspace_agents(self, workspace_name: str):
response = self.faraday_api.agent.list(workspace_name)
def list_agents(self):
response = self.faraday_api.agent.list()
return response.body

@handle_errors
def get_agent(self, workspace_name: str, agent_id: int):
response = self.faraday_api.agent.get(workspace_name, agent_id)
def get_agent(self, agent_id: int):
response = self.faraday_api.agent.get(agent_id)
return response.body

@handle_errors
def run_executor(self, workspace_name: str, agent_id, executor_name, args):
def run_executor(
self,
workspaces_names: list,
agent_id,
executor_name,
args,
ignore_info,
hostname_resolution,
):
body = {
"executorData": {
"executor_data": {
"agent_id": agent_id,
"args": args,
"executor": executor_name,
}
},
"workspaces_names": workspaces_names,
"ignore_info": ignore_info,
"hostname_resolution": hostname_resolution,
}
response = self.faraday_api.agent.run(
workspace_name, agent_id, body=body
)
response = self.faraday_api.agent.run(agent_id, body=body)
return response.body

@handle_errors
Expand Down
6 changes: 3 additions & 3 deletions faraday_cli/api_client/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class CredentialResource(Resource):

class AgentResource(Resource):
actions = {
"list": {"method": "GET", "url": "v3/ws/{}/agents"},
"get": {"method": "GET", "url": "v3/ws/{}/agents/{}"},
"run": {"method": "POST", "url": "v3/ws/{}/agents/{}/run"},
"list": {"method": "GET", "url": "v3/agents"},
"get": {"method": "GET", "url": "v3/agents/{}"},
"run": {"method": "POST", "url": "v3/agents/{}/run"},
}


Expand Down
97 changes: 37 additions & 60 deletions faraday_cli/shell/modules/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ def __init__(self):

# List Agents
list_agent_parser = argparse.ArgumentParser()
list_agent_parser.add_argument(
"-w", "--workspace-name", type=str, help="Workspace name"
)
list_agent_parser.add_argument(
"-j", "--json-output", action="store_true", help="Show output in json"
)
Expand All @@ -40,53 +37,38 @@ def __init__(self):
)
def list_agents(self, args: argparse.Namespace):
"""List agents"""
if not args.workspace_name:
if active_config.workspace:
workspace_name = active_config.workspace
else:
self._cmd.perror("No active Workspace")
return
else:
workspace_name = args.workspace_name
try:
agents = self._cmd.api_client.get_workspace_agents(workspace_name)
except NotFoundError:
self._cmd.perror("Workspace not found")
agents = self._cmd.api_client.list_agents()
if not agents:
self._cmd.perror("No agents in server")
else:
if not agents:
self._cmd.perror(f"No agents in workspace: {workspace_name}")
if args.json_output:
self._cmd.poutput(json.dumps(agents, indent=4))
else:
if args.json_output:
self._cmd.poutput(json.dumps(agents, indent=4))
else:
data = [
OrderedDict(
{
"ID": x["id"],
"NAME": x["name"],
"ACTIVE": x["active"],
"STATUS": x["status"],
"EXECUTORS": ", ".join(
[i["name"] for i in x["executors"]]
),
}
)
for x in agents
]
data = [
OrderedDict(
{
"ID": x["id"],
"NAME": x["name"],
"ACTIVE": x["active"],
"STATUS": x["status"],
"EXECUTORS": ", ".join(
[i["name"] for i in x["executors"]]
),
}
)
for x in agents
]

self._cmd.poutput(
tabulate(
data,
headers="keys",
tablefmt="psql" if args.pretty else "simple",
)
self._cmd.poutput(
tabulate(
data,
headers="keys",
tablefmt="psql" if args.pretty else "simple",
)
)

get_agent_parser = argparse.ArgumentParser()
get_agent_parser.add_argument("agent_id", type=int, help="ID of the Agent")
get_agent_parser.add_argument(
"-w", "--workspace-name", type=str, help="Workspace name "
)
get_agent_parser.add_argument(
"-j", "--json-output", action="store_true", help="Show output in json"
)
Expand All @@ -102,18 +84,8 @@ def list_agents(self, args: argparse.Namespace):
)
def get_agent(self, args: argparse.Namespace):
"""Get agent"""
if not args.workspace_name:
if active_config.workspace:
workspace_name = active_config.workspace
else:
self._cmd.perror("No active Workspace")
return
else:
workspace_name = args.workspace_name
try:
agent = self._cmd.api_client.get_agent(
workspace_name, args.agent_id
)
agent = self._cmd.api_client.get_agent(args.agent_id)
except NotFoundError:
self._cmd.perror("Agent not found")
else:
Expand Down Expand Up @@ -146,6 +118,7 @@ def get_agent(self, args: argparse.Namespace):
].items()
]
),
"LAST_RUN": x["last_run"],
}
)
for x in agent["executors"]
Expand Down Expand Up @@ -184,7 +157,11 @@ def get_agent(self, args: argparse.Namespace):
"--stdin", action="store_true", help="Read executor-params from stdin"
)
run_executor_parser.add_argument(
"-w", "--workspace-name", type=str, help="Workspace name"
"-w",
"--workspace-name",
action="append",
type=str,
help="Workspace name",
)

@cmd2.as_subcommand_to(
Expand All @@ -204,7 +181,7 @@ def run_executor(self, args):
executor_params = args.executor_params
if not args.workspace_name:
if active_config.workspace:
workspace_name = active_config.workspace
workspace_name = [active_config.workspace]
else:
self._cmd.perror("No active Workspace")
return
Expand All @@ -216,9 +193,7 @@ def run_executor(self, args):
self._cmd.perror(e)
else:
try:
agent = self._cmd.api_client.get_agent(
workspace_name, args.agent_id
)
agent = self._cmd.api_client.get_agent(args.agent_id)
except NotFoundError:
self._cmd.perror("Agent not found")
else:
Expand Down Expand Up @@ -301,13 +276,15 @@ def run_executor(self, args):
args.agent_id,
executor["name"],
json.loads(executor_params),
self._cmd.ignore_info_severity,
self._cmd.hostname_resolution,
)
except Exception as e:
self._cmd.perror(str(e))
else:
self._cmd.poutput(
cmd2.style(
f"Generated Command: {response['command_id']}", # noqa: E501
f"Generated Command: {response['commands_id']}", # noqa: E501
fg=COLORS.GREEN,
)
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ arrow>=1.1.0
py-sneakers>=1.0.1
luddite>=1.0.2
packaging>=21.3

0 comments on commit 22235e2

Please sign in to comment.