Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/but-forge-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ description = "Store and retrieve information about forges"
doctest = false

[dependencies]
but-secret.workspace = true
but-fs = { workspace = true, features = ["legacy"] }

serde.workspace = true
Expand Down
124 changes: 6 additions & 118 deletions crates/but-forge-storage/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{path::PathBuf, sync::Mutex};

use crate::settings::ForgeSettings;
use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::settings::{ForgeSettings, GitHubAccount};
use std::path::PathBuf;

const FORGE_SETTINGS_FILE: &str = "forge_settings.json";

Expand All @@ -20,127 +17,18 @@ impl Storage {
}

pub fn read(&self) -> Result<ForgeSettings> {
let settings = match self.inner.read(FORGE_SETTINGS_FILE)? {
match self.inner.read(FORGE_SETTINGS_FILE)? {
Some(settings) => {
let settings: ForgeSettings = serde_json::from_str(&settings)?;
settings
Ok(settings)
}
None => Default::default(),
};

self.migrate_accounts(settings)
None => Ok(Default::default()),
}
}

pub fn save(&self, settings: &ForgeSettings) -> Result<()> {
let data = serde_json::to_string_pretty(settings)?;
self.inner.write(FORGE_SETTINGS_FILE, &data)?;
Ok(())
}

/// Migrate known GitHub accounts from secret storage to the new settings structure.
/// This can be removed after the stable release.
fn migrate_accounts(&self, settings: ForgeSettings) -> Result<ForgeSettings> {
let old_handle = "github_known_accounts";
let namespace = but_secret::secret::Namespace::BuildKind;

if settings.github.known_accounts.is_empty()
&& let Some(known_accounts) = but_secret::secret::retrieve(old_handle, namespace)?
{
// Migrate old known accounts from secret storage
let known_account_keys: Vec<String> = serde_json::from_str(&known_accounts)?;
let mut new_settings = settings.clone();
for account_key in known_account_keys {
if let Some(migrated_account) = migrate_account(&account_key)? {
new_settings.github.known_accounts.push(migrated_account);
}
}
self.save(&new_settings)?;
return Ok(new_settings);
}

Ok(settings)
}
}

// ===============================
// Migration of GitHub accounts.
//
// All of the code below this line is only needed to migrate old GitHub accounts,
// and can be removed after the stable release.
// ===============================

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum DeprecatedSerializableGitHubAccount {
OAuth {
username: String,
access_token: String,
},
Pat {
username: String,
access_token: String,
},
}

fn read_deprecated_github_secret(
account_secret_handle: &str,
) -> Result<Option<DeprecatedSerializableGitHubAccount>> {
static FAIR_QUEUE: Mutex<()> = Mutex::new(());
let _one_at_a_time_to_prevent_races = FAIR_QUEUE.lock().unwrap();
if let Some(secret_value) = but_secret::secret::retrieve(
account_secret_handle,
but_secret::secret::Namespace::BuildKind,
)? {
let account: DeprecatedSerializableGitHubAccount = serde_json::from_str(&secret_value)?;
Ok(Some(account))
} else {
Ok(None)
}
}

fn store_access_token_in_secret(account_secret_handle: &str, access_token: &str) -> Result<()> {
static FAIR_QUEUE: Mutex<()> = Mutex::new(());
let _one_at_a_time_to_prevent_races = FAIR_QUEUE.lock().unwrap();
but_secret::secret::persist(
account_secret_handle,
&but_secret::Sensitive(access_token.to_string()),
but_secret::secret::Namespace::BuildKind,
)
}

fn migrate_account(account_secret_handle: &str) -> Result<Option<GitHubAccount>> {
// Read the deprecated account structure.
// If it fails to parse it we assume there is nothing to migrate.
if let Some(deprecated_account) = read_deprecated_github_secret(account_secret_handle)
.ok()
.flatten()
{
let new_account = match deprecated_account {
DeprecatedSerializableGitHubAccount::OAuth {
username,
access_token,
} => {
// Store the plain access token in the same secret
store_access_token_in_secret(account_secret_handle, &access_token)?;
GitHubAccount::OAuth {
username: username.clone(),
access_token_key: account_secret_handle.to_string(),
}
}
DeprecatedSerializableGitHubAccount::Pat {
username,
access_token,
} => {
// Store the plain access token in the same secret
store_access_token_in_secret(account_secret_handle, &access_token)?;
GitHubAccount::Pat {
username: username.clone(),
access_token_key: account_secret_handle.to_string(),
}
}
};

return Ok(Some(new_account));
}
Ok(None)
}
1 change: 1 addition & 0 deletions crates/but-forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ but-github.workspace = true
but-gitlab.workspace = true
but-forge-storage.workspace = true
but-db.workspace = true
but-path.workspace = true

chrono.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread"] }
Expand Down
Loading
Loading