private void CreateConfiguration()
        {
            var publicSettings  = new DscPublicSettings();
            var privateSettings = new DscPrivateSettings();

            publicSettings.ProtocolVersion = CurrentProtocolVersion;

            if (!string.IsNullOrEmpty(this.ConfigurationArchive))
            {
                ConfigurationUris configurationUris = UploadConfigurationDataToBlob();

                publicSettings.SasToken              = configurationUris.SasToken;
                publicSettings.ModulesUrl            = configurationUris.ModulesUrl;
                publicSettings.ConfigurationFunction = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}\\{1}",
                    Path.GetFileNameWithoutExtension(this.ConfigurationArchive),
                    this.ConfigurationName);
                Tuple <DscPublicSettings.Property[], Hashtable> settings =
                    DscSettingsSerializer.SeparatePrivateItems(this.ConfigurationArgument);
                publicSettings.Properties = settings.Item1;
                privateSettings.Items     = settings.Item2;

                privateSettings.DataBlobUri = configurationUris.DataBlobUri;
            }

            //
            // Define the public and private property bags that will be passed to the extension.
            //
            this.PublicConfiguration = DscSettingsSerializer.SerializePublicSettings(publicSettings);
            //
            // PrivateConfuguration contains sensitive data in a plain text.
            //
            this.PrivateConfiguration = DscSettingsSerializer.SerializePrivateSettings(privateSettings);
        }
        private void CreateConfiguration()
        {
            var publicSettings = new DscPublicSettings();
            var privateSettings = new DscPrivateSettings();
            publicSettings.ProtocolVersion = CurrentProtocolVersion;

            if (!string.IsNullOrEmpty(this.ConfigurationArchive))
            {
                ConfigurationUris configurationUris = UploadConfigurationDataToBlob();

                publicSettings.SasToken = configurationUris.SasToken;
                publicSettings.ModulesUrl = configurationUris.ModulesUrl;
                publicSettings.ConfigurationFunction = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}\\{1}",
                    Path.GetFileNameWithoutExtension(this.ConfigurationArchive),
                    this.ConfigurationName);
                Tuple<DscPublicSettings.Property[], Hashtable> settings =
                    DscSettingsSerializer.SeparatePrivateItems(this.ConfigurationArgument);
                publicSettings.Properties = settings.Item1;
                privateSettings.Items = settings.Item2;

                privateSettings.DataBlobUri = configurationUris.DataBlobUri;
            }

            //
            // Define the public and private property bags that will be passed to the extension.
            //
            this.PublicConfiguration = DscSettingsSerializer.SerializePublicSettings(publicSettings);
            //
            // PrivateConfuguration contains sensitive data in a plain text.
            //
            this.PrivateConfiguration = DscSettingsSerializer.SerializePrivateSettings(privateSettings);
        }
예제 #3
0
        private void CreateConfiguration()
        {
            var publicSettings = new DscPublicSettings();

            var privateSettings = new DscPrivateSettings();

            if (!string.IsNullOrEmpty(this.ConfigurationArchive))
            {
                //
                // Get a reference to the container in blob storage
                //
                var storageAccount = string.IsNullOrEmpty(this.StorageEndpointSuffix)
                                   ? new CloudStorageAccount(this._storageCredentials, true)
                                   : new CloudStorageAccount(this._storageCredentials, this.StorageEndpointSuffix, true);

                var blobClient = storageAccount.CreateCloudBlobClient();

                var containerReference = blobClient.GetContainerReference(this.ContainerName);

                //
                // Get a reference to the configuration blob and create a SAS token to access it
                //
                var blobAccessPolicy = new SharedAccessBlobPolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    Permissions            = SharedAccessBlobPermissions.Read
                };

                var configurationBlobName = this.ConfigurationArchive;

                var configurationBlobReference = containerReference.GetBlockBlobReference(configurationBlobName);

                var configurationBlobSasToken = configurationBlobReference.GetSharedAccessSignature(blobAccessPolicy);

                //
                // Upload the configuration data to blob storage and get a SAS token
                //
                string configurationDataBlobUri = null;

                if (this.ConfigurationDataPath != null)
                {
                    this.ConfirmAction(true, string.Empty, string.Format(CultureInfo.CurrentUICulture, Resources.AzureVMDscUploadToBlobStorageAction, this.ConfigurationDataPath), configurationBlobReference.Uri.AbsoluteUri, () =>
                    {
                        var guid = Guid.NewGuid(); // there may be multiple VMs using the same configuration

                        var configurationDataBlobName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}.psd1", this.ConfigurationName, guid);

                        var configurationDataBlobReference = containerReference.GetBlockBlobReference(configurationDataBlobName);

                        if (!this.Force && configurationDataBlobReference.Exists())
                        {
                            this.ThrowTerminatingError(
                                new ErrorRecord(
                                    new UnauthorizedAccessException(string.Format(CultureInfo.CurrentUICulture, Resources.AzureVMDscStorageBlobAlreadyExists, configurationDataBlobName)),
                                    string.Empty,
                                    ErrorCategory.PermissionDenied,
                                    null));
                        }

                        configurationDataBlobReference.UploadFromFile(this.ConfigurationDataPath, FileMode.Open);

                        var configurationDataBlobSasToken = configurationDataBlobReference.GetSharedAccessSignature(blobAccessPolicy);

                        configurationDataBlobUri = configurationDataBlobReference.StorageUri.PrimaryUri.AbsoluteUri + configurationDataBlobSasToken;
                    });
                }

                publicSettings.SasToken              = configurationBlobSasToken;
                publicSettings.ModulesUrl            = configurationBlobReference.StorageUri.PrimaryUri.AbsoluteUri;
                publicSettings.ConfigurationFunction = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", Path.GetFileNameWithoutExtension(this.ConfigurationArchive), this.ConfigurationName);
                publicSettings.Properties            = this.ConfigurationArgument;

                privateSettings.DataBlobUri = configurationDataBlobUri;
            }

            //
            // Define the public and private property bags that will be passed to the extension.
            //
            this.PublicConfiguration  = JsonUtilities.TryFormatJson(JsonConvert.SerializeObject(publicSettings));
            this.PrivateConfiguration = JsonUtilities.TryFormatJson(JsonConvert.SerializeObject(privateSettings));
        }
        /// <summary>
        /// Helper function for tests.
        /// </summary>
        /// <param name="configurationArguments"></param>
        /// <param name="privateSettings"></param>
        /// <returns></returns>
        private static DscPublicSettings GetPublicPrivateAfterDeseriazlization(
            Hashtable configurationArguments,
            out DscPrivateSettings privateSettings)
        {
            Tuple<DscPublicSettings.Property[], Hashtable> separatedSettings =
                DscSettingsSerializer.SeparatePrivateItems(configurationArguments);
            DscPublicSettings publicSettings = new DscPublicSettings();
            privateSettings = new DscPrivateSettings();
            publicSettings.Properties = separatedSettings.Item1;
            privateSettings.Items = separatedSettings.Item2;

            string serializedPublic = DscSettingsSerializer.SerializePublicSettings(publicSettings);
            string serializedPrivate = DscSettingsSerializer.SerializePrivateSettings(privateSettings);

            publicSettings = DscSettingsSerializer.DeserializePublicSettings(serializedPublic);
            privateSettings = DeserializePrivateSettings(serializedPrivate);
            return publicSettings;
        }
        private void CreateConfiguration()
        {
            var publicSettings = new DscPublicSettings();
            
            var privateSettings = new DscPrivateSettings();

            if (!string.IsNullOrEmpty(this.ConfigurationArchive))
            {
                //
                // Get a reference to the container in blob storage
                //
                var storageAccount = string.IsNullOrEmpty(this.StorageEndpointSuffix)
                                   ? new CloudStorageAccount(this._storageCredentials, true)
                                   : new CloudStorageAccount(this._storageCredentials, this.StorageEndpointSuffix, true);

                var blobClient = storageAccount.CreateCloudBlobClient();

                var containerReference = blobClient.GetContainerReference(this.ContainerName);
            
                //
                // Get a reference to the configuration blob and create a SAS token to access it
                //
                var blobAccessPolicy = new SharedAccessBlobPolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    Permissions = SharedAccessBlobPermissions.Read
                };

                var configurationBlobName = this.ConfigurationArchive;

                var configurationBlobReference = containerReference.GetBlockBlobReference(configurationBlobName);

                var configurationBlobSasToken = configurationBlobReference.GetSharedAccessSignature(blobAccessPolicy);

                //
                // Upload the configuration data to blob storage and get a SAS token
                //
                string configurationDataBlobUri = null;

                if (this.ConfigurationDataPath != null)
                {
                    this.ConfirmAction(true, string.Empty, string.Format(CultureInfo.CurrentUICulture, Resources.AzureVMDscUploadToBlobStorageAction, this.ConfigurationDataPath), configurationBlobReference.Uri.AbsoluteUri, ()=>
                    {
                        var guid = Guid.NewGuid(); // there may be multiple VMs using the same configuration

                        var configurationDataBlobName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}.psd1", this.ConfigurationName, guid);

                        var configurationDataBlobReference = containerReference.GetBlockBlobReference(configurationDataBlobName);

                        if (!this.Force && configurationDataBlobReference.Exists())
                        {
                            this.ThrowTerminatingError(
                                new ErrorRecord(
                                    new UnauthorizedAccessException(string.Format(CultureInfo.CurrentUICulture, Resources.AzureVMDscStorageBlobAlreadyExists, configurationDataBlobName)),
                                    string.Empty,
                                    ErrorCategory.PermissionDenied,
                                    null));
                        }

                        configurationDataBlobReference.UploadFromFile(this.ConfigurationDataPath, FileMode.Open);

                        var configurationDataBlobSasToken = configurationDataBlobReference.GetSharedAccessSignature(blobAccessPolicy);

                        configurationDataBlobUri = configurationDataBlobReference.StorageUri.PrimaryUri.AbsoluteUri + configurationDataBlobSasToken;
                    });
                }

                publicSettings.SasToken              = configurationBlobSasToken;
                publicSettings.ModulesUrl            = configurationBlobReference.StorageUri.PrimaryUri.AbsoluteUri;
                publicSettings.ConfigurationFunction = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", Path.GetFileNameWithoutExtension(this.ConfigurationArchive), this.ConfigurationName);
                publicSettings.Properties            = this.ConfigurationArgument;

                privateSettings.DataBlobUri = configurationDataBlobUri;
            }

            //
            // Define the public and private property bags that will be passed to the extension.
            //
            this.PublicConfiguration = JsonUtilities.TryFormatJson(JsonConvert.SerializeObject(publicSettings));
            this.PrivateConfiguration = JsonUtilities.TryFormatJson(JsonConvert.SerializeObject(privateSettings));
        }