Exemple #1
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (!this.IsVirtualNetworkGatewayPresent(ResourceGroupName, VirtualNetworkGatewayName))
            {
                throw new ArgumentException(Microsoft.Azure.Commands.Network.Properties.Resources.ResourceNotFound);
            }

            PSVpnClientParameters vpnClientParams = new PSVpnClientParameters();

            vpnClientParams.ProcessorArchitecture = this.ProcessorArchitecture;
            var vnetVpnClientParametersModel = Mapper.Map <MNM.VpnClientParameters>(vpnClientParams);

            //TODO:- This code is added just for current release of P2S feature as Generatevpnclientpackage API is broken & need to be fixed on server
            //side as well as in overall Poweshell flow
            //string packageUrl = this.VirtualNetworkGatewayClient.Generatevpnclientpackage(ResourceGroupName, VirtualNetworkGatewayName, vnetVpnClientParametersModel);

            string packageUrl = this.NetworkClient.Generatevpnclientpackage(ResourceGroupName, VirtualNetworkGatewayName, vnetVpnClientParametersModel);

            WriteObject(packageUrl);
        }
        public override void Execute()
        {
            base.Execute();
            string shouldProcessMessage = string.Format("Execute AzureRmVpnClientConfiguration for ResourceGroupName {0} VirtualNetworkGateway {1}", ResourceGroupName, Name);

            if (ShouldProcess(shouldProcessMessage, VerbsCommon.New))
            {
                PSVpnClientParameters vpnClientParams = new PSVpnClientParameters();

                vpnClientParams.ProcessorArchitecture = string.IsNullOrWhiteSpace(this.ProcessorArchitecture) ?
                                                        MNM.ProcessorArchitecture.Amd64.ToString() :
                                                        this.ProcessorArchitecture;

                vpnClientParams.AuthenticationMethod = string.IsNullOrWhiteSpace(this.AuthenticationMethod)
                    ? MNM.AuthenticationMethod.EAPTLS.ToString()
                    : this.AuthenticationMethod;

                // Read the radius server root certificate if present
                if (!string.IsNullOrWhiteSpace(this.RadiusRootCertificateFile))
                {
                    if (File.Exists(this.RadiusRootCertificateFile))
                    {
                        try
                        {
                            X509Certificate2 radiusRootCertificate = new X509Certificate2(this.RadiusRootCertificateFile);
                            vpnClientParams.RadiusServerAuthCertificate = Convert.ToBase64String(radiusRootCertificate.Export(X509ContentType.Cert));
                        }
                        catch (Exception)
                        {
                            WriteWarning("Invalid radius root certificate specified at path " + this.RadiusRootCertificateFile);
                        }
                    }
                    else
                    {
                        WriteWarning("Cannot find radius root certificate with path " + this.RadiusRootCertificateFile);
                    }
                }

                // Read the radius server root certificate if present
                if (this.ClientRootCertificateFileList != null)
                {
                    foreach (string clientRootCertPath in this.ClientRootCertificateFileList)
                    {
                        vpnClientParams.ClientRootCertificates = new List <string>();
                        if (File.Exists(clientRootCertPath))
                        {
                            try
                            {
                                X509Certificate2 clientRootCertificate = new X509Certificate2(clientRootCertPath);
                                vpnClientParams.ClientRootCertificates.Add(
                                    Convert.ToBase64String(clientRootCertificate.Export(X509ContentType.Cert)));
                            }
                            catch (Exception)
                            {
                                WriteWarning("Invalid cer file specified for client root certificate with path " +
                                             clientRootCertPath);
                            }
                        }
                        else
                        {
                            WriteWarning("Cannot find client root certificate with path " +
                                         clientRootCertPath);
                        }
                    }
                }

                var vnetVpnClientParametersModel = NetworkResourceManagerProfile.Mapper.Map <MNM.VpnClientParameters>(vpnClientParams);

                // There may be a required Json serialize for the package URL to conform to REST-API
                // The try-catch below handles the case till the change is made and deployed to PROD
                string serializedPackageUrl = this.NetworkClient.GenerateVpnProfile(this.ResourceGroupName, this.Name, vnetVpnClientParametersModel);
                string packageUrl           = string.Empty;
                try
                {
                    packageUrl = JsonConvert.DeserializeObject <string>(serializedPackageUrl);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    packageUrl = serializedPackageUrl;
                }

                PSVpnProfile vpnProfile = new PSVpnProfile()
                {
                    VpnProfileSASUrl = packageUrl
                };
                WriteObject(vpnProfile);
            }
        }