forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWSLCLocalRegistry.cpp
More file actions
121 lines (90 loc) · 3.33 KB
/
WSLCLocalRegistry.cpp
File metadata and controls
121 lines (90 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WSLCLocalRegistry.cpp
Abstract:
Implementation of WSLCLocalRegistry.
--*/
#include "WSLCLocalRegistry.h"
using wsl::windows::common::RunningWSLCContainer;
using wsl::windows::common::WSLCContainerLauncher;
using wsl::windows::common::WSLCLocalRegistry;
namespace {
constexpr auto c_registryImage = "registry:3";
constexpr auto c_htpasswdImage = "httpd:2";
std::string GenerateHtpasswd(IWSLCSession& session, const std::string& username, const std::string& password)
{
THROW_IF_FAILED(session.PullImage(c_htpasswdImage, nullptr, nullptr));
const auto command = std::format("htpasswd -Bbn '{}' '{}'", username, password);
WSLCContainerLauncher launcher(c_htpasswdImage, {}, {"/bin/sh", "-c", command});
launcher.SetContainerFlags(WSLCContainerFlagsRm);
auto container = launcher.Launch(session);
auto result = container.GetInitProcess().WaitAndCaptureOutput();
THROW_HR_IF_MSG(E_FAIL, result.Code != 0, "%hs", launcher.FormatResult(result).c_str());
auto output = result.Output[1];
output.erase(output.find_last_not_of("\n\r") + 1);
THROW_HR_IF_MSG(E_FAIL, output.empty(), "%hs", launcher.FormatResult(result).c_str());
return output;
}
std::vector<std::string> BuildRegistryEnv(IWSLCSession& session, const std::string& username, const std::string& password)
{
std::vector<std::string> env = {
"REGISTRY_HTTP_ADDR=0.0.0.0:5000",
};
if (!username.empty())
{
auto htpasswdEntry = GenerateHtpasswd(session, username, password);
env.push_back(std::format("HTPASSWD_CONTENT={}", htpasswdEntry));
env.push_back("REGISTRY_AUTH=htpasswd");
env.push_back("REGISTRY_AUTH_HTPASSWD_REALM=WSLC Test Registry");
env.push_back("REGISTRY_AUTH_HTPASSWD_PATH=/htpasswd");
}
return env;
}
} // namespace
WSLCLocalRegistry::WSLCLocalRegistry(
IWSLCSession& session,
RunningWSLCContainer&& container,
std::string&& username,
std::string&& password) :
m_session(wil::com_ptr<IWSLCSession>(&session)),
m_username(std::move(username)),
m_password(std::move(password)),
m_container(std::move(container))
{
}
WSLCLocalRegistry::~WSLCLocalRegistry()
{
// Delete the container first while the session is still active.
m_container.Reset();
}
WSLCLocalRegistry WSLCLocalRegistry::Start(
IWSLCSession& session, const std::string& username, const std::string& password)
{
THROW_IF_FAILED(session.PullImage(c_registryImage, nullptr, nullptr));
auto env = BuildRegistryEnv(session, username, password);
WSLCContainerLauncher launcher(c_registryImage, {}, {}, env);
launcher.AddPort(5000, 5000, AF_INET);
if (!username.empty())
{
launcher.SetEntrypoint({"/bin/sh", "-c", "echo \"$HTPASSWD_CONTENT\" > /htpasswd && registry serve /etc/distribution/config.yml"});
}
auto container = launcher.Launch(session, WSLCContainerStartFlagsNone);
return WSLCLocalRegistry(
session,
std::move(container),
std::string(username),
std::string(password));
}
const char* WSLCLocalRegistry::GetServerAddress() const
{
return "127.0.0.1:5000";
}
const std::string& WSLCLocalRegistry::GetUsername() const
{
return m_username;
}
const std::string& WSLCLocalRegistry::GetPassword() const
{
return m_password;
}