Skip to content

Commit

Permalink
From review
Browse files Browse the repository at this point in the history
  • Loading branch information
sonndinh committed Jul 28, 2023
1 parent 45fe3b7 commit ca01244
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
39 changes: 22 additions & 17 deletions src/certs_downloader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Config {
{4: <15} Top level directory to store the docs\n\
{5: <15} Subdirectory to store docs specific to this participant (optional)\n\
{6: <15} Subdirectory to store identity CA doc (optional)\n\
{7: <15} Subdirectory to store permission CA doc (optional)",
{7: <15} Subdirectory to store permissions CA doc (optional)",
"<url>", "<username>", "<password>", "<nonce>", "<dir>", "[part_dir]", "[id_ca_dir]", "[perm_ca_dir]");
return Err(usage_msg);
}
Expand Down Expand Up @@ -105,42 +105,47 @@ pub fn download_certs(config: &Config) -> Result<(), Box<dyn std::error::Error>>
String::from("")
}
);
download_cert(&client, &base_url, "identity_ca.pem", None, &id_ca_dir)?;
download_cert(&client, &base_url, "permissions_ca.pem", None, &perm_ca_dir)?;
download_cert(&client, &base_url, "governance.xml.p7s", None, &config.directory)?;
download_cert(&client, &base_url, "key_pair", Some(&config.nonce), &config.directory)?;
download_cert(&client, &base_url, "permissions.xml.p7s", Some(&config.nonce), &part_dir)?;
download_file(&client, &base_url, "identity_ca.pem", None, &id_ca_dir)?;
download_file(&client, &base_url, "permissions_ca.pem", None, &perm_ca_dir)?;
download_file(&client, &base_url, "governance.xml.p7s", None, &config.directory)?;
download_file(&client, &base_url, "permissions.xml.p7s", Some(&config.nonce), &part_dir)?;

let kp_file = format!("{}/key_pair", config.directory);
let kp_str = fs::read_to_string(&kp_file)?;
let kp_str = get_body(&client, &base_url, "key_pair", Some(&config.nonce))?;
let kp: KeyPair = serde_json::from_str(&kp_str)?;

let public_file = format!("{}/identity.pem", part_dir);
let private_file = format!("{}/identity_key.pem", part_dir);
fs::File::create(public_file)?.write_all(kp.public.as_bytes())?;
fs::File::create(private_file)?.write_all(kp.private.as_bytes())?;
fs::remove_file(&kp_file)?;
Ok(())
}

fn download_cert(
fn download_file(
client: &Client,
base_url: &str,
filename: &str,
nonce: Option<&str>,
directory: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let body = get_body(client, base_url, filename, nonce)?;
let path = format!("{}/{}", directory, filename);
fs::create_dir_all(directory)?;
let mut file = fs::File::create(path)?;
file.write_all(body.as_bytes())?;
Ok(())
}

fn get_body(
client: &Client,
base_url: &str,
filename: &str,
nonce: Option<&str>,
) -> Result<String, Box<dyn std::error::Error>> {
let url;
if nonce.is_some() {
url = format!("{}/{}?nonce={}", base_url, filename, nonce.unwrap());
} else {
url = format!("{}/{}", base_url, filename);
}
let body = client.get(&url).send()?.text()?;

let path = format!("{}/{}", directory, filename);
fs::create_dir_all(directory)?;
let mut file = fs::File::create(path)?;
file.write_all(body.as_bytes())?;
Ok(())
Ok(body)
}
3 changes: 1 addition & 2 deletions src/certs_downloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ fn main() {
process::exit(1);
});

let result = download_certs(&config);
match result {
match download_certs(&config) {
Ok(_) => (),
Err(err) => {
eprintln!("download_certs failed: {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion src/smartlock.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[smartlock]
topic_prefix = "C.53."
domain_id = 1
username = "54"
username = "47"
api_url = "https://dpm.unityfoundation.io/api"
36 changes: 21 additions & 15 deletions src/smartlock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ fi
echo "CMD: '$CMD', SECURITY: '$SECURITY', LOCK_ID: '$LOCK', SECURITY_ARGS: '$SECURITY_ARGS'"

function update_certs_curl {
APP_PASSWORD=$(cat ${BASE_PATH}/dpm_password)
APP_NONCE=${LOCK}
API_URL=$(grep api_url ${smartlock_ini} | sed 's/api_url *= *"//; s/".*//')
USERNAME=$(grep username ${smartlock_ini} | sed 's/username *= *"//; s/".*//')
API_URL=$1
USERNAME=$2
PASSWORD=$3
NONCE=$4

mkdir -p ${cert_dir}/id_ca ${cert_dir}/${LOCK} ${cert_dir}/perm_ca

curl -c cookies.txt -H'Content-Type: application/json' -d"{\"username\":\"${USERNAME}\",\"password\":\"$APP_PASSWORD\"}" ${API_URL}/login
curl -c cookies.txt -H'Content-Type: application/json' -d"{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" ${API_URL}/login

curl --silent -b cookies.txt "${API_URL}/applications/identity_ca.pem" > ${ID_CA}
curl --silent -b cookies.txt "${API_URL}/applications/permissions_ca.pem" > ${PERM_CA}
curl --silent -b cookies.txt "${API_URL}/applications/governance.xml.p7s" > ${PERM_GOV}
curl --silent -b cookies.txt "${API_URL}/applications/key_pair?nonce=${APP_NONCE}" > key-pair
curl --silent -b cookies.txt "${API_URL}/applications/permissions.xml.p7s?nonce=${APP_NONCE}" > ${PERM_PERMS}
curl --silent -b cookies.txt "${API_URL}/applications/key_pair?nonce=${NONCE}" > key-pair
curl --silent -b cookies.txt "${API_URL}/applications/permissions.xml.p7s?nonce=${NONCE}" > ${PERM_PERMS}

jq -r '.public' key-pair > ${ID_CERT}
jq -r '.private' key-pair > ${ID_PKEY}
Expand All @@ -111,14 +111,15 @@ function update_certs_curl {
}

function update_certs_rust {
API_URL=$(grep api_url ${smartlock_ini} | sed 's/api_url *= *"//; s/".*//')
USERNAME=$(grep username ${smartlock_ini} | sed 's/username *= *"//; s/".*//')
PASSWORD=$(cat ${BASE_PATH}/dpm_password)
NONCE=${LOCK}
API_URL=$1
USERNAME=$2
PASSWORD=$3
NONCE=$4

if ! command -v rustc &> /dev/null
then
curl https://sh.rustup.rs -sSf | sh -s -- -y
echo "ERROR: Rust is not installed! Install it and try again or use the --curl-certs-downloader option."
exit 1
fi

cd certs_downloader
Expand All @@ -128,10 +129,15 @@ function update_certs_rust {
}

function update_certs {
if (( $CURL_CERT_DOWNLOADER )); then
update_certs_curl
API_URL=$(grep api_url ${smartlock_ini} | sed 's/api_url *= *"//; s/".*//')
USERNAME=$(grep username ${smartlock_ini} | sed 's/username *= *"//; s/".*//')
PASSWORD=$(cat ${BASE_PATH}/dpm_password)
NONCE=${LOCK}

if (( $CURL_CERTS_DOWNLOADER )); then
update_certs_curl $API_URL $USERNAME $PASSWORD $NONCE
else
update_certs_rust
update_certs_rust $API_URL $USERNAME $PASSWORD $NONCE
fi
}

Expand Down

0 comments on commit ca01244

Please sign in to comment.