public void GetWhiteListIps(ref ProviderData providerData)
        {
            var numberOfWhiteListIps = _xenStore.Read(Constants.Combine(Constants.ReadOnlyDataConfigBase, Constants.ProviderDataBase, Constants.IpWhiteList));

            if (numberOfWhiteListIps != null && numberOfWhiteListIps.Any())
            {
                providerData.ip_whitelist.AddRange(numberOfWhiteListIps);

                foreach (var numberOfWhiteListIp in numberOfWhiteListIps)
                {
                    var jsonData = _xenStore.ReadVmProviderDataKey((Constants.Combine(Constants.IpWhiteList, numberOfWhiteListIp)));
                    if (!string.IsNullOrEmpty(jsonData)) providerData.white_List_Ips.Add(jsonData);
                }
            }
        }
        public void Setup()
        {
            xenNetworkInformation = MockRepository.GenerateMock<IXenNetworkInformation>();
            setNetworkInterface = MockRepository.GenerateMock<ISetNetworkInterface>();
            setNetworkRoutes = MockRepository.GenerateMock<ISetNetworkRoutes>();

            xenProviderDataInformation = MockRepository.GenerateMock<IXenProviderDataInformation>();
            setProviderData = MockRepository.GenerateMock<ISetProviderData>();
            setHostname = MockRepository.GenerateMock<ISetHostnameAction>();
            _xenStore = MockRepository.GenerateMock<IXenStore>();

            networkInterface = new NetworkInterface();
            network = new Network();
            network.Interfaces.Add("fakemac", networkInterface);

            providerData = new ProviderData();

            command = new ResetNetwork(setNetworkInterface, xenNetworkInformation, setNetworkRoutes, setProviderData, xenProviderDataInformation, setHostname, _xenStore);
        }
        public void Setup()
        {
            xenNetworkInformation = MockRepository.GenerateMock<IXenNetworkInformation>();
            setNetworkInterface = MockRepository.GenerateMock<ISetNetworkInterface>();
            setNetworkRoutes = MockRepository.GenerateMock<ISetNetworkRoutes>();

            xenProviderDataInformation = MockRepository.GenerateMock<IXenProviderDataInformation>();
            setProviderData = MockRepository.GenerateMock<ISetProviderData>();

            networkInterface = new NetworkInterface();
            network = new Network();
            network.Interfaces.Add("fakemac", networkInterface);

            providerData = new ProviderData();

            command = new ResetNetwork(setNetworkInterface, xenNetworkInformation, setNetworkRoutes, setProviderData, xenProviderDataInformation);
            xenNetworkInformation.Stub(x => x.Get()).Return(network);
            xenProviderDataInformation.Stub(x => x.Get()).Return(providerData);

            result = command.Execute(null);
        }
        public void Execute(ProviderData providerData, List<string> userMetadata)
        {
            string logMessage = string.Format(" Provider Data Deserialzed : {0}", new Json<ProviderData>().Serialize(providerData));

            _logger.Log(logMessage);

            if (CheckRoleNameMatch(providerData, userMetadata))
            {
                if (providerData.white_List_Ips.Count > 0)
                {
                    var command = string.Empty;
                    if (_netshFirewallRuleNameAvailable.IsRuleAvailable(Constants.SoftwareFirewallRuleName))
                    {
                        command = string.Format(
                            "advfirewall firewall set rule name=\"{0}\" new enable=yes remoteip={1}",
                            Constants.SoftwareFirewallRuleName, string.Join(",", providerData.white_List_Ips.ToArray()));
                    }
                    else
                    {
                        command =
                            string.Format(
                                "advfirewall firewall add rule name=\"{0}\" enable=yes dir=in profile=public,private,domain localip=any remoteip={1} protocol=tcp localport=445 remoteport=any edge=no action=allow",
                                Constants.SoftwareFirewallRuleName,
                                string.Join(",", providerData.white_List_Ips.ToArray()));

                    }
                    _executableProcessQueue.Enqueue("netsh", command);
                    _executableProcessQueue.Go();
                }
                else
                {
                    _logger.Log("White List Ips not available. Firewall rules will not be added/updated.");
                }
            }
            else
            {
                _logger.Log(string.Format("Role Names did not match. Roles names from provider data {0}. Role names from configuration {1}. UserMetadata:{2}",
                                            string.Join(",", providerData.roles.ToArray()), string.Join(",", GetFirewallRoles().ToArray()), string.Join(",", userMetadata.ToArray())));
            }
        }
        private bool CheckRoleNameMatch(ProviderData providerData, List<string> userMetadata)
        {
            var result = false;

            var configFirewallRoles = GetFirewallRoles();

            _logger.Log(string.Format("Validating Roles: {0}.", string.Join(",", providerData.roles.ToArray())));
            foreach (var roleName in providerData.roles)
            {
                if (configFirewallRoles.Any(configRoleName => string.Equals(roleName, configRoleName, StringComparison.OrdinalIgnoreCase)))
                {
                    result = true;
                }
            }
            _logger.Log(string.Format("Roles did not match, validating user-metadata:{0}", string.Join(",", userMetadata.ToArray())));
            if (userMetadata.Contains(SvcConfiguration.FirewallMetadataKey))
                result = true;

            return result;
        }
        internal void Setup()
        {
            NetshFirewallRuleNameAvailable = MockRepository.GenerateMock<INetshFirewallRuleNameAvailable>();
            XenUserMetadata = MockRepository.GenerateMock<IXenUserMetadata>();
            Logger = MockRepository.GenerateMock<ILogger>();

            XenUserMetadata.Stub(a => a.GetKeys()).Return(new List<string>());
            ExecutableProcessQueue = MockRepository.GenerateMock<IExecutableProcessQueue>();
            ExecutableProcessQueue.Stub(x => x.Enqueue(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return(
                ExecutableProcessQueue);

            SetProviderData = new SetProviderData(ExecutableProcessQueue, NetshFirewallRuleNameAvailable, Logger);
            UserMetadata = new List<string>();
            ProviderData = GetProviderDataWithFakeRoles();
        }
        private bool CheckRoleNameMatch(ProviderData providerData)
        {
            var result = false;

            var configFirewallRoles = GetFirewallRoles();

            foreach (var roleName in providerData.roles)
            {
                if (configFirewallRoles.Any(configRoleName => string.Equals(roleName, configRoleName, StringComparison.OrdinalIgnoreCase)))
                {
                    result = true;
                }
            }

            return result;
        }
        public void should_serialize_partial_provider_data()
        {
            var providerData = new ProviderData()
            {
                provider = "TheDarkKnightRises",
                region = "Gotham City",
                ip_whitelist = new List<string>() { "" },
                white_List_Ips = new List<string>() { },
                roles = new List<string>() { "rav_connect", "rav_managed" }
            };

            var serialzed = _jsonProviderData.Serialize(providerData);

            Assert.That(serialzed, Is.EqualTo("{\"region\":\"Gotham City\",\"roles\":[\"rav_connect\",\"rav_managed\"],\"ip_whitelist\":[\"\"],\"provider\":\"TheDarkKnightRises\",\"white_List_Ips\":[]}"));
        }
        private ProviderData GetProviderData()
        {
            var providerData = new ProviderData();

            GetProviderName(ref providerData);
            GetRoles(ref providerData);
            GetRegion(ref providerData);
            GetWhiteListIps(ref providerData);
            return providerData;
        }
 public void GetRoles(ref ProviderData providerData)
 {
     var jsonData = _xenStore.ReadVmProviderDataKey(Constants.Roles);
     providerData.roles = new Json<List<string>>().Deserialize(jsonData);
 }
 public void GetRegion(ref ProviderData providerData)
 {
     providerData.region = _xenStore.ReadVmProviderDataKey(Constants.Region);
 }
 public void GetProviderName(ref ProviderData providerData)
 {
     providerData.provider = _xenStore.ReadVmProviderDataKey(Constants.Provider);
 }