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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//! Provides types for lambdas used for Secret Manager rotation.
//!
//! # Usage
//!
//! ```no_run
//! #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
//! struct Secret {
//! user: String,
//! password: String,
//! }
//!
//! struct Runner;
//!
//! #[async_trait::async_trait]
//! impl<'a> lambda_runtime_types::rotate::RotateRunner<'a, (), Secret> for Runner {
//! async fn setup(region: &'a str) -> anyhow::Result<()> {
//! // Setup logging to make sure that errors are printed
//! Ok(())
//! }
//!
//! async fn create(
//! shared: &'a (),
//! secret_cur: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! smc: &lambda_runtime_types::rotate::Smc,
//! ) -> anyhow::Result<lambda_runtime_types::rotate::SecretContainer<Secret>> {
//! // Create a new secret without setting it yet.
//! // Only called if there is no pending secret available
//! // (which may happen if rotation fails at any stage)
//! unimplemented!()
//! }
//!
//! async fn set(
//! shared: &'a (),
//! secret_cur: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! secret_new: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! ) -> anyhow::Result<()> {
//! // Set the secret in the service
//! // Only called if password is not already set, checked by
//! // calling [`test`] with new password beforehand. The reason
//! // for that it, that a failure in a later stage means all
//! // stages are called again with set failing as the old password
//! // does not work anymore
//! Ok(())
//! }
//!
//! async fn test(
//! shared: &'a (),
//! secret_new: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! ) -> anyhow::Result<()> {
//! // Test whether a connection with the given secret works
//! Ok(())
//! }
//!
//! async fn finish(
//! shared: &'a (),
//! secret_cur: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! secret_new: lambda_runtime_types::rotate::SecretContainer<Secret>,
//! ) -> anyhow::Result<()> {
//! // Optional: Perform any work which may be necessary to
//! // complete rotation
//! Ok(())
//! }
//!
//! }
//!
//! pub fn main() -> anyhow::Result<()> {
//! lambda_runtime_types::exec_tokio::<_, _, Runner, _>()
//! }
//! ```
//!
//! For further usage like `Shared` Data, refer to the main [documentation](`crate`)
#[cfg(feature = "rotate_aws_sdk")]
mod aws_sdk;
#[cfg(feature = "rotate_rusoto")]
mod rusoto;
mod smc;
pub use smc::{SecretContainer, Smc};
/// `Event` which is send by the `SecretManager` to the rotation lambda
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "rotate_rusoto", feature = "rotate_aws_sdk")))
)]
#[derive(Clone, serde::Deserialize)]
pub struct Event<Secret> {
/// Request Token used for `SecretManager` Operations
#[serde(rename = "ClientRequestToken")]
pub client_request_token: String,
/// Id of the secret to rotate
#[serde(rename = "SecretId")]
pub secret_id: String,
/// Current step of the rotation
#[serde(rename = "Step")]
pub step: Step,
#[doc(hidden)]
#[serde(skip)]
pub _m: std::marker::PhantomData<Secret>,
}
impl<Secret> std::fmt::Debug for Event<Secret> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Event")
.field("client_request_token", &self.client_request_token)
.field("secret_id", &self.secret_id)
.field("step", &self.step)
.finish()
}
}
/// Available steps for in a Secret Manager rotation
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "rotate_rusoto", feature = "rotate_aws_sdk")))
)]
#[derive(Debug, Copy, Clone, serde::Deserialize)]
pub enum Step {
/// Secret creation
#[serde(rename = "createSecret")]
Create,
/// Secret configuration in service
#[serde(rename = "setSecret")]
Set,
/// Secret testing in service
#[serde(rename = "testSecret")]
Test,
/// Secret rotation finalization
#[serde(rename = "finishSecret")]
Finish,
}
/// Defines a type which is executed every time a lambda
/// is invoced. This type is made for `SecretManager`
/// rotation lambdas.
///
/// Types:
/// * `Shared`: Type which is shared between lambda
/// invocations. Note that lambda will
/// create multiple environments for
/// simulations invokations and environments
/// are only kept alive for a certain time.
/// It is thus not guaranteed that data
/// can be reused, but with this types
/// its possible.
/// * `Secret`: The structure of the secret stored in
/// the `SecretManager`. May contain only
/// necessary fields, as other undefined
/// fields are internally preserved.
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "rotate_rusoto", feature = "rotate_aws_sdk")))
)]
#[async_trait::async_trait]
pub trait RotateRunner<'a, Shared, Secret>
where
Shared: Send + Sync + 'a,
Secret: 'static + Send,
{
/// See documentation of [`super::Runner::setup`]
async fn setup(region: &'a str) -> anyhow::Result<Shared>;
/// Create a new secret without setting it yet.
/// Only called if there is no pending secret available
/// (which may happen if rotation fails at any stage)
async fn create(
shared: &'a Shared,
secret_cur: SecretContainer<Secret>,
smc: &Smc,
) -> anyhow::Result<SecretContainer<Secret>>;
/// Set the secret in the service
/// Only called if password is not already set, checked by
/// calling [`test`] with new password beforehand. The reason
/// for that it, that a failure in a later stage means all
/// stages are called again with set failing as the old password
/// does not work anymore
async fn set(
shared: &'a Shared,
secret_cur: SecretContainer<Secret>,
secret_new: SecretContainer<Secret>,
) -> anyhow::Result<()>;
/// Test whether a connection with the given secret works
async fn test(shared: &'a Shared, secret_new: SecretContainer<Secret>) -> anyhow::Result<()>;
/// Perform any work which may be necessary to complete rotation
async fn finish(
_shared: &'a Shared,
_secret_cur: SecretContainer<Secret>,
_secret_new: SecretContainer<Secret>,
) -> anyhow::Result<()> {
Ok(())
}
}
#[async_trait::async_trait]
impl<'a, Type, Shared, Sec> super::Runner<'a, Shared, Event<Sec>, ()> for Type
where
Shared: Send + Sync + 'a,
Sec: 'static + Send + Sync + Clone + serde::de::DeserializeOwned + serde::Serialize,
Type: 'static + RotateRunner<'a, Shared, Sec>,
{
async fn setup(region: &'a str) -> anyhow::Result<Shared> {
Self::setup(region).await
}
async fn run(
shared: &'a Shared,
event: crate::LambdaEvent<'a, Event<Sec>>,
) -> anyhow::Result<()> {
let smc = Smc::new(event.region).await?;
log::info!("{:?}", event.event.step);
match event.event.step {
Step::Create => {
let secret_cur = smc
.get_secret_value_current::<Sec>(&event.event.secret_id)
.await?;
let secret_new = smc
.get_secret_value_pending::<Sec>(&event.event.secret_id)
.await;
if let Ok(secret_new) = secret_new {
if secret_new.version_id != secret_cur.version_id {
log::info!("Found existing pending value.");
return Ok(());
}
}
log::info!("Creating new secret value.");
let secret = Self::create(shared, secret_cur.inner, &smc).await?;
smc.put_secret_value_pending(
&event.event.secret_id,
Some(&event.event.client_request_token),
&secret,
)
.await?;
Ok(())
}
Step::Set => {
log::info!("Setting secret on remote system.");
let secret_new = smc
.get_secret_value_pending(&event.event.secret_id)
.await?
.inner;
if Self::test(shared, SecretContainer::clone(&secret_new))
.await
.is_err()
{
let secret_cur = smc
.get_secret_value_current(&event.event.secret_id)
.await?
.inner;
Self::set(shared, secret_cur, secret_new).await?;
} else {
log::info!("Password already set in remote system.");
}
Ok(())
}
Step::Test => {
log::info!("Testing secret on remote system.");
let secret = smc
.get_secret_value_pending(&event.event.secret_id)
.await?
.inner;
Self::test(shared, secret).await?;
Ok(())
}
Step::Finish => {
log::info!("Finishing secret deployment.");
let secret_current: smc::Secret<Sec> =
smc.get_secret_value_current(&event.event.secret_id).await?;
let secret_pending: smc::Secret<Sec> =
smc.get_secret_value_pending(&event.event.secret_id).await?;
Self::finish(shared, secret_current.inner, secret_pending.inner).await?;
smc.set_pending_secret_value_to_current(
secret_current.arn,
secret_current.version_id,
secret_pending.version_id,
)
.await?;
Ok(())
}
}
}
}