Skip to content

Commit a5f4f5a

Browse files
authored
Merge pull request #440 from foundriesio/fetch-apps-thru-proxy
appengine: Fetch apps through proxy if set
2 parents 067a72f + 2d7a8d2 commit a5f4f5a

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

src/composeapp/appengine.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@ static bool isNullOrEmptyOrUnset(const Json::Value& val, const std::string& fiel
1414

1515
AppEngine::Result AppEngine::fetch(const App& app) {
1616
Result res{false};
17+
bool was_proxy_set{false};
1718
try {
1819
// If a given app was fetched before, then don't consider it as a fetched app if a caller tries to fetch it again
1920
// for one reason or another - hence remove it from the set of fetched apps.
2021
fetched_apps_.erase(app.uri);
2122
if (local_source_path_.empty()) {
23+
if (proxy_) {
24+
// If the proxy provider is set, then obtain the proxy URL and CA from it,
25+
// and set the corresponding environment variables for `composectl`.
26+
const auto proxy{proxy_()};
27+
if (!proxy.first.empty()) {
28+
::setenv("COMPOSE_APPS_PROXY", proxy.first.c_str(), 1);
29+
::setenv("COMPOSE_APPS_PROXY_CA", proxy.second.c_str(), 1);
30+
was_proxy_set = true;
31+
}
32+
}
2233
exec(boost::format{"%s --store %s pull -p %s --storage-usage-watermark %d"} % composectl_cmd_ % storeRoot() %
2334
app.uri % storage_watermark_,
2435
"failed to pull compose app", "", nullptr, "4h", true);
@@ -40,6 +51,10 @@ AppEngine::Result AppEngine::fetch(const App& app) {
4051
} catch (const std::exception& exc) {
4152
res = {false, exc.what()};
4253
}
54+
if (was_proxy_set) {
55+
::unsetenv("COMPOSE_APPS_PROXY");
56+
::unsetenv("COMPOSE_APPS_PROXY_CA");
57+
}
4358
return res;
4459
}
4560

src/composeapp/appengine.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace composeapp {
77

8+
using ProxyProvider = std::function<std::pair<std::string, std::string>()>;
9+
810
class AppEngine : public Docker::RestorableAppEngine {
911
public:
1012
AppEngine(boost::filesystem::path store_root, boost::filesystem::path install_root,
@@ -14,14 +16,15 @@ class AppEngine : public Docker::RestorableAppEngine {
1416
int storage_watermark = 80,
1517
StorageSpaceFunc storage_space_func = RestorableAppEngine::GetDefStorageSpaceFunc(),
1618
ClientImageSrcFunc client_image_src_func = nullptr, bool create_containers_if_install = true,
17-
const std::string& local_source_path = "")
19+
const std::string& local_source_path = "", ProxyProvider proxy = nullptr)
1820
: Docker::RestorableAppEngine(
1921
std::move(store_root), std::move(install_root), std::move(docker_root), std::move(registry_client),
2022
std::move(docker_client), "", std::move(docker_host), std::move(compose_cmd), std::move(storage_space_func),
2123
std::move(client_image_src_func), create_containers_if_install, !local_source_path.empty()),
2224
composectl_cmd_{std::move(composectl_cmd)},
2325
storage_watermark_{storage_watermark},
24-
local_source_path_{local_source_path} {}
26+
local_source_path_{local_source_path},
27+
proxy_{proxy} {}
2528

2629
Result fetch(const App& app) override;
2730
void remove(const App& app) override;
@@ -37,6 +40,7 @@ class AppEngine : public Docker::RestorableAppEngine {
3740
const std::string composectl_cmd_;
3841
const int storage_watermark_;
3942
const std::string local_source_path_;
43+
ProxyProvider proxy_;
4044
mutable std::set<std::string> fetched_apps_;
4145
};
4246

src/composeappmanager.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ ComposeAppManager::Config::Config(const PackageConfig& pconfig) {
6363
if (raw.count("composectl_bin") == 1) {
6464
composectl_bin = raw.at("composectl_bin");
6565
}
66+
if (raw.count("compose_apps_proxy") == 1) {
67+
apps_proxy = raw.at("compose_apps_proxy");
68+
}
69+
if (raw.count("compose_apps_proxy_ca") == 1) {
70+
apps_proxy_ca = raw.at("compose_apps_proxy_ca");
71+
}
6672
#endif // USE_COMPOSEAPP_ENGINE
6773

6874
if (raw.count("docker_prune") == 1) {
@@ -145,10 +151,24 @@ ComposeAppManager::ComposeAppManager(const PackageConfig& pconfig, const Bootloa
145151
}
146152
#ifdef USE_COMPOSEAPP_ENGINE
147153
const auto composectl_cmd{boost::filesystem::canonical(cfg_.composectl_bin).string()};
154+
composeapp::ProxyProvider proxy{nullptr};
155+
if (!cfg_.apps_proxy.empty()) {
156+
proxy = [this]() {
157+
std::string proxy_url;
158+
const auto& proxyUrlResp{http_->post(cfg_.apps_proxy, "")};
159+
if (proxyUrlResp.isOk()) {
160+
proxy_url = proxyUrlResp.body;
161+
LOG_DEBUG << "Got app proxy URL: " << proxy_url;
162+
} else {
163+
LOG_ERROR << "Failed to obtain proxy URL: " << proxyUrlResp.getStatusStr();
164+
}
165+
return std::make_pair(proxy_url, cfg_.apps_proxy_ca);
166+
};
167+
}
148168
app_engine_ = std::make_shared<composeapp::AppEngine>(
149169
cfg_.reset_apps_root, cfg_.apps_root, cfg_.images_data_root, registry_client,
150170
std::make_shared<Docker::DockerClient>(), docker_host, compose_cmd, composectl_cmd, cfg_.storage_watermark,
151-
Docker::RestorableAppEngine::GetDefStorageSpaceFunc(cfg_.storage_watermark));
171+
Docker::RestorableAppEngine::GetDefStorageSpaceFunc(cfg_.storage_watermark), nullptr, true, "", proxy);
152172
#else
153173
const std::string skopeo_cmd{boost::filesystem::canonical(cfg_.skopeo_bin).string()};
154174
app_engine_ = std::make_shared<Docker::RestorableAppEngine>(

src/composeappmanager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class ComposeAppManager : public RootfsTreeManager {
2727
boost::filesystem::path skopeo_bin{"/sbin/skopeo"};
2828
#ifdef USE_COMPOSEAPP_ENGINE
2929
boost::filesystem::path composectl_bin{"/usr/bin/composectl"};
30+
std::string apps_proxy;
31+
std::string apps_proxy_ca;
3032
#endif // USE_COMPOSEAPP_ENGINE
3133
bool docker_prune{true};
3234
bool force_update{false};

src/liteclient.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ LiteClient::LiteClient(Config config_in, const AppEngine::Ptr& app_engine, const
149149
// Enforce the restorable app engine usage
150150
config.pacman.extra["reset_apps"] = "";
151151
}
152+
config.pacman.extra["compose_apps_proxy_ca"] = config.storage.tls_cacert_path.get(config.storage.path).string();
152153
basepacman = std::make_shared<ComposeAppManager>(config.pacman, config.bootloader, storage, http_client,
153154
ostree_sysroot, *key_manager_, app_engine);
154155
} else if (config.pacman.type == RootfsTreeManager::Name) {

0 commit comments

Comments
 (0)