summaryrefslogtreecommitdiff
path: root/src/state/activating_state.cpp
blob: 61927112a595019a47c4903cddffdc83e62b226f (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "activating_state.hpp"

#include "active_state.hpp"

#include <sys/mount.h>

#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/process.hpp>
#include <filesystem>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>

ActivatingState::ActivatingState(interfaces::MountPointStateMachine& machine) :
    BasicStateT(machine){};

std::unique_ptr<BasicState> ActivatingState::onEnter()
{
    // Reset previous exit code
    machine.getExitCode() = -1;

    if (machine.getConfig().mode == Configuration::Mode::proxy)
    {
        return activateProxyMode();
    }
    return activateLegacyMode();
}

std::unique_ptr<BasicState>
    ActivatingState::handleEvent(UdevStateChangeEvent event)
{
    if (event.devState == StateChange::inserted)
    {
        gadget = std::make_unique<resource::Gadget>(machine, event.devState);

        if (gadget)
        {
            return std::make_unique<ActiveState>(machine, std::move(process),
                                                 std::move(gadget));
        }

        return std::make_unique<ReadyState>(machine,
                                            std::errc::device_or_resource_busy,
                                            "Unable to configure gadget");
    }

    return std::make_unique<ReadyState>(
        machine, std::errc::operation_not_supported,
        "Unexpected udev event: " + static_cast<int>(event.devState));
}

std::unique_ptr<BasicState>
    ActivatingState::handleEvent(SubprocessStoppedEvent event)
{
    LogMsg(Logger::Error, "Process ended prematurely");
    return std::make_unique<ReadyState>(machine);
}

std::unique_ptr<BasicState> ActivatingState::activateProxyMode()
{
    process = std::make_unique<resource::Process>(
        machine, std::make_shared<::Process>(
                     machine.getIoc(), machine.getName(),
                     "/usr/sbin/nbd-client", machine.getConfig().nbdDevice));

    if (!process->spawn(Configuration::MountPoint::toArgs(machine.getConfig()),
                        [& machine = machine](int exitCode, bool isReady) {
                            LogMsg(Logger::Info, machine.getName(),
                                   " process ended.");
                            machine.getExitCode() = exitCode;
                            machine.emitSubprocessStoppedEvent();
                        }))
    {
        return std::make_unique<ReadyState>(
            machine, std::errc::operation_canceled, "Failed to spawn process");
    }

    return nullptr;
}

std::unique_ptr<BasicState> ActivatingState::activateLegacyMode()
{
    LogMsg(Logger::Debug, machine.getName(),
           " Mount requested on address: ", machine.getTarget()->imgUrl,
           " ; RW: ", machine.getTarget()->rw);

    if (isCifsUrl(machine.getTarget()->imgUrl))
    {
        return mountSmbShare();
    }
    else if (isHttpsUrl(machine.getTarget()->imgUrl))
    {
        return mountHttpsShare();
    }

    return std::make_unique<ReadyState>(machine, std::errc::invalid_argument,
                                        "URL not recognized");
}

std::unique_ptr<BasicState> ActivatingState::mountSmbShare()
{
    try
    {
        auto mountDir =
            std::make_unique<resource::Directory>(machine.getName());

        SmbShare smb(mountDir->getPath());
        fs::path remote = getImagePath(machine.getTarget()->imgUrl);
        auto remoteParent = "/" + remote.parent_path().string();
        auto localFile = mountDir->getPath() / remote.filename();

        LogMsg(Logger::Debug, machine.getName(), " Remote name: ", remote,
               "\n Remote parent: ", remoteParent,
               "\n Local file: ", localFile);

        machine.getTarget()->mountPoint = std::make_unique<resource::Mount>(
            std::move(mountDir), smb, remoteParent, machine.getTarget()->rw,
            machine.getTarget()->credentials);

        process = spawnNbdKit(machine, localFile);
        if (!process)
        {
            return std::make_unique<ReadyState>(machine,
                                                std::errc::operation_canceled,
                                                "Unable to setup NbdKit");
        }

        return nullptr;
    }
    catch (const resource::Error& e)
    {
        return std::make_unique<ReadyState>(machine, e.errorCode, e.what());
    }
}

std::unique_ptr<BasicState> ActivatingState::mountHttpsShare()
{
    process = spawnNbdKit(machine, machine.getTarget()->imgUrl);
    if (!process)
    {
        return std::make_unique<ReadyState>(machine,
                                            std::errc::invalid_argument,
                                            "Failed to mount HTTPS share");
    }

    return nullptr;
}

std::unique_ptr<resource::Process>
    ActivatingState::spawnNbdKit(interfaces::MountPointStateMachine& machine,
                                 std::unique_ptr<utils::VolatileFile>&& secret,
                                 const std::vector<std::string>& params)
{
    // Investigate
    auto process = std::make_unique<resource::Process>(
        machine, std::make_shared<::Process>(
                     machine.getIoc(), std::string(machine.getName()),
                     "/usr/sbin/nbdkit", machine.getConfig().nbdDevice));

    // Cleanup of previous socket
    if (fs::exists(machine.getConfig().unixSocket))
    {
        LogMsg(Logger::Debug, machine.getName(),
               " Removing previously mounted socket: ",
               machine.getConfig().unixSocket);
        if (!fs::remove(machine.getConfig().unixSocket))
        {
            LogMsg(Logger::Error, machine.getName(),
                   " Unable to remove pre-existing socket :",
                   machine.getConfig().unixSocket);
            return {};
        }
    }

    std::string nbd_client =
        "/usr/sbin/nbd-client " +
        boost::algorithm::join(
            Configuration::MountPoint::toArgs(machine.getConfig()), " ");

    std::vector<std::string> args = {
        // Listen for client on this unix socket...
        "--unix",
        machine.getConfig().unixSocket,

        // ... then connect nbd-client to served image
        "--run",
        nbd_client,

#if VM_VERBOSE_NBDKIT_LOGS
        "--verbose", // swarm of debug logs - only for brave souls
#endif
    };

    if (!machine.getTarget()->rw)
    {
        args.push_back("--readonly");
    }

    // Insert extra params
    args.insert(args.end(), params.begin(), params.end());

    if (!process->spawn(args, [& machine = machine, secret = std::move(secret)](
                                  int exitCode, bool isReady) {
            LogMsg(Logger::Info, machine.getName(), " process ended.");
            machine.getExitCode() = exitCode;
            machine.emitSubprocessStoppedEvent();
        }))
    {
        LogMsg(Logger::Error, machine.getName(),
               " Failed to spawn Process for: ", machine.getName());
        return {};
    }

    return process;
}

std::unique_ptr<resource::Process>
    ActivatingState::spawnNbdKit(interfaces::MountPointStateMachine& machine,
                                 const fs::path& file)
{
    return spawnNbdKit(machine, {},
                       {// Use file plugin ...
                        "file",
                        // ... to mount file at this location
                        "file=" + file.string()});
}

std::unique_ptr<resource::Process>
    ActivatingState::spawnNbdKit(interfaces::MountPointStateMachine& machine,
                                 const std::string& url)
{
    std::unique_ptr<utils::VolatileFile> secret;
    std::vector<std::string> params = {// Use curl plugin ...
                                       "curl",
                                       // ... to mount http resource at url
                                       "url=" + url};

    // Authenticate if needed
    if (machine.getTarget()->credentials)
    {
        // Pack password into buffer
        utils::CredentialsProvider::SecureBuffer buff =
            machine.getTarget()->credentials->pack([](const std::string& user,
                                                      const std::string& pass,
                                                      std::vector<char>& buff) {
                std::copy(pass.begin(), pass.end(), std::back_inserter(buff));
            });

        // Prepare file to provide the password with
        secret = std::make_unique<utils::VolatileFile>(std::move(buff));

        params.push_back("user=" + machine.getTarget()->credentials->user());
        params.push_back("password=+" + secret->path());
    }

    return spawnNbdKit(machine, std::move(secret), params);
}

bool ActivatingState::checkUrl(const std::string& urlScheme,
                               const std::string& imageUrl)
{
    return (urlScheme.compare(imageUrl.substr(0, urlScheme.size())) == 0);
}

bool ActivatingState::getImagePathFromUrl(const std::string& urlScheme,
                                          const std::string& imageUrl,
                                          std::string* imagePath)
{
    if (checkUrl(urlScheme, imageUrl))
    {
        if (imagePath != nullptr)
        {
            *imagePath = imageUrl.substr(urlScheme.size() - 1);
            return true;
        }
        else
        {
            LogMsg(Logger::Error, "Invalid parameter provied");
            return false;
        }
    }
    else
    {
        LogMsg(Logger::Error, "Provied url does not match scheme");
        return false;
    }
}

bool ActivatingState::isHttpsUrl(const std::string& imageUrl)
{
    return checkUrl("https://", imageUrl);
}

bool ActivatingState::getImagePathFromHttpsUrl(const std::string& imageUrl,
                                               std::string* imagePath)
{
    return getImagePathFromUrl("https://", imageUrl, imagePath);
}

bool ActivatingState::isCifsUrl(const std::string& imageUrl)
{
    return checkUrl("smb://", imageUrl);
}

bool ActivatingState::getImagePathFromCifsUrl(const std::string& imageUrl,
                                              std::string* imagePath)
{
    return getImagePathFromUrl("smb://", imageUrl, imagePath);
}

fs::path ActivatingState::getImagePath(const std::string& imageUrl)
{
    std::string imagePath;

    if (isHttpsUrl(imageUrl) && getImagePathFromHttpsUrl(imageUrl, &imagePath))
    {
        return fs::path(imagePath);
    }
    else if (isCifsUrl(imageUrl) &&
             getImagePathFromCifsUrl(imageUrl, &imagePath))
    {
        return fs::path(imagePath);
    }
    else
    {
        LogMsg(Logger::Error, "Unrecognized url's scheme encountered");
        return fs::path("");
    }
}