Skip to content

Commit

Permalink
Add correct audience to token for non Azure public clouds (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Jul 11, 2024
1 parent bef012b commit 1aadf23
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public AccessToken getAccessToken() {
ClientSecretCredential clientSecretCredential = getClientSecretCredential();

TokenRequestContext tokenRequestContext = new TokenRequestContext();
tokenRequestContext.setScopes(singletonList("https://graph.microsoft.com/.default"));
String graphResource = AzureEnvironment.getGraphResource(getAzureEnvironmentName());
tokenRequestContext.setScopes(singletonList(graphResource + ".default"));

Check warning on line 144 in src/main/java/com/microsoft/jenkins/azuread/AzureSecurityRealm.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 143-144 are not covered by tests

AccessToken accessToken = clientSecretCredential.getToken(tokenRequestContext).block();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hudson.ProxyConfiguration;
import hudson.util.Secret;
import io.jenkins.plugins.azuresdk.HttpClientRetriever;
import java.net.URI;
import jenkins.model.Jenkins;
import jenkins.util.JenkinsJVM;
import okhttp3.Credentials;
Expand All @@ -21,6 +22,7 @@

import static com.microsoft.jenkins.azuread.AzureEnvironment.AZURE_PUBLIC_CLOUD;
import static com.microsoft.jenkins.azuread.AzureEnvironment.getAuthorityHost;
import static com.microsoft.jenkins.azuread.AzureEnvironment.getGraphResource;
import static com.microsoft.jenkins.azuread.AzureEnvironment.getServiceRoot;

public class GraphClientCache {
Expand All @@ -38,7 +40,7 @@ private static GraphServiceClient<Request> createGraphClient(GraphClientCacheKey
OkHttpClient.Builder builder = HttpClients.createDefault(authProvider)
.newBuilder();

builder = addProxyToHttpClientIfRequired(builder);
builder = addProxyToHttpClientIfRequired(builder, key.getAzureEnvironmentName());
final OkHttpClient graphHttpClient = builder.build();

GraphServiceClient<Request> graphServiceClient = GraphServiceClient
Expand Down Expand Up @@ -79,11 +81,13 @@ public static GraphServiceClient<Request> getClient(AzureSecurityRealm azureSecu
return TOKEN_CACHE.get(key);
}

public static OkHttpClient.Builder addProxyToHttpClientIfRequired(OkHttpClient.Builder builder) {
public static OkHttpClient.Builder addProxyToHttpClientIfRequired(OkHttpClient.Builder builder, String azureEnvironmentName) {
if (JenkinsJVM.isJenkinsJVM()) {
ProxyConfiguration proxyConfiguration = Jenkins.get().getProxy();
if (proxyConfiguration != null && StringUtils.isNotBlank(proxyConfiguration.getName())) {
Proxy proxy = proxyConfiguration.createProxy("graph.microsoft.com");

String graphHost = URI.create(getGraphResource(azureEnvironmentName)).getHost();
Proxy proxy = proxyConfiguration.createProxy(graphHost);

Check warning on line 90 in src/main/java/com/microsoft/jenkins/azuread/GraphClientCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 43-90 are not covered by tests

builder = builder.proxy(proxy);
if (StringUtils.isNotBlank(proxyConfiguration.getUserName())) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/microsoft/jenkins/azuread/GraphProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ private void proxy(StaplerRequest request, StaplerResponse response) throws IOEx
private OkHttpClient getClient() {
ProxyConfiguration proxyConfiguration = Jenkins.get().getProxy();
if (proxyConfiguration != null && StringUtils.isNotBlank(proxyConfiguration.getName())) {
return addProxyToHttpClientIfRequired(new OkHttpClient().newBuilder()).build();
SecurityRealm securityRealm = Jenkins.get().getSecurityRealm();
AzureSecurityRealm azureSecurityRealm = ((AzureSecurityRealm) securityRealm);

String azureEnvironmentName = azureSecurityRealm.getAzureEnvironmentName();

return addProxyToHttpClientIfRequired(new OkHttpClient().newBuilder(), azureEnvironmentName).build();

Check warning on line 173 in src/main/java/com/microsoft/jenkins/azuread/GraphProxy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 168-173 are not covered by tests
}
return DEFAULT_CLIENT;
}
Expand Down

0 comments on commit 1aadf23

Please sign in to comment.