test bash qui fonctionne!
This commit is contained in:
parent
55ae13e45c
commit
6f1ec7f3d1
@ -12,11 +12,12 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//destructor inline
|
//constructor
|
||||||
BashModule::BashModule()
|
BashModule::BashModule()
|
||||||
{
|
{
|
||||||
name="Bash";
|
name="Bash";
|
||||||
}
|
}
|
||||||
|
//destructor inline
|
||||||
|
|
||||||
//private methods
|
//private methods
|
||||||
int BashModule::executeScript(string serviceUsername, string script)
|
int BashModule::executeScript(string serviceUsername, string script)
|
||||||
@ -51,10 +52,7 @@ int BashModule::executeScript(string serviceUsername, string script)
|
|||||||
return status;
|
return status;
|
||||||
} else {
|
} else {
|
||||||
//child process
|
//child process
|
||||||
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", "--", script.c_str(), (char *)0)==-1)
|
execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", "--", script.c_str(), (char *)0);
|
||||||
{
|
|
||||||
cerr << "Error in the execl call of " << script << endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,6 +92,7 @@ int BashModule::executeScriptAs(string serviceUsername, string script)
|
|||||||
return -1;
|
return -1;
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
//parent process
|
//parent process
|
||||||
|
cout << "in parent process" << endl;
|
||||||
int status;
|
int status;
|
||||||
waitpid(-1,&status,0);
|
waitpid(-1,&status,0);
|
||||||
if(status==-1){
|
if(status==-1){
|
||||||
@ -105,10 +104,7 @@ int BashModule::executeScriptAs(string serviceUsername, string script)
|
|||||||
//executing as the user corresponding to the service
|
//executing as the user corresponding to the service
|
||||||
setgid(p->pw_gid);
|
setgid(p->pw_gid);
|
||||||
setuid(p->pw_uid);
|
setuid(p->pw_uid);
|
||||||
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", "--",script.c_str(), serviceUsername, (char *)0)==-1)
|
execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", "--",script.c_str(), serviceUsername, (char *)0);
|
||||||
{
|
|
||||||
cerr << "Error in the execl call of " << script << endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,7 +117,7 @@ int BashModule::Prepare()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BashModule::Deploy (string serviceUsername)
|
int BashModule::Deploy(string serviceUsername)
|
||||||
{
|
{
|
||||||
string deployScript="./services/"+serviceUsername+"/deploy.sh"; //to be executed as current user
|
string deployScript="./services/"+serviceUsername+"/deploy.sh"; //to be executed as current user
|
||||||
string deployAsScript="./services/"+serviceUsername+"/deploy_user.sh"; //to be executed as the user corresponding to the service
|
string deployAsScript="./services/"+serviceUsername+"/deploy_user.sh"; //to be executed as the user corresponding to the service
|
||||||
|
@ -12,10 +12,10 @@ class DockerModule : public Module
|
|||||||
public:
|
public:
|
||||||
DockerModule();
|
DockerModule();
|
||||||
~DockerModule(){} //inline
|
~DockerModule(){} //inline
|
||||||
int Prepare ();
|
int Prepare();
|
||||||
int Deploy (string serviceUsername);
|
int Deploy(string serviceUsername);
|
||||||
int Remove(string serviceUsername);
|
int Remove(string serviceUsername);
|
||||||
int Clean ();
|
int Clean();
|
||||||
private:
|
private:
|
||||||
int removeContainersCreatedByDockerCompose();
|
int removeContainersCreatedByDockerCompose();
|
||||||
int removeContainersWithServiceName (string serviceUsename);
|
int removeContainersWithServiceName (string serviceUsename);
|
||||||
|
@ -2,12 +2,15 @@
|
|||||||
// Copyright (C) 2024 Jean-Cloud
|
// Copyright (C) 2024 Jean-Cloud
|
||||||
// GNU General Public License v3
|
// GNU General Public License v3
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include "NginxModule.h"
|
#include "NginxModule.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
namespace fs=filesystem;
|
||||||
|
|
||||||
//constructor
|
//constructor
|
||||||
NginxModule::NginxModule(){
|
NginxModule::NginxModule(){
|
||||||
name="Let's Encrypt";
|
name="Nginx";
|
||||||
}
|
}
|
||||||
|
|
||||||
//destructor inline
|
//destructor inline
|
||||||
@ -15,11 +18,48 @@ NginxModule::NginxModule(){
|
|||||||
//public methods
|
//public methods
|
||||||
int NginxModule::Prepare ()
|
int NginxModule::Prepare ()
|
||||||
{
|
{
|
||||||
|
cout << "nginx preparation" << endl;
|
||||||
|
//create proxy_dir
|
||||||
|
string proxy_dir=getenv("proxy_dir");
|
||||||
|
fs::create_directories(proxy_dir);
|
||||||
|
|
||||||
|
//delete current new_conf directory (to start from scratch)
|
||||||
|
string new_nginx_conf_path=getenv("new_nginx_conf_path");
|
||||||
|
fs::remove_all(new_nginx_conf_path);
|
||||||
|
|
||||||
|
//create new new_conf directory
|
||||||
|
fs::create_directories(new_nginx_conf_path);
|
||||||
|
|
||||||
|
//create new conf file (for tests purposes)
|
||||||
|
string nginx_conf_path=getenv("nginx_conf_path");
|
||||||
|
//open the old conf file
|
||||||
|
ifstream input("./services/_proxy/nginx.conf");
|
||||||
|
if (!input) {
|
||||||
|
std::cerr << "Error when opening /services/_proxy/nginx.conf" << std::endl;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
stringstream buffer;
|
||||||
|
buffer << input.rdbuf();
|
||||||
|
string content = buffer.str();
|
||||||
|
input.close();
|
||||||
|
//replace nginx_conf_path with the new nginx_conf_path in the conf
|
||||||
|
size_t pos=string::npos;
|
||||||
|
while (content.find(nginx_conf_path)!=string::npos){
|
||||||
|
content.replace(content.find(nginx_conf_path),nginx_conf_path.length(),new_nginx_conf_path);
|
||||||
|
}
|
||||||
|
//fill the new conf file
|
||||||
|
ofstream output (proxy_dir+"/new_nginx.conf");
|
||||||
|
output << content;
|
||||||
|
output.close();
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int NginxModule::Deploy (string serviceUsername)
|
int NginxModule::Deploy (string serviceUsername)
|
||||||
{
|
{
|
||||||
|
//write the new conf
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,5 +70,6 @@ int NginxModule::Remove(string serviceUsername)
|
|||||||
|
|
||||||
int NginxModule::Clean ()
|
int NginxModule::Clean ()
|
||||||
{
|
{
|
||||||
|
cout << "nginx cleaning" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
21
src/main.cpp
21
src/main.cpp
@ -28,10 +28,10 @@ int isServiceOnServer(string serviceUsername)
|
|||||||
string cmd ="getent hosts " +serviceUsername;
|
string cmd ="getent hosts " +serviceUsername;
|
||||||
string result = BashManager::ExecuteAndReadResult(cmd);
|
string result = BashManager::ExecuteAndReadResult(cmd);
|
||||||
if(result.find("::1")!=string::npos){ //if result contains "::1" which is the notation for loopback in IpV6
|
if(result.find("::1")!=string::npos){ //if result contains "::1" which is the notation for loopback in IpV6
|
||||||
cout << "service on server" << endl;
|
cout << "service " << serviceUsername << " on server" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cout << "service not on server" << endl;
|
cout << "service" << serviceUsername << " not on server" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -99,7 +99,6 @@ string findCertificate(string serviceUsername)
|
|||||||
string dns_certs_path=getenv("dns_certs_path"); //dns_certs_path is an environment variable
|
string dns_certs_path=getenv("dns_certs_path"); //dns_certs_path is an environment variable
|
||||||
//finding the serviceUsername* directory
|
//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);
|
string name = BashManager::ExecuteAndReadResult(cmd);
|
||||||
cout << "result 1: " << name << endl;
|
cout << "result 1: " << name << endl;
|
||||||
if (!name.empty()){
|
if (!name.empty()){
|
||||||
@ -141,12 +140,12 @@ int createEnvService(string serviceUsername)
|
|||||||
string docker_dir="/services/"+serviceUsername;
|
string docker_dir="/services/"+serviceUsername;
|
||||||
string jc_service=serviceUsername;
|
string jc_service=serviceUsername;
|
||||||
string home="/data/"+serviceUsername;
|
string home="/data/"+serviceUsername;
|
||||||
string jc_id=to_string(services.FindByUsername(serviceUsername)->GetUserID());
|
string jc_id=to_string(services.FindByUsername(serviceUsername)->GetUserID()+stoi(getenv("services_uid_start")));
|
||||||
string net="172.29."+jc_id;
|
string net="172.29."+jc_id;
|
||||||
string jc_cert=findCertificate(serviceUsername);
|
string jc_cert=findCertificate(serviceUsername);
|
||||||
|
|
||||||
//create a .env file accessible outside the C++ program
|
//create a .env file accessible outside the C++ program
|
||||||
string file = "/services/"+serviceUsername+"/.env";
|
string file = "./services/"+serviceUsername+"/.env";
|
||||||
ofstream outfile(file);
|
ofstream outfile(file);
|
||||||
outfile << "http_dir=" << http_dir << endl;
|
outfile << "http_dir=" << http_dir << endl;
|
||||||
outfile << "data_dir=" << data_dir << endl;
|
outfile << "data_dir=" << data_dir << endl;
|
||||||
@ -173,14 +172,12 @@ int createEnvService(string serviceUsername)
|
|||||||
//create the directories
|
//create the directories
|
||||||
//data_dir
|
//data_dir
|
||||||
fs::create_directories(data_dir);
|
fs::create_directories(data_dir);
|
||||||
if (chown(data_dir.c_str(), (unsigned int)stoi(jc_id),(unsigned int)stoi(jc_id)) != 0) {
|
string cmd="chown "+ jc_id +" "+data_dir;
|
||||||
cerr << "Error changing ownership of" << data_dir << endl;
|
BashManager::Execute(cmd);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fs::permissions(data_dir,fs::perms::owner_all|fs::perms::group_read|fs::perms::group_exec|fs::perms::others_exec,fs::perm_options::replace);
|
fs::permissions(data_dir,fs::perms::owner_all|fs::perms::group_read|fs::perms::group_exec|fs::perms::others_exec,fs::perm_options::replace);
|
||||||
//http_dir
|
//http_dir
|
||||||
fs::create_directories(http_dir);
|
fs::create_directories(http_dir);
|
||||||
string cmd="chown "+ jc_id +":www-data -R "+http_dir;
|
cmd="chown "+ jc_id +":www-data -R "+http_dir;
|
||||||
BashManager::Execute(cmd);
|
BashManager::Execute(cmd);
|
||||||
//secret_dir
|
//secret_dir
|
||||||
fs::create_directories(secret_dir);
|
fs::create_directories(secret_dir);
|
||||||
@ -189,8 +186,8 @@ int createEnvService(string serviceUsername)
|
|||||||
return -1;
|
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);
|
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;
|
cout << "service " << serviceUsername << " environment created" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int removeEnvService()
|
int removeEnvService()
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// GNU General Public License v3
|
// GNU General Public License v3
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include "BashManager.h"
|
#include "BashManager.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -13,14 +14,65 @@ void Help()
|
|||||||
}
|
}
|
||||||
|
|
||||||
//tests
|
//tests
|
||||||
void environmentTest()
|
|
||||||
{
|
|
||||||
cout << "Test of the environment setup." << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bashTest()
|
void bashTest()
|
||||||
{
|
{
|
||||||
cout << "Test of BashModule" << endl;
|
//test of the execution of deploy.sh
|
||||||
|
int res1=-1;
|
||||||
|
ifstream deployResult("deployResult");
|
||||||
|
if (!deployResult){
|
||||||
|
cout << "The deployResult file cannot be opened. The Bash module must have failed at executing deploy.sh." << endl;
|
||||||
|
} else {
|
||||||
|
string user; //deploy.sh writes in deployResult the result of the whoami command
|
||||||
|
getline(deployResult,user);
|
||||||
|
if (user!="root"){
|
||||||
|
cout << "The deploy.sh has not been executed with the right user." << endl;
|
||||||
|
} else {
|
||||||
|
res1=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//test of the execution of deploy_user.sh
|
||||||
|
//deploy_user.sh of test.sh8s.sh creates deployAsResult in the data_dir as the user associated with the service
|
||||||
|
int res2=-1;
|
||||||
|
//extract the data_dir path from the .env file
|
||||||
|
ifstream env ("services/test.sh8s.sh/.env");
|
||||||
|
if (!env){
|
||||||
|
cout << ".env file non-existent. The deployer must have failed to create it." << endl;
|
||||||
|
} else {
|
||||||
|
string line;
|
||||||
|
string data_dir;
|
||||||
|
size_t pos = string::npos;
|
||||||
|
while (getline (env, line)){
|
||||||
|
pos=line.find("data_dir=");
|
||||||
|
if (pos!=string::npos){
|
||||||
|
data_dir=line.substr(pos+9);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (data_dir.empty()){
|
||||||
|
cout << "http_dir variable not defined in the .env. The deployer must have failed to initialize it." << endl;
|
||||||
|
} else {
|
||||||
|
//read the deployAsResult file
|
||||||
|
string path=data_dir+"/deployAsResult";
|
||||||
|
ifstream deployAsResult(path);
|
||||||
|
if (!deployAsResult){
|
||||||
|
cout << "The deployAsResult file cannot be opened. The Bash module must have failed at executing deploy_user.sh." << endl;
|
||||||
|
} else {
|
||||||
|
string user; //deploy.sh writes in deployAsResult the result of the whoami command
|
||||||
|
getline(deployAsResult,user);
|
||||||
|
if (user!="test.sh8s.sh"){
|
||||||
|
cout << "The deploy.sh has not been executed with the right user." << endl;
|
||||||
|
} else {
|
||||||
|
res2=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int res=res1+res2;
|
||||||
|
if (res==0){
|
||||||
|
cout << "Test of BashModule: OK" << endl;
|
||||||
|
} else {
|
||||||
|
cout << "Test of BashModule: FAIL" << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dockerTest()
|
void dockerTest()
|
||||||
@ -48,11 +100,9 @@ int main(int argc, char **argv)
|
|||||||
if(argc!=2){
|
if(argc!=2){
|
||||||
cerr << "Invalid number of arguments." << endl;
|
cerr << "Invalid number of arguments." << endl;
|
||||||
} else{
|
} else{
|
||||||
BashManager::Execute("./deployer deploy all");
|
BashManager::Execute("./deployer deploy test.sh8s.sh");
|
||||||
string action=argv[1];
|
string action=argv[1];
|
||||||
if (action=="environment"){
|
if (action=="bash"){
|
||||||
environmentTest();
|
|
||||||
} else if (action=="bash"){
|
|
||||||
bashTest();
|
bashTest();
|
||||||
} else if (action=="docker"){
|
} else if (action=="docker"){
|
||||||
dockerTest();
|
dockerTest();
|
||||||
@ -63,7 +113,6 @@ int main(int argc, char **argv)
|
|||||||
} else if (action=="encryption"){
|
} else if (action=="encryption"){
|
||||||
encryptionTest();
|
encryptionTest();
|
||||||
} else if (action=="all"){
|
} else if (action=="all"){
|
||||||
environmentTest();
|
|
||||||
bashTest();
|
bashTest();
|
||||||
dockerTest();
|
dockerTest();
|
||||||
nginxTest();
|
nginxTest();
|
||||||
|
1
testenv/services/_proxy/nginx
Normal file
1
testenv/services/_proxy/nginx
Normal file
@ -0,0 +1 @@
|
|||||||
|
example
|
1
testenv/services/_proxy/nginx.conf
Normal file
1
testenv/services/_proxy/nginx.conf
Normal file
@ -0,0 +1 @@
|
|||||||
|
example
|
@ -1,5 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
touch $http_dir/deployAsResult
|
touch $data_dir/deployAsResult
|
||||||
echo $(whoami) >> $http_dir/deployResult
|
echo $(whoami) >> $data_dir/deployAsResult
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user