Esempio n. 1
0
        /// <summary>
        /// Convert hashtable of public settings into two parts:
        /// 1) Array of public settings in format:
        /// [
        ///             {
        ///                 "Name":  "String Parameter",
        ///                 "Value":  "String Value",
        ///                 "TypeName":  "System.String"
        ///             }
        /// ]
        /// 2) Private settings hashtable. We extract all sensitive information (like password from PSCredential)
        ///    and store it in private settings. Public settings will reference them in form:
        ///             {
        ///                 "Name":  "AdminCredential",
        ///                 "Value":
        ///                 {
        ///                     "Password" : "PrivateSettings:28AC4D36-A99B-41DE-8421-2BCC1C7C1A3B"
        ///                     "UserName" : "DOMAIN\LOGIN"
        ///                 },
        ///                 "TypeName":  "System.Management.Automation.PSCredential"
        ///             }
        /// and private hashtable will look like that:
        /// {
        ///     "28AC4D36-A99B-41DE-8421-2BCC1C7C1A3B" : "password"
        /// }
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns>tuple of array (public settings) and hashtable (private settings)</returns>
        public static Tuple <DscExtensionPublicSettings.Property[], Hashtable> SeparatePrivateItems(Hashtable arguments)
        {
            var publicSettings  = new List <DscExtensionPublicSettings.Property>();
            var privateSettings = new Hashtable();

            if (arguments != null)
            {
                foreach (DictionaryEntry argument in arguments)
                {
                    object entryValue = argument.Value;
                    string entryType  = argument.Value == null ? "null" : argument.Value.GetType().ToString();
                    string entryName  = argument.Key.ToString();
                    // Special case for PSCredential
                    PSCredential credential = argument.Value as PSCredential;
                    if (credential == null)
                    {
                        PSObject psObject = argument.Value as PSObject;
                        if (psObject != null)
                        {
                            credential = psObject.BaseObject as PSCredential;
                        }
                    }
                    if (credential != null)
                    {
                        // plainTextPassword is a string object with sensitive information  in plain text.
                        // We pass it to 3rd party serializer which may create copies of the string.
                        string plainTextPassword = ConvertToUnsecureString(credential.Password);
                        string userName          = credential.UserName;
                        string passwordRef       = Guid.NewGuid().ToString();
                        privateSettings.Add(passwordRef, plainTextPassword);
                        var newValue = new Hashtable();
                        newValue["UserName"] = String.Format(CultureInfo.InvariantCulture, userName);
                        newValue["Password"] = String.Format(CultureInfo.InvariantCulture, "PrivateSettingsRef:{0}",
                                                             passwordRef);
                        entryValue = newValue;
                        entryType  = typeof(PSCredential).ToString();
                    }

                    var entry = new DscExtensionPublicSettings.Property
                    {
                        Name     = entryName,
                        TypeName = entryType,
                        Value    = entryValue,
                    };
                    publicSettings.Add(entry);
                }
            }
            return(new Tuple <DscExtensionPublicSettings.Property[], Hashtable>(publicSettings.ToArray(), privateSettings));
        }
         /// <summary>
        /// Convert hashtable of public settings into two parts:
        /// 1) Array of public settings in format:
        /// [
        ///             {
        ///                 "Name":  "String Parameter",
        ///                 "Value":  "String Value",
        ///                 "TypeName":  "System.String"
        ///             }
        /// ]
        /// 2) Private settings hashtable. We extract all sensitive information (like password from PSCredential)
        ///    and store it in private settings. Public settings will reference them in form:
        ///             {
        ///                 "Name":  "AdminCredential",
        ///                 "Value":  
        ///                 {
        ///                     "Password" : "PrivateSettings:28AC4D36-A99B-41DE-8421-2BCC1C7C1A3B"
        ///                     "UserName" : "DOMAIN\LOGIN"
        ///                 },
        ///                 "TypeName":  "System.Management.Automation.PSCredential"
        ///             }
        /// and private hashtable will look like that:
        /// {
        ///     "28AC4D36-A99B-41DE-8421-2BCC1C7C1A3B" : "password"
        /// }
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns>tuple of array (public settings) and hashtable (private settings)</returns>
        public static Tuple<DscExtensionPublicSettings.Property[], Hashtable> SeparatePrivateItems(Hashtable arguments)
        {
            var publicSettings = new List<DscExtensionPublicSettings.Property>();
            var privateSettings = new Hashtable();
            if (arguments != null)
            {
                foreach (DictionaryEntry argument in arguments)
                {
                    object entryValue = argument.Value;
                    string entryType = argument.Value == null ? "null" : argument.Value.GetType().ToString();
                    string entryName = argument.Key.ToString();
                    // Special case for PSCredential
                    PSCredential credential = argument.Value as PSCredential;
                    if (credential == null)
                    {
                        PSObject psObject = argument.Value as PSObject;
                        if (psObject != null)
                        {
                            credential = psObject.BaseObject as PSCredential;
                        }
                    }
                    if (credential != null)
                    {
                        // plainTextPassword is a string object with sensitive information  in plain text. 
                        // We pass it to 3rd party serializer which may create copies of the string.
                        string plainTextPassword = ConvertToUnsecureString(credential.Password);
                        string userName = credential.UserName;
                        string passwordRef = Guid.NewGuid().ToString();
                        privateSettings.Add(passwordRef, plainTextPassword);
                        var newValue = new Hashtable();
                        newValue["UserName"] = String.Format(CultureInfo.InvariantCulture, userName);
                        newValue["Password"] = String.Format(CultureInfo.InvariantCulture, "PrivateSettingsRef:{0}",
                            passwordRef);
                        entryValue = newValue;
                        entryType = typeof (PSCredential).ToString();
                    }

                    var entry = new DscExtensionPublicSettings.Property
                    {
                        Name = entryName,
                        TypeName = entryType,
                        Value = entryValue,
                    };
                    publicSettings.Add(entry);
                }
            }
            return new Tuple<DscExtensionPublicSettings.Property[], Hashtable>(publicSettings.ToArray(), privateSettings);
        }