-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaudit_logs.rs
More file actions
227 lines (214 loc) · 6.83 KB
/
audit_logs.rs
File metadata and controls
227 lines (214 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::cli::{FORMAT_OPT, LIST_SUBCMD};
use crate::database::{AuditLogs, Environments, OpenApiConfig, Parameters, Projects, Users};
use crate::table::Table;
use crate::utils::{error_message, help_message, parse_datetime, warn_missing_subcommand};
use clap::ArgMatches;
use color_eyre::eyre::Result;
use indoc::printdoc;
use std::process;
const OBJECT_TYPE_VALUES: &[&str] = &[
"AwsIntegration",
"Environment",
"GitHubIntegration",
"Invitation",
"Membership",
"Organization",
"Parameter",
"ParameterRule",
"ParameterType",
"ParameterTypeRule",
"Project",
"Pull",
"Push",
"ServiceAccount",
"Tag",
"Task",
"Template",
"Value",
];
/// Print a consistent `error_message()`
fn invalid_time_format(arg: &str) {
error_message(format!("Invalid '{arg}' value"));
}
fn resolve_object_type(input: &str) -> String {
let lowerin = input.to_lowercase();
for v in OBJECT_TYPE_VALUES {
if v.to_lowercase() == lowerin {
return v.to_string();
}
}
if lowerin == "service-account" {
return "ServiceAccount".to_string();
}
input.to_string()
}
fn proc_audit_list(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
audit_logs: &AuditLogs,
) -> Result<()> {
let action = subcmd_args.value_of("action");
let object_type = subcmd_args.value_of("object-type").map(resolve_object_type);
let before = parse_datetime(subcmd_args.value_of("before"));
let after = parse_datetime(subcmd_args.value_of("after"));
let name = subcmd_args.value_of("contains");
let username = subcmd_args.value_of("username");
let max_entries = subcmd_args
.value_of("max-entries")
.unwrap()
.parse::<usize>()
.unwrap();
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
let bad_before = subcmd_args.occurrences_of("before") > 0 && before.is_none();
let bad_after = subcmd_args.occurrences_of("after") > 0 && after.is_none();
if bad_before || bad_after {
if bad_before {
invalid_time_format("--before");
}
if bad_after {
invalid_time_format("--after");
}
process::exit(34);
}
let mut user_id = None;
if let Some(uname) = username {
let users = Users::new();
user_id = users.get_id(rest_cfg, uname)?;
if user_id.is_none() {
error_message(format!("User '{uname}' not found."));
process::exit(35);
}
}
let mut env_id = None;
if let Some(env_name) = subcmd_args.value_of("environment") {
let environments = Environments::new();
env_id = environments.get_id(rest_cfg, env_name)?;
if env_id.is_none() {
error_message(format!("Environment '{env_name}' not found"));
process::exit(36);
}
}
let mut proj_id = None;
if let Some(proj_name) = subcmd_args.value_of("project") {
let projects = Projects::new();
proj_id = projects.get_id(rest_cfg, proj_name)?;
if proj_id.is_none() {
error_message(format!("Project '{proj_name}' not found"));
process::exit(37);
}
}
let mut param_id = None;
if let Some(param_name) = subcmd_args.value_of("parameter") {
if let Some(ref project_id) = proj_id {
let default_env_name = "default".to_string(); // kinda hacky to use a name instead of value here... may have been renamed
let environment_id = env_id.clone().unwrap_or(default_env_name);
let parameters = Parameters::new();
if let Some(param_details) = parameters.get_details_by_name(
rest_cfg,
project_id,
&environment_id,
param_name,
false,
false,
true,
None,
None,
)? {
param_id = Some(param_details.id);
} else {
error_message(format!("Parameter '{param_name}' not found"));
process::exit(38)
}
} else {
error_message("Must specify a project when specifying a parameter".to_string());
process::exit(39);
}
}
let details = audit_logs.get_audit_log_details(
rest_cfg,
object_type.as_deref(),
action,
name,
max_entries,
before,
after,
user_id.as_deref(),
env_id.as_deref(),
proj_id.as_deref(),
param_id.as_deref(),
)?;
if details.is_empty() {
let mut constraints: Vec<String> = vec![];
if let Some(o) = object_type {
constraints.push(format!("type=={o}"));
if !OBJECT_TYPE_VALUES.contains(&o.as_str()) {
help_message(format!(
"The specified --type is not one of the recognized values: {}",
OBJECT_TYPE_VALUES.join(", ")
));
}
}
if let Some(n) = name {
constraints.push(format!("name-contains '{n}'"));
}
if let Some(a) = action {
constraints.push(format!("action=={a}"));
}
if constraints.is_empty() {
println!("No audit log entries found");
} else {
println!(
"No audit log entries found matching {}",
constraints.join(", ")
);
}
} else {
let hdr = vec!["Time", "Object Name", "Type", "Action", "User"];
let mut table = Table::new("audit-logs");
table.set_header(&hdr);
for entry in details {
let row = vec![
entry.timestamp,
entry.object_name,
entry.object_type.to_string(),
entry.action,
entry.user,
];
table.add_row(row);
}
table.render(fmt)?;
}
Ok(())
}
fn proc_audit_summary(
_subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
audit_logs: &AuditLogs,
) -> Result<()> {
let summary = audit_logs.get_audit_log_summary(rest_cfg)?;
printdoc!(
r#"
Record count: {}
Earliest record: {}
Policy:
Maximum records: {}
Maximum days: {}
"#,
summary.total_records,
summary.earliest,
summary.max_records,
summary.max_days,
);
Ok(())
}
pub fn process_audit_log_command(subcmd_args: &ArgMatches, rest_cfg: &OpenApiConfig) -> Result<()> {
let audit_logs = AuditLogs::new();
if let Some(subcmd_args) = subcmd_args.subcommand_matches(LIST_SUBCMD) {
proc_audit_list(subcmd_args, rest_cfg, &audit_logs)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches("summary") {
proc_audit_summary(subcmd_args, rest_cfg, &audit_logs)?;
} else {
warn_missing_subcommand("audit-logs");
}
Ok(())
}