Exemple #1
0
        internal static StorageCredentials GetStorageCredentials(this AzurePSCmdlet cmdlet, String resourceGroupName, String storageAccountName)
        {
            StorageCredentials credentials = null;
            var storageClient = GetStorageClient(cmdlet);

            if (storageClient != null && storageClient.StorageAccounts != null)
            {
                var keys = storageClient.StorageAccounts.ListKeys(resourceGroupName, storageAccountName);

                if (keys != null && keys.StorageAccountKeys != null)
                {
                    var storageAccountKey = string.IsNullOrEmpty(keys.StorageAccountKeys.Key1) ? keys.StorageAccountKeys.Key2 : keys.StorageAccountKeys.Key1;

                    credentials = new StorageCredentials(storageAccountName, storageAccountKey);
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Properties.Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        "CredentialsNotFound",
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                ThrowInvalidArgumentError(cmdlet, Properties.Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return(credentials);
        }
 internal static void ThrowInvalidArgumentError(this AzurePSCmdlet cmdlet, string format, params object[] args)
 {
     cmdlet.ThrowTerminatingError(
         new ErrorRecord(
             new ArgumentException(string.Format(CultureInfo.CurrentUICulture, format, args)),
             "InvalidArgument",
             ErrorCategory.InvalidArgument,
             null));
 }
        /// <summary>
        /// Attempts to get the user's credentials from the given Storage Context or the current subscription, if the former is null.
        /// Throws a terminating error if the credentials cannot be determined.
        /// </summary>
        internal static StorageCredentials GetStorageCredentials(this AzurePSCmdlet cmdlet, AzureStorageContext storageContext)
        {
            StorageCredentials credentials = null;

            if (storageContext != null)
            {
                credentials = storageContext.StorageAccount.Credentials;
            }
            else
            {
                var storageAccountName = cmdlet.Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);

                var storageClient = AzureSession.ClientFactory.CreateClient <StorageManagementClient>(
                    cmdlet.Profile, cmdlet.Profile.Context.Subscription, AzureEnvironment.Endpoint.ServiceManagement);

                if (!string.IsNullOrEmpty(storageAccountName) && storageClient != null)
                {
                    var keys = storageClient.StorageAccounts.GetKeys(storageAccountName);

                    if (keys != null)
                    {
                        var storageAccountKey = string.IsNullOrEmpty(keys.PrimaryKey) ? keys.SecondaryKey : keys.PrimaryKey;

                        credentials = new StorageCredentials(storageAccountName, storageAccountKey);
                    }
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        "CredentialsNotFound",
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                ThrowInvalidArgumentError(cmdlet, Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return(credentials);
        }