Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct parsing of organization credentials #403

Merged
merged 2 commits into from
Aug 23, 2024
Merged
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
60 changes: 40 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ impl ShellConfig {
}

for value in config.capella_orgs_mut() {
let identifier = value.identifier().to_owned();
let config_credentials = value.credentials_mut();

for cred in &standalone.capella_orgs {
if config_credentials.secret_key.is_empty() && !cred.secret_key.is_empty() {
config_credentials.secret_key = cred.secret_key.clone()
}
if config_credentials.access_key.is_empty() && !cred.access_key.is_empty() {
config_credentials.access_key = cred.access_key.clone()
if cred.identifier() == identifier {
if config_credentials.secret_key.is_empty() && !cred.secret_key.is_empty() {
config_credentials.secret_key = cred.secret_key.clone()
}
if config_credentials.access_key.is_empty() && !cred.access_key.is_empty() {
config_credentials.access_key = cred.access_key.clone()
}
}
}
}
Expand Down Expand Up @@ -210,17 +213,17 @@ fn try_config_from_path(mut path: PathBuf) -> Option<ShellConfig> {
}
}

fn try_credentials_from_dot_path(mut path: PathBuf) -> Option<StandaloneCredentialsConfig> {
fn try_credentials_from_dot_path(mut path: PathBuf) -> Option<CredentialsFromFile> {
path.push(".cbsh");
try_credentials_from_path(path)
}

fn try_credentials_from_path(mut path: PathBuf) -> Option<StandaloneCredentialsConfig> {
fn try_credentials_from_path(mut path: PathBuf) -> Option<CredentialsFromFile> {
path.push("credentials");

let read = fs::read_to_string(&path);
match read {
Ok(r) => Some(StandaloneCredentialsConfig::from_str(&r)),
Ok(r) => Some(CredentialsFromFile::from_str(&r)),
Err(e) => {
debug!("Could not locate {:?} because of {:?}", path, e);
None
Expand All @@ -232,7 +235,7 @@ fn try_credentials_from_path(mut path: PathBuf) -> Option<StandaloneCredentialsC
pub struct CapellaOrganizationConfig {
identifier: String,
#[serde(flatten)]
credentials: CapellaOrganizationCredentials,
credentials: OrganizationCredentials,
#[serde(default)]
#[serde(
rename(deserialize = "management-timeout", serialize = "management-timeout"),
Expand All @@ -253,7 +256,7 @@ impl CapellaOrganizationConfig {
) -> Self {
Self {
identifier,
credentials: CapellaOrganizationCredentials {
credentials: OrganizationCredentials {
access_key,
secret_key,
},
Expand All @@ -277,7 +280,7 @@ impl CapellaOrganizationConfig {
self.default_project.as_ref().cloned()
}

pub fn credentials_mut(&mut self) -> &mut CapellaOrganizationCredentials {
pub fn credentials_mut(&mut self) -> &mut OrganizationCredentials {
&mut self.credentials
}
}
Expand Down Expand Up @@ -529,7 +532,7 @@ impl From<(String, &RemoteCluster)> for ClusterConfig {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CapellaOrganizationCredentials {
pub struct OrganizationCredentials {
#[serde(default)]
#[serde(rename(deserialize = "access-key", serialize = "access-key"))]
access_key: String,
Expand All @@ -538,7 +541,7 @@ pub struct CapellaOrganizationCredentials {
secret_key: String,
}

impl CapellaOrganizationCredentials {}
impl OrganizationCredentials {}

#[derive(Debug, Deserialize, Serialize)]
pub struct ClusterCredentials {
Expand Down Expand Up @@ -678,22 +681,22 @@ impl Default for ClusterTlsConfig {
}

#[derive(Debug, Deserialize)]
pub struct StandaloneCredentialsConfig {
pub struct CredentialsFromFile {
#[allow(dead_code)]
version: usize,
/// Note: clusters is kept for backwards compatibility and convenience
#[serde(alias = "cluster", default)]
#[serde(alias = "clusters")]
clusters: Vec<StandaloneClusterCredentials>,
clusters: Vec<ClusterCredentialsFromFile>,

#[serde(alias = "capella-organization", default)]
capella_orgs: Vec<CapellaOrganizationCredentials>,
capella_orgs: Vec<OrganizationCredentialsFromFile>,

#[serde(alias = "llm", default)]
llms: Vec<LLMCredentials>,
}

impl StandaloneCredentialsConfig {
impl CredentialsFromFile {
/// Builds the config from a raw input string.
pub fn from_str(input: &str) -> Self {
// Note: ideally this propagates up into a central error handling facility,
Expand All @@ -708,7 +711,7 @@ impl StandaloneCredentialsConfig {
}
}

impl Default for StandaloneCredentialsConfig {
impl Default for CredentialsFromFile {
fn default() -> Self {
Self {
clusters: vec![],
Expand All @@ -719,14 +722,31 @@ impl Default for StandaloneCredentialsConfig {
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OrganizationCredentialsFromFile {
identifier: String,
#[serde(default)]
#[serde(rename(deserialize = "access-key", serialize = "access-key"))]
access_key: String,
#[serde(default)]
#[serde(rename(deserialize = "secret-key", serialize = "secret-key"))]
secret_key: String,
}

impl OrganizationCredentialsFromFile {
fn identifier(&self) -> String {
self.identifier.clone()
}
}

#[derive(Debug, Deserialize)]
pub struct StandaloneClusterCredentials {
pub struct ClusterCredentialsFromFile {
identifier: String,
username: Option<String>,
password: Option<String>,
}

impl StandaloneClusterCredentials {
impl ClusterCredentialsFromFile {
fn identifier(&self) -> &str {
self.identifier.as_ref()
}
Expand Down
Loading