11
11
import com .azure .core .management .profile .AzureProfile ;
12
12
import com .azure .core .test .TestMode ;
13
13
import com .azure .core .util .Configuration ;
14
+ import com .azure .identity .AzureAuthorityHosts ;
15
+ import com .azure .identity .ClientSecretCredentialBuilder ;
14
16
import com .azure .identity .DefaultAzureCredentialBuilder ;
15
17
import com .azure .resourcemanager .containerregistry .ContainerRegistryManager ;
16
18
import com .azure .resourcemanager .containerregistry .models .ImportImageParameters ;
@@ -48,6 +50,8 @@ public class TestUtils {
48
50
public static final String REGISTRY_NAME ;
49
51
public static final String RESOURCE_GROUP ;
50
52
public static final String SUBSCRIPTION_ID ;
53
+ public static final String TENANT_ID ;
54
+ public static final String CLIENT_ID ;
51
55
public static final String REGISTRY_URI ;
52
56
public static final String REGISTRY_ENDPOINT ;
53
57
public static final String ANONYMOUS_REGISTRY_ENDPOINT ;
@@ -61,6 +65,7 @@ public class TestUtils {
61
65
public static final int HTTP_STATUS_CODE_202 ;
62
66
public static final String AZURE_GLOBAL_AUTHENTICATION_SCOPE ;
63
67
public static final String AZURE_GOV_AUTHENTICATION_SCOPE ;
68
+ public static final String CONTAINERREGISTRY_CLIENT_SECRET ;
64
69
65
70
static {
66
71
CONFIGURATION = Configuration .getGlobalConfiguration ().clone ();
@@ -85,12 +90,15 @@ public class TestUtils {
85
90
WINDOWS_OPERATING_SYSTEM = "windows" ;
86
91
RESOURCE_GROUP = CONFIGURATION .get ("CONTAINERREGISTRY_RESOURCE_GROUP" );
87
92
SUBSCRIPTION_ID = CONFIGURATION .get ("CONTAINERREGISTRY_SUBSCRIPTION_ID" );
93
+ TENANT_ID = CONFIGURATION .get ("CONTAINERREGISTRY_TENANT_ID" );
94
+ CLIENT_ID = CONFIGURATION .get ("CONTAINERREGISTRY_CLIENT_ID" );
88
95
REGISTRY_NAME = CONFIGURATION .get ("CONTAINERREGISTRY_REGISTRY_NAME" );
89
96
REGISTRY_ENDPOINT = CONFIGURATION .get ("CONTAINERREGISTRY_ENDPOINT" );
90
97
REGISTRY_URI = "registry.hub.docker.com" ;
91
98
SLEEP_TIME_IN_MILLISECONDS = 5000 ;
92
99
ANONYMOUS_REGISTRY_NAME = CONFIGURATION .get ("CONTAINERREGISTRY_ANONREGISTRY_NAME" );
93
100
ANONYMOUS_REGISTRY_ENDPOINT = CONFIGURATION .get ("CONTAINERREGISTRY_ANONREGISTRY_ENDPOINT" );
101
+ CONTAINERREGISTRY_CLIENT_SECRET = CONFIGURATION .get ("CONTAINERREGISTRY_CLIENT_SECRET" );
94
102
LOGIN_SERVER_SUFFIX = "azurecr.io" ;
95
103
REGISTRY_ENDPOINT_PLAYBACK = "https://pallavitcontainerregistry.azurecr.io" ;
96
104
REGISTRY_NAME_PLAYBACK = "pallavitcontainerregistry" ;
@@ -124,12 +132,29 @@ static <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable)
124
132
return true ;
125
133
}
126
134
127
- static TokenCredential getCredential (TestMode testMode ) {
135
+ static TokenCredential getCredentialsByEndpoint (TestMode testMode , String endpoint ) {
128
136
if (testMode == TestMode .PLAYBACK ) {
129
137
return new FakeCredentials ();
130
138
}
131
139
132
- return new DefaultAzureCredentialBuilder ().build ();
140
+ String authority = getAuthority (endpoint );
141
+ return getCredentialByAuthority (testMode , authority );
142
+ }
143
+
144
+ static TokenCredential getCredentialByAuthority (TestMode testMode , String authority ) {
145
+ if (testMode == TestMode .PLAYBACK ) {
146
+ return new FakeCredentials ();
147
+ }
148
+
149
+ if (authority == AzureAuthorityHosts .AZURE_PUBLIC_CLOUD ) {
150
+ return new DefaultAzureCredentialBuilder ().build ();
151
+ } else {
152
+ return new ClientSecretCredentialBuilder ()
153
+ .tenantId (TENANT_ID )
154
+ .clientId (CLIENT_ID )
155
+ .clientSecret (CONTAINERREGISTRY_CLIENT_SECRET )
156
+ .authorityHost (authority ).build ();
157
+ }
133
158
}
134
159
135
160
static void importImage (TestMode mode , String repository , List <String > tags ) {
@@ -145,20 +170,69 @@ static void importImage(TestMode mode, String repository, List<String> tags) {
145
170
}
146
171
}
147
172
173
+ public static String getAuthority (String endpoint ) {
174
+ if (endpoint == null ) {
175
+ return AzureAuthorityHosts .AZURE_PUBLIC_CLOUD ;
176
+ }
177
+
178
+ if (endpoint .contains (".azurecr.io" )) {
179
+ return AzureAuthorityHosts .AZURE_PUBLIC_CLOUD ;
180
+ }
181
+
182
+ if (endpoint .contains (".azurecr.cn" )) {
183
+ return AzureAuthorityHosts .AZURE_CHINA ;
184
+ }
185
+
186
+ if (endpoint .contains (".azurecr.us" )) {
187
+ return AzureAuthorityHosts .AZURE_GOVERNMENT ;
188
+ }
189
+
190
+ // By default we will assume that the authority is public
191
+ return AzureAuthorityHosts .AZURE_PUBLIC_CLOUD ;
192
+ }
193
+
194
+ public static String getAuthenticationScope (String endpoint ) {
195
+ String authority = getAuthority (endpoint );
196
+ switch (authority ) {
197
+ case AzureAuthorityHosts .AZURE_PUBLIC_CLOUD :
198
+ return "https://management.core.windows.net/.default" ;
199
+
200
+ case AzureAuthorityHosts .AZURE_CHINA :
201
+ return "https://management.chinacloudapi.cn/.default" ;
202
+
203
+ case AzureAuthorityHosts .AZURE_GOVERNMENT :
204
+ return "https://management.usgovcloudapi.net/.default" ;
205
+
206
+ default :
207
+ return null ;
208
+ }
209
+ }
210
+
211
+ static AzureProfile getAzureProfile (String authority ) {
212
+ switch (authority ) {
213
+ case AzureAuthorityHosts .AZURE_PUBLIC_CLOUD : return new AzureProfile (TENANT_ID , SUBSCRIPTION_ID , AzureEnvironment .AZURE );
214
+ case AzureAuthorityHosts .AZURE_CHINA : return new AzureProfile (TENANT_ID , SUBSCRIPTION_ID , AzureEnvironment .AZURE_CHINA );
215
+ case AzureAuthorityHosts .AZURE_GOVERNMENT : return new AzureProfile (TENANT_ID , SUBSCRIPTION_ID , AzureEnvironment .AZURE_US_GOVERNMENT );
216
+ default : return null ;
217
+ }
218
+ }
219
+
148
220
static Mono <Void > importImageAsync (TestMode mode , String repository , List <String > tags ) {
149
- return importImageAsync (mode , REGISTRY_NAME , repository , tags );
221
+ return importImageAsync (mode , REGISTRY_NAME , repository , tags , REGISTRY_ENDPOINT );
150
222
}
151
223
152
- static Mono <Void > importImageAsync (TestMode mode , String registryName , String repository , List <String > tags ) {
224
+ static Mono <Void > importImageAsync (TestMode mode , String registryName , String repository , List <String > tags , String endpoint ) {
153
225
if (mode == TestMode .PLAYBACK ) {
154
226
return Mono .empty ();
155
227
}
156
228
157
- TokenCredential credential = getCredential ( mode );
229
+ String authority = getAuthority ( endpoint );
158
230
231
+ TokenCredential credential = getCredentialByAuthority (mode , authority );
159
232
tags = tags .stream ().map (tag -> String .format ("%1$s:%2$s" , repository , tag )).collect (Collectors .toList ());
233
+ AzureProfile profile = getAzureProfile (authority );
160
234
161
- ContainerRegistryManager manager = ContainerRegistryManager .authenticate (credential , new AzureProfile ( AzureEnvironment . AZURE ) );
235
+ ContainerRegistryManager manager = ContainerRegistryManager .authenticate (credential , profile );
162
236
163
237
return manager .serviceClient ().getRegistries ().importImageAsync (
164
238
RESOURCE_GROUP ,
0 commit comments