Add ALD Transition Console preview and Astra lab build workflow
This commit is contained in:
1 parent
f330a3c00a
commit
e5c6a2fc0e
199 files changed
+9866
No files matched your search
@@ -0,0 +1,45 @@
|
||||
#include "OperationRegistry.h"
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <stdexcept>
|
||||
namespace ald {
|
||||
static QByteArray readAsset(const QString &path){QFile f(path);if(!f.open(QIODevice::ReadOnly))throw std::runtime_error("Missing immutable asset");return f.readAll();}
|
||||
QStringList OperationRegistry::ids(){return {"local.os","local.kernel","local.packages","local.services","local.addresses","local.routes","local.time","local.hostname","local.resolvers","local.yama","local.machine","local.ssh_version"};}
|
||||
ProcessSpec OperationRegistry::resolve(const QString &id,const QJsonObject ¶ms) {
|
||||
if(!params.isEmpty())throw std::runtime_error("Unknown parameters: operation takes no input");
|
||||
ProcessSpec s;s.operationId=id;
|
||||
if(id=="local.os"){s.program="/usr/bin/cat";s.arguments=QStringList{"/etc/os-release"};}
|
||||
else if(id=="local.kernel"){s.program="/usr/bin/uname";s.arguments=QStringList{"-r","-m"};}
|
||||
else if(id=="local.packages"){s.program="/usr/bin/dpkg-query";s.arguments=QStringList{"-W","-f=${binary:Package}\t${Version}\t${db:Status-Status}\n"};}
|
||||
else if(id=="local.services"){s.program="/usr/bin/systemctl";s.arguments=QStringList{"show","--no-pager","--property=Id,LoadState,ActiveState,SubState","ipa.service","krb5-kdc.service","bind9.service","sssd.service","aldpro-mp-services.service"};}
|
||||
else if(id=="local.addresses"){s.program="/usr/sbin/ip";s.arguments=QStringList{"-j","address","show"};}
|
||||
else if(id=="local.routes"){s.program="/usr/sbin/ip";s.arguments=QStringList{"-j","route","show"};}
|
||||
else if(id=="local.time"){s.program="/usr/bin/timedatectl";s.arguments=QStringList{"show","--property=NTPSynchronized,Timezone,TimeUSec"};}
|
||||
else if(id=="local.hostname"){s.program="/usr/bin/hostname";s.arguments=QStringList{"-f"};}
|
||||
else if(id=="local.resolvers"){s.program="/usr/bin/cat";s.arguments=QStringList{"/etc/resolv.conf"};}
|
||||
else if(id=="local.yama"){s.program="/usr/bin/cat";s.arguments=QStringList{"/proc/sys/kernel/yama/ptrace_scope"};}
|
||||
else if(id=="local.machine"){s.program="/usr/bin/cat";s.arguments=QStringList{"/etc/machine-id"};s.suppressOutput=true;}
|
||||
else if(id=="local.ssh_version"){s.program="/usr/bin/ssh";s.arguments=QStringList{"-V"};}
|
||||
else throw std::runtime_error("Unknown or unavailable executable operation ID");
|
||||
return s;
|
||||
}
|
||||
void OperationRegistry::verifyAssets() {
|
||||
const auto assets=QJsonDocument::fromJson(readAsset(":/asset-manifest.json")).object();
|
||||
if(assets.value("schema_version").toInt()!=1)throw std::runtime_error("Unsupported asset manifest");
|
||||
for(const auto &v:assets.value("assets").toArray()){auto o=v.toObject();if(QCryptographicHash::hash(readAsset(":/"+o.value("path").toString()),QCryptographicHash::Sha256).toHex()!=o.value("sha256").toString().toLatin1())throw std::runtime_error("Installed asset integrity failed");}
|
||||
const auto manifest=QJsonDocument::fromJson(readAsset(":/tasks/manifest.json")).object();
|
||||
if(manifest.value("schema_version").toInt()!=1)throw std::runtime_error("Unsupported task manifest");
|
||||
for(const auto &v:manifest.value("tasks").toArray()) {
|
||||
const auto o=v.toObject();const auto bytes=readAsset(":/tasks/"+o.value("path").toString());
|
||||
if(QCryptographicHash::hash(bytes,QCryptographicHash::Sha256).toHex()!=o.value("sha256").toString().toLatin1())throw std::runtime_error("Task integrity verification failed");
|
||||
}
|
||||
}
|
||||
QByteArray OperationRegistry::task(const QString &id) {
|
||||
verifyAssets();
|
||||
if(id=="windows.discovery")return readAsset(":/tasks/windows/discovery.ps1");
|
||||
if(id=="ad.inventory")return readAsset(":/tasks/windows/ad-inventory.ps1");
|
||||
throw std::runtime_error("Unknown fixed task");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <QByteArray>
|
||||
#include <QJsonObject>
|
||||
#include <QStringList>
|
||||
namespace ald {
|
||||
struct ProcessSpec {
|
||||
QString operationId,program;QStringList arguments;
|
||||
int timeoutMs = 15000;qsizetype outputLimit = 4*1024*1024;
|
||||
bool readOnly = true, remote = false, suppressOutput = false;
|
||||
QByteArray input;
|
||||
};
|
||||
class OperationRegistry {
|
||||
public:
|
||||
static ProcessSpec resolve(const QString &operation,const QJsonObject ¶meters = {});
|
||||
static QStringList ids();
|
||||
static QByteArray task(const QString &id);
|
||||
static void verifyAssets();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "PlanExecutor.h"
|
||||
#include <QTimer>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
namespace ald {
|
||||
PlanExecutor::PlanExecutor(QObject *parent):QObject(parent),runner_(this){connect(&runner_,&ProcessRunner::finished,this,&PlanExecutor::onResult);connect(&runner_,&ProcessRunner::output,this,&PlanExecutor::output);}
|
||||
void PlanExecutor::start(const std::vector<rules::Step> &steps,const QMap<QString,ProcessSpec> &specs,const QMap<QString,Verifier> &verifiers,rules::ErrorPolicy policy) {
|
||||
if(running_)throw std::runtime_error("Run already active");
|
||||
auto order=rules::topological(steps);
|
||||
for(const auto &s:steps) {auto id=QString::fromStdString(s.id);if(s.mutating||!specs.contains(id)||!specs[id].readOnly||!verifiers.contains(id))throw std::runtime_error("Unvalidated step or missing postcondition verifier");}
|
||||
steps_=steps;order_=order;specs_=specs;verifiers_=verifiers;policy_=policy;results_.clear();index_=0;running_=true;stopping_=paused_=pauseRequested_=hasGaps_=false;
|
||||
QTimer::singleShot(0,this,&PlanExecutor::next);
|
||||
}
|
||||
void PlanExecutor::next() {
|
||||
if(!running_)return;
|
||||
if(stopping_||index_==order_.size()){running_=false;paused_=false;emit finished(!hasGaps_&&!stopping_);return;}
|
||||
if(pauseRequested_){paused_=true;emit pausedForDecision();return;}
|
||||
current_=QString::fromStdString(order_[index_++]);const auto id=current_.toStdString();
|
||||
auto step=std::find_if(steps_.begin(),steps_.end(),[&](const rules::Step&s){return s.id==id;});
|
||||
if(!rules::dependenciesPassed(*step,results_)){results_[id]=rules::Result::NotChecked;hasGaps_=true;emit blocked(current_,"Предыдущая зависимая операция не подтверждена");QTimer::singleShot(0,this,&PlanExecutor::next);return;}
|
||||
emit stepStarted(current_);
|
||||
// A journal/authorization observer may refuse scheduling at this boundary.
|
||||
if(stopping_){QTimer::singleShot(0,this,&PlanExecutor::next);return;}
|
||||
try{runner_.start(specs_.value(current_));}catch(const std::exception&){ProcessResult r;r.operationId=specs_.value(current_).operationId;r.failedToStart=true;onResult(r);}
|
||||
}
|
||||
void PlanExecutor::onResult(const ProcessResult &p) {
|
||||
rules::Result post=rules::Result::Error;
|
||||
if(!p.cancelled&&!p.outputExceeded&&!p.timedOut&&!p.failedToStart){try{post=verifiers_.value(current_)(p);}catch(...){post=rules::Result::Error;}}
|
||||
const auto result=rules::reconcile(p.exitCode,p.normalExit,post,p.remote&&(p.exitCode==255||p.timedOut),false);
|
||||
results_[current_.toStdString()]=result;hasGaps_|=result!=rules::Result::Pass;
|
||||
emit stepFinished(current_,int(result),p);
|
||||
auto action=rules::afterResult(result,policy_);if(action==rules::BatchDecision::Stop)stopping_=true;else if(action==rules::BatchDecision::Pause)pauseRequested_=true;
|
||||
QTimer::singleShot(0,this,&PlanExecutor::next);
|
||||
}
|
||||
void PlanExecutor::stop(){stopping_=true;pauseRequested_=paused_=false;if(runner_.active())runner_.cancel();else QTimer::singleShot(0,this,&PlanExecutor::next);}
|
||||
void PlanExecutor::pause(){if(running_)pauseRequested_=true;}
|
||||
void PlanExecutor::resume(){if(!paused_)return;paused_=pauseRequested_=false;QTimer::singleShot(0,this,&PlanExecutor::next);}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "ProcessRunner.h"
|
||||
#include "core/Rules.h"
|
||||
#include <QMap>
|
||||
#include <functional>
|
||||
namespace ald {
|
||||
class PlanExecutor : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using Verifier=std::function<rules::Result(const ProcessResult&)>;
|
||||
explicit PlanExecutor(QObject *parent=nullptr);
|
||||
void start(const std::vector<rules::Step> &steps,const QMap<QString,ProcessSpec> &specs,
|
||||
const QMap<QString,Verifier> &verifiers,rules::ErrorPolicy policy);
|
||||
void stop();void pause();void resume();bool active()const{return running_;}bool paused()const{return paused_;}
|
||||
signals:
|
||||
void stepStarted(const QString &id);
|
||||
void stepFinished(const QString &id,int result,const ald::ProcessResult &process);
|
||||
void blocked(const QString &id,const QString &reason);
|
||||
void output(const QString &stream,const QString &redacted);
|
||||
void pausedForDecision();void finished(bool complete);
|
||||
private:
|
||||
void next();void onResult(const ProcessResult &result);
|
||||
ProcessRunner runner_;std::vector<rules::Step> steps_;std::vector<std::string> order_;
|
||||
QMap<QString,ProcessSpec> specs_;QMap<QString,Verifier> verifiers_;
|
||||
std::map<std::string,rules::Result> results_;rules::ErrorPolicy policy_=rules::ErrorPolicy::Stop;
|
||||
std::size_t index_=0;QString current_;bool running_=false,stopping_=false,paused_=false,pauseRequested_=false,hasGaps_=false;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "ProcessRunner.h"
|
||||
#include <QProcessEnvironment>
|
||||
#include <stdexcept>
|
||||
namespace ald {
|
||||
ProcessRunner::ProcessRunner(QObject *parent):QObject(parent) {
|
||||
process_.setProcessChannelMode(QProcess::SeparateChannels);timeout_.setSingleShot(true);killTimer_.setSingleShot(true);
|
||||
connect(&process_,&QProcess::readyReadStandardOutput,this,[this]{drain(false);});
|
||||
connect(&process_,&QProcess::readyReadStandardError,this,[this]{drain(true);});
|
||||
connect(&process_,QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished),this,&ProcessRunner::complete);
|
||||
connect(&process_,&QProcess::errorOccurred,this,[this](QProcess::ProcessError e){if(e==QProcess::FailedToStart){result_.failedToStart=true;complete(-1,QProcess::CrashExit);}});
|
||||
connect(&timeout_,&QTimer::timeout,this,[this]{result_.timedOut=true;stopReadOnly();});
|
||||
connect(&killTimer_,&QTimer::timeout,this,[this]{if(active_&&spec_.readOnly)process_.kill();});
|
||||
connect(&process_,&QProcess::started,this,[this]{if(!spec_.input.isEmpty())process_.write(spec_.input);process_.closeWriteChannel();});
|
||||
}
|
||||
void ProcessRunner::start(const ProcessSpec &spec,const QStringList &secrets) {
|
||||
if(active_)throw std::runtime_error("Operation is already active");
|
||||
// This release has no validated automated mutation adapter. Deny at the final
|
||||
// process boundary, even if a crafted plan bypasses UI or capability checks.
|
||||
if(!spec.readOnly)throw std::runtime_error("Mutating adapter is not laboratory validated");
|
||||
if(!spec.program.startsWith('/')||spec.timeoutMs<100||spec.timeoutMs>300000||spec.outputLimit<1024||spec.outputLimit>16*1024*1024)throw std::runtime_error("Invalid process contract");
|
||||
spec_=spec;result_=ProcessResult{};result_.operationId=spec.operationId;result_.remote=spec.remote;
|
||||
stdoutRedactor_=std::make_unique<SecretRedactor>(secrets);stderrRedactor_=std::make_unique<SecretRedactor>(secrets);
|
||||
QProcessEnvironment env;env.insert("PATH","/usr/sbin:/usr/bin:/sbin:/bin");env.insert("LANG","C.UTF-8");env.insert("LC_ALL","C.UTF-8");
|
||||
// Explicit native credential mechanism: already unlocked session ssh-agent.
|
||||
// Its socket is never logged, exported or forwarded.
|
||||
if(spec.remote&&!qEnvironmentVariableIsEmpty("SSH_AUTH_SOCK"))env.insert("SSH_AUTH_SOCK",qEnvironmentVariable("SSH_AUTH_SOCK"));
|
||||
process_.setProcessEnvironment(env);process_.setProgram(spec.program);process_.setArguments(spec.arguments);
|
||||
process_.setWorkingDirectory("/");active_=true;done_=false;elapsed_.start();timeout_.start(spec.timeoutMs);process_.start();
|
||||
}
|
||||
void ProcessRunner::drain(bool error) {
|
||||
const auto bytes=error?process_.readAllStandardError():process_.readAllStandardOutput();
|
||||
auto &buffer=error?result_.err:result_.out;
|
||||
if(result_.out.size()+result_.err.size()+bytes.size()>spec_.outputLimit){result_.outputExceeded=true;stopReadOnly();return;}
|
||||
buffer+=bytes;
|
||||
const auto text=(error?stderrRedactor_:stdoutRedactor_)->feed(bytes);
|
||||
if(!spec_.suppressOutput&&!text.isEmpty())emit output(error?"stderr":"stdout",text);
|
||||
}
|
||||
void ProcessRunner::complete(int code,QProcess::ExitStatus status) {
|
||||
if(done_)return;drain(false);drain(true);done_=true;timeout_.stop();killTimer_.stop();active_=false;
|
||||
for(bool error:{false,true}){const auto text=(error?stderrRedactor_:stdoutRedactor_)->feed({},true);if(!spec_.suppressOutput&&!text.isEmpty())emit output(error?"stderr":"stdout",text);}
|
||||
result_.exitCode=code;result_.normalExit=status==QProcess::NormalExit;result_.elapsedMs=elapsed_.elapsed();emit finished(result_);
|
||||
}
|
||||
void ProcessRunner::stopReadOnly(){if(active_&&spec_.readOnly){process_.terminate();killTimer_.start(2000);}}
|
||||
void ProcessRunner::cancel(){if(!active_)return;result_.cancelled=true;stopReadOnly();}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "OperationRegistry.h"
|
||||
#include "security/SecretRedactor.h"
|
||||
#include <QElapsedTimer>
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QTimer>
|
||||
namespace ald {
|
||||
struct ProcessResult {
|
||||
QString operationId;int exitCode=-1;bool normalExit=false,timedOut=false,cancelled=false,outputExceeded=false,failedToStart=false,remote=false;
|
||||
QByteArray out,err;qint64 elapsedMs=0;
|
||||
};
|
||||
class ProcessRunner : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ProcessRunner(QObject *parent=nullptr);
|
||||
void start(const ProcessSpec &spec,const QStringList &secrets = {});
|
||||
bool active()const{return active_;}
|
||||
void cancel();
|
||||
signals:
|
||||
void output(const QString &stream,const QString &redacted);
|
||||
void finished(const ald::ProcessResult &result);
|
||||
private:
|
||||
void drain(bool err);void complete(int code,QProcess::ExitStatus status);void stopReadOnly();
|
||||
QProcess process_;QTimer timeout_,killTimer_;QElapsedTimer elapsed_;ProcessSpec spec_;ProcessResult result_;
|
||||
std::unique_ptr<SecretRedactor> stdoutRedactor_,stderrRedactor_;bool active_=false,done_=true;
|
||||
};
|
||||
}
|
||||
Q_DECLARE_METATYPE(ald::ProcessResult)
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "RunJournal.h"
|
||||
#include "domain/Types.h"
|
||||
namespace ald {void RunJournal::write(const QString &id,const QString &state,const QString &operation,const QJsonObject &details){auto o=details;o["timestamp_utc"]=utcNow();o["operation_id"]=operation;repo_.journalRun(id,state,o);}}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "storage/ProjectRepository.h"
|
||||
namespace ald {
|
||||
class RunJournal {
|
||||
public:
|
||||
explicit RunJournal(ProjectRepository &repo):repo_(repo){}
|
||||
void write(const QString &id,const QString &state,const QString &operation,const QJsonObject &details={});
|
||||
private:ProjectRepository &repo_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "ShutdownCoordinator.h"
|
||||
namespace ald {
|
||||
ShutdownCoordinator::ShutdownCoordinator(PlanExecutor &e,QObject *p):QObject(p),executor_(e){connect(&e,&PlanExecutor::finished,this,[this]{if(requested_){requested_=false;emit readyForRestorationSummary();}});}
|
||||
void ShutdownCoordinator::request(){requested_=true;if(executor_.active())executor_.stop();else{requested_=false;emit readyForRestorationSummary();}}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "PlanExecutor.h"
|
||||
namespace ald {
|
||||
class ShutdownCoordinator:public QObject {
|
||||
Q_OBJECT
|
||||
public:explicit ShutdownCoordinator(PlanExecutor &executor,QObject *parent=nullptr);void request();
|
||||
signals:void readyForRestorationSummary();
|
||||
private:PlanExecutor &executor_;bool requested_=false;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user