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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using Duplicati.Library.Interface;
using System.Linq;
using System.IO;
using Duplicati.Library.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Duplicati.Library.Strings;
using System.Net;
using System.Text;
using Duplicati.Library.Common.IO;
namespace Duplicati.Library.Backend.OpenStack
{
public class OpenStackStorage : IBackend, IStreamingBackend
{
private const string DOMAINNAME_OPTION = "openstack-domain-name";
private const string USERNAME_OPTION = "auth-username";
private const string PASSWORD_OPTION = "auth-password";
private const string TENANTNAME_OPTION = "openstack-tenant-name";
private const string AUTHURI_OPTION = "openstack-authuri";
private const string VERSION_OPTION = "openstack-version";
private const string APIKEY_OPTION = "openstack-apikey";
private const string REGION_OPTION = "openstack-region";
private const int PAGE_LIMIT = 500;
private readonly string m_container;
private readonly string m_prefix;
private readonly string m_domainName;
private readonly string m_username;
private readonly string m_password;
private readonly string m_authUri;
private readonly string m_version;
private readonly string m_tenantName;
private readonly string m_apikey;
private readonly string m_region;
protected string m_simplestorageendpoint;
private readonly WebHelper m_helper;
private OpenStackAuthResponse.TokenClass m_accessToken;
public static readonly KeyValuePair<string, string>[] KNOWN_OPENSTACK_PROVIDERS = {
new KeyValuePair<string, string>("Rackspace US", "https://identity.api.rackspacecloud.com/v2.0"),
new KeyValuePair<string, string>("Rackspace UK", "https://lon.identity.api.rackspacecloud.com/v2.0"),
new KeyValuePair<string, string>("OVH Cloud Storage", "https://auth.cloud.ovh.net/v2.0"),
new KeyValuePair<string, string>("Selectel Cloud Storage", "https://auth.selcdn.ru"),
new KeyValuePair<string, string>("Memset Cloud Storage", "https://auth.storage.memset.com"),
};
public static readonly KeyValuePair<string, string>[] OPENSTACK_VERSIONS = {
new KeyValuePair<string, string>("v2.0", "v2"),
new KeyValuePair<string, string>("v3", "v3"),
};
private class Keystone3AuthRequest
{
public class AuthContainer
{
public Identity identity { get; set; }
public Scope scope { get; set; }
}
public class Identity
{
[JsonProperty(ItemConverterType=typeof(StringEnumConverter))]
public IdentityMethods[] methods { get; set; }
[JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]
public PasswordBasedRequest PasswordCredentials { get; set; }
public Identity()
{
this.methods = new [] { IdentityMethods.password };
}
}
public class Scope
{
public Project project;
}
public enum IdentityMethods
{
password,
}
public class PasswordBasedRequest
{
public UserCredentials user { get; set; }
}
public class UserCredentials
{
public Domain domain { get; set; }
public string name { get; set; }
public string password { get; set; }
public UserCredentials()
{
}
public UserCredentials(Domain domain, string name, string password)
{
this.domain = domain;
this.name = name;
this.password = password;
}
}
public class Domain
{
public string name { get; set; }
public Domain(string name)
{
this.name = name;
}
}
public class Project {
public Domain domain { get; set; }
public string name { get; set; }
public Project(Domain domain, string name)
{
this.domain = domain;
this.name = name;
}
}
public AuthContainer auth { get; set; }
public Keystone3AuthRequest(string domain_name, string username, string password, string project_name)
{
Domain domain = new Domain(domain_name);
this.auth = new AuthContainer();
this.auth.identity = new Identity();
this.auth.identity.PasswordCredentials = new PasswordBasedRequest();
this.auth.identity.PasswordCredentials.user = new UserCredentials(domain,username,password);
this.auth.scope = new Scope();
this.auth.scope.project = new Project(domain, project_name);
}
}
private class OpenStackAuthRequest
{
public class AuthContainer
{
[JsonProperty("RAX-KSKEY:apiKeyCredentials", NullValueHandling = NullValueHandling.Ignore)]
public ApiKeyBasedRequest ApiCredentials { get; set; }
[JsonProperty("passwordCredentials", NullValueHandling = NullValueHandling.Ignore)]
public PasswordBasedRequest PasswordCredentials { get; set; }
[JsonProperty("tenantName", NullValueHandling = NullValueHandling.Ignore)]
public string TenantName { get; set; }
[JsonProperty("token", NullValueHandling = NullValueHandling.Ignore)]
public TokenBasedRequest Token { get; set; }
}
public class ApiKeyBasedRequest
{
public string username { get; set; }
public string apiKey { get; set; }
}
public class PasswordBasedRequest
{
public string username { get; set; }
public string password { get; set; }
public string tenantName { get; set; }
}
public class TokenBasedRequest
{
public string id { get; set; }
}
public AuthContainer auth { get; set; }
public OpenStackAuthRequest(string tenantname, string username, string password, string apikey)
{
this.auth = new AuthContainer();
this.auth.TenantName = tenantname;
if (string.IsNullOrEmpty(apikey))
{
this.auth.PasswordCredentials = new PasswordBasedRequest
{
username = username,
password = password,
};
}
else
{
this.auth.ApiCredentials = new ApiKeyBasedRequest
{
username = username,
apiKey = apikey
};
}
}
}
private class Keystone3AuthResponse
{
public TokenClass token { get; set; }
public class EndpointItem
{
// 'interface' is a reserved keyword, so we need this decorator to map it
[JsonProperty(PropertyName = "interface")]
public string interface_name { get; set; }
public string region { get; set; }
public string url { get; set; }
}
public class CatalogItem
{
public EndpointItem[] endpoints { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class TokenClass
{
public CatalogItem[] catalog { get; set; }
public DateTime? expires_at { get; set; }
}
}
private class OpenStackAuthResponse
{
public AccessClass access { get; set; }
public class TokenClass
{
public string id { get; set; }
public DateTime? expires { get; set; }
}
public class EndpointItem
{
public string region { get; set; }
public string tenantId { get; set; }
public string publicURL { get; set; }
public string internalURL { get; set; }
}
public class ServiceItem
{
public string name { get; set; }
public string type { get; set; }
public EndpointItem[] endpoints { get; set; }
}
public class AccessClass
{
public TokenClass token { get; set; }
public ServiceItem[] serviceCatalog { get; set; }
}
}
private class OpenStackStorageItem
{
public string name { get; set; }
public DateTime? last_modified { get; set; }
public long? bytes { get; set; }
public string content_type { get; set; }
public string subdir { get; set; }
}
private class WebHelper : JSONWebHelper
{
private readonly OpenStackStorage m_parent;
public WebHelper(OpenStackStorage parent) { m_parent = parent; }
public override HttpWebRequest CreateRequest(string url, string method = null)
{
var req = base.CreateRequest(url, method);
req.Headers["X-Auth-Token"] = m_parent.AccessToken;
return req;
}
}
public OpenStackStorage()
{
}
public OpenStackStorage(string url, Dictionary<string, string> options)
{
var uri = new Utility.Uri(url);
m_container = uri.Host;
m_prefix = Util.AppendDirSeparator("/" + uri.Path, "/");
// For OpenStack we do not use a leading slash
if (m_prefix.StartsWith("/", StringComparison.Ordinal))
m_prefix = m_prefix.Substring(1);
options.TryGetValue(DOMAINNAME_OPTION, out m_domainName);
options.TryGetValue(USERNAME_OPTION, out m_username);
options.TryGetValue(PASSWORD_OPTION, out m_password);
options.TryGetValue(TENANTNAME_OPTION, out m_tenantName);
options.TryGetValue(AUTHURI_OPTION, out m_authUri);
options.TryGetValue(VERSION_OPTION, out m_version);
options.TryGetValue(APIKEY_OPTION, out m_apikey);
options.TryGetValue(REGION_OPTION, out m_region);
if (string.IsNullOrWhiteSpace(m_username))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(USERNAME_OPTION), "OpenStackMissingUsername");
if (string.IsNullOrWhiteSpace(m_authUri))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(AUTHURI_OPTION), "OpenStackMissingAuthUri");
switch (m_version)
{
case "v3":
if (string.IsNullOrWhiteSpace(m_password))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(PASSWORD_OPTION), "OpenStackMissingPassword");
if (string.IsNullOrWhiteSpace(m_domainName))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(DOMAINNAME_OPTION), "OpenStackMissingDomainName");
if (string.IsNullOrWhiteSpace(m_tenantName))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(TENANTNAME_OPTION), "OpenStackMissingTenantName");
break;
case "v2":
default:
if (string.IsNullOrWhiteSpace(m_apikey))
{
if (string.IsNullOrWhiteSpace(m_password))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(PASSWORD_OPTION), "OpenStackMissingPassword");
if (string.IsNullOrWhiteSpace(m_tenantName))
throw new UserInformationException(Strings.OpenStack.MissingOptionError(TENANTNAME_OPTION), "OpenStackMissingTenantName");
}
break;
}
m_helper = new WebHelper(this);
}
protected virtual string AccessToken
{
get
{
if (m_accessToken == null || (m_accessToken.expires.HasValue && (m_accessToken.expires.Value - DateTime.UtcNow).TotalSeconds < 30))
GetAuthResponse();
return m_accessToken.id;
}
}
private static string JoinUrls(string uri, string fragment)
{
fragment = fragment ?? "";
return uri + (uri.EndsWith("/", StringComparison.Ordinal) ? "" : "/") + (fragment.StartsWith("/", StringComparison.Ordinal) ? fragment.Substring(1) : fragment);
}
private static string JoinUrls(string uri, string fragment1, string fragment2)
{
return JoinUrls(JoinUrls(uri, fragment1), fragment2);
}
private void GetAuthResponse()
{
switch (this.m_version)
{
case "v3":
GetKeystone3AuthResponse();
break;
case "v2":
default:
GetOpenstackAuthResponse();
break;
}
}
private Keystone3AuthResponse GetKeystone3AuthResponse()
{
var helper = new JSONWebHelper();
var req = helper.CreateRequest(JoinUrls(m_authUri, "auth/tokens"));
req.Accept = "application/json";
req.Method = "POST";
var data = Encoding.UTF8.GetBytes(
JsonConvert.SerializeObject(
new Keystone3AuthRequest(m_domainName, m_username, m_password, m_tenantName)
));
req.ContentLength = data.Length;
req.ContentType = "application/json; charset=UTF-8";
using (var rs = req.GetRequestStream())
rs.Write(data, 0, data.Length);
WebResponse http_response = req.GetResponse();
Keystone3AuthResponse resp;
using (var reader = new StreamReader(http_response.GetResponseStream()))
{
resp = Newtonsoft.Json.JsonConvert.DeserializeObject<Keystone3AuthResponse>(
reader.ReadToEnd());
}
string token = http_response.Headers["X-Subject-Token"];
this.m_accessToken = new OpenStackAuthResponse.TokenClass();
this.m_accessToken.id = token;
this.m_accessToken.expires = resp.token.expires_at;
// Grab the endpoint now that we have received it anyway
var fileservice = resp.token.catalog.FirstOrDefault(x => string.Equals(x.type, "object-store", StringComparison.OrdinalIgnoreCase));
if (fileservice == null)
throw new Exception("No object-store service found, is this service supported by the provider?");
var endpoint = fileservice.endpoints.FirstOrDefault(x => (string.Equals(m_region, x.region) && string.Equals(x.interface_name, "public", StringComparison.OrdinalIgnoreCase))) ?? fileservice.endpoints.First();
m_simplestorageendpoint = endpoint.url;
return resp;
}
private OpenStackAuthResponse GetOpenstackAuthResponse()
{
var helper = new JSONWebHelper();
var req = helper.CreateRequest(JoinUrls(m_authUri, "tokens"));
req.Accept = "application/json";
req.Method = "POST";
var resp = helper.ReadJSONResponse<OpenStackAuthResponse>(
req,
new OpenStackAuthRequest(m_tenantName, m_username, m_password, m_apikey)
);
m_accessToken = resp.access.token;
// Grab the endpoint now that we have received it anyway
var fileservice = resp.access.serviceCatalog.FirstOrDefault(x => string.Equals(x.type, "object-store", StringComparison.OrdinalIgnoreCase));
if (fileservice == null)
throw new Exception("No object-store service found, is this service supported by the provider?");
var endpoint = fileservice.endpoints.FirstOrDefault(x => string.Equals(m_region, x.region)) ?? fileservice.endpoints.First();
m_simplestorageendpoint = endpoint.publicURL;
return resp;
}
protected virtual string SimpleStorageEndPoint
{
get
{
if (m_simplestorageendpoint == null)
GetAuthResponse();
return m_simplestorageendpoint;
}
}
#region IStreamingBackend implementation
public void Put(string remotename, System.IO.Stream stream)
{
var url = JoinUrls(SimpleStorageEndPoint, m_container, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
using(m_helper.GetResponse(url, stream, "PUT"))
{ }
}
public void Get(string remotename, System.IO.Stream stream)
{
var url = JoinUrls(SimpleStorageEndPoint, m_container, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
try
{
using(var resp = m_helper.GetResponse(url))
using(var rs = AsyncHttpRequest.TrySetTimeout(resp.GetResponseStream()))
Library.Utility.Utility.CopyStream(rs, stream);
}
catch(WebException wex)
{
if (wex.Response is HttpWebResponse && ((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
throw new FileMissingException();
else
throw;
}
}
#endregion
#region IBackend implementation
private T HandleListExceptions<T>(Func<T> func)
{
try
{
return func();
}
catch (WebException wex)
{
if (wex.Response is HttpWebResponse && (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound))
throw new FolderMissingException();
else
throw;
}
}
public IEnumerable<IFileEntry> List()
{
var plainurl = JoinUrls(SimpleStorageEndPoint, m_container) + string.Format("?format=json&delimiter=/&limit={0}", PAGE_LIMIT);
if (!string.IsNullOrEmpty(m_prefix))
plainurl += "&prefix=" + Library.Utility.Uri.UrlEncode(m_prefix);
var url = plainurl;
while (true)
{
var req = m_helper.CreateRequest(url);
req.Accept = "application/json";
var items = HandleListExceptions(() => m_helper.ReadJSONResponse<OpenStackStorageItem[]>(req));
foreach (var n in items)
{
var name = n.name;
if (name.StartsWith(m_prefix, StringComparison.Ordinal))
name = name.Substring(m_prefix.Length);
if (n.bytes == null)
yield return new FileEntry(name);
else if (n.last_modified == null)
yield return new FileEntry(name, n.bytes.Value);
else
yield return new FileEntry(name, n.bytes.Value, n.last_modified.Value, n.last_modified.Value);
}
if (items.Length != PAGE_LIMIT)
yield break;
// Prepare next listing entry
url = plainurl + string.Format("&marker={0}", Library.Utility.Uri.UrlEncode(items.Last().name));
}
}
public void Put(string remotename, string filename)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
Put(remotename, fs);
}
public void Get(string remotename, string filename)
{
using (System.IO.FileStream fs = System.IO.File.Create(filename))
Get(remotename, fs);
}
public void Delete(string remotename)
{
var url = JoinUrls(SimpleStorageEndPoint, m_container, Library.Utility.Uri.UrlPathEncode(m_prefix + remotename));
m_helper.ReadJSONResponse<object>(url, null, "DELETE");
}
public void Test()
{
this.TestList();
}
public void CreateFolder()
{
var url = JoinUrls(SimpleStorageEndPoint, m_container);
using(m_helper.GetResponse(url, null, "PUT"))
{ }
}
public string DisplayName
{
get
{
return Strings.OpenStack.DisplayName;
}
}
public string ProtocolKey
{
get
{
return "openstack";
}
}
public IList<ICommandLineArgument> SupportedCommands
{
get
{
var authuris = new StringBuilder();
foreach(var s in KNOWN_OPENSTACK_PROVIDERS)
authuris.AppendLine(string.Format("{0}: {1}", s.Key, s.Value));
return new List<ICommandLineArgument>(new [] {
new CommandLineArgument(DOMAINNAME_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.DomainnameOptionShort, Strings.OpenStack.UsernameOptionLong),
new CommandLineArgument(USERNAME_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.UsernameOptionShort, Strings.OpenStack.UsernameOptionLong),
new CommandLineArgument(PASSWORD_OPTION, CommandLineArgument.ArgumentType.Password, Strings.OpenStack.PasswordOptionShort, Strings.OpenStack.PasswordOptionLong(TENANTNAME_OPTION)),
new CommandLineArgument(TENANTNAME_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.TenantnameOptionShort, Strings.OpenStack.TenantnameOptionLong),
new CommandLineArgument(APIKEY_OPTION, CommandLineArgument.ArgumentType.Password, Strings.OpenStack.ApikeyOptionShort, Strings.OpenStack.ApikeyOptionLong),
new CommandLineArgument(AUTHURI_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.AuthuriOptionShort, Strings.OpenStack.AuthuriOptionLong(authuris.ToString())),
new CommandLineArgument(VERSION_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.VersionOptionShort, Strings.OpenStack.AuthuriOptionLong(authuris.ToString())),
new CommandLineArgument(REGION_OPTION, CommandLineArgument.ArgumentType.String, Strings.OpenStack.RegionOptionShort, Strings.OpenStack.RegionOptionLong),
});
}
}
public string Description
{
get
{
return Strings.OpenStack.Description;
}
}
public virtual string[] DNSName
{
get
{
return new string[] {
new System.Uri(m_authUri).Host,
string.IsNullOrWhiteSpace(m_simplestorageendpoint) ? null : new System.Uri(m_simplestorageendpoint).Host
};
}
}
#endregion
#region IDisposable implementation
public void Dispose()
{
}
#endregion
}
}
|