Skip to content

Commit

Permalink
Merge pull request #619 from dcSpark/nico/fix_tls_storage
Browse files Browse the repository at this point in the history
fix tls storage
  • Loading branch information
nicarq authored Oct 18, 2024
2 parents b060d10 + 29f7ca1 commit 969ee5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
31 changes: 15 additions & 16 deletions shinkai-bin/shinkai-node/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,25 @@ pub async fn initialize_node() -> Result<
let encryption_secret_key_string = encryption_secret_key_to_string(node_keys.encryption_secret_key.clone());

// Add the HTTPS certificates to the secret content
let private_cert = node_keys
.private_https_certificate
.clone()
.unwrap_or_default()
.replace('\n', "\\n");

let public_cert = node_keys
.public_https_certificate
.clone()
.unwrap_or_default()
.replace('\n', "\\n");

let secret_content = format!(
"GLOBAL_IDENTITY_NAME={}\nIDENTITY_SECRET_KEY={}\nENCRYPTION_SECRET_KEY={}\nPRIVATE_HTTPS_CERTIFICATE={}\nPUBLIC_HTTPS_CERTIFICATE={}",
global_identity_name,
identity_secret_key_string,
encryption_secret_key_string,
node_keys.private_https_certificate.clone().unwrap_or_default(),
node_keys.public_https_certificate.clone().unwrap_or_default()
private_cert,
public_cert
);

if !node_env.no_secrets_file {
Expand Down Expand Up @@ -337,26 +349,13 @@ fn parse_secrets_file(secrets_file_path: &str) -> HashMap<String, String> {
let contents = fs::read_to_string(secrets_file_path).unwrap_or_default();

let mut map = HashMap::new();
let mut current_key = String::new();
let mut current_value = String::new();

for line in contents.lines() {
if let Some((key, value)) = line.split_once('=') {
if !current_key.is_empty() {
map.insert(current_key.clone(), current_value.trim_end().to_string());
}
current_key = key.to_string();
current_value = value.to_string();
} else {
current_value.push_str("\n");
current_value.push_str(line);
map.insert(key.to_string(), value.to_string());
}
}

if !current_key.is_empty() {
map.insert(current_key, current_value.trim_end().to_string());
}

map
}

Expand Down
34 changes: 15 additions & 19 deletions shinkai-bin/shinkai-node/src/utils/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,14 @@ pub fn generate_or_load_keys(secrets_file_path: &str) -> NodeKeys {
// First check for .secret file
if let Ok(contents) = fs::read_to_string(secrets_file_path) {
// Parse the contents of the file
let mut lines = contents.lines();
let mut map = HashMap::new();
let mut current_key = None;
let mut current_value = String::new();

while let Some(line) = lines.next() {
if line.contains('=') && current_key.is_none() {
if let Some(key) = current_key.take() {
map.insert(key, current_value.trim().to_string());
}
let mut parts = line.splitn(2, '=');
current_key = parts.next().map(|s| s.to_string());
current_value = parts.next().unwrap_or("").to_string();
} else if let Some(_) = current_key {
current_value.push('\n');
current_value.push_str(line);
for line in contents.lines() {
if let Some((key, value)) = line.split_once('=') {
map.insert(key.trim().to_string(), value.trim().to_string());
}
}

if let Some(key) = current_key {
map.insert(key, current_value.trim().to_string());
}

// Use the values from the file if they exist
if let (Some(identity_secret_key_string), Some(encryption_secret_key_string)) =
Expand All @@ -64,8 +50,18 @@ pub fn generate_or_load_keys(secrets_file_path: &str) -> NodeKeys {
let encryption_public_key = x25519_dalek::PublicKey::from(&encryption_secret_key);

// Read the HTTPS certificates if they exist
let private_https_certificate = map.get("PRIVATE_HTTPS_CERTIFICATE").cloned();
let public_https_certificate = map.get("PUBLIC_HTTPS_CERTIFICATE").cloned();
let private_https_certificate = match map.get("PRIVATE_HTTPS_CERTIFICATE").cloned() {
Some(certificate) if certificate.trim().len() > 0 => {
Some(certificate.replace("\\n", "\n"))
},
_ => None,
};
let public_https_certificate = match map.get("PUBLIC_HTTPS_CERTIFICATE").cloned() {
Some(certificate) if certificate.trim().len() > 0 => {
Some(certificate.replace("\\n", "\n"))
},
_ => None,
};

return NodeKeys {
identity_secret_key,
Expand Down

0 comments on commit 969ee5d

Please sign in to comment.