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
//! Explicit, rule-driven credential revocation for embedding applications.
use std::{collections::BTreeMap, time::Duration};
use anyhow::{Context, Result, ensure};
use kingfisher_rules::{Revocation, Rule};
use liquid::{Object, model::Value};
use reqwest::Client;
use super::revocation::{
DirectRevocationResult, execute_http_revocation, execute_multi_step_revocation,
};
/// Reusable, explicitly invoked revocation runner (requires `validation`).
///
/// Scanning and validation never call this API. The caller selects the credential
/// and supplies companion variables and endpoint overrides required by the rule.
/// HTTP rule requests are not retried. AWS retains its provider-specific retry
/// policy. Requires a Tokio runtime with I/O and time enabled.
/// Provider responses in results and errors may contain sensitive information.
pub struct Revoker {
client: Client,
parser: liquid::Parser,
timeout: Duration,
}
impl Revoker {
/// Classify a failure without exposing URLs, credentials, or provider bodies.
/// Categories describe transport/response failures; an unknown category can
/// also indicate rule configuration or a provider-specific helper failure.
pub fn error_category(error: &anyhow::Error) -> &'static str {
if error.downcast_ref::<tokio::time::error::Elapsed>().is_some() {
return "timeout";
}
if let Some(error) = error.downcast_ref::<reqwest::Error>() {
if error.is_timeout() {
"timeout"
} else if error.is_connect() {
"connection"
} else if error.is_status() {
"http_status"
} else if error.is_body() || error.is_decode() {
"response"
} else {
"request"
}
} else if error.downcast_ref::<std::io::Error>().is_some() {
"io"
} else {
"configuration_or_provider"
}
}
/// Create a runner with strict TLS, redirects disabled, and a 10-second deadline.
///
/// # Errors
///
/// Returns an error if the HTTP client or Liquid template parser cannot be initialized.
pub fn new() -> Result<Self> {
Self::with_client(Client::builder().redirect(reqwest::redirect::Policy::none()).build()?)
}
/// Use an application-owned HTTP client. Configure TLS, proxies, and redirects
/// for the intended endpoints. AWS and GCP helpers use their own transports;
/// the total deadline still applies. HTTP rules may reach internal addresses.
///
/// # Errors
///
/// Returns an error if the Liquid template parser cannot be initialized.
pub fn with_client(client: Client) -> Result<Self> {
Ok(Self {
client,
parser: kingfisher_rules::register_liquid_filters(liquid::ParserBuilder::with_stdlib())
.build()?,
timeout: Duration::from_secs(10),
})
}
/// Set a nonzero total deadline for each call, including multi-step requests.
/// A timeout cannot undo an already submitted revocation request; its outcome
/// may be unknown. Do not automatically retry a timed-out operation.
///
/// # Errors
///
/// Returns an error if the duration is zero.
pub fn timeout(mut self, timeout: Duration) -> Result<Self> {
ensure!(!timeout.is_zero(), "Revocation timeout must be nonzero");
self.timeout = timeout;
Ok(self)
}
/// Revoke a known secret using a built-in or custom rule, without scanning.
///
/// Variable names are case-sensitive (normally uppercase). `TOKEN` is reserved
/// and always comes from `secret`. No environment variables or CLI defaults are
/// read. Missing configuration and execution failures return errors;
/// a completed provider response is evaluated by the rule's success matcher.
///
/// # Errors
///
/// Returns an error for an empty secret, reserved `TOKEN` variable, missing or
/// unsupported revocation configuration, template/transport failures, or timeout.
/// A completed response that fails the success matcher is returned with
/// `revoked: false`, rather than treated as a transport error.
pub async fn revoke(
&self,
rule: &Rule,
secret: &str,
variables: &BTreeMap<String, String>,
) -> Result<DirectRevocationResult> {
ensure!(!secret.is_empty(), "Secret cannot be empty");
ensure!(!variables.contains_key("TOKEN"), "TOKEN is reserved; pass it as the secret");
let revocation = rule.syntax().revocation.as_ref().context("Rule has no revocation")?;
let mut globals = Object::new();
for (name, value) in variables {
globals.insert(name.clone().into(), Value::scalar(value.clone()));
}
globals.insert("TOKEN".into(), Value::scalar(secret.to_owned()));
let operation = async {
let mut result = match revocation {
Revocation::Http(http) => {
ensure!(
http.request.response_matcher.as_ref().is_some_and(|m| !m.is_empty()),
"Revocation requires a response matcher"
);
execute_http_revocation(
http,
&globals,
&self.client,
&self.parser,
self.timeout,
0,
)
.await?
}
Revocation::HttpMultiStep(multi) => {
ensure!(
(1..=2).contains(&multi.steps.len()),
"Revocation requires one or two steps"
);
ensure!(
multi
.steps
.last()
.and_then(|step| step.request.response_matcher.as_ref())
.is_some_and(|m| !m.is_empty()),
"Final revocation step requires a response matcher"
);
execute_multi_step_revocation(
multi,
&mut globals,
&self.client,
&self.parser,
self.timeout,
0,
)
.await?
}
Revocation::AWS => {
let akid = variables
.get("AKID")
.or_else(|| variables.get("ACCESS_KEY_ID"))
.context("AWS revocation requires AKID or ACCESS_KEY_ID")?;
super::aws::validate_aws_credentials_input(akid, secret)
.map_err(anyhow::Error::msg)?;
let (revoked, message) =
super::aws::revoke_aws_access_key(akid, secret).await?;
DirectRevocationResult {
rule_id: String::new(),
rule_name: String::new(),
revoked,
status_code: None,
message,
}
}
Revocation::GCP => {
let key_id =
variables.get("KEY_ID").or_else(|| variables.get("PRIVATE_KEY_ID"));
let outcome = super::gcp::revoke_gcp_service_account_key(
secret,
key_id.map(String::as_str),
)
.await?;
DirectRevocationResult {
rule_id: String::new(),
rule_name: String::new(),
revoked: outcome.revoked,
status_code: outcome.status_code,
message: outcome.message,
}
}
};
result.rule_id = rule.id().to_owned();
result.rule_name = rule.name().to_owned();
Ok(result)
};
tokio::time::timeout(self.timeout, operation)
.await
.context("Revocation timed out; the credential may already have been revoked")?
}
}