Skip to content

Commit

Permalink
hostname support wildcard form (Azure#26918)
Browse files Browse the repository at this point in the history
* hostname support wildcard form

* changelog and session-records

* arraylist implementation update

* add additional test for overriding hostnames by hostname

* nit

* checkstyle

* support multiple host names

* Update sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md

Co-authored-by: Weidong Xu <weidxu@microsoft.com>

* Update sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/models/HasHostname.java

Co-authored-by: Weidong Xu <weidxu@microsoft.com>

* fix null situation in withHostname() and withHostnames()

Co-authored-by: Weidong Xu <weidxu@microsoft.com>
  • Loading branch information
XiaofeiCao and weidongxu-microsoft authored Feb 9, 2022
1 parent a487656 commit b3fc31a
Show file tree
Hide file tree
Showing 6 changed files with 1,590 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
- Supported wildcard hostname in `ApplicationGateway` listener.
- Supported `withHostnames()` and `hostnames()` in `HasHostname`.

## 2.11.0 (2022-01-17)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.azure.resourcemanager.network.implementation;

import com.azure.core.management.SubResource;
import com.azure.core.util.CoreUtils;
import com.azure.resourcemanager.network.models.ApplicationGateway;
import com.azure.resourcemanager.network.models.ApplicationGatewayFrontend;
import com.azure.resourcemanager.network.models.ApplicationGatewayHttpListener;
Expand All @@ -16,6 +17,9 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Implementation for ApplicationGatewayListener. */
class ApplicationGatewayListenerImpl
Expand Down Expand Up @@ -61,7 +65,24 @@ public boolean requiresServerNameIndication() {

@Override
public String hostname() {
return this.innerModel().hostname();
if (this.innerModel().hostname() != null) {
return this.innerModel().hostname();
}
if (!CoreUtils.isNullOrEmpty(this.innerModel().hostNames())) {
return this.innerModel().hostNames().get(0);
}
return null;
}

@Override
public List<String> hostnames() {
if (this.innerModel().hostname() != null) {
return Collections.singletonList(this.innerModel().hostname());
}
if (CoreUtils.isNullOrEmpty(this.innerModel().hostNames())) {
return Collections.emptyList();
}
return Collections.unmodifiableList(this.innerModel().hostNames());
}

@Override
Expand Down Expand Up @@ -237,7 +258,21 @@ public ApplicationGatewayListenerImpl withHttps() {

@Override
public ApplicationGatewayListenerImpl withHostname(String hostname) {
this.innerModel().withHostname(hostname);
if (hostname != null) {
this.innerModel().withHostname(null);
List<String> hostNames = new ArrayList<>();
hostNames.add(hostname);
this.innerModel().withHostNames(hostNames);
}
return this;
}

@Override
public ApplicationGatewayListenerImpl withHostnames(List<String> hostnames) {
if (!CoreUtils.isNullOrEmpty(hostnames)) {
this.innerModel().withHostname(null);
this.innerModel().withHostNames(hostnames);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package com.azure.resourcemanager.network.implementation;

import com.azure.core.management.SubResource;
import com.azure.core.util.CoreUtils;
import com.azure.resourcemanager.network.fluent.models.ApplicationGatewayRequestRoutingRuleInner;
import com.azure.resourcemanager.network.models.ApplicationGateway;
import com.azure.resourcemanager.network.models.ApplicationGatewayBackend;
import com.azure.resourcemanager.network.models.ApplicationGatewayBackendAddress;
Expand All @@ -15,7 +17,6 @@
import com.azure.resourcemanager.network.models.ApplicationGatewaySslCertificate;
import com.azure.resourcemanager.network.models.ApplicationGatewayUrlPathMap;
import com.azure.resourcemanager.network.models.PublicIpAddress;
import com.azure.resourcemanager.network.fluent.models.ApplicationGatewayRequestRoutingRuleInner;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;
import com.azure.resourcemanager.resources.fluentcore.arm.models.implementation.ChildResourceImpl;
import reactor.core.publisher.Mono;
Expand All @@ -25,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/** Implementation for ApplicationGatewayRequestRoutingRule. */
class ApplicationGatewayRequestRoutingRuleImpl
Expand Down Expand Up @@ -89,6 +91,12 @@ public String hostname() {
return (listener != null) ? listener.hostname() : null;
}

@Override
public List<String> hostnames() {
final ApplicationGatewayListener listener = this.listener();
return (listener != null) ? listener.hostnames() : Collections.emptyList();
}

@Override
public int frontendPort() {
final ApplicationGatewayListener listener = this.listener();
Expand Down Expand Up @@ -347,6 +355,15 @@ public ApplicationGatewayRequestRoutingRuleImpl withHostname(String hostName) {
return this;
}

@Override
public ApplicationGatewayRequestRoutingRuleImpl withHostnames(List<String> hostnames) {
if (CoreUtils.isNullOrEmpty(hostnames)) {
return this;
}
this.parent().updateListener(ensureListener().name()).withHostnames(hostnames);
return this;
}

@Override
public ApplicationGatewayRequestRoutingRuleImpl withServerNameIndication() {
this.parent().updateListener(ensureListener().name()).withServerNameIndication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@

import com.azure.core.annotation.Fluent;

/** An interface representing a model's ability to reference a host name. */
import java.util.List;

/**
* An interface representing a model's ability to reference a host name.
* The hostname supports wildcard format, e.g. "*.contoso.com", but is available only for {@link ApplicationGatewaySkuName#STANDARD_V2} and {@link ApplicationGatewaySkuName#WAF_V2} SKU
*/
@Fluent
public interface HasHostname {
/** @return the associated host name */
/**
* @return the associated host name, or the first one if there exists multiple host names
*/
String hostname();

/** @return the associated host names */
List<String> hostnames();

/** Grouping of definition stages involving specifying the host name. */
interface DefinitionStages {
/**
* The stage of a definition allowing to specify a host name.
* The stage of a definition allowing to specify host names.
*
* @param <ReturnT> the next stage of the definition
*/
Expand All @@ -25,13 +35,20 @@ interface WithHostname<ReturnT> {
* @return the next stage of the definition
*/
ReturnT withHostname(String hostname);

/**
* Specifies up to 5 hostnames to reference.
* @param hostnames existing frontend hostnames on this load balancer
* @return the next stage of the definition
*/
ReturnT withHostnames(List<String> hostnames);
}
}

/** Grouping of update stages involving specifying the host name. */
interface UpdateStages {
/**
* The stage of an update allowing to specify a host name.
* The stage of an update allowing to specify host names.
*
* @param <ReturnT> the next stage of the update
*/
Expand All @@ -43,13 +60,20 @@ interface WithHostname<ReturnT> {
* @return the next stage of the update
*/
ReturnT withHostname(String hostname);

/**
* Specifies up to 5 hostnames to reference.
* @param hostnames existing host names
* @return the next stage of the definition
*/
ReturnT withHostnames(List<String> hostnames);
}
}

/** Grouping of definition stages applicable as part of a parent resource update. */
interface UpdateDefinitionStages {
/**
* The stage of a definition allowing to specify a host name.
* The stage of a definition allowing to specify host names.
*
* @param <ReturnT> the next stage of the definition
*/
Expand All @@ -61,6 +85,13 @@ interface WithHostname<ReturnT> {
* @return the next stage of the definition
*/
ReturnT withHostname(String hostname);

/**
* Specifies up to 5 hostnames to reference.
* @param hostnames existing host names
* @return the next stage of the definition
*/
ReturnT withHostnames(List<String> hostnames);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.resourcemanager.network;

import com.azure.core.management.Region;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
Expand All @@ -21,27 +22,26 @@
import com.azure.resourcemanager.network.models.PublicIPSkuType;
import com.azure.resourcemanager.network.models.PublicIpAddress;
import com.azure.resourcemanager.network.models.ResourceIdentityType;
import com.azure.core.management.Region;
import com.azure.security.keyvault.certificates.CertificateClient;
import com.azure.security.keyvault.certificates.CertificateClientBuilder;
import com.azure.security.keyvault.certificates.models.CertificatePolicy;
import com.azure.security.keyvault.certificates.models.KeyVaultCertificateWithPolicy;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class ApplicationGatewayTests extends NetworkManagementTest {

@Test
Expand Down Expand Up @@ -135,6 +135,78 @@ public void canCRUDApplicationGatewayWithWAF() throws Exception {
"REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION");
}

@Test
public void canSpecifyWildcardListeners() {
String appGatewayName = generateRandomResourceName("agwaf", 15);
String appPublicIp = generateRandomResourceName("pip", 15);

PublicIpAddress pip =
networkManager
.publicIpAddresses()
.define(appPublicIp)
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withSku(PublicIPSkuType.STANDARD)
.withStaticIP()
.create();
String listener1 = "listener1";
// regular hostname
String hostname1 = "my.contoso.com";
ApplicationGateway gateway = networkManager.applicationGateways()
.define(appGatewayName)
.withRegion(Region.US_EAST)
.withExistingResourceGroup(rgName)

// Request routing rules
.defineRequestRoutingRule("rule80")
.fromPublicFrontend()
.fromFrontendHttpPort(80)
.toBackendHttpPort(8080)
.toBackendIPAddress("11.1.1.1")
.toBackendIPAddress("11.1.1.2")
.withCookieBasedAffinity()
.attach()

// Additional/explicit frontend listeners
.defineListener(listener1)
.withPublicFrontend()
.withFrontendPort(9000)
.withHttp()
.withHostname(hostname1)
.attach()

.withTier(ApplicationGatewayTier.WAF_V2)
.withSize(ApplicationGatewaySkuName.WAF_V2)
.withAutoScale(2, 5)
.withExistingPublicIpAddress(pip)
.create();

Assertions.assertEquals(hostname1, gateway.listeners().get(listener1).hostname());

// wildcard hostname
String hostname2 = "*.contoso.com";
gateway.update()
.updateListener(listener1)
.withHostname(hostname2)
.parent()
.apply();

Assertions.assertEquals(hostname2, gateway.listeners().get(listener1).hostname());

// multiple host names, mixed regular and wildcard
List<String> hostnames = new ArrayList<>();
hostnames.add(hostname1);
hostnames.add(hostname2);

gateway.update()
.updateListener(listener1)
.withHostnames(hostnames)
.parent()
.apply();

Assertions.assertEquals(hostnames, gateway.listeners().get(listener1).hostnames());
}

@Test
@Disabled("Need client id for key vault usage")
public void canCreateApplicationGatewayWithSecret() throws Exception {
Expand Down
Loading

0 comments on commit b3fc31a

Please sign in to comment.