internal PsApiManagementHostnameConfiguration(HostnameConfiguration hostnameConfigurationResource)
            : this()
        {
            if (hostnameConfigurationResource == null)
            {
                throw new ArgumentNullException("hostnameConfigurationResource");
            }

            HostnameCertificate = new PsApiManagementHostnameCertificate(hostnameConfigurationResource.Certificate);
        }
Ejemplo n.º 2
0
        public static HostnameConfiguration GetHostnameConfiguration(
            this PsApiManagementCustomHostNameConfiguration hostnameConfig)
        {
            if (hostnameConfig == null)
            {
                return(null);
            }

            var hostnameConfiguration = new HostnameConfiguration(
                Mappers.MapHostnameType(hostnameConfig.HostnameType),
                hostnameConfig.Hostname);

            if (!string.IsNullOrWhiteSpace(hostnameConfig.EncodedCertificate))
            {
                hostnameConfiguration.EncodedCertificate = hostnameConfig.EncodedCertificate;
            }

            if (hostnameConfig.CertificatePassword != null)
            {
                hostnameConfiguration.CertificatePassword = hostnameConfig.CertificatePassword;
            }

            if (!string.IsNullOrWhiteSpace(hostnameConfig.KeyVaultId))
            {
                hostnameConfiguration.KeyVaultId = hostnameConfig.KeyVaultId;
            }

            if (!string.IsNullOrWhiteSpace(hostnameConfig.IdentityClientId))
            {
                hostnameConfiguration.IdentityClientId = hostnameConfig.IdentityClientId;
            }

            if (hostnameConfig.DefaultSslBinding.HasValue)
            {
                hostnameConfiguration.DefaultSslBinding = hostnameConfig.DefaultSslBinding.Value;
            }

            if (hostnameConfig.NegotiateClientCertificate.HasValue)
            {
                hostnameConfiguration.NegotiateClientCertificate = hostnameConfig.NegotiateClientCertificate.Value;
            }

            // if no new certificate is provided, then copy over details of existing certificate
            if (hostnameConfig.CertificateInformation != null &&
                string.IsNullOrEmpty(hostnameConfig.EncodedCertificate))
            {
                hostnameConfiguration.Certificate = hostnameConfig.CertificateInformation.GetCertificateInformation();
            }

            return(hostnameConfiguration);
        }
Ejemplo n.º 3
0
        internal PsApiManagementCustomHostNameConfiguration(HostnameConfiguration hostnameConfigurationResource)
            : this()
        {
            if (hostnameConfigurationResource == null)
            {
                throw new ArgumentNullException("hostnameConfigurationResource");
            }

            CertificateInformation = hostnameConfigurationResource.Certificate != null ? new PsApiManagementCertificateInformation(hostnameConfigurationResource.Certificate) : null;
            Hostname                   = hostnameConfigurationResource.HostName;
            KeyVaultId                 = hostnameConfigurationResource.KeyVaultId;
            DefaultSslBinding          = hostnameConfigurationResource.DefaultSslBinding;
            NegotiateClientCertificate = hostnameConfigurationResource.NegotiateClientCertificate;
            HostnameType               = Mappers.MapHostnameType(hostnameConfigurationResource.Type);
        }
Ejemplo n.º 4
0
        public static HostnameConfiguration GetHostnameConfiguration(
            this PsApiManagementCustomHostNameConfiguration hostnameConfig)
        {
            if (hostnameConfig == null)
            {
                return(null);
            }

            var hostnameConfiguration = new HostnameConfiguration(
                Mappers.MapHostnameType(hostnameConfig.HostnameType),
                hostnameConfig.Hostname);

            if (!string.IsNullOrWhiteSpace(hostnameConfig.EncodedCertificate))
            {
                hostnameConfiguration.EncodedCertificate = hostnameConfig.EncodedCertificate;
            }

            if (hostnameConfig.CertificatePassword != null)
            {
                hostnameConfiguration.CertificatePassword = hostnameConfig.CertificatePassword;
            }

            if (!string.IsNullOrWhiteSpace(hostnameConfig.KeyVaultId))
            {
                hostnameConfiguration.KeyVaultId = hostnameConfig.KeyVaultId;
            }

            if (hostnameConfig.DefaultSslBinding.HasValue)
            {
                hostnameConfiguration.DefaultSslBinding = hostnameConfig.DefaultSslBinding.Value;
            }

            if (hostnameConfig.NegotiateClientCertificate.HasValue)
            {
                hostnameConfiguration.NegotiateClientCertificate = hostnameConfig.NegotiateClientCertificate.Value;
            }

            return(hostnameConfiguration);
        }
        public void CreateMultiHostNameService()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);

                testBase.serviceProperties.Sku.Name = SkuType.Premium;
                var hostnameConfig1 = new HostnameConfiguration()
                {
                    Type                = HostnameType.Proxy,
                    HostName            = "gateway1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword,
                    DefaultSslBinding   = true
                };

                var hostnameConfig2 = new HostnameConfiguration()
                {
                    Type                       = HostnameType.Proxy,
                    HostName                   = "gateway2.msitesting.net",
                    EncodedCertificate         = testBase.base64EncodedTestCertificateData,
                    CertificatePassword        = testBase.testCertificatePassword,
                    NegotiateClientCertificate = true
                };

                var hostnameConfig3 = new HostnameConfiguration()
                {
                    Type                = HostnameType.Portal,
                    HostName            = "portal1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword
                };

                var hostnameConfig4 = new HostnameConfiguration()
                {
                    Type                = HostnameType.DeveloperPortal,
                    HostName            = "devportal1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword
                };

                testBase.serviceProperties.HostnameConfigurations = new List <HostnameConfiguration>
                {
                    hostnameConfig1,
                    hostnameConfig2,
                    hostnameConfig3,
                    hostnameConfig4
                };

                var base64ArrayCertificate = Convert.FromBase64String(testBase.base64EncodedTestCertificateData);
                var cert = new X509Certificate2(base64ArrayCertificate, testBase.testCertificatePassword);

                var createdService = testBase.client.ApiManagementService.CreateOrUpdate(
                    resourceGroupName: testBase.rgName,
                    serviceName: testBase.serviceName,
                    parameters: testBase.serviceProperties);

                ValidateService(createdService,
                                testBase.serviceName,
                                testBase.rgName,
                                testBase.subscriptionId,
                                testBase.location,
                                testBase.serviceProperties.PublisherEmail,
                                testBase.serviceProperties.PublisherName,
                                testBase.serviceProperties.Sku.Name,
                                testBase.tags);

                Assert.NotNull(createdService.HostnameConfigurations);
                Assert.Equal(5, createdService.HostnameConfigurations.Count()); // customhostname config + 1 default proxy
                var defaultHostname = new Uri(createdService.GatewayUrl).Host;
                var hostnameConfigurationToValidate = createdService.HostnameConfigurations
                                                      .Where(h => !h.HostName.Equals(defaultHostname, StringComparison.InvariantCultureIgnoreCase));

                foreach (HostnameConfiguration hostnameConfig in hostnameConfigurationToValidate)
                {
                    var hostnameConfiguration = createdService.HostnameConfigurations
                                                .SingleOrDefault(h => hostnameConfig.HostName.Equals(h.HostName));
                    Assert.NotNull(hostnameConfiguration);
                    Assert.Equal(hostnameConfig.Type, hostnameConfiguration.Type);
                    Assert.NotNull(hostnameConfiguration.Certificate);
                    Assert.NotNull(hostnameConfiguration.Certificate.Subject);
                    Assert.Equal(cert.Thumbprint, hostnameConfiguration.Certificate.Thumbprint);

                    if (HostnameType.Proxy == hostnameConfiguration.Type)
                    {
                        Assert.True(hostnameConfiguration.DefaultSslBinding);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.DefaultSslBinding);
                    }

                    if (hostnameConfig2.HostName.Equals(hostnameConfiguration.HostName))
                    {
                        Assert.True(hostnameConfiguration.NegotiateClientCertificate);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.NegotiateClientCertificate);
                    }
                }

                // update the service
                int intialTagsCount = createdService.Tags.Count;
                createdService.Tags.Add("client", "test");
                var updatedService = testBase.client.ApiManagementService.CreateOrUpdate(testBase.rgName,
                                                                                         testBase.serviceName,
                                                                                         createdService);
                Assert.NotNull(updatedService);
                Assert.NotEmpty(updatedService.Tags);
                Assert.Equal(intialTagsCount + 1, updatedService.Tags.Count);
                Assert.Equal(5, updatedService.HostnameConfigurations.Count());

                hostnameConfigurationToValidate = updatedService.HostnameConfigurations
                                                  .Where(h => !h.HostName.Equals(defaultHostname, StringComparison.InvariantCultureIgnoreCase));
                foreach (HostnameConfiguration hostnameConfig in hostnameConfigurationToValidate)
                {
                    var hostnameConfiguration = updatedService.HostnameConfigurations
                                                .SingleOrDefault(h => hostnameConfig.HostName.Equals(h.HostName));
                    Assert.NotNull(hostnameConfiguration);
                    Assert.Equal(hostnameConfig.Type, hostnameConfiguration.Type);
                    Assert.NotNull(hostnameConfiguration.Certificate);
                    Assert.NotNull(hostnameConfiguration.Certificate.Subject);
                    Assert.Equal(cert.Thumbprint, hostnameConfiguration.Certificate.Thumbprint);

                    if (HostnameType.Proxy == hostnameConfiguration.Type)
                    {
                        Assert.True(hostnameConfiguration.DefaultSslBinding);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.DefaultSslBinding);
                    }

                    if (hostnameConfig2.HostName.Equals(hostnameConfiguration.HostName))
                    {
                        Assert.True(hostnameConfiguration.NegotiateClientCertificate);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.NegotiateClientCertificate);
                    }
                }

                // Delete
                testBase.client.ApiManagementService.Delete(
                    resourceGroupName: testBase.rgName,
                    serviceName: testBase.serviceName);

                Assert.Throws <CloudException>(() =>
                {
                    testBase.client.ApiManagementService.Get(
                        resourceGroupName: testBase.rgName,
                        serviceName: testBase.serviceName);
                });
            }
        }
        public void UpdateHostname()
        {
            using (var context = UndoContext.Current)
            {
                context.Start("ResourceProviderFunctionalTests", "UpdateHostname");

                TryCreateApiService();

                var apiManagementClient = GetServiceClient <ApiManagementClient>(new CSMTestEnvironmentFactory());

                byte[]       certificate;
                const string certPath = "./Resources/testcertificate.pfx";
                using (var certStream = File.OpenRead(certPath))
                {
                    certificate = new byte[certStream.Length];
                    certStream.Read(certificate, 0, certificate.Length);
                }

                var response = apiManagementClient.ResourceProvider.UploadCertificate(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    new ApiServiceUploadCertificateParameters
                {
                    CertificatePassword = "******",
                    EncodedCertificate  = Convert.ToBase64String(certificate),
                    Type = HostnameType.Portal
                });

                Assert.NotNull(response);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.NotNull(response.Value);
                Assert.Equal("CN=*.powershelltest.net", response.Value.Subject);
                Assert.Equal("E861A19B22EE98AC71F84AC00C5A05E2E7206820", response.Value.Thumbprint, StringComparer.OrdinalIgnoreCase);

                // now setup the hostname for proxy
                var proxyHostConfig = new HostnameConfiguration
                {
                    Type        = HostnameType.Proxy,
                    Certificate = new CertificateInformation
                    {
                        Thumbprint = response.Value.Thumbprint,
                        Subject    = response.Value.Subject,
                        Expiry     = response.Value.Expiry
                    },
                    Hostname = "apimproxy.powershelltest.net"
                };

                var updateHostNameResponse = apiManagementClient.ResourceProvider.UpdateHostname(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    new ApiServiceUpdateHostnameParameters
                {
                    HostnamesToCreateOrUpdate = new List <HostnameConfiguration> {
                        proxyHostConfig
                    }
                });

                Assert.NotNull(updateHostNameResponse);
                Assert.Equal(HttpStatusCode.OK, updateHostNameResponse.StatusCode);
                Assert.NotNull(updateHostNameResponse.Value);
                Assert.NotNull(updateHostNameResponse.Value.Properties);
                Assert.NotNull(updateHostNameResponse.Value.Properties.HostnameConfigurations);
                Assert.True(updateHostNameResponse.Value.Properties.HostnameConfigurations.Any());
                Assert.Equal("apimproxy.powershelltest.net",
                             updateHostNameResponse.Value.Properties.HostnameConfigurations[0].Hostname);
                Assert.Equal(HostnameType.Proxy,
                             updateHostNameResponse.Value.Properties.HostnameConfigurations[0].Type);
                Assert.Equal("E861A19B22EE98AC71F84AC00C5A05E2E7206820",
                             updateHostNameResponse.Value.Properties.HostnameConfigurations[0].Certificate.Thumbprint);
            }
        }
Ejemplo n.º 7
0
        public void CreateMultiHostNameZoneAwareService()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);

                var domainOwnershipIdentifierResult = testBase.client.ApiManagementService.GetDomainOwnershipIdentifier();
                Assert.NotNull(domainOwnershipIdentifierResult);
                Assert.NotEmpty(domainOwnershipIdentifierResult.DomainOwnershipIdentifier);

                testBase.serviceProperties.Zones        = new[] { "1", "2" };
                testBase.serviceProperties.Sku.Name     = SkuType.Premium;
                testBase.serviceProperties.Sku.Capacity = 2; // unit count in multiple of zones
                testBase.location = "eastus2euap";
                testBase.serviceProperties.Location = testBase.location;
                var hostnameConfig1 = new HostnameConfiguration()
                {
                    Type                = HostnameType.Proxy,
                    HostName            = "gateway1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword,
                    DefaultSslBinding   = true
                };

                var hostnameConfig2 = new HostnameConfiguration()
                {
                    Type                       = HostnameType.Proxy,
                    HostName                   = "gateway2.msitesting.net",
                    EncodedCertificate         = testBase.base64EncodedTestCertificateData,
                    CertificatePassword        = testBase.testCertificatePassword,
                    NegotiateClientCertificate = true
                };

                var hostnameConfig3 = new HostnameConfiguration()
                {
                    Type                = HostnameType.Portal,
                    HostName            = "portal1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword
                };

                var hostnameConfig4 = new HostnameConfiguration()
                {
                    Type                = HostnameType.DeveloperPortal,
                    HostName            = "devportal1.msitesting.net",
                    EncodedCertificate  = testBase.base64EncodedTestCertificateData,
                    CertificatePassword = testBase.testCertificatePassword
                };

                testBase.serviceProperties.HostnameConfigurations = new List <HostnameConfiguration>
                {
                    hostnameConfig1,
                    hostnameConfig2,
                    hostnameConfig3,
                    hostnameConfig4
                };

                var base64ArrayCertificate = Convert.FromBase64String(testBase.base64EncodedTestCertificateData);
                var cert = new X509Certificate2(base64ArrayCertificate, testBase.testCertificatePassword);

                var createdService = testBase.client.ApiManagementService.CreateOrUpdate(
                    resourceGroupName: testBase.rgName,
                    serviceName: testBase.serviceName,
                    parameters: testBase.serviceProperties);

                ValidateService(createdService,
                                testBase.serviceName,
                                testBase.rgName,
                                testBase.subscriptionId,
                                testBase.location,
                                testBase.serviceProperties.PublisherEmail,
                                testBase.serviceProperties.PublisherName,
                                testBase.serviceProperties.Sku.Name,
                                testBase.tags,
                                PlatformVersion.Stv2);

                Assert.Equal(2, createdService.Sku.Capacity);
                Assert.Equal(2, createdService.Zones.Count);
                Assert.True(createdService.Zones.Contains("1"));
                Assert.True(createdService.Zones.Contains("2"));
                Assert.NotNull(createdService.HostnameConfigurations);
                Assert.Equal(5, createdService.HostnameConfigurations.Count()); // customhostname config + 1 default proxy
                var defaultHostname = new Uri(createdService.GatewayUrl).Host;
                var hostnameConfigurationToValidate = createdService.HostnameConfigurations
                                                      .Where(h => !h.HostName.Equals(defaultHostname, StringComparison.InvariantCultureIgnoreCase));

                foreach (HostnameConfiguration hostnameConfig in hostnameConfigurationToValidate)
                {
                    var hostnameConfiguration = createdService.HostnameConfigurations
                                                .SingleOrDefault(h => hostnameConfig.HostName.Equals(h.HostName));
                    Assert.NotNull(hostnameConfiguration);
                    Assert.Equal(hostnameConfig.Type, hostnameConfiguration.Type);
                    Assert.NotNull(hostnameConfiguration.Certificate);
                    Assert.NotNull(hostnameConfiguration.Certificate.Subject);
                    Assert.Null(hostnameConfig.IdentityClientId);
                    Assert.Null(hostnameConfig.KeyVaultId);
                    Assert.Equal(cert.Thumbprint, hostnameConfiguration.Certificate.Thumbprint);

                    if (HostnameType.Proxy == hostnameConfiguration.Type)
                    {
                        Assert.True(hostnameConfiguration.DefaultSslBinding);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.DefaultSslBinding);
                    }

                    if (hostnameConfig2.HostName.Equals(hostnameConfiguration.HostName))
                    {
                        Assert.True(hostnameConfiguration.NegotiateClientCertificate);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.NegotiateClientCertificate);
                    }
                }

                // update the service
                int intialTagsCount = createdService.Tags.Count;

                var updateParameters = new ApiManagementServiceUpdateParameters()
                {
                    Zones = new[] { "2", "3" },
                    Tags  = new Dictionary <string, string>()
                    {
                        { "client", "test" }
                    }
                };
                var updatedService = testBase.client.ApiManagementService.Update(testBase.rgName,
                                                                                 testBase.serviceName,
                                                                                 updateParameters);
                Assert.NotNull(updatedService);
                Assert.NotEmpty(updatedService.Tags);
                Assert.Equal(1, updatedService.Tags.Count);
                Assert.Equal(5, updatedService.HostnameConfigurations.Count());
                Assert.Equal(2, updatedService.Zones.Count);
                Assert.True(updatedService.Zones.Contains("2"));
                Assert.True(updatedService.Zones.Contains("3"));

                hostnameConfigurationToValidate = updatedService.HostnameConfigurations
                                                  .Where(h => !h.HostName.Equals(defaultHostname, StringComparison.InvariantCultureIgnoreCase));
                foreach (HostnameConfiguration hostnameConfig in hostnameConfigurationToValidate)
                {
                    var hostnameConfiguration = updatedService.HostnameConfigurations
                                                .SingleOrDefault(h => hostnameConfig.HostName.Equals(h.HostName));
                    Assert.NotNull(hostnameConfiguration);
                    Assert.Equal(hostnameConfig.Type, hostnameConfiguration.Type);
                    Assert.NotNull(hostnameConfiguration.Certificate);
                    Assert.NotNull(hostnameConfiguration.Certificate.Subject);
                    Assert.Equal(cert.Thumbprint, hostnameConfiguration.Certificate.Thumbprint);

                    if (HostnameType.Proxy == hostnameConfiguration.Type)
                    {
                        Assert.True(hostnameConfiguration.DefaultSslBinding);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.DefaultSslBinding);
                    }

                    if (hostnameConfig2.HostName.Equals(hostnameConfiguration.HostName))
                    {
                        Assert.True(hostnameConfiguration.NegotiateClientCertificate);
                    }
                    else
                    {
                        Assert.False(hostnameConfiguration.NegotiateClientCertificate);
                    }
                }

                // Delete
                testBase.client.ApiManagementService.Delete(
                    resourceGroupName: testBase.rgName,
                    serviceName: testBase.serviceName);

                Assert.Throws <ErrorResponseException>(() =>
                {
                    testBase.client.ApiManagementService.Get(
                        resourceGroupName: testBase.rgName,
                        serviceName: testBase.serviceName);
                });
            }
        }