avancees environnement et main

This commit is contained in:
eleonore12345 2024-08-13 13:11:09 +02:00
parent 05d596286f
commit 0057717820
2 changed files with 86 additions and 27 deletions

View File

@ -40,7 +40,7 @@ int createUser(string serviceUsername)
{
//this method creates a Unix user dedicated to the service
//get the User ID from servers.csv
int uidStart=2000; //so that the uids do not overlap with existing uids
int uidStart=stoi(getenv("services_uid_start")); //so that the uids do not overlap with existing uids
Services services = Services();
const Service * service = services.FindByUsername(serviceUsername);
int uid = (*service).GetUserID()+uidStart;
@ -62,6 +62,33 @@ int createUser(string serviceUsername)
return 0;
}
int deleteUser(string serviceUsername)
{
//this method deletes the Unix user dedicated to the service
//get the User ID from servers.csv
int uidStart=stoi(getenv("services_uid_start"));
Services services = Services();
const Service * service = services.FindByUsername(serviceUsername);
int uid = (*service).GetUserID()+uidStart;
//test if user exists
string cmd = "id -u "+serviceUsername;
string res = BashManager::ExecuteAndReadResult (cmd);
if(res!=to_string(uid)){
cout << "no user to delete" << endl;
return 0;
}
//delete user
string cmd2 ="deluser --delete-home " + serviceUsername;
string res2 = BashManager::ExecuteAndReadResult(cmd2);
if (res2.find("Done") == std::string::npos){
cerr << "Error when executing the bash command to delete the user specific to the service." << endl;
cerr << res2 << endl;
return -1;
}
cout << "user deleted" << endl;
return 0;
}
string findCertificate(string serviceUsername)
{
//this method searches for a specific ssl certificate for the service, either in dns or http directories
@ -71,8 +98,10 @@ string findCertificate(string serviceUsername)
//searching is dns_certs_path
string dns_certs_path=getenv("dns_certs_path"); //dns_certs_path is an environment variable
//finding the serviceUsername* directory
string cmd="ls $dns_certs_path/"+serviceUsername+" | grep \"^"+serviceUsername+"\(-[0-9]\{4\}\)\?$";
string cmd="ls $dns_certs_path/"+serviceUsername+" | grep '^"+serviceUsername+"\\(-[0-9]\\{4\\}\\)\\?$'";
cout << "before"<< endl;
string name = BashManager::ExecuteAndReadResult(cmd);
cout << "result 1: " << name << endl;
if (!name.empty()){
//finding the certificate
string cert = dns_certs_path+"/"+name+"/fullchain.pem";
@ -81,27 +110,24 @@ string findCertificate(string serviceUsername)
} else {
cout << "No certificate in " << dns_certs_path << endl;
}
} else {
cout << "No certificate in " << dns_certs_path << endl;
//searching in http_certs_path
string http_certs_path=getenv(("http_certs_path")); //http_certs_path is an environment variable
//finding the serviceUsername* directory
string cmd="ls $http_certs_path/"+serviceUsername+" | grep \"^"+serviceUsername+"\(-[0-9]\{4\}\)\?$";
string name = BashManager::ExecuteAndReadResult(cmd);
if (!name.empty()){
//finding the certificate
string cert = http_certs_path+"/"+name+"/fullchain.pem";
if (fs::exists(cert)){
return cert;
} else {
cout << "No certificate in " << http_certs_path << endl;
}
} else {
cout << "Using dummy certificate" << endl;
return getenv("dummy_cert_path");
}
}
//searching in http_certs_path
string http_certs_path=getenv(("http_certs_path")); //http_certs_path is an environment variable
//finding the serviceUsername* directory
cmd="ls $http_certs_path/"+serviceUsername+" | grep '^"+serviceUsername+"\\(-[0-9]\\{4\\}\\)\\?$'";
name = BashManager::ExecuteAndReadResult(cmd);
cout << "result 2: " << name << endl;
if (!name.empty()){
//finding the certificate
string cert = http_certs_path+"/"+name+"/fullchain.pem";
if (fs::exists(cert)){
return cert;
} else {
cout << "No certificate in " << http_certs_path << endl;
}
}
cout << "Using dummy certificate" << endl;
return getenv("dummy_cert_path");
}
int createEnvService(string serviceUsername)
@ -131,6 +157,7 @@ int createEnvService(string serviceUsername)
outfile << "jc_id=" << jc_id << endl;
outfile << "net=" << net << endl;
outfile << "jc_cert=" << jc_cert << endl;
outfile.close();
//setting the environment variables for all the shell commands called in this C++ programm
setenv("http_dir",http_dir.c_str(),1);
@ -156,18 +183,23 @@ int createEnvService(string serviceUsername)
string cmd="chown "+ jc_id +":www-data -R "+http_dir;
BashManager::Execute(cmd);
//secret_dir
fs::create_directories(secret_dir);
if (chown(secret_dir.c_str(), (unsigned int)stoi(jc_id),(unsigned int)stoi(jc_id)) != 0) {
cerr << "Error changing ownership of" << secret_dir << endl;
return -1;
}
fs::permissions(secret_dir,fs::perms::owner_all|fs::perms::group_read|fs::perms::group_exec|fs::perms::others_exec,fs::perm_options::replace);
cout << "service environment created" << endl;
return 0;
}
int removeEnvService()
{
/*
[ -d "$HTTP_DIR" ] && rm -r "$HTTP_DIR"*/
string http_dir=getenv("http_dir");
if(fs::exists(http_dir)){
fs::remove_all(http_dir);
}
cout << "http directory of service deleted" << endl;
return 0;
}
@ -204,8 +236,7 @@ int createEnv()
setenv("dummy_cert_path",dummy_cert_path.c_str(),1);
setenv("servicefile",servicefile.c_str(),1);
setenv("services_uid_start",services_uid_start.c_str(),1);
cout << "global environment created" << endl;
return 0;
}
@ -241,23 +272,49 @@ int deployAll()
cout << "deploying all" <<endl;
createEnv();
//initializing modules
for(Module * mod_ptr : modules){
int modPrep = (*mod_ptr).Prepare();
int modClean = (*mod_ptr).Clean();
if ((modPrep && modClean) !=0){
cerr << "Error in " << (*mod_ptr) << " when preparing and cleaning." << endl;
}
}
Services services = Services();
for (Service service : services.GetServices()){
deployService(service.GetUsername());
}
//for each service deploy service
return 0;
}
int removeAll()
{
cout << "removing all"<<endl;
Services services = Services();
for (Service service : services.GetServices()){
deployService(service.GetUsername());
}
for(Module * mod_ptr : modules){
int modClean = (*mod_ptr).Clean();
if ((modClean) !=0){
cerr << "Error in " << (*mod_ptr) << " when preparing and cleaning." << endl;
}
}
return 0;
}
int removeService(string serviceUsername)
{
cout<< "removing service"<<endl;
if (isServiceOnServer(serviceUsername)==0){
for(Module * mod_ptr : modules){
int modResult = (*mod_ptr).Remove(serviceUsername);
if (modResult!=0){
cerr << "Error in " << (*mod_ptr) << " when removing " << serviceUsername << endl;
}
}
removeEnvService();
}
return 0;
}

View File

@ -1,4 +1,6 @@
FROM ubuntu:22.04
WORKDIR /usr/src/deployer_test
RUN mkdir -p etc/nginx /etc/nginx/sites-enabled /etc/nginx/new-sites-enabled /data/dnscerts.jean-cloud.org/certs/live/ /etc/letsencrypt/live
RUN touch /etc/letsencrypt/live/dummy
CMD ["sh"]