Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add legal hold membership to device reporting #192

Merged
merged 23 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b285a68
Legal Hold work to meet Issue 176
maddie-vargo Dec 28, 2020
ed72879
Fix to Changelog
maddie-vargo Dec 28, 2020
ea4381a
Minor fix to CHANGELOG
maddie-vargo Dec 29, 2020
a0ebe57
Added to legal hold user guide
maddie-vargo Dec 29, 2020
077dea1
Adjusting build parameters to bypass 3.5 for this PR
maddie-vargo Dec 29, 2020
03a3814
Merge branch 'master' into iss176
maddie-vargo Dec 29, 2020
f174992
Fix low hanging fruit for initial PR review
maddie-vargo Jan 4, 2021
205d559
Move development from legal-hold command to devices command, add new …
maddie-vargo Jan 29, 2021
02d6530
remove whitespaces that are coming through as edits
maddie-vargo Feb 2, 2021
e5784cf
fix changes identfied by tox style run
maddie-vargo Feb 2, 2021
9bfcd42
remove duplication in setup.py - file should have no edits
maddie-vargo Feb 2, 2021
2e303de
remove duplication in setup.py - file should have no edits
maddie-vargo Feb 2, 2021
94fe3b9
refactor membership function to use generator and remove NaNs from ou…
maddie-vargo Feb 11, 2021
0b79911
fix tox style run issue
maddie-vargo Feb 11, 2021
1bd1e2f
Fix tox style run x2
maddie-vargo Feb 11, 2021
e4725c7
flipping back to using NaN, awaiting PR #245
maddie-vargo Feb 16, 2021
237ea31
Adding --include-total-storage option, which calculates total number …
maddie-vargo Feb 18, 2021
7212fbf
Remove V2 archives from storage calcuation; rename columns
maddie-vargo Feb 22, 2021
2a917bd
fix small change to the incldue/excluded archive types
maddie-vargo Feb 23, 2021
2d1db8c
reword
maddie-vargo Feb 25, 2021
9a0afcb
conflict reconciliation in changelog, part I
maddie-vargo Feb 26, 2021
a3dd28f
conflict reconciliation in changelog, part II (repulled from upstream…
maddie-vargo Feb 26, 2021
9677f23
fix style run
maddie-vargo Feb 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.

## 1.3.1 - 2021-02-25

### Changed

- Command options for `profile update`:
- `-n` `--name` is not required, and if omitted will use the default profile.
- `-s` `--server` and `-u` `--username` are not required and can be updated independently now.
- Example: `code42 profile update -s 1.2.3.4:1234`

## 1.3.0 - 2021-02-11

### Fixed

- Issue where `code42 alert-rules bulk add` would show as successful when adding users to a non-existent alert rule.

### Added

- New choice `TLS-TCP` for `--protocol` option used by `send-to` commands:
- `code42 security-data send-to`
- `code42 alerts send-to`
- `code42 audit-logs send-to`
for more securely transporting data. Included are new flags:
- `--certs`
- `--ignore-cert-validation`

### Changed

- The error text in cases command when:
- `cases create` sets a name that already exists in the system.
- `cases create` sets a description that has more than 250 characters.
- `cases update` sets a description that has more than 250 characters.
- `cases file-events add` is performed on an already closed case.
- `cases file-events add` sets an event id that is already added to the case.
- `cases file-events remove` is performed on an already closed case.

## 1.2.0 - 2021-01-25

### Added
Expand Down Expand Up @@ -71,6 +106,12 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
- Now, when adding a cloud alias to a detection list user, such as during `departing-employee add`, it will remove the existing cloud alias if one exists.
- Before, it would error and the cloud alias would not get added.

### Added

- `code42 devices list` option:
- `--include-legal-hold-membership` prints the legal hold matter name and ID for any active device on legal hold
- `--include-total-storage` prints the backup archive count and total storage

## 1.0.0 - 2020-08-31

### Fixed
Expand Down
87 changes: 86 additions & 1 deletion src/code42cli/cmds/devices.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import date

import click
import numpy as np
from pandas import concat
from pandas import DataFrame
from pandas import json_normalize
from pandas import Series
from pandas import to_datetime
from py42 import exceptions
from py42.exceptions import Py42NotFoundError
Expand Down Expand Up @@ -244,6 +247,22 @@ def _get_device_info(sdk, device_guid):
is_flag=True,
help="Include device settings in output.",
)
@click.option(
"--include-legal-hold-membership",
required=False,
type=bool,
default=False,
is_flag=True,
help="Include legal hold membership in output.",
)
@click.option(
"--include-total-storage",
required=False,
type=bool,
default=False,
is_flag=True,
help="Include backup archive count and total storage in output.",
)
@click.option(
"--exclude-most-recently-connected",
type=int,
Expand Down Expand Up @@ -285,6 +304,8 @@ def list_devices(
include_backup_usage,
include_usernames,
include_settings,
include_legal_hold_membership,
include_total_storage,
exclude_most_recently_connected,
last_connected_after,
last_connected_before,
Expand All @@ -309,7 +330,11 @@ def list_devices(
"userUid",
]
df = _get_device_dataframe(
state.sdk, columns, active, org_uid, include_backup_usage
state.sdk,
columns,
active,
org_uid,
(include_backup_usage or include_total_storage),
)
if last_connected_after:
df = df.loc[to_datetime(df.lastConnected) > last_connected_after]
Expand All @@ -326,17 +351,57 @@ def list_devices(
.head(exclude_most_recently_connected)
)
df = df.drop(most_recent.index)
if include_total_storage:
df = _add_storage_totals_to_dataframe(df, include_backup_usage)
if include_settings:
df = _add_settings_to_dataframe(state.sdk, df)
if include_usernames:
df = _add_usernames_to_device_dataframe(state.sdk, df)
if include_legal_hold_membership:
df = _add_legal_hold_membership_to_device_dataframe(state.sdk, df)
if df.empty:
click.echo("No results found.")
else:
formatter = DataFrameOutputFormatter(format)
formatter.echo_formatted_dataframe(df)


def _add_legal_hold_membership_to_device_dataframe(sdk, df):
columns = ["legalHold.legalHoldUid", "legalHold.name", "user.userUid"]

legal_hold_member_dataframe = (
json_normalize(list(_get_all_active_hold_memberships(sdk)))[columns]
.groupby(["user.userUid"])
.agg(",".join)
.rename(
{
"legalHold.legalHoldUid": "legalHoldUid",
"legalHold.name": "legalHoldName",
},
axis=1,
)
)
df = df.merge(
legal_hold_member_dataframe,
how="left",
left_on="userUid",
right_on="user.userUid",
)

df.loc[df["status"] == "Deactivated", ["legalHoldUid", "legalHoldName"]] = np.nan

return df


def _get_all_active_hold_memberships(sdk):
for page in sdk.legalhold.get_all_matters(active=True):
for matter in page["legalHolds"]:
for _page in sdk.legalhold.get_all_matter_custodians(
legal_hold_uid=matter["legalHoldUid"], active=True
):
yield from _page["legalHoldMemberships"]


def _get_device_dataframe(
sdk, columns, active=None, org_uid=None, include_backup_usage=False
):
Expand Down Expand Up @@ -392,6 +457,26 @@ def _add_usernames_to_device_dataframe(sdk, device_dataframe):
return device_dataframe.merge(users_dataframe, how="left", on="userUid")


def _add_storage_totals_to_dataframe(df, include_backup_usage):
df[["archiveCount", "totalStorageBytes"]] = df["backupUsage"].apply(
_break_backup_usage_into_total_storage
)

if not include_backup_usage:
df = df.drop("backupUsage", axis=1)
return df


def _break_backup_usage_into_total_storage(backup_usage):
total_storage = 0
archive_count = 0
for archive in backup_usage:
maddie-vargo marked this conversation as resolved.
Show resolved Hide resolved
if archive["archiveFormat"] != "ARCHIVE_V2":
archive_count += 1
total_storage += archive["archiveBytes"]
return Series([archive_count, total_storage])


@devices.command()
@active_option
@inactive_option
Expand Down
Loading