Exemple #1
0
        public bool AddStorageProvider(Guid nodeId, string nodeName, AzureBlobConfig azureBlobConfig, StorageProviderVisibility visibility)
        {
            if (string.IsNullOrWhiteSpace(nodeName))
            {
                throw new ArgumentException("The node name is empty", nameof(nodeName));
            }
            if (azureBlobConfig == null)
            {
                throw new ArgumentNullException(nameof(azureBlobConfig));
            }

            lock (StorageProviderInstances)
            {
                if (StorageProviderInstances.Any(x => nodeId.Equals(x.RuntimeId) || string.Equals(x.ProviderInfo.Name, nodeName, StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(false);
                }
                var instance = new StorageProviderInstance_AzureBlob(new StorageProviderInfo {
                    Id         = nodeId,
                    Type       = StorageProviderInstance.TypeAzure,
                    Name       = nodeName,
                    Visibility = visibility,
                    Settings   = JsonConvert.SerializeObject(azureBlobConfig)
                });
                StorageProviderInstances.Add(instance);
                ResyncClientListToStorageProviderInstances();
                return(true);
            }
        }
Exemple #2
0
 public StorageProviderInstance_AzureBlob(StorageProviderInfo providerInfo) : base(providerInfo)
 {
     AzureBlobConfig = JsonConvert.DeserializeObject <AzureBlobConfig>(ProviderInfo.Settings);
     if (AzureBlobConfig == null || !AzureBlobConfig.IsValid())
     {
         throw new Exception("Invalid config for Azure Blob");
     }
 }
Exemple #3
0
        public static IServiceCollection AppAddAzureStorageService(this IServiceCollection services, IConfiguration config)
        {
            var azureBlobConfig = new AzureBlobConfig();

            config.GetSection(nameof(AzureBlobConfig)).Bind(azureBlobConfig);

            services.AddSingleton(azureBlobConfig);

            services.AddSingleton <IFileUtils, FileUtils>();

            return(services);
        }
        public IEnumerable <PersonalCloudInfo> LoadCloud()
        {
            var deviceName = Globals.Database.LoadSetting(UserSettings.DeviceName) ?? Environment.MachineName;

            return(Globals.Database.Table <CloudModel>().Select(x => {
                var alibaba = Globals.Database.Table <AlibabaOSS>().Where(y => y.Cloud == x.Id).Select(y => {
                    var config = new OssConfig {
                        OssEndpoint = y.Endpoint,
                        BucketName = y.Bucket,
                    };
                    using var cipher = new PasswordCipher(y.Id.ToString("N", CultureInfo.InvariantCulture), x.Key);
                    config.AccessKeyId = cipher.DecryptContinuousText(y.AccessID);
                    config.AccessKeySecret = cipher.DecryptContinuousText(y.AccessSecret);
                    return new StorageProviderInfo {
                        Id = y.Id,
                        Type = StorageProviderInstance.TypeAliYun,
                        Name = y.Name,
                        Visibility = (StorageProviderVisibility)y.Visibility,
                        Settings = JsonConvert.SerializeObject(config)
                    };
                });
                var azure = Globals.Database.Table <AzureBlob>().Where(y => y.Cloud == x.Id).Select(y => {
                    var config = new AzureBlobConfig {
                        BlobName = y.Container
                    };
                    using var cipher = new PasswordCipher(y.Id.ToString("N", CultureInfo.InvariantCulture), x.Key);
                    config.ConnectionString = cipher.DecryptTextOnce(y.Parameters);
                    return new StorageProviderInfo {
                        Id = y.Id,
                        Type = StorageProviderInstance.TypeAzure,
                        Name = y.Name,
                        Visibility = (StorageProviderVisibility)y.Visibility,
                        Settings = JsonConvert.SerializeObject(config)
                    };
                });
                var providers = new List <StorageProviderInfo>();
                providers.AddRange(alibaba);
                providers.AddRange(azure);
                return new PersonalCloudInfo(providers)
                {
                    Id = x.Id.ToString("N", CultureInfo.InvariantCulture),
                    DisplayName = x.Name,
                    NodeDisplayName = deviceName,
                    MasterKey = Convert.FromBase64String(x.Key),
                    TimeStamp = x.Version,
                    Apps = new List <AppLauncher>(),
                };
            }));
        }
Exemple #5
0
        public bool AddStorageProvider(string cloudId, Guid nodeId, string nodeName, AzureBlobConfig azureConfig, StorageProviderVisibility visibility, bool saveChanges = true)
        {
            PersonalCloud personalCloud = null;

            lock (_PersonalClouds)
            {
                personalCloud = _PersonalClouds.FirstOrDefault(x => x.Id == cloudId);
            }

            if (personalCloud != null)
            {
                var haveChanges = personalCloud.AddStorageProvider(nodeId, nodeName, azureConfig, visibility);
                if (haveChanges && saveChanges)
                {
                    SavePCList();
                }
                return(haveChanges);
            }
            else
            {
                throw new NoSuchCloudException();
            }
        }
Exemple #6
0
 public void ConnectToAzure(Guid cloudId, string name, AzureBlobConfig config, StorageProviderVisibility visibility)
 {
     Globals.CloudService.AddStorageProvider(cloudId.ToString("N"), Guid.NewGuid(), name, config, visibility);
 }
        private void SaveCredentials(object sender, EventArgs e)
        {
            var name           = R.connection_name.EditText.Text;
            var invalidCharHit = false;

            foreach (var character in Consts.InvalidCharacters)
            {
                if (name?.Contains(character) == true)
                {
                    invalidCharHit = true;
                }
            }
            if (string.IsNullOrEmpty(name) || invalidCharHit)
            {
                this.ShowAlert(GetString(Resource.String.connection_bad_name), GetString(Resource.String.connection_name_cannot_be_empty), () => {
                    R.connection_name.EditText.RequestFocus();
                });
                return;
            }

            var             selection = R.connection_to_spinner.SelectedItemPosition;
            OssConfig       alibaba   = null;
            AzureBlobConfig azure     = null;

            switch (selection)
            {
            case 0:     // Alibaba Cloud OSS
            {
                var endpoint = R.connection_endpoint.EditText.Text;
                if (string.IsNullOrEmpty(endpoint))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.aliyun_bad_endpoint), () => {
                            R.connection_endpoint.EditText.RequestFocus();
                        });
                    return;
                }

                var bucket = R.connection_container.EditText.Text;
                if (string.IsNullOrEmpty(bucket))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.aliyun_bad_bucket), () => {
                            R.connection_container.EditText.RequestFocus();
                        });
                    return;
                }

                var accessId = R.connection_account.EditText.Text;
                if (string.IsNullOrEmpty(accessId))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.aliyun_bad_user_id), () => {
                            R.connection_account.EditText.RequestFocus();
                        });
                    return;
                }

                var accessSecret = R.connection_secret.EditText.Text;
                if (string.IsNullOrEmpty(accessSecret))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.aliyun_bad_secret), () => {
                            R.connection_secret.EditText.RequestFocus();
                        });
                    return;
                }

                alibaba = new OssConfig {
                    OssEndpoint     = endpoint,
                    BucketName      = bucket,
                    AccessKeyId     = accessId,
                    AccessKeySecret = accessSecret
                };

                break;
            }

            case 1:     // Azure Blob Storage
            {
                var endpoint = R.connection_endpoint.EditText.Text;
                if (string.IsNullOrEmpty(endpoint))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.connection_bad_azure_endpoint), () => {
                            R.connection_endpoint.EditText.RequestFocus();
                        });
                    return;
                }

                var accountName = R.connection_account.EditText.Text;

                var accessKey = R.connection_secret.EditText.Text;
                if (string.IsNullOrEmpty(accessKey))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.connection_bad_azure_secret), () => {
                            R.connection_secret.EditText.RequestFocus();
                        });
                    return;
                }

                var container = R.connection_container.EditText.Text;
                if (string.IsNullOrEmpty(container))
                {
                    this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.connection_bad_azure_container), () => {
                            R.connection_container.RequestFocus();
                        });
                    return;
                }

                string connection;
                if (endpoint.Contains(accountName, StringComparison.Ordinal))
                {
                    accountName = null;
                }
                if (endpoint.StartsWith("http://", StringComparison.Ordinal))
                {
                    endpoint.Replace("http://", "https://");
                }
                if (endpoint.StartsWith("https://", StringComparison.Ordinal))
                {
                    if (string.IsNullOrEmpty(accountName))
                    {
                        if (string.IsNullOrEmpty(accessKey))
                        {
                            connection = endpoint;
                        }
                        else
                        {
                            connection = $"BlobEndpoint={endpoint};SharedAccessSignature={accessKey}";
                        }
                    }
                    else
                    {
                        this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.connection_mismatch_azure_endpoint));
                        return;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(accountName))
                    {
                        this.ShowAlert(GetString(Resource.String.connection_invalid_account), GetString(Resource.String.connection_bad_azure_id));
                        return;
                    }
                    else
                    {
                        connection = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accessKey};EndpointSuffix={endpoint}";
                    }
                }

                azure = new AzureBlobConfig {
                    ConnectionString = connection,
                    BlobName         = container
                };
                break;
            }
            }

            if (!Globals.Database.IsStorageNameUnique(name))
            {
                this.ShowAlert(GetString(Resource.String.connection_name_exists), GetString(Resource.String.connection_use_different_name), () => {
                    R.connection_name.EditText.RequestFocus();
                });
                return;
            }

            var shareScope = R.connection_share_spinner.SelectedItemPosition switch
            {
                0 => StorageProviderVisibility.Public,
                1 => StorageProviderVisibility.Private,
                _ => throw new IndexOutOfRangeException()
            };

#pragma warning disable 0618
            var progress = new ProgressDialog(this);
            progress.SetCancelable(false);
            progress.SetMessage(GetString(Resource.String.connection_verifying));
            progress.Show();
#pragma warning restore 0618

            Task.Run(() => {
                switch (selection)
                {
                case 0:
                    {
                        if (alibaba.Verify())
                        {
                            Globals.CloudManager.AddStorageProvider(Globals.CloudManager.PersonalClouds[0].Id, Guid.NewGuid(), name, alibaba, shareScope);
                            RunOnUiThread(() => {
                                progress.Dismiss();
                                Finish();
                            });
                        }
                        else
                        {
                            RunOnUiThread(() => {
                                progress.Dismiss();
                                this.ShowAlert(GetString(Resource.String.connection_bad_account), GetString(Resource.String.connection_bad_aliyun_account));
                            });
                        }
                        return;
                    }

                case 1:
                    {
                        if (azure.Verify())
                        {
                            Globals.CloudManager.AddStorageProvider(Globals.CloudManager.PersonalClouds[0].Id, Guid.NewGuid(), name, azure, shareScope);
                            RunOnUiThread(() => {
                                progress.Dismiss();
                                Finish();
                            });
                        }
                        else
                        {
                            RunOnUiThread(() => {
                                progress.Dismiss();
                                this.ShowAlert(GetString(Resource.String.connection_bad_account), GetString(Resource.String.connection_bad_azure_account));
                            });
                        }
                        return;
                    }
                }
            });
        }
    }
Exemple #8
0
        private void VerifyCredentials(object sender, EventArgs e)
        {
            var name           = ServiceName.Text;
            var invalidCharHit = false;

            foreach (var character in PathConsts.InvalidCharacters)
            {
                if (name?.Contains(character) == true)
                {
                    invalidCharHit = true;
                }
            }
            if (string.IsNullOrEmpty(name) || invalidCharHit)
            {
                this.ShowWarning(this.Localize("Online.BadName"), this.Localize("Online.IllegalName"), () => {
                    ServiceName.BecomeFirstResponder();
                });
                return;
            }

            var endpoint = EndpointSuffix.Text;

            if (string.IsNullOrEmpty(endpoint))
            {
                this.ShowWarning(this.Localize("Online.BadCredential"), this.Localize("Azure.BadEndpoint"), () => {
                    EndpointSuffix.BecomeFirstResponder();
                });
                return;
            }

            var accountName = AccountName.Text;

            var accessKey = AccountKey.Text;

            if (string.IsNullOrEmpty(accessKey))
            {
                this.ShowWarning(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountKey"), () => {
                    AccountKey.BecomeFirstResponder();
                });
                return;
            }

            var container = Container.Text;

            if (string.IsNullOrEmpty(container))
            {
                this.ShowWarning(this.Localize("Online.BadCredential"), this.Localize("Azure.BadContainer"), () => {
                    Container.BecomeFirstResponder();
                });
                return;
            }

            if (!Globals.Database.IsStorageNameUnique(name))
            {
                this.ShowWarning(this.Localize("Online.ServiceAlreadyExists"), this.Localize("Online.ChooseADifferentName"), () => {
                    ServiceName.BecomeFirstResponder();
                });
                return;
            }

            string connection;

            if (endpoint.Contains(accountName, StringComparison.Ordinal))
            {
                accountName = null;
            }
            if (endpoint.StartsWith("http://", StringComparison.Ordinal))
            {
                endpoint = endpoint.Replace("http://", "https://");
            }
            if (endpoint.StartsWith("https://", StringComparison.Ordinal))
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    if (string.IsNullOrEmpty(accessKey))
                    {
                        connection = endpoint;
                    }
                    else
                    {
                        connection = $"BlobEndpoint={endpoint};SharedAccessSignature={accessKey}";
                    }
                }
                else
                {
                    this.ShowError(this.Localize("Azure.BadAccount"), this.Localize("Azure.EndpointAndNameMismatch"));
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    this.ShowError(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountName"));
                    return;
                }
                else
                {
                    connection = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accessKey};EndpointSuffix={endpoint}";
                }
            }

            var hud = MBProgressHUD.ShowHUD(NavigationController.View, true);

            hud.Label.Text = this.Localize("Online.Verifying");

            Task.Run(() => {
                var config = new AzureBlobConfig {
                    ConnectionString = connection,
                    BlobName         = container
                };

                if (config.Verify())
                {
                    try
                    {
                        Globals.CloudManager.AddStorageProvider(Globals.CloudManager.PersonalClouds[0].Id, Guid.NewGuid(), name, config, visibility);
                        InvokeOnMainThread(() => {
                            hud.Hide(true);
                            NavigationController.DismissViewController(true, null);
                        });
                    }
                    catch
                    {
                        InvokeOnMainThread(() => {
                            hud.Hide(true);
                            this.ShowError(this.Localize("Azure.CannotAddService"), this.Localize("Error.Internal"));
                        });
                    }
                }
                else
                {
                    InvokeOnMainThread(() => {
                        hud.Hide(true);
                        this.ShowError(this.Localize("Error.Authentication"), this.Localize("Azure.Unauthorized"));
                    });
                }
            });
        }
        private void VerifyAzureCredentials()
        {
            ToggleBusyIndicator(true);

            var name           = ConnectionNameBox.Text;
            var invalidCharHit = false;

            foreach (var character in PathConsts.InvalidCharacters)
            {
                if (name?.Contains(character) == true)
                {
                    invalidCharHit = true;
                }
            }
            if (string.IsNullOrEmpty(name) || invalidCharHit)
            {
                Dispatcher.ShowAlert(UIStorage.ConnectionBadName, null);
                ToggleBusyIndicator(false);
                return;
            }

            var endpoint = EndpointBox.Text;

            if (string.IsNullOrEmpty(endpoint))
            {
                Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                ToggleBusyIndicator(false);
                return;
            }

            var accountName = AccessIDBox.Text;

            /*
             * if (string.IsNullOrEmpty(accountName) &&
             *  (endpoint == "core.windows.net" || endpoint == "blob.core.windows.net" ||
             *  endpoint == "core.chinacloudapi.cn" || endpoint == "blob.core.chinacloudapi.cn"))
             * {
             *  this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountName"), action => {
             *      AccountName.BecomeFirstResponder();
             *  });
             *  return;
             * }
             */

            var accessKey = AccessSecretBox.Text;

            if (string.IsNullOrEmpty(accessKey))
            {
                Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                ToggleBusyIndicator(false);
                return;
            }

            var container = ContainerBox.Text;

            if (string.IsNullOrEmpty(container))
            {
                Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                ToggleBusyIndicator(false);
                return;
            }

            string connection;

            if (endpoint.Contains(accountName, StringComparison.Ordinal))
            {
                accountName = null;
            }
            if (endpoint.StartsWith("http://", StringComparison.Ordinal))
            {
                endpoint.Replace("http://", "https://");
            }
            if (endpoint.StartsWith("https://", StringComparison.Ordinal))
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    if (string.IsNullOrEmpty(accessKey))
                    {
                        connection = endpoint;
                    }
                    else
                    {
                        connection = $"BlobEndpoint={endpoint};SharedAccessSignature={accessKey}";
                    }
                }
                else
                {
                    Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                    ToggleBusyIndicator(false);
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                    ToggleBusyIndicator(false);
                    return;
                }
                else
                {
                    connection = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accessKey};EndpointSuffix={endpoint}";
                }
            }

            var config = new AzureBlobConfig {
                ConnectionString = connection,
                BlobName         = container
            };
            var shareScope = ShareCredentialsBox.IsChecked == true ? StorageProviderVisibility.Public : StorageProviderVisibility.Private;

            Task.Run(async() => {
                if (config.Verify())
                {
                    try
                    {
                        await Globals.CloudManager.InvokeAsync(x => x.ConnectToAzure(Globals.PersonalCloud.Value, name, config, shareScope)).ConfigureAwait(false);
                    }
                    finally
                    {
                        await Task.Delay(3000).ConfigureAwait(false);
                        Dispatcher.Invoke(() => {
                            ToggleBusyIndicator(false);
                            DismissDialog?.Invoke(this, EventArgs.Empty);
                        });
                    }
                }
                else
                {
                    Dispatcher.Invoke(() => {
                        Dispatcher.ShowAlert(UIStorage.ErrorAuthenticating, UIStorage.ErrorAuthenticatingAzure);
                        ToggleBusyIndicator(false);
                    });
                }
            });
        }
        private void VerifyCredentials(object sender, EventArgs e)
        {
            var name           = ServiceName.Text;
            var invalidCharHit = false;

            foreach (var character in VirtualFileSystem.InvalidCharacters)
            {
                if (name?.Contains(character) == true)
                {
                    invalidCharHit = true;
                }
            }
            if (string.IsNullOrEmpty(name) || invalidCharHit)
            {
                this.ShowAlert(this.Localize("Online.BadName"), this.Localize("Online.IllegalName"), action => {
                    ServiceName.BecomeFirstResponder();
                });
                return;
            }

            var endpoint = EndpointSuffix.Text;

            if (string.IsNullOrEmpty(endpoint))
            {
                this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadEndpoint"), action => {
                    EndpointSuffix.BecomeFirstResponder();
                });
                return;
            }

            var accountName = AccountName.Text;

            /*
             * if (string.IsNullOrEmpty(accountName) &&
             *  (endpoint == "core.windows.net" || endpoint == "blob.core.windows.net" ||
             *  endpoint == "core.chinacloudapi.cn" || endpoint == "blob.core.chinacloudapi.cn"))
             * {
             *  this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountName"), action => {
             *      AccountName.BecomeFirstResponder();
             *  });
             *  return;
             * }
             */

            var accessKey = AccountKey.Text;

            if (string.IsNullOrEmpty(accessKey))
            {
                this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountKey"), action => {
                    AccountKey.BecomeFirstResponder();
                });
                return;
            }

            var container = Container.Text;

            if (string.IsNullOrEmpty(container))
            {
                this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadContainer"), action => {
                    Container.BecomeFirstResponder();
                });
                return;
            }

            if (!Globals.Database.IsStorageNameUnique(name))
            {
                this.ShowAlert(this.Localize("Online.ServiceAlreadyExists"), this.Localize("Online.ChooseADifferentName"), action => {
                    ServiceName.BecomeFirstResponder();
                });
                return;
            }

            string connection;

            if (endpoint.Contains(accountName, StringComparison.Ordinal))
            {
                accountName = null;
            }
            if (endpoint.StartsWith("http://", StringComparison.Ordinal))
            {
                endpoint = endpoint.Replace("http://", "https://");
            }
            if (endpoint.StartsWith("https://", StringComparison.Ordinal))
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    if (string.IsNullOrEmpty(accessKey))
                    {
                        connection = endpoint;
                    }
                    else
                    {
                        connection = $"BlobEndpoint={endpoint};SharedAccessSignature={accessKey}";
                    }
                }
                else
                {
                    this.ShowAlert(this.Localize("Azure.BadAccount"), this.Localize("Azure.EndpointAndNameMismatch"));
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    this.ShowAlert(this.Localize("Online.BadCredential"), this.Localize("Azure.BadAccountName"));
                    return;
                }
                else
                {
                    connection = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accessKey};EndpointSuffix={endpoint}";
                }
            }

            var alert = UIAlertController.Create(this.Localize("Online.Verifying"), null, UIAlertControllerStyle.Alert);

            PresentViewController(alert, true, () => {
                Task.Run(() => {
                    var config = new AzureBlobConfig {
                        ConnectionString = connection,
                        BlobName         = container
                    };

                    if (config.Verify())
                    {
                        try
                        {
                            Globals.CloudManager.AddStorageProvider(Globals.CloudManager.PersonalClouds[0].Id, Guid.NewGuid(), name, config, visibility);
                            InvokeOnMainThread(() => {
                                DismissViewController(true, () => {
                                    NavigationController.DismissViewController(true, null);
                                });
                            });
                        }
                        catch
                        {
                            InvokeOnMainThread(() => {
                                DismissViewController(true, () => {
                                    this.ShowAlert(this.Localize("Azure.CannotAddService"), this.Localize("Error.Internal"));
                                });
                            });
                        }
                    }
                    else
                    {
                        InvokeOnMainThread(() => {
                            DismissViewController(true, () => {
                                this.ShowAlert(this.Localize("Error.Authentication"), this.Localize("Azure.Unauthorized"));
                            });
                        });
                    }
                });
            });
        }
Exemple #11
0
 public FileUtils(AzureBlobConfig config)
 {
     _config = config;
 }