Skip to content

Commit

Permalink
Dzejkop/tag_keys_and_dev_deployment_workflow (#36)
Browse files Browse the repository at this point in the history
* Auto update dev deployment

* Add extra metadata/tags to KMS keys
  • Loading branch information
Dzejkop authored May 15, 2024
1 parent 988dc5a commit 29de92e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/update-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Update dev deployment

on:
push:
branches:
- main

jobs:
update-deployment:
runs-on: ubuntu-latest

steps:
- uses: 8BitJonny/[email protected]
id: PR
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.GIT_HUB_TOKEN }}
repository: worldcoin/signup-sequencer-deploy
event-type: update-dev
client-payload: |
{
"ref": "${{ github.sha }}",
"issuer": "${{ steps.PR.outputs.pr_url }}"
}
5 changes: 4 additions & 1 deletion src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub use universal_signer::UniversalSigner;
#[async_trait::async_trait]
pub trait KeysSource: Send + Sync + 'static {
/// Returns a key id and signer
async fn new_signer(&self) -> eyre::Result<(String, UniversalSigner)>;
async fn new_signer(
&self,
meta_name: &str,
) -> eyre::Result<(String, UniversalSigner)>;

/// Loads the key using the provided id
async fn load_signer(&self, id: String) -> eyre::Result<UniversalSigner>;
Expand Down
40 changes: 38 additions & 2 deletions src/keys/kms_keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use aws_config::BehaviorVersion;
use aws_sdk_kms::types::{KeySpec, KeyUsageType};
use aws_sdk_kms::types::{KeySpec, KeyUsageType, Tag};
use ethers::signers::Signer;
use eyre::{Context, ContextCompat};

use super::{KeysSource, UniversalSigner};
Expand All @@ -23,12 +24,22 @@ impl KmsKeys {

#[async_trait::async_trait]
impl KeysSource for KmsKeys {
async fn new_signer(&self) -> eyre::Result<(String, UniversalSigner)> {
async fn new_signer(
&self,
meta_name: &str,
) -> eyre::Result<(String, UniversalSigner)> {
let kms_key = self
.kms_client
.create_key()
.key_spec(KeySpec::EccSecgP256K1)
.key_usage(KeyUsageType::SignVerify)
.tags(
Tag::builder()
.tag_key("CreatedBy")
.tag_value("tx-sitter")
.build()?,
)
.description(format!("Key of relayer {meta_name}"))
.send()
.await
.context("AWS Error")?;
Expand All @@ -43,6 +54,31 @@ impl KeysSource for KmsKeys {
)
.await?;

let address = signer.address();

self.kms_client
.update_alias()
.target_key_id(key_id.clone())
.alias_name(format!("{meta_name}-{:?}", address));

self.kms_client
.tag_resource()
.key_id(key_id.clone())
.tags(
Tag::builder()
.tag_key("RelayerName")
.tag_value(meta_name)
.build()?,
)
.tags(
Tag::builder()
.tag_key("RelayerAddress")
.tag_value(format!("{:?}", address))
.build()?,
)
.send()
.await?;

Ok((key_id, UniversalSigner::Aws(signer)))
}

Expand Down
7 changes: 5 additions & 2 deletions src/keys/local_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ impl LocalKeys {

#[async_trait::async_trait]
impl KeysSource for LocalKeys {
async fn new_signer(&self) -> eyre::Result<(String, UniversalSigner)> {
async fn new_signer(
&self,
_meta_name: &str,
) -> eyre::Result<(String, UniversalSigner)> {
let signing_key = SigningKey::random(&mut self.rng.clone());

let key_id = signing_key.to_bytes().to_vec();
Expand Down Expand Up @@ -56,7 +59,7 @@ mod tests {
async fn local_roundtrip() -> eyre::Result<()> {
let keys_source = LocalKeys::new(&LocalKeysConfig::default());

let (id, signer) = keys_source.new_signer().await?;
let (id, signer) = keys_source.new_signer("meta name").await?;

let address = signer.address();

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn create_relayer(
State(app): State<Arc<App>>,
Json(req): Json<CreateRelayerRequest>,
) -> Result<Json<CreateRelayerResponse>, ApiError> {
let (key_id, signer) = app.keys_source.new_signer().await?;
let (key_id, signer) = app.keys_source.new_signer(&req.name).await?;

let address = signer.address();

Expand Down

0 comments on commit 29de92e

Please sign in to comment.