Skip to content

Commit 420323d

Browse files
author
Rujun Chen
authored
Use Directory.Read.All intead of Directory.AccessAsUser.All (Azure#18901)
* To avoid admin consent, use "Directory.Read.All" instead of "Directory.AccessAsUser.All". * Add check: When azure.activedirectory.teannt-id=common, azure.activedirectory.user-group.allowed-groups should be empty.
1 parent 49f1bf7 commit 420323d

File tree

10 files changed

+55
-49
lines changed

10 files changed

+55
-49
lines changed

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-webapp/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ azure:
77
graph:
88
scopes:
99
- https://graph.microsoft.com/User.Read
10-
- https://graph.microsoft.com/Directory.AccessAsUser.All
10+
- https://graph.microsoft.com/Directory.Read.All
1111
office:
1212
scopes:
1313
- https://manage.office.com/ActivityFeed.Read

sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ public void testAccessTokenScopes() {
3333
+ "https://manage.office.com/ServiceHealth.Read");
3434
properties.put(
3535
"azure.activedirectory.authorization-clients.graph.scopes",
36-
"https://graph.microsoft.com/User.Read, https://graph.microsoft.com/Directory.AccessAsUser.All");
36+
"https://graph.microsoft.com/User.Read, https://graph.microsoft.com/Directory.Read.All");
3737
aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, properties);
3838
aadSeleniumITHelper.logIn();
3939
String httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/azure");
4040
Assert.assertTrue(httpResponse.contains("profile"));
41-
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All"));
41+
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.Read.All"));
4242
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read"));
4343

4444
httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/graph");
4545
Assert.assertTrue(httpResponse.contains("profile"));
46-
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All"));
46+
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.Read.All"));
4747
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read"));
4848

4949
httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/office");

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private Set<String> accessTokenScopes() {
116116
if (properties.allowedGroupsConfigured()) {
117117
// The 2 scopes are need to get group name from graph.
118118
result.add(properties.getGraphBaseUri() + "User.Read");
119-
result.add(properties.getGraphBaseUri() + "Directory.AccessAsUser.All");
119+
result.add(properties.getGraphBaseUri() + "Directory.Read.All");
120120
}
121121
return result;
122122
}

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class AADAuthenticationProperties implements InitializingBean {
8484
/**
8585
* Azure Tenant ID.
8686
*/
87-
private String tenantId = "common";
87+
private String tenantId;
8888

8989
private String postLogoutRedirectUri;
9090

@@ -337,6 +337,16 @@ public void afterPropertiesSet() throws Exception {
337337
+ "azure.activedirectory.graph-base-uri = " + graphBaseUri + ", "
338338
+ "azure.activedirectory.graph-membership-uri = " + graphMembershipUri + ".");
339339
}
340+
341+
if (!StringUtils.hasText(tenantId)) {
342+
tenantId = "common";
343+
}
344+
if ("common".equals(tenantId) && !userGroup.getAllowedGroups().isEmpty()) {
345+
throw new IllegalStateException("When azure.activedirectory.teannt-id=common, "
346+
+ "azure.activedirectory.user-group.allowed-groups should be empty. "
347+
+ "But actually azure.activedirectory.user-group.allowed-groups="
348+
+ userGroup.getAllowedGroups());
349+
}
340350
}
341351

342352
private String addSlash(String uri) {

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/aad/webapi/AADResourceServerConfigurationTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818

1919
public class AADResourceServerConfigurationTest {
2020

21-
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
22-
.withPropertyValues("azure.activedirectory.user-group.allowed-groups=User");
21+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner();
2322

2423
@Test
2524
public void testNotExistBearerTokenAuthenticationToken() {
2625
this.contextRunner
2726
.withUserConfiguration(AADResourceServerConfiguration.class)
2827
.withClassLoader(new FilteredClassLoader(BearerTokenAuthenticationToken.class))
29-
.run(context -> {
30-
assertThat(context).doesNotHaveBean("jwtDecoderByJwkKeySetUri");
31-
});
28+
.run(context -> assertThat(context).doesNotHaveBean("jwtDecoderByJwkKeySetUri"));
3229
}
3330

3431
@Test

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/aad/webapp/AADWebAppConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public void groupConfiguration() {
301301
assertDefaultScopes(
302302
clientRepo.getAzureClient(),
303303
"openid", "profile", "https://graph.microsoft.com/User.Read",
304-
"https://graph.microsoft.com/Directory.AccessAsUser.All"
304+
"https://graph.microsoft.com/Directory.Read.All"
305305
);
306306
});
307307
}
@@ -349,7 +349,7 @@ public void resourceServerCountTest() {
349349
assertEquals(resourceServerCount(scopes), 0);
350350
scopes.add("https://graph.microsoft.com/User.Read");
351351
assertEquals(resourceServerCount(scopes), 1);
352-
scopes.add("https://graph.microsoft.com/Directory.AccessAsUser.All");
352+
scopes.add("https://graph.microsoft.com/Directory.Read.All");
353353
assertEquals(resourceServerCount(scopes), 1);
354354
scopes.add("https://manage.office.com/ActivityFeed.Read");
355355
assertEquals(resourceServerCount(scopes), 2);

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADAuthenticationFilterPropertiesTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
public class AADAuthenticationFilterPropertiesTest {
2424
@After
2525
public void clearAllProperties() {
26-
System.clearProperty(TestConstants.SERVICE_ENVIRONMENT_PROPERTY);
27-
System.clearProperty(TestConstants.CLIENT_ID_PROPERTY);
28-
System.clearProperty(TestConstants.CLIENT_SECRET_PROPERTY);
29-
System.clearProperty(TestConstants.TARGETED_GROUPS_PROPERTY);
26+
System.clearProperty("azure.activedirectory.environment");
27+
System.clearProperty("azure.activedirectory.tenant-id");
28+
System.clearProperty("azure.activedirectory.client-id");
29+
System.clearProperty("azure.activedirectory.client-secret");
30+
System.clearProperty("azure.activedirectory.user-group.allowed-groups");
3031
}
3132

3233
@Test
@@ -47,18 +48,19 @@ public void canSetProperties() {
4748
}
4849

4950
private void configureAllRequiredProperties() {
50-
System.setProperty(TestConstants.CLIENT_ID_PROPERTY, TestConstants.CLIENT_ID);
51-
System.setProperty(TestConstants.CLIENT_SECRET_PROPERTY, TestConstants.CLIENT_SECRET);
52-
System.setProperty(TestConstants.TARGETED_GROUPS_PROPERTY,
53-
TestConstants.TARGETED_GROUPS.toString().replace("[", "").replace("]", ""));
54-
System.setProperty(TestConstants.ALLOW_TELEMETRY_PROPERTY, "false");
51+
System.setProperty("azure.activedirectory.tenant-id", "demo-tenant-id");
52+
System.setProperty("azure.activedirectory.client-id", TestConstants.CLIENT_ID);
53+
System.setProperty("azure.activedirectory.client-secret", TestConstants.CLIENT_SECRET);
54+
System.setProperty("azure.activedirectory.user-group.allowed-groups",
55+
TestConstants.TARGETED_GROUPS.toString().replace("[", "").replace("]", ""));
56+
System.setProperty("azure.activedirectory.allow-telemetry", "false");
5557
}
5658

5759
@Test
5860
@Ignore // TODO (wepa) clientId and clientSecret can also be configured in oauth2 config, test to be refactored
5961
public void emptySettingsNotAllowed() {
60-
System.setProperty(TestConstants.CLIENT_ID_PROPERTY, "");
61-
System.setProperty(TestConstants.CLIENT_SECRET_PROPERTY, "");
62+
System.setProperty("azure.activedirectory.client-id", "");
63+
System.setProperty("azure.activedirectory.client-secret", "");
6264

6365
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
6466
Exception exception = null;

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADAuthenticationFilterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public AADAuthenticationFilterTest() {
5555
@Test
5656
@Ignore
5757
public void doFilterInternal() {
58-
this.contextRunner.withPropertyValues(TestConstants.CLIENT_ID_PROPERTY, TestConstants.CLIENT_ID)
59-
.withPropertyValues(TestConstants.CLIENT_SECRET_PROPERTY, TestConstants.CLIENT_SECRET)
60-
.withPropertyValues(TestConstants.TARGETED_GROUPS_PROPERTY,
58+
this.contextRunner.withPropertyValues("azure.activedirectory.client-id", TestConstants.CLIENT_ID)
59+
.withPropertyValues("azure.activedirectory.client-secret", TestConstants.CLIENT_SECRET)
60+
.withPropertyValues("azure.activedirectory.client-secret",
6161
TestConstants.TARGETED_GROUPS.toString()
6262
.replace("[", "").replace("]", ""));
6363

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/ResourceRetrieverTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
public class ResourceRetrieverTest {
1818
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
19-
.withConfiguration(AutoConfigurations.of(AADAuthenticationFilterAutoConfiguration.class))
20-
.withClassLoader(new FilteredClassLoader(BearerTokenAuthenticationToken.class))
21-
.withPropertyValues("azure.activedirectory.client-id=fake-client-id",
22-
"azure.activedirectory.client-secret=fake-client-secret",
23-
"azure.activedirectory.user-group.allowed-groups=fake-group",
24-
"azure.service.endpoints.global.aadKeyDiscoveryUri=http://fake.aad.discovery.uri");
19+
.withConfiguration(AutoConfigurations.of(AADAuthenticationFilterAutoConfiguration.class))
20+
.withClassLoader(new FilteredClassLoader(BearerTokenAuthenticationToken.class))
21+
.withPropertyValues(
22+
"azure.activedirectory.client-id=fake-client-id",
23+
"azure.activedirectory.client-secret=fake-client-secret",
24+
"azure.service.endpoints.global.aadKeyDiscoveryUri=http://fake.aad.discovery.uri");
2525

2626
@Test
2727
public void resourceRetrieverDefaultConfig() {
@@ -39,19 +39,21 @@ public void resourceRetrieverDefaultConfig() {
3939

4040
@Test
4141
public void resourceRetriverIsConfigurable() {
42-
this.contextRunner.withPropertyValues("azure.activedirectory.jwt-connect-timeout=1234",
42+
this.contextRunner
43+
.withPropertyValues(
44+
"azure.activedirectory.jwt-connect-timeout=1234",
4345
"azure.activedirectory.jwt-read-timeout=1234",
4446
"azure.activedirectory.jwt-size-limit=123400",
4547
"azure.service.endpoints.global.aadKeyDiscoveryUri=http://fake.aad.discovery.uri")
46-
.run(context -> {
47-
assertThat(context).hasSingleBean(ResourceRetriever.class);
48-
final ResourceRetriever retriever = context.getBean(ResourceRetriever.class);
49-
assertThat(retriever).isInstanceOf(DefaultResourceRetriever.class);
50-
51-
final DefaultResourceRetriever defaultRetriever = (DefaultResourceRetriever) retriever;
52-
assertThat(defaultRetriever.getConnectTimeout()).isEqualTo(1234);
53-
assertThat(defaultRetriever.getReadTimeout()).isEqualTo(1234);
54-
assertThat(defaultRetriever.getSizeLimit()).isEqualTo(123400);
55-
});
48+
.run(context -> {
49+
assertThat(context).hasSingleBean(ResourceRetriever.class);
50+
final ResourceRetriever retriever = context.getBean(ResourceRetriever.class);
51+
assertThat(retriever).isInstanceOf(DefaultResourceRetriever.class);
52+
53+
final DefaultResourceRetriever defaultRetriever = (DefaultResourceRetriever) retriever;
54+
assertThat(defaultRetriever.getConnectTimeout()).isEqualTo(1234);
55+
assertThat(defaultRetriever.getReadTimeout()).isEqualTo(1234);
56+
assertThat(defaultRetriever.getSizeLimit()).isEqualTo(123400);
57+
});
5658
}
5759
}

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/TestConstants.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
import java.util.List;
88

99
public class TestConstants {
10-
public static final String SERVICE_ENVIRONMENT_PROPERTY = "azure.activedirectory.environment";
11-
public static final String CLIENT_ID_PROPERTY = "azure.activedirectory.client-id";
12-
public static final String CLIENT_SECRET_PROPERTY = "azure.activedirectory.client-secret";
13-
public static final String TARGETED_GROUPS_PROPERTY = "azure.activedirectory.user-group.allowed-groups";
14-
public static final String ALLOW_TELEMETRY_PROPERTY = "azure.activedirectory.allow-telemetry";
1510

1611
public static final String CLIENT_ID = "real_client_id";
1712
public static final String CLIENT_SECRET = "real_client_secret";

0 commit comments

Comments
 (0)