summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-client/CloudGateway.cpp
blob: ad7ec6bf0f7275428de948b032ccff15dc87a352 (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
/* $Id: CloudGateway.cpp $ */
/** @file
 * Implementation of local and cloud gateway management.
 */

/*
 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#define LOG_GROUP LOG_GROUP_MAIN_CONSOLE

/* Make sure all the stdint.h macros are included - must come first! */
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif

#include "LoggingNew.h"
#include "ApplianceImpl.h"
#include "CloudNetworkImpl.h"
#include "CloudGateway.h"

#include <iprt/http.h>
#include <iprt/inifile.h>
#include <iprt/net.h>
#include <iprt/path.h>
#include <iprt/vfs.h>
#include <iprt/uri.h>
#ifdef DEBUG
#include <iprt/file.h>
#include <VBox/com/utils.h>
#endif

#ifdef VBOX_WITH_LIBSSH
/* Prevent inclusion of Winsock2.h */
#define _WINSOCK2API_
#include <libssh/libssh.h>
#endif /* VBOX_WITH_LIBSSH */


static HRESULT setMacAddress(const Utf8Str& str, RTMAC& mac)
{
    int vrc = RTNetStrToMacAddr(str.c_str(), &mac);
    if (RT_FAILURE(vrc))
    {
        LogRel(("CLOUD-NET: Invalid MAC address '%s'\n", str.c_str()));
        return E_INVALIDARG;
    }
    return S_OK;
}


HRESULT GatewayInfo::setCloudMacAddress(const Utf8Str& mac)
{
    return setMacAddress(mac, mCloudMacAddress);
}


HRESULT GatewayInfo::setLocalMacAddress(const Utf8Str& mac)
{
    return setMacAddress(mac, mLocalMacAddress);
}


class CloudError
{
public:
    CloudError(HRESULT hrc, const Utf8Str& strText) : mHrc(hrc), mText(strText) {};
    HRESULT getRc() { return mHrc; };
    Utf8Str getText() { return mText; };

private:
    HRESULT mHrc;
    Utf8Str mText;
};


static void handleErrors(HRESULT hrc, const char *pszFormat, ...)
{
    if (FAILED(hrc))
    {
        va_list va;
        va_start(va, pszFormat);
        Utf8Str strError(pszFormat, va);
        va_end(va);
        LogRel(("CLOUD-NET: %s (hrc=%x)\n", strError.c_str(), hrc));
        throw CloudError(hrc, strError);
    }

}


class CloudClient
{
public:
    CloudClient(ComPtr<IVirtualBox> virtualBox, const Bstr& strProvider, const Bstr& strProfile);
    ~CloudClient() {};

    void startCloudGateway(const ComPtr<ICloudNetwork> &network, GatewayInfo& gateway);
    void stopCloudGateway(const GatewayInfo& gateway);

private:
    ComPtr<ICloudProviderManager> mManager;
    ComPtr<ICloudProvider>        mProvider;
    ComPtr<ICloudProfile>         mProfile;
    ComPtr<ICloudClient>          mClient;
};


CloudClient::CloudClient(ComPtr<IVirtualBox> virtualBox, const Bstr& strProvider, const Bstr& strProfile)
{
    HRESULT hrc = virtualBox->COMGETTER(CloudProviderManager)(mManager.asOutParam());
    handleErrors(hrc, "Failed to obtain cloud provider manager object");
    hrc = mManager->GetProviderByShortName(strProvider.raw(), mProvider.asOutParam());
    handleErrors(hrc, "Failed to obtain cloud provider '%ls'", strProvider.raw());
    hrc = mProvider->GetProfileByName(strProfile.raw(), mProfile.asOutParam());
    handleErrors(hrc, "Failed to obtain cloud profile '%ls'", strProfile.raw());
    hrc = mProfile->CreateCloudClient(mClient.asOutParam());
    handleErrors(hrc, "Failed to create cloud client");
}


void CloudClient::startCloudGateway(const ComPtr<ICloudNetwork> &network, GatewayInfo& gateway)
{
    ComPtr<IProgress> progress;
    ComPtr<ICloudNetworkGatewayInfo> gatewayInfo;
    HRESULT hrc = mClient->StartCloudNetworkGateway(network, Bstr(gateway.mPublicSshKey).raw(),
                                                    gatewayInfo.asOutParam(), progress.asOutParam());
    handleErrors(hrc, "Failed to launch compute instance");
    hrc = progress->WaitForCompletion(-1);
    handleErrors(hrc, "Failed to launch compute instance (wait)");

    Bstr instanceId;
    hrc = gatewayInfo->COMGETTER(InstanceId)(instanceId.asOutParam());
    handleErrors(hrc, "Failed to get launched compute instance id");
    gateway.mGatewayInstanceId = instanceId;

    Bstr publicIP;
    hrc = gatewayInfo->COMGETTER(PublicIP)(publicIP.asOutParam());
    handleErrors(hrc, "Failed to get cloud gateway public IP address");
    gateway.mCloudPublicIp = publicIP;

    Bstr secondaryPublicIP;
    hrc = gatewayInfo->COMGETTER(SecondaryPublicIP)(secondaryPublicIP.asOutParam());
    handleErrors(hrc, "Failed to get cloud gateway secondary public IP address");
    gateway.mCloudSecondaryPublicIp = secondaryPublicIP;

    Bstr macAddress;
    hrc = gatewayInfo->COMGETTER(MacAddress)(macAddress.asOutParam());
    handleErrors(hrc, "Failed to get cloud gateway public IP address");
    gateway.setCloudMacAddress(macAddress);
}


void CloudClient::stopCloudGateway(const GatewayInfo& gateway)
{
    ComPtr<IProgress> progress;
    HRESULT hrc = mClient->TerminateInstance(Bstr(gateway.mGatewayInstanceId).raw(), progress.asOutParam());
    handleErrors(hrc, "Failed to terminate compute instance");
#if 0
    /* Someday we may want to wait until the cloud gateway has terminated. */
    hrc = progress->WaitForCompletion(-1);
    handleErrors(hrc, "Failed to terminate compute instance (wait)");
#endif
}


HRESULT startCloudGateway(ComPtr<IVirtualBox> virtualBox, ComPtr<ICloudNetwork> network, GatewayInfo& gateway)
{
    HRESULT hrc = S_OK;

    try {
        hrc = network->COMGETTER(Provider)(gateway.mCloudProvider.asOutParam());
        hrc = network->COMGETTER(Profile)(gateway.mCloudProfile.asOutParam());
        CloudClient client(virtualBox, gateway.mCloudProvider, gateway.mCloudProfile);
        client.startCloudGateway(network, gateway);
    }
    catch (CloudError e)
    {
        hrc = e.getRc();
    }

    return hrc;
}


HRESULT stopCloudGateway(ComPtr<IVirtualBox> virtualBox, GatewayInfo& gateway)
{
    if (gateway.mGatewayInstanceId.isEmpty())
        return S_OK;

    LogRel(("CLOUD-NET: Terminating cloud gateway instance '%s'...\n", gateway.mGatewayInstanceId.c_str()));

    HRESULT hrc = S_OK;
    try {
        CloudClient client(virtualBox, gateway.mCloudProvider, gateway.mCloudProfile);
        client.stopCloudGateway(gateway);
#if 0
# ifdef DEBUG
        char szKeyPath[RTPATH_MAX];

        int vrc = GetVBoxUserHomeDirectory(szKeyPath, sizeof(szKeyPath), false /* fCreateDir */);
        if (RT_SUCCESS(vrc))
        {
            vrc = RTPathAppend(szKeyPath, sizeof(szKeyPath), "gateway-key.pem");
            AssertRCReturn(vrc, vrc);
            vrc = RTFileDelete(szKeyPath);
            if (RT_FAILURE(vrc))
                LogRel(("WARNING! Failed to delete private key %s with vrc=%d\n", szKeyPath, vrc));
        }
        else
            LogRel(("WARNING! Failed to get VirtualBox user home directory with '%Rrc'\n", vrc));
# endif /* DEBUG */
#endif
    }
    catch (CloudError e)
    {
        hrc = e.getRc();
        LogRel(("CLOUD-NET: Failed to terminate cloud gateway instance (hrc=%x).\n", hrc));
    }
    gateway.mGatewayInstanceId.setNull();
    return hrc;
}


HRESULT generateKeys(GatewayInfo& gateway)
{
#ifndef VBOX_WITH_LIBSSH
    RT_NOREF(gateway);
    return E_NOTIMPL;
#else /* VBOX_WITH_LIBSSH */
    ssh_key single_use_key;
    int iRcSsh = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &single_use_key);
    if (iRcSsh != SSH_OK)
    {
        LogRel(("Failed to generate a key pair. iRcSsh = %d\n", iRcSsh));
        return E_FAIL;
    }

    char *pstrKey = NULL;
    iRcSsh = ssh_pki_export_privkey_base64(single_use_key, NULL, NULL, NULL, &pstrKey);
    if (iRcSsh != SSH_OK)
    {
        LogRel(("Failed to export private key. iRcSsh = %d\n", iRcSsh));
        return E_FAIL;
    }
    gateway.mPrivateSshKey = pstrKey;
#if 0
# ifdef DEBUG
    char szConfigPath[RTPATH_MAX];

    vrc = GetVBoxUserHomeDirectory(szConfigPath, sizeof(szConfigPath), false /* fCreateDir */);
    if (RT_SUCCESS(vrc))
    {
        vrc = RTPathAppend(szConfigPath, sizeof(szConfigPath), "gateway-key.pem");
        AssertRCReturn(vrc, E_FAIL);
        iRcSsh = ssh_pki_export_privkey_file(single_use_key, NULL, NULL, NULL, szConfigPath);
        if (iRcSsh != SSH_OK)
        {
            LogRel(("Failed to export private key to %s with iRcSsh=%d\n", szConfigPath, iRcSsh));
            return E_FAIL;
        }
#  ifndef RT_OS_WINDOWS
        vrc = RTPathSetMode(szConfigPath, RTFS_UNIX_IRUSR | RTFS_UNIX_IWUSR); /* Satisfy ssh client */
        AssertRCReturn(vrc, E_FAIL);
#  endif
    }
    else
    {
        LogRel(("Failed to get VirtualBox user home directory with '%Rrc'\n", vrc));
        return E_FAIL;
    }
# endif /* DEBUG */
#endif
    ssh_string_free_char(pstrKey);
    pstrKey = NULL;
    iRcSsh = ssh_pki_export_pubkey_base64(single_use_key, &pstrKey);
    if (iRcSsh != SSH_OK)
    {
        LogRel(("Failed to export public key. iRcSsh = %d\n", iRcSsh));
        return E_FAIL;
    }
    gateway.mPublicSshKey = Utf8StrFmt("ssh-rsa %s single-use-key", pstrKey);
    ssh_string_free_char(pstrKey);
    ssh_key_free(single_use_key);

    return S_OK;
#endif /* VBOX_WITH_LIBSSH */
}