FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
07-21-2022 04:32 PM - edited 07-21-2022 04:34 PM
Hello,
I created a custom app that needs to load some keys and certificates for encryption and I am trying to load these files from my cpp code with no luck so far.
My setup is as following:
I am using the dump plugin to simply transfer my files into the snap as follows:
parts:
keys:
plugin: dump
source: ./keys
organize:
test.txt: ./keys/test.txt
server_cert.der: ./keys/server_cert.der
server_key.der: ./keys/server_key.der
client_cert.der: ./keys/client_cert.der
client_key.der: ./keys/client_key.der
Please note that I am using test.txt for testing reading the file from the cpp code.
In my cpp code I tried many different paths to read this file (test.txt) with no luck. I installed the app on my linux machine and it worked without any problem, but the issue occurs only on CtrlX core (I am currently using virtual core).
Here is the relevant part of my cpp code:
std::string path = "keys/test.txt";
std::cout << "write " << path << std::endl;
std::ofstream outfile (path);
if (outfile.is_open()){
std::cout << "opened: "<< path << std::endl;
outfile << "Test txt with some words\nand two lines\n" << path;
outfile.close();
}
else{
std::cout << "error: " << path << std::endl;
}
std::string sum;
char x;
std::ifstream inFile;
inFile.open("keys/test.txt");
if (!inFile) {
std::cout << "Unable to open file" << std::endl;
exit(1); // terminate with error
}
while (inFile >> x) {
sum = sum + x;
}
inFile.close();
std::cout << "Sum = " << sum << std::endl;
I also tried many different plugins such as: personal-files, system-files but I can't get it to work, I also saw some posts using rexroth-solutions, but I didn't know how to exactly use it for my use case.
My questions:
1) Where should I store my files (.der & .txt) files and how to organize them correctly
2) What is the correct path that I should use within my cpp code to be able to access these files
Your help is highly appreciated.
Many thanks,
Osama Ayoub
Solved! Go to Solution.
07-25-2022 09:11 AM
Hello Osama,
I'll answer your second question first: paths in snaps can be a little complicated. It is best to use the available environment variables set by the Snap daemon: https://snapcraft.io/docs/environment-variables. In your case, the files should be available in the $SNAP/keys directory.
If you need to manage certificates for your app, the following example may be of interest:https://github.com/boschrexroth/ctrlx-automation-sdk/tree/main/samples-sh/tpm2.consumer
Best,
Filipp
07-26-2022 09:56 AM
Hello,
Thank you for your reply, I tried setting the path as you indicated, but I am still unable to open the files.
Here is what I tried:
YAML:
parts:
keys:
plugin: dump
source: ./keys
organize:
'*': ./keys/
When I check the prime folder I can see that the folder keys is there and has all the contents needed, if I use $SNAP/keys I get a folder named $SNAP which is not correct for sure
CPP:
std::string sum;
char x;
std::ifstream inFile;
inFile.open("snap/opcserver/x1/keys/test.txt");
if (!inFile) {
std::cout << "Unable to open file" << std::endl;
exit(1); // terminate with error
}
while (inFile >> x) {
sum = sum + x;
}
inFile.close();
std::cout << "Sum = " << sum << std::endl;
I tried this path: "snap/opcserver/x1/keys/test.txt" as well as this path: "$SNAP/keys/test.txt" and in both I get the same error which is Unable to open the file
I also tried to add the following slots to the YAML:
slots:
keys:
interface: content
content: keys
source:
read:
- $SNAP/keys
write:
- $SNAP/keys
Is there something that I am missing?
Thanks in advanced,
Osama
07-26-2022 10:17 AM
Hi again,
you cannot use environment variables directly in C strings as you can with shell scripts. You need to first resolve the value of the environment variable and then use it to construct a path. For example:
Hope this helps,
Filipp
07-26-2022 10:36 AM
Thank you so much! I thought that if I resolved it manually it would work, but seems not.