Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add correct audience to token for non Azure public clouds #593

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
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,52 +40,54 @@
OkHttpClient.Builder builder = HttpClients.createDefault(authProvider)
.newBuilder();

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

GraphServiceClient<Request> graphServiceClient = GraphServiceClient
.builder()
.httpClient(graphHttpClient)
.buildClient();

String azureEnv = key.getAzureEnvironmentName();

if (!azureEnv.equals(AZURE_PUBLIC_CLOUD)) {
graphServiceClient.setServiceRoot(getServiceRoot(azureEnv));
}
return graphServiceClient;
}

static ClientSecretCredential getClientSecretCredential(GraphClientCacheKey key) {
return new ClientSecretCredentialBuilder()
.clientId(key.getClientId())
.clientSecret(key.getClientSecret())
.tenantId(key.getTenantId())
.authorityHost(getAuthorityHost(key.getAzureEnvironmentName()))
.httpClient(HttpClientRetriever.get())
.build();
}

static GraphServiceClient<Request> getClient(GraphClientCacheKey key) {
return TOKEN_CACHE.get(key);
}

public static GraphServiceClient<Request> getClient(AzureSecurityRealm azureSecurityRealm) {
GraphClientCacheKey key = new GraphClientCacheKey(
azureSecurityRealm.getClientId(),
Secret.toString(azureSecurityRealm.getClientSecret()),
azureSecurityRealm.getTenant(),
azureSecurityRealm.getAzureEnvironmentName()
);

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 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