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

Feat: General SDK improvements #48

Merged
merged 3 commits into from
Aug 19, 2024
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
6 changes: 3 additions & 3 deletions crates/infisical/src/api/secrets/get_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub async fn get_secret_request(
let json: &serde_json::Value = &serde_json::json!({
"workspaceId": input.project_id,
"environment": input.environment,
"secretPath": input.path.as_ref().unwrap_or(&"/".to_string()), // default is "/"
"type": input.r#type.as_ref().unwrap_or(&"shared".to_string()), // default is shared
"include_imports": input.include_imports.as_ref().unwrap_or(&false), // default is false
"secretPath": input.path.clone().unwrap_or("/".to_string()), // default is "/"
"type": input.r#type.clone().unwrap_or("shared".to_string()), // default is shared
"include_imports": input.include_imports.unwrap_or(false).to_string(),
});

let secret_type = match input.r#type.as_ref() {
Expand Down
23 changes: 21 additions & 2 deletions crates/infisical/src/client/auth_method_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::constants::{
INFISICAL_AWS_IAM_AUTH_IDENTITY_ID_ENV_NAME, INFISICAL_AZURE_AUTH_IDENTITY_ID_ENV_NAME,
INFISICAL_GCP_AUTH_IDENTITY_ID_ENV_NAME,
INFISICAL_ACCESS_TOKEN_ENV_NAME, INFISICAL_AWS_IAM_AUTH_IDENTITY_ID_ENV_NAME,
INFISICAL_AZURE_AUTH_IDENTITY_ID_ENV_NAME, INFISICAL_GCP_AUTH_IDENTITY_ID_ENV_NAME,
INFISICAL_GCP_IAM_SERVICE_ACCOUNT_KEY_FILE_PATH_ENV_NAME,
INFISICAL_KUBERNETES_IDENTITY_ID_ENV_NAME,
INFISICAL_KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH_ENV_NAME,
Expand Down Expand Up @@ -105,11 +105,20 @@ pub enum AuthMethod {
GcpIdToken,
GcpIam,
AwsIam,
AccessToken,
}

// Custom validation to ensure that if universal_auth or gcp_auth are present, their fields are populated
impl AuthenticationOptions {
pub fn validate(&mut self) -> Result<AuthMethod, String> {
// ACCESS TOKEN:
if let Some(ref access_token) = self.access_token {
if !access_token.is_empty() {
return Ok(AuthMethod::AccessToken);
}
return Err("access_token is present but is empty".into());
}

// UNIVERSAL AUTH:
if let Some(ref auth) = self.universal_auth {
if !auth.client_id.is_empty() && !auth.client_secret.is_empty() {
Expand Down Expand Up @@ -155,6 +164,10 @@ impl AuthenticationOptions {
} else {
debug!("No authentication method is set. Checking environment variables.");

// access token env
let access_token_env =
std::env::var(INFISICAL_ACCESS_TOKEN_ENV_NAME).unwrap_or_default();

// universal auth env's
let universal_auth_client_id_env =
std::env::var(INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_ENV_NAME).unwrap_or_default();
Expand Down Expand Up @@ -183,6 +196,12 @@ impl AuthenticationOptions {
let azure_auth_identity_id_env =
std::env::var(INFISICAL_AZURE_AUTH_IDENTITY_ID_ENV_NAME).unwrap_or_default();

// access token env check
if !access_token_env.is_empty() {
self.access_token = Some(access_token_env);
return Ok(AuthMethod::AccessToken);
}

// universal auth env check
if !universal_auth_client_id_env.is_empty()
&& !universal_auth_client_secret_env.is_empty()
Expand Down
4 changes: 3 additions & 1 deletion crates/infisical/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ impl Client {
// Move the deprecated fields to the new auth object for backwards compatibility.
#[allow(deprecated)]
{
settings.auth.access_token = settings.access_token;
if settings.auth.access_token.is_none() {
settings.auth.access_token = settings.access_token;
}

if settings.client_id.is_some() && settings.client_secret.is_some() {
settings.auth.universal_auth = Some(UniversalAuthMethod {
Expand Down
2 changes: 2 additions & 0 deletions crates/infisical/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub const INFISICAL_AZURE_AUTH_IDENTITY_ID_ENV_NAME: &str = "INFISICAL_AZURE_AUT

pub const INFISICAL_KUBERNETES_IDENTITY_ID_ENV_NAME: &str = "INFISICAL_KUBERNETES_IDENTITY_ID";

pub const INFISICAL_ACCESS_TOKEN_ENV_NAME: &str = "INFISICAL_ACCESS_TOKEN";

// AWS EC2 Metadata Service:
pub const AWS_EC2_METADATA_TOKEN_URL: &str = "http://169.254.169.254/latest/api/token";
pub const AWS_EC2_INSTANCE_IDENTITY_DOCUMENT_URL: &str =
Expand Down
6 changes: 5 additions & 1 deletion crates/infisical/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ pub async fn handle_authentication(client: &mut Client) -> Result<()> {
debug!("Auth validation passed");

let auth_method = validation_result.unwrap_or(AuthMethod::UniversalAuth);

let result;

match auth_method {
AuthMethod::AccessToken => {
// Special case, since we don't need to do any authentication with Infisical.
client.set_access_token(client.auth.access_token.clone().unwrap_or("".to_string()));
return Ok(());
}
AuthMethod::UniversalAuth => {
debug!("Auth method is Universal Auth");

Expand Down
3 changes: 1 addition & 2 deletions crates/infisical/src/manager/secrets/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub struct GetSecretOptions {
pub environment: String,
pub project_id: String,
pub path: Option<String>,

pub expand_secret_references: Option<bool>,
pub r#type: Option<String>,

pub include_imports: Option<bool>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/infisical/tests/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod tests {
project_id: variables.project_id.to_string(),
path: None,
r#type: None,
expand_secret_references: None,
include_imports: None,
};

Expand Down
Loading