improve type safety for secrets

This commit is contained in:
Quaternions 2024-07-03 12:20:11 -07:00
parent ee7f06a243
commit 08b0c311cb
2 changed files with 66 additions and 61 deletions

View File

@ -205,15 +205,26 @@ fn read_readable(mut readable:impl std::io::Read)->std::io::Result<Vec<u8>>{
} }
#[derive(Clone)] #[derive(Clone)]
pub struct RobloxContext{ pub struct ApiKey(String);
impl ApiKey{
pub fn new(api_key:String)->Self{
Self(api_key)
}
pub fn get(self)->String{
self.0
}
}
#[derive(Clone)]
pub struct CloudContext{
pub api_key:String, pub api_key:String,
pub client:reqwest::Client, pub client:reqwest::Client,
} }
impl RobloxContext{ impl CloudContext{
pub fn new(api_key:String)->Self{ pub fn new(api_key:ApiKey)->Self{
Self{ Self{
api_key, api_key:api_key.get(),
client:reqwest::Client::new(), client:reqwest::Client::new(),
} }
} }

View File

@ -2,7 +2,7 @@ use std::{io::Read,path::PathBuf};
use clap::{Args,Parser,Subcommand}; use clap::{Args,Parser,Subcommand};
use anyhow::Result as AResult; use anyhow::Result as AResult;
use futures::StreamExt; use futures::StreamExt;
use rbx_asset::context::{AssetVersion,InventoryItem,RobloxContext}; use rbx_asset::context::{ApiKey, AssetVersion, CloudContext, InventoryItem};
type AssetID=u64; type AssetID=u64;
type AssetIDFileMap=Vec<(AssetID,PathBuf)>; type AssetIDFileMap=Vec<(AssetID,PathBuf)>;
@ -283,21 +283,21 @@ async fn main()->AResult<()>{
end_version:subcommand.end_version, end_version:subcommand.end_version,
start_version:subcommand.start_version.unwrap_or(0), start_version:subcommand.start_version.unwrap_or(0),
output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()), output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
asset_id:subcommand.asset_id, asset_id:subcommand.asset_id,
}).await, }).await,
Commands::Download(subcommand)=>{ Commands::Download(subcommand)=>{
let output_folder=subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()); let output_folder=subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap());
download_list( download_list(
ApiKey::from_args( api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
subcommand.asset_ids.into_iter().map(|asset_id|{ subcommand.asset_ids.into_iter().map(|asset_id|{
let mut path=output_folder.clone(); let mut path=output_folder.clone();
path.push(asset_id.to_string()); path.push(asset_id.to_string());
@ -307,11 +307,11 @@ async fn main()->AResult<()>{
}, },
Commands::DownloadDecompile(subcommand)=>{ Commands::DownloadDecompile(subcommand)=>{
download_decompile(DownloadDecompileConfig{ download_decompile(DownloadDecompileConfig{
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
asset_id:subcommand.asset_id, asset_id:subcommand.asset_id,
output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()), output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
style:subcommand.style.rox(), style:subcommand.style.rox(),
@ -321,20 +321,20 @@ async fn main()->AResult<()>{
}).await }).await
}, },
Commands::DownloadGroupInventoryJson(subcommand)=>download_group_inventory_json( Commands::DownloadGroupInventoryJson(subcommand)=>download_group_inventory_json(
ApiKey::from_args( api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
subcommand.group, subcommand.group,
subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()), subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
).await, ).await,
Commands::CreateAsset(subcommand)=>create(CreateConfig{ Commands::CreateAsset(subcommand)=>create(CreateConfig{
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
creator_user_id:subcommand.creator_user_id, creator_user_id:subcommand.creator_user_id,
creator_group_id:subcommand.creator_group_id, creator_group_id:subcommand.creator_group_id,
input_file:subcommand.input_file, input_file:subcommand.input_file,
@ -342,20 +342,20 @@ async fn main()->AResult<()>{
description:subcommand.description.unwrap_or_else(||String::with_capacity(0)), description:subcommand.description.unwrap_or_else(||String::with_capacity(0)),
}).await, }).await,
Commands::UploadAsset(subcommand)=>upload_asset(UploadAssetConfig{ Commands::UploadAsset(subcommand)=>upload_asset(UploadAssetConfig{
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
asset_id:subcommand.asset_id, asset_id:subcommand.asset_id,
input_file:subcommand.input_file, input_file:subcommand.input_file,
}).await, }).await,
Commands::UploadPlace(subcommand)=>upload_place(UploadPlaceConfig{ Commands::UploadPlace(subcommand)=>upload_place(UploadPlaceConfig{
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
place_id:subcommand.place_id, place_id:subcommand.place_id,
universe_id:subcommand.universe_id, universe_id:subcommand.universe_id,
input_file:subcommand.input_file, input_file:subcommand.input_file,
@ -370,22 +370,22 @@ async fn main()->AResult<()>{
input_folder:subcommand.input_folder.unwrap_or_else(||std::env::current_dir().unwrap()), input_folder:subcommand.input_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
template:subcommand.template, template:subcommand.template,
style:subcommand.style.map(|s|s.rox()), style:subcommand.style.map(|s|s.rox()),
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
asset_id:subcommand.asset_id, asset_id:subcommand.asset_id,
}).await, }).await,
Commands::CompileUploadPlace(subcommand)=>compile_upload_place(CompileUploadPlaceConfig{ Commands::CompileUploadPlace(subcommand)=>compile_upload_place(CompileUploadPlaceConfig{
input_folder:subcommand.input_folder.unwrap_or_else(||std::env::current_dir().unwrap()), input_folder:subcommand.input_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
template:subcommand.template, template:subcommand.template,
style:subcommand.style.map(|s|s.rox()), style:subcommand.style.map(|s|s.rox()),
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
place_id:subcommand.place_id, place_id:subcommand.place_id,
universe_id:subcommand.universe_id, universe_id:subcommand.universe_id,
}).await, }).await,
@ -410,11 +410,11 @@ async fn main()->AResult<()>{
Commands::DownloadAndDecompileHistoryIntoGit(subcommand)=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{ Commands::DownloadAndDecompileHistoryIntoGit(subcommand)=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{
git_committer_name:subcommand.git_committer_name, git_committer_name:subcommand.git_committer_name,
git_committer_email:subcommand.git_committer_email, git_committer_email:subcommand.git_committer_email,
api_key:ApiKey::from_args( api_key:api_key_from_args(
subcommand.api_key_literal, subcommand.api_key_literal,
subcommand.api_key_envvar, subcommand.api_key_envvar,
subcommand.api_key_file, subcommand.api_key_file,
).await?.get(), ).await?,
asset_id:subcommand.asset_id, asset_id:subcommand.asset_id,
output_folder:std::env::current_dir()?, output_folder:std::env::current_dir()?,
style:subcommand.style.rox(), style:subcommand.style.rox(),
@ -425,24 +425,18 @@ async fn main()->AResult<()>{
} }
} }
struct ApiKey(String); async fn api_key_from_args(literal:Option<String>,environment:Option<String>,file:Option<PathBuf>)->AResult<ApiKey>{
impl ApiKey{ let api_key=match (literal,environment,file){
fn get(self)->String{ (Some(api_key_literal),None,None)=>api_key_literal,
self.0 (None,Some(api_key_environment),None)=>std::env::var(api_key_environment)?,
} (None,None,Some(api_key_file))=>tokio::fs::read_to_string(api_key_file).await?,
async fn from_args(literal:Option<String>,environment:Option<String>,file:Option<PathBuf>)->AResult<Self>{ _=>Err(anyhow::Error::msg("Illegal api key argument triple"))?,
let api_key=match (literal,environment,file){ };
(Some(api_key_literal),None,None)=>api_key_literal, Ok(ApiKey::new(api_key))
(None,Some(api_key_environment),None)=>std::env::var(api_key_environment)?,
(None,None,Some(api_key_file))=>tokio::fs::read_to_string(api_key_file).await?,
_=>Err(anyhow::Error::msg("Illegal api key argument triple"))?,
};
Ok(Self(api_key))
}
} }
struct CreateConfig{ struct CreateConfig{
api_key:String, api_key:ApiKey,
model_name:String, model_name:String,
description:String, description:String,
input_file:PathBuf, input_file:PathBuf,
@ -452,7 +446,7 @@ struct CreateConfig{
///This is hardcoded to create models atm ///This is hardcoded to create models atm
async fn create(config:CreateConfig)->AResult<()>{ async fn create(config:CreateConfig)->AResult<()>{
let resp=RobloxContext::new(config.api_key) let resp=CloudContext::new(config.api_key)
.create_asset(rbx_asset::context::CreateAssetRequest{ .create_asset(rbx_asset::context::CreateAssetRequest{
assetType:rbx_asset::context::AssetType::Model, assetType:rbx_asset::context::AssetType::Model,
displayName:config.model_name, displayName:config.model_name,
@ -470,12 +464,12 @@ async fn create(config:CreateConfig)->AResult<()>{
} }
struct UploadAssetConfig{ struct UploadAssetConfig{
api_key:String, api_key:ApiKey,
asset_id:u64, asset_id:u64,
input_file:PathBuf, input_file:PathBuf,
} }
async fn upload_asset(config:UploadAssetConfig)->AResult<()>{ async fn upload_asset(config:UploadAssetConfig)->AResult<()>{
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
context.update_asset(rbx_asset::context::UpdateAssetRequest{ context.update_asset(rbx_asset::context::UpdateAssetRequest{
assetId:config.asset_id, assetId:config.asset_id,
displayName:None, displayName:None,
@ -485,13 +479,13 @@ async fn upload_asset(config:UploadAssetConfig)->AResult<()>{
} }
struct UploadPlaceConfig{ struct UploadPlaceConfig{
api_key:String, api_key:ApiKey,
place_id:u64, place_id:u64,
universe_id:u64, universe_id:u64,
input_file:PathBuf, input_file:PathBuf,
} }
async fn upload_place(config:UploadPlaceConfig)->AResult<()>{ async fn upload_place(config:UploadPlaceConfig)->AResult<()>{
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
context.update_place(rbx_asset::context::UpdatePlaceRequest{ context.update_place(rbx_asset::context::UpdatePlaceRequest{
placeId:config.place_id, placeId:config.place_id,
universeId:config.universe_id, universeId:config.universe_id,
@ -499,8 +493,8 @@ async fn upload_place(config:UploadPlaceConfig)->AResult<()>{
Ok(()) Ok(())
} }
async fn download_list(api_key:String,asset_id_file_map:AssetIDFileMap)->AResult<()>{ async fn download_list(api_key:ApiKey,asset_id_file_map:AssetIDFileMap)->AResult<()>{
let context=RobloxContext::new(api_key); let context=CloudContext::new(api_key);
futures::stream::iter(asset_id_file_map.into_iter() futures::stream::iter(asset_id_file_map.into_iter()
.map(|(asset_id,file)|{ .map(|(asset_id,file)|{
let context=&context; let context=&context;
@ -523,7 +517,7 @@ async fn download_list(api_key:String,asset_id_file_map:AssetIDFileMap)->AResult
Ok(()) Ok(())
} }
async fn get_inventory_pages(context:&RobloxContext,group:u64)->AResult<Vec<InventoryItem>>{ async fn get_inventory_pages(context:&CloudContext,group:u64)->AResult<Vec<InventoryItem>>{
let mut cursor:Option<String>=None; let mut cursor:Option<String>=None;
let mut asset_list=Vec::new(); let mut asset_list=Vec::new();
loop{ loop{
@ -537,8 +531,8 @@ async fn get_inventory_pages(context:&RobloxContext,group:u64)->AResult<Vec<Inve
Ok(asset_list) Ok(asset_list)
} }
async fn download_group_inventory_json(api_key:String,group:u64,output_folder:PathBuf)->AResult<()>{ async fn download_group_inventory_json(api_key:ApiKey,group:u64,output_folder:PathBuf)->AResult<()>{
let context=RobloxContext::new(api_key); let context=CloudContext::new(api_key);
let item_list=get_inventory_pages(&context,group).await?; let item_list=get_inventory_pages(&context,group).await?;
let mut path=output_folder.clone(); let mut path=output_folder.clone();
@ -548,7 +542,7 @@ async fn download_group_inventory_json(api_key:String,group:u64,output_folder:Pa
Ok(()) Ok(())
} }
async fn get_version_history(context:&RobloxContext,asset_id:AssetID)->AResult<Vec<AssetVersion>>{ async fn get_version_history(context:&CloudContext,asset_id:AssetID)->AResult<Vec<AssetVersion>>{
let mut cursor:Option<String>=None; let mut cursor:Option<String>=None;
let mut asset_list=Vec::new(); let mut asset_list=Vec::new();
loop{ loop{
@ -568,7 +562,7 @@ struct DownloadHistoryConfig{
end_version:Option<u64>, end_version:Option<u64>,
start_version:u64, start_version:u64,
output_folder:PathBuf, output_folder:PathBuf,
api_key:String, api_key:ApiKey,
asset_id:AssetID, asset_id:AssetID,
} }
@ -609,7 +603,7 @@ async fn download_history(mut config:DownloadHistoryConfig)->AResult<()>{
None=>Err(anyhow::Error::msg("Cannot continue from versions.json - there are no previous versions"))?, None=>Err(anyhow::Error::msg("Cannot continue from versions.json - there are no previous versions"))?,
} }
} }
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
//limit concurrent downloads //limit concurrent downloads
let mut join_set=tokio::task::JoinSet::new(); let mut join_set=tokio::task::JoinSet::new();
@ -742,7 +736,7 @@ async fn decompile(config:DecompileConfig)->AResult<()>{
} }
struct DownloadDecompileConfig{ struct DownloadDecompileConfig{
api_key:String, api_key:ApiKey,
asset_id:AssetID, asset_id:AssetID,
style:rox_compiler::Style, style:rox_compiler::Style,
output_folder:PathBuf, output_folder:PathBuf,
@ -752,7 +746,7 @@ struct DownloadDecompileConfig{
} }
async fn download_decompile(config:DownloadDecompileConfig)->AResult<()>{ async fn download_decompile(config:DownloadDecompileConfig)->AResult<()>{
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
let file=context.get_asset(rbx_asset::context::GetAssetRequest{asset_id:config.asset_id,version:None}).await?; let file=context.get_asset(rbx_asset::context::GetAssetRequest{asset_id:config.asset_id,version:None}).await?;
let dom=load_dom(std::io::Cursor::new(file))?; let dom=load_dom(std::io::Cursor::new(file))?;
@ -910,7 +904,7 @@ async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{
} }
struct DownloadAndDecompileHistoryConfig{ struct DownloadAndDecompileHistoryConfig{
api_key:String, api_key:ApiKey,
asset_id:AssetID, asset_id:AssetID,
git_committer_name:String, git_committer_name:String,
git_committer_email:String, git_committer_email:String,
@ -922,7 +916,7 @@ struct DownloadAndDecompileHistoryConfig{
} }
async fn download_and_decompile_history_into_git(config:DownloadAndDecompileHistoryConfig)->AResult<()>{ async fn download_and_decompile_history_into_git(config:DownloadAndDecompileHistoryConfig)->AResult<()>{
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
//poll paged list of all asset versions //poll paged list of all asset versions
let asset_list=get_version_history(&context,config.asset_id).await?; let asset_list=get_version_history(&context,config.asset_id).await?;
@ -993,7 +987,7 @@ struct CompileUploadAssetConfig{
input_folder:PathBuf, input_folder:PathBuf,
template:Option<PathBuf>, template:Option<PathBuf>,
style:Option<rox_compiler::Style>, style:Option<rox_compiler::Style>,
api_key:String, api_key:ApiKey,
asset_id:AssetID, asset_id:AssetID,
} }
async fn compile_upload_asset(config:CompileUploadAssetConfig)->AResult<()>{ async fn compile_upload_asset(config:CompileUploadAssetConfig)->AResult<()>{
@ -1013,7 +1007,7 @@ async fn compile_upload_asset(config:CompileUploadAssetConfig)->AResult<()>{
rbx_binary::to_writer(std::io::Cursor::new(&mut data),&dom,dom.root().children())?; rbx_binary::to_writer(std::io::Cursor::new(&mut data),&dom,dom.root().children())?;
//upload it //upload it
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
context.update_asset(rbx_asset::context::UpdateAssetRequest{ context.update_asset(rbx_asset::context::UpdateAssetRequest{
assetId:config.asset_id, assetId:config.asset_id,
displayName:None, displayName:None,
@ -1026,7 +1020,7 @@ struct CompileUploadPlaceConfig{
input_folder:PathBuf, input_folder:PathBuf,
template:Option<PathBuf>, template:Option<PathBuf>,
style:Option<rox_compiler::Style>, style:Option<rox_compiler::Style>,
api_key:String, api_key:ApiKey,
place_id:u64, place_id:u64,
universe_id:u64, universe_id:u64,
} }
@ -1047,7 +1041,7 @@ async fn compile_upload_place(config:CompileUploadPlaceConfig)->AResult<()>{
rbx_binary::to_writer(std::io::Cursor::new(&mut data),&dom,dom.root().children())?; rbx_binary::to_writer(std::io::Cursor::new(&mut data),&dom,dom.root().children())?;
//upload it //upload it
let context=RobloxContext::new(config.api_key); let context=CloudContext::new(config.api_key);
context.update_place(rbx_asset::context::UpdatePlaceRequest{ context.update_place(rbx_asset::context::UpdatePlaceRequest{
universeId:config.universe_id, universeId:config.universe_id,
placeId:config.place_id, placeId:config.place_id,