Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e61d0ab
Enable revapi plugin
mnriem Oct 28, 2020
cc52f55
Enable revapi plugin
mnriem Oct 28, 2020
adb4daf
Adding initial plumbing for user-assigned managed identity
mnriem Oct 29, 2020
b86e7fa
Added support for user-assigned managed identity
mnriem Oct 30, 2020
46b637c
Communication - Purchase Search Follow-up PR (#16887)
jbeauregardb Oct 28, 2020
82cc38f
[Service Bus] Prepare tracing methods for message processor and sched…
YijunXieMS Oct 28, 2020
3307b60
Prepare Azure Core for November 2020 Release (#16924)
alzimmermsft Oct 28, 2020
f6a2000
Sb track2 schedule multiple message validate batch size (#16767)
hemanttanwar Oct 29, 2020
1ff1621
Mgmt: generate avs 2020 03 (#16954)
ChenTanyi Oct 29, 2020
c7a0679
mgmt rename session records by azure-core-test changes (#16921)
xseeseesee Oct 29, 2020
66d9527
Fixing live test failures in Event Hubs. (#16934)
conniey Oct 29, 2020
2b73ce8
Sb t2 schedule multiple message validate batch size (#16959)
hemanttanwar Oct 29, 2020
e2fd457
update MSI usage doc for service bus multi-binder sample (#16736)
Oct 29, 2020
23213a9
Fix redactor to skip redaction for empty key-value pairs values (#16943)
samvaity Oct 29, 2020
ecc9d52
Remove JsonPatchDocument.getOperations(), JsonPatchOperation, JsonPat…
alzimmermsft Oct 29, 2020
8be3526
Delete unused pipeline (#16945)
mikeharder Oct 29, 2020
3d6f053
Update CHANGELOG dates and added new CHANGELOG updates (#16967)
alzimmermsft Oct 29, 2020
321ccb2
Update CommunicationClientCredential.java (#16966)
chrwhit Oct 29, 2020
595b139
Communication - Added release phone number LRO (#16821)
jbeauregardb Oct 29, 2020
e2d4503
[Service Bus] Change getter of boolean values to isXyz() (#15890)
YijunXieMS Oct 29, 2020
16ba326
Fixed diagnostics information and other APIs on cosmos stored procedu…
kushagraThapar Oct 29, 2020
cc20791
Sync eng/common directory with azure-sdk-tools for PR 1146 (#16968)
azure-sdk Oct 29, 2020
6ae58f4
Update VM OS Image (#16976)
alzimmermsft Oct 29, 2020
18c3030
Fix digital twins client not deserializing date times correctly (#16975)
timtay-microsoft Oct 29, 2020
a300aa4
Addressing SpotBugs issues (#16894)
mnriem Oct 30, 2020
a717801
Add manual merge instructions to eng/common workflow (#16971)
azure-sdk Oct 30, 2020
e42ac84
Add PR Validation for Long Paths (#16980)
alzimmermsft Oct 30, 2020
996f3d1
Add etag property to BasicRelationship (#16981)
timtay-microsoft Oct 30, 2020
945d7dd
Test the common Generate_docindex scripts in each lang repo. (#16974)
sima-zhu Oct 30, 2020
4ed9c17
mgmt, appservice onedeploy (#16957)
weidongxu-microsoft Oct 30, 2020
8152105
Increment version for core releases (#17004)
azure-sdk Oct 30, 2020
f7796e2
Close client and check for link id when link is stolen (#16977)
srnagar Oct 30, 2020
bd750d5
Add ServiceBus Session Receiver Client (#16690)
YijunXieMS Oct 30, 2020
0106095
API ServiceBusErrorSource to represent source of error (#16710)
hemanttanwar Oct 30, 2020
895ee2a
[Service Bus] Migration Guide (#17003)
ramya-rao-a Oct 30, 2020
aad67fe
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
mnriem Oct 30, 2020
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 @@ -3,12 +3,13 @@

package com.azure.security.keyvault.jca;

import com.azure.security.keyvault.jca.rest.OAuthToken;
import com.azure.security.keyvault.jca.model.OAuthToken;

import java.util.HashMap;
import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.INFO;

/**
* The REST client specific to getting an access token for Azure REST APIs.
Expand Down Expand Up @@ -71,16 +72,17 @@ class AuthClient extends DelegateRestClient {
* Get an access token for a managed identity.
*
* @param resource the resource.
* @param identity the user-assigned identity (null if system-assigned)
* @return the authorization token.
*/
public String getAccessToken(String resource) {
public String getAccessToken(String resource, String identity) {
String result;

if (System.getenv("WEBSITE_SITE_NAME") != null
&& !System.getenv("WEBSITE_SITE_NAME").isEmpty()) {
result = getAccessTokenOnAppService(resource);
result = getAccessTokenOnAppService(resource, identity);
} else {
result = getAccessTokenOnOthers(resource);
result = getAccessTokenOnOthers(resource, identity);
}
return result;
}
Expand Down Expand Up @@ -126,17 +128,25 @@ public String getAccessToken(String resource, String tenantId,
* Get the access token on Azure App Service.
*
* @param resource the resource.
* @param identity the user-assigned identity (null if system-assigned).
* @return the authorization token.
*/
private String getAccessTokenOnAppService(String resource) {
private String getAccessTokenOnAppService(String resource, String identity) {
LOGGER.entering("AuthClient", "getAccessTokenOnAppService", resource);
LOGGER.info("Getting access token using managed identity based on MSI_SECRET");
if (identity != null) {
LOGGER.log(INFO, "Using managed identity with object ID: {0}", identity);
}
String result = null;

StringBuilder url = new StringBuilder();
url.append(System.getenv("MSI_ENDPOINT"))
.append("?api-version=2017-09-01")
.append(RESOURCE_FRAGMENT).append(resource);

if (identity != null) {
url.append("&objectid=").append(identity);
}

HashMap<String, String> headers = new HashMap<>();
headers.put("Metadata", "true");
Expand All @@ -156,16 +166,25 @@ private String getAccessTokenOnAppService(String resource) {
* Get the authorization token on everything else but Azure App Service.
*
* @param resource the resource.
* @param identity the user-assigned identity (null if system-assigned).
* @return the authorization token.
*/
private String getAccessTokenOnOthers(String resource) {
private String getAccessTokenOnOthers(String resource, String identity) {
LOGGER.entering("AuthClient", "getAccessTokenOnOthers", resource);
LOGGER.info("Getting access token using managed identity");
if (identity != null) {
LOGGER.log(INFO, "Using managed identity with object ID: {0}", identity);
}

String result = null;

StringBuilder url = new StringBuilder();
url.append(OAUTH2_MANAGED_IDENTITY_TOKEN_URL)
.append(RESOURCE_FRAGMENT).append(resource);

if (identity != null) {
url.append("&object_id=").append(identity);
}

HashMap<String, String> headers = new HashMap<>();
headers.put("Metadata", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT License.
package com.azure.security.keyvault.jca;

import com.azure.security.keyvault.jca.rest.CertificateBundle;
import com.azure.security.keyvault.jca.rest.CertificateItem;
import com.azure.security.keyvault.jca.rest.CertificateListResult;
import com.azure.security.keyvault.jca.rest.CertificatePolicy;
import com.azure.security.keyvault.jca.rest.KeyProperties;
import com.azure.security.keyvault.jca.rest.SecretBundle;
import com.azure.security.keyvault.jca.model.CertificateBundle;
import com.azure.security.keyvault.jca.model.CertificateItem;
import com.azure.security.keyvault.jca.model.CertificateListResult;
import com.azure.security.keyvault.jca.model.CertificatePolicy;
import com.azure.security.keyvault.jca.model.KeyProperties;
import com.azure.security.keyvault.jca.model.SecretBundle;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -67,6 +67,12 @@ class KeyVaultClient extends DelegateRestClient {
*/
private String clientSecret;

/**
* Stores the managed identity (either the user-assigned managed identity
* object ID or null if system-assigned)
*/
private String managedIdentity;

/**
* Constructor.
*
Expand All @@ -81,6 +87,22 @@ class KeyVaultClient extends DelegateRestClient {
this.keyVaultUrl = keyVaultUri;
}

/**
* Constructor.
*
* @param keyVaultUri the Azure Key Vault URI.
* @param managedIdentity the managed identity object ID.
*/
KeyVaultClient(String keyVaultUri, String managedIdentity) {
super(RestClientFactory.createClient());
LOGGER.log(INFO, "Using Azure Key Vault: {0}", keyVaultUri);
if (!keyVaultUri.endsWith("/")) {
keyVaultUri = keyVaultUri + "/";
}
this.keyVaultUrl = keyVaultUri;
this.managedIdentity = managedIdentity;
}

/**
* Constructor.
*
Expand All @@ -106,11 +128,16 @@ private String getAccessToken() {
String accessToken = null;
try {
AuthClient authClient = new AuthClient();

String resource = URLEncoder.encode("https://vault.azure.net", "UTF-8");
if (managedIdentity != null) {
managedIdentity = URLEncoder.encode(managedIdentity, "UTF-8");
}

if (tenantId != null && clientId != null && clientSecret != null) {
accessToken = authClient.getAccessToken(resource, tenantId, clientId, clientSecret);
} else {
accessToken = authClient.getAccessToken(resource);
accessToken = authClient.getAccessToken(resource, managedIdentity);
}
} catch (UnsupportedEncodingException uee) {
LOGGER.log(WARNING, "Unsupported encoding", uee);
Expand Down Expand Up @@ -235,13 +262,12 @@ Key getKey(String alias, char[] password) {
}
}
}

//
// If the private key is not available the certificate cannot be
// used for server side certificates or mTLS. Then we do not know
// the intent of the usage at this stage we skip this key.
//

LOGGER.exiting("KeyVaultClient", "getKey", key);
return key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public final class KeyVaultKeyStore extends KeyStoreSpi {
* The constructor uses System.getProperty for
* <code>azure.keyvault.uri</code>, <code>azure.keyvault.tenantId</code>,
* <code>azure.keyvault.clientId</code>,
* <code>azure.keyvault.clientSecret</code> to initialize the keyvault
* client.
* <code>azure.keyvault.clientSecret</code> and
* <code>azure.keyvault.userAssignedIdentity</code> to initialize the
* keyvault client.
* </p>
*/
public KeyVaultKeyStore() {
Expand All @@ -83,7 +84,12 @@ public KeyVaultKeyStore() {
String tenantId = System.getProperty("azure.keyvault.tenantId");
String clientId = System.getProperty("azure.keyvault.clientId");
String clientSecret = System.getProperty("azure.keyvault.clientSecret");
keyVaultClient = new KeyVaultClient(keyVaultUri, tenantId, clientId, clientSecret);
String managedIdentity = System.getProperty("azure.keyvault.managedIdentity");
if (clientId != null) {
keyVaultClient = new KeyVaultClient(keyVaultUri, tenantId, clientId, clientSecret);
} else {
keyVaultClient = new KeyVaultClient(keyVaultUri, managedIdentity);
}
}

@Override
Expand Down Expand Up @@ -198,11 +204,20 @@ public boolean engineIsKeyEntry(String alias) {
public void engineLoad(KeyStore.LoadStoreParameter param) {
if (param instanceof KeyVaultLoadStoreParameter) {
KeyVaultLoadStoreParameter parameter = (KeyVaultLoadStoreParameter) param;
keyVaultClient = new KeyVaultClient(
parameter.getUri(),
parameter.getTenantId(),
parameter.getClientId(),
parameter.getClientSecret());
if (parameter.getClientId() != null) {
keyVaultClient = new KeyVaultClient(
parameter.getUri(),
parameter.getTenantId(),
parameter.getClientId(),
parameter.getClientSecret());
} else if (parameter.getUserAssignedIdentity() != null) {
keyVaultClient = new KeyVaultClient(
parameter.getUri(),
parameter.getUserAssignedIdentity()
);
} else {
keyVaultClient = new KeyVaultClient(parameter.getUri());
}
}
sideLoad();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class KeyVaultLoadStoreParameter implements KeyStore.LoadStoreParameter {
* Stores the client secret.
*/
private final String clientSecret;

/**
* Stores the user-assigned identity.
*/
private final String userAssignedIdentity;

/**
* Constructor.
Expand All @@ -43,8 +48,36 @@ public KeyVaultLoadStoreParameter(String uri, String tenantId, String clientId,
this.tenantId = tenantId;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.userAssignedIdentity = null;
}

/**
* Constructor.
*
* @param uri the Azure Key Vault URI.
* @param userAssignedIdentity the user-assigned identity.
*/
public KeyVaultLoadStoreParameter(String uri, String userAssignedIdentity) {
this.uri = uri;
this.tenantId = null;
this.clientId = null;
this.clientSecret = null;
this.userAssignedIdentity = userAssignedIdentity;
}

/**
* Constructor.
*
* @param uri the Azure Key Vault URI.
*/
public KeyVaultLoadStoreParameter(String uri) {
this.uri = uri;
this.tenantId = null;
this.clientId = null;
this.clientSecret = null;
this.userAssignedIdentity = null;
}

/**
* Get the protection parameter.
*
Expand Down Expand Up @@ -90,4 +123,13 @@ public String getTenantId() {
public String getUri() {
return uri;
}

/**
* Get the user-assigned identity.
*
* @return the user-assign identity.
*/
public String getUserAssignedIdentity() {
return userAssignedIdentity;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import java.io.Serializable;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/**
* The Azure Key Vault JCA Provider models package.
*/
package com.azure.security.keyvault.jca.rest;
package com.azure.security.keyvault.jca.model;
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public class AuthClientTest {

/**
* Test getAuthorizationToken method.
*
* @throws Exception when a serious error occurs.
*/
@Test
public void testGetAuthorizationToken() throws Exception {
String tenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
String clientId = "2b8f123b-b18a-4077-bce0-42e10ce8bbab";
String clientSecret = "72-~tZ9~cG~rimDI0EkQSMQ1D9DYmGmI_I";
String tenantId = System.getProperty("azure.tenant.id");
String clientId = System.getProperty("azure.client.id");
String clientSecret = System.getProperty("azure.client.secret");
AuthClient authClient = new AuthClient();
String result = authClient.getAccessToken(
"https://management.azure.com/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.security.keyvault.jca;

import com.azure.security.keyvault.jca.rest.CertificateBundle;
import com.azure.security.keyvault.jca.model.CertificateBundle;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ azure.keyvault.clientId=${AZURE_KEYVAULT_CLIENT_ID}
# if you are not using managed identity).
azure.keyvault.clientSecret=${AZURE_KEYVAULT_CLIENT_SECRET}
# The server port.

# The user-assigned managed identity object-id to use.
#azure.keyvault.managedIdentity=

server.port=8443
# Just for debugging purposes.
management.endpoints.web.exposure.include=*
Loading