33
33
import com .google .api .gax .rpc .mtls .MtlsProvider ;
34
34
import com .google .auto .value .AutoValue ;
35
35
import com .google .common .annotations .VisibleForTesting ;
36
+ import com .google .common .base .Strings ;
36
37
import java .io .IOException ;
37
38
import javax .annotation .Nullable ;
38
39
39
- /** Contains the fields required to resolve the endpoint */
40
+ /** Contains the fields required to resolve the endpoint and Universe Domain */
40
41
@ InternalApi
41
42
@ AutoValue
42
43
public abstract class EndpointContext {
44
+ // Default to port 443 for HTTPS. Using HTTP requires explicitly setting the endpoint
45
+ private static final String ENDPOINT_TEMPLATE = "SERVICE_NAME.UNIVERSE_DOMAIN:443" ;
46
+ static final String GOOGLE_DEFAULT_UNIVERSE = "googleapis.com" ;
47
+
48
+ /**
49
+ * ServiceName is host URI for Google Cloud Services. It follows the format of
50
+ * `{ServiceName}.googleapis.com`. For example, speech.googleapis.com would have a ServiceName of
51
+ * speech and cloudasset.googleapis.com would have a ServiceName of cloudasset.
52
+ */
53
+ // TODO: Remove @Nullable after first release 2024 (Builder will default to "").
54
+ @ Nullable
55
+ public abstract String serviceName ();
56
+
57
+ @ Nullable
58
+ public abstract String universeDomain ();
59
+
43
60
/**
44
61
* ServiceName is host URI for Google Cloud Services. It follows the format of
45
62
* `{ServiceName}.googleapis.com`. For example, speech.googleapis.com would have a ServiceName of
@@ -69,28 +86,76 @@ public abstract class EndpointContext {
69
86
@ Nullable
70
87
public abstract MtlsProvider mtlsProvider ();
71
88
89
+ public abstract boolean usingGDCH ();
90
+
72
91
public abstract Builder toBuilder ();
73
92
74
93
private String resolvedEndpoint ;
94
+ private String resolvedUniverseDomain ;
75
95
76
96
public static Builder newBuilder () {
77
- return new AutoValue_EndpointContext .Builder ().setSwitchToMtlsEndpointAllowed (false );
97
+ return new AutoValue_EndpointContext .Builder ()
98
+ .setSwitchToMtlsEndpointAllowed (false )
99
+ .setUsingGDCH (false );
78
100
}
79
101
102
+ /** Determines the fully resolved endpoint and universe domain values */
80
103
@ VisibleForTesting
81
104
void determineEndpoint () throws IOException {
82
105
MtlsProvider mtlsProvider = mtlsProvider () == null ? new MtlsProvider () : mtlsProvider ();
106
+ // TransportChannelProvider's endpoint will override the ClientSettings' endpoint
83
107
String customEndpoint =
84
108
transportChannelProviderEndpoint () == null
85
109
? clientSettingsEndpoint ()
86
110
: transportChannelProviderEndpoint ();
87
- resolvedEndpoint =
88
- mtlsEndpointResolver (
89
- customEndpoint , mtlsEndpoint (), switchToMtlsEndpointAllowed (), mtlsProvider );
111
+
112
+ // GDC-H has a separate flow
113
+ if (usingGDCH ()) {
114
+ determineGDCHEndpoint (customEndpoint );
115
+ return ;
116
+ }
117
+ // Check for "" (empty string)
118
+ if (universeDomain () != null && universeDomain ().isEmpty ()) {
119
+ throw new IllegalArgumentException ("The universe domain value cannot be empty." );
120
+ }
121
+ // Universe Domain defaults to the GDU if it's not provided by the user.
122
+ resolvedUniverseDomain = universeDomain () != null ? universeDomain () : GOOGLE_DEFAULT_UNIVERSE ;
123
+
124
+ if (Strings .isNullOrEmpty (customEndpoint )) {
125
+ customEndpoint = buildEndpointTemplate (serviceName (), resolvedUniverseDomain );
126
+ resolvedEndpoint =
127
+ mtlsEndpointResolver (
128
+ customEndpoint , mtlsEndpoint (), switchToMtlsEndpointAllowed (), mtlsProvider );
129
+ // Check if mTLS is configured with non-GDU
130
+ if (resolvedEndpoint .equals (mtlsEndpoint ())
131
+ && !resolvedUniverseDomain .equals (GOOGLE_DEFAULT_UNIVERSE )) {
132
+ throw new IllegalArgumentException (
133
+ "mTLS is not supported in any universe other than googleapis.com" );
134
+ }
135
+ } else {
136
+ resolvedEndpoint = customEndpoint ;
137
+ }
138
+ }
139
+
140
+ // GDC-H has no concept of Universe Domain. Do not set the resolvedUniverseDomain value
141
+ private void determineGDCHEndpoint (String customEndpoint ) {
142
+ if (universeDomain () != null ) {
143
+ throw new IllegalArgumentException (
144
+ "Universe domain configuration is incompatible with GDC-H" );
145
+ } else if (customEndpoint == null ) {
146
+ resolvedEndpoint = buildEndpointTemplate (serviceName (), GOOGLE_DEFAULT_UNIVERSE );
147
+ } else {
148
+ resolvedEndpoint = customEndpoint ;
149
+ }
150
+ }
151
+
152
+ // Construct the endpoint with the template
153
+ private String buildEndpointTemplate (String serviceName , String resolvedUniverseDomain ) {
154
+ return ENDPOINT_TEMPLATE
155
+ .replace ("SERVICE_NAME" , serviceName )
156
+ .replace ("UNIVERSE_DOMAIN" , resolvedUniverseDomain );
90
157
}
91
158
92
- // This takes in parameters because determineEndpoint()'s logic will be updated
93
- // to pass custom values in.
94
159
// Follows https://google.aip.dev/auth/4114 for resolving the endpoint
95
160
@ VisibleForTesting
96
161
String mtlsEndpointResolver (
@@ -123,6 +188,14 @@ public String getResolvedEndpoint() {
123
188
return resolvedEndpoint ;
124
189
}
125
190
191
+ /**
192
+ * The resolved Universe Domain is the computed Universe Domain after accounting for the custom
193
+ * Universe Domain
194
+ */
195
+ public String getResolvedUniverseDomain () {
196
+ return resolvedUniverseDomain ;
197
+ }
198
+
126
199
@ AutoValue .Builder
127
200
public abstract static class Builder {
128
201
/**
@@ -132,6 +205,15 @@ public abstract static class Builder {
132
205
*/
133
206
public abstract Builder setServiceName (String serviceName );
134
207
208
+ public abstract Builder setUniverseDomain (String universeDomain );
209
+
210
+ /**
211
+ * ServiceName is host URI for Google Cloud Services. It follows the format of
212
+ * `{ServiceName}.googleapis.com`. For example, speech.googleapis.com would have a ServiceName
213
+ * of speech and cloudasset.googleapis.com would have a ServiceName of cloudasset.
214
+ */
215
+ public abstract Builder setServiceName (String serviceName );
216
+
135
217
/**
136
218
* ClientSettingsEndpoint is the endpoint value set via the ClientSettings/StubSettings classes.
137
219
*/
@@ -149,6 +231,8 @@ public abstract static class Builder {
149
231
150
232
public abstract Builder setMtlsProvider (MtlsProvider mtlsProvider );
151
233
234
+ public abstract Builder setUsingGDCH (boolean usingGDCH );
235
+
152
236
abstract EndpointContext autoBuild ();
153
237
154
238
public EndpointContext build () throws IOException {
0 commit comments