2016-11-21 6 views
1

Ich habe eine Netzwerkzugriffssteuerungsliste in meinem Cloud-Dienst ähnlich der folgenden. Wie konfiguriere ich das programmatisch statt aus der Konfigurationsdatei?Wie erstelle ich Zugriffskontrollregeln aus Code für einen azure Cloud-Dienst?

Einige dieser IP-Adressen können sich ändern. Ich möchte die IP-Adresse von einem Domänennamen aufzulösen und fügen Sie die Konfiguration:

<NetworkConfiguration> 
<AccessControls> 
    <AccessControl name="security"> 
    <Rule action="permit" description="Allow access from A" order="100" remoteSubnet="xxx.xxx.xxx.xxx/32" /> 
    <Rule action="permit" description="Allow access from B" order="200" remoteSubnet="xxx.xxx.xxx.xxx/32" /> 
    <Rule action="permit" description="Allow access from C" order="300" remoteSubnet="xxx.xxx.xxx.xxx/32" /> 
    <Rule action="deny" description="Deny access to everyone else" order="400" remoteSubnet="0.0.0.0/0" /> 
    </AccessControl> 
</AccessControls> 

Antwort

0

Ok. Ich habe am Ende eine Konsolenanwendung geschrieben, die während des Builds aufgerufen wird und die IP-Adresse des Remove-Cloud-Dienstes abruft und prüft, ob sie mit dem in der Konfigurationsdatei übereinstimmt.

Wenn nicht, dann aktualisiere ich es. Ziemlich einfach. Hier

ist der Befehl build:

$(SolutionDir)<MyProjectName>\$(OutDir)$(ConfigurationName)\MyExeName Update-FrontEnd-IPAddress-For-Azure-MicroService "$(SolutionDir)<AzureDeploymentProjectName>\ServiceConfiguration.Cloud.cscfg" 

Die Konsolenanwendung funktioniert:

 private static void HandleCheckRoleEnvironment(string[] args) 
     { 
      if (args[0] == "Check-Role-Environment") 
      { 
       Console.WriteLine("Found Command: Check-Role-Environment"); 

       if (RoleEnvironment.IsAvailable && !RoleEnvironment.IsEmulated) 
       { 
        Console.WriteLine("Running in Azure Cloud Environment"); 
        Environment.Exit(0); 
        return; 
       } 
       else 
       { 
        Console.WriteLine("NOT Running in Azure Cloud Environment"); 
        Environment.Exit(1); 
        return; 
       } 
      } 
     } 

Hier ist der Code die Config-Datei zu aktualisieren:

 private static void ExecuteUpdateFrontEndIPAddressForAzureMicroService(string configFilePath) 
     { 
      if (!File.Exists(configFilePath)) 
      { 
       return; 
      } 

      var ipAddressList = Dns.GetHostAddresses("MyDomainName"); 
      Console.WriteLine($"The IP address for MyDomainName is {ipAddressList[0].ToString()}"); 

      var correctValue = $"{ipAddressList[0].ToString()}/32"; 

      var document = new XmlDocument(); 
      document.Load(configFilePath); 

      //Rule nodes 
      var rules = document.ChildNodes[1].LastChild.FirstChild.FirstChild.ChildNodes; 

      var rule = (from XmlNode p in rules 
         where p.Attributes["description"].Value == "Allow access from MyDomainName" 
         select p).FirstOrDefault(); 

      var ipAddressValue = rule.Attributes["remoteSubnet"].Value; 
      Console.WriteLine($"The IP address in the config file is {ipAddressValue}"); 

      if (correctValue != ipAddressValue) 
      { 
       rule.Attributes["remoteSubnet"].Value = correctValue; 
       document.Save(configFilePath); 

       Console.WriteLine("The config file has been updated with the correct IP address."); 
      } 
      else 
      { 
       Console.WriteLine("The config file is upto date and will not be updated."); 
      } 
     } 
Verwandte Themen