-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Cosmos Key Credential #4885
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
Cosmos Key Credential #4885
Changes from all commits
1c2d737
b179514
b83391f
4860536
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,24 +29,27 @@ public class CosmosClient implements AutoCloseable { | |
| private final ConsistencyLevel desiredConsistencyLevel; | ||
| private final List<Permission> permissions; | ||
| private final TokenResolver tokenResolver; | ||
| private final CosmosKeyCredential cosmosKeyCredential; | ||
|
|
||
|
|
||
| CosmosClient(CosmosClientBuilder builder) { | ||
| this.configs = builder.configs(); | ||
| this.serviceEndpoint = builder.endpoint(); | ||
| this.keyOrResourceToken = builder.key(); | ||
| this.connectionPolicy = builder.connectionPolicy(); | ||
| this.desiredConsistencyLevel = builder.consistencyLevel(); | ||
| this.permissions = builder.permissions(); | ||
| this.tokenResolver = builder.tokenResolver(); | ||
| this.asyncDocumentClient = new AsyncDocumentClient.Builder() | ||
| .withServiceEndpoint(this.serviceEndpoint) | ||
| .withMasterKeyOrResourceToken(this.keyOrResourceToken) | ||
| .withConnectionPolicy(this.connectionPolicy) | ||
| .withConsistencyLevel(this.desiredConsistencyLevel) | ||
| .withConfigs(this.configs) | ||
| .withTokenResolver(this.tokenResolver) | ||
| .build(); | ||
| this.keyOrResourceToken = builder.key(); | ||
| this.connectionPolicy = builder.connectionPolicy(); | ||
| this.desiredConsistencyLevel = builder.consistencyLevel(); | ||
| this.permissions = builder.permissions(); | ||
| this.tokenResolver = builder.tokenResolver(); | ||
| this.cosmosKeyCredential = builder.cosmosKeyCredential(); | ||
| this.asyncDocumentClient = new AsyncDocumentClient.Builder() | ||
| .withServiceEndpoint(this.serviceEndpoint) | ||
| .withMasterKeyOrResourceToken(this.keyOrResourceToken) | ||
| .withConnectionPolicy(this.connectionPolicy) | ||
| .withConsistencyLevel(this.desiredConsistencyLevel) | ||
| .withConfigs(this.configs) | ||
| .withTokenResolver(this.tokenResolver) | ||
| .withCosmosKeyCredential(this.cosmosKeyCredential) | ||
| .build(); | ||
| } | ||
|
|
||
| AsyncDocumentClient getContextClient() { | ||
|
|
@@ -121,10 +124,18 @@ TokenResolver getTokenResolver() { | |
| return tokenResolver; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the cosmos key credential | ||
| * @return cosmos key credential | ||
| */ | ||
| CosmosKeyCredential cosmosKeyCredential() { | ||
| return cosmosKeyCredential; | ||
| } | ||
|
|
||
| /** | ||
| * CREATE a Database if it does not already exist on the service | ||
| * | ||
| * The {@link Mono} upon successful completion will contain a single cosmos database response with the | ||
| * | ||
| * The {@link Mono} upon successful completion will contain a single cosmos database response with the | ||
christopheranderson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * created or existing database. | ||
| * @param databaseSettings CosmosDatabaseProperties | ||
| * @return a {@link Mono} containing the cosmos database response with the created or existing database or | ||
|
|
@@ -136,7 +147,7 @@ public Mono<CosmosDatabaseResponse> createDatabaseIfNotExists(CosmosDatabaseProp | |
|
|
||
| /** | ||
| * CREATE a Database if it does not already exist on the service | ||
| * The {@link Mono} upon successful completion will contain a single cosmos database response with the | ||
| * The {@link Mono} upon successful completion will contain a single cosmos database response with the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this read better with the following:
Potentially remove the statement given that its similar to the |
||
| * created or existing database. | ||
| * @param id the id of the database | ||
| * @return a {@link Mono} containing the cosmos database response with the created or existing database or | ||
|
|
@@ -145,7 +156,7 @@ public Mono<CosmosDatabaseResponse> createDatabaseIfNotExists(CosmosDatabaseProp | |
| public Mono<CosmosDatabaseResponse> createDatabaseIfNotExists(String id) { | ||
| return createDatabaseIfNotExistsInternal(getDatabase(id)); | ||
| } | ||
|
|
||
| private Mono<CosmosDatabaseResponse> createDatabaseIfNotExistsInternal(CosmosDatabase database){ | ||
| return database.read().onErrorResume(exception -> { | ||
| if (exception instanceof CosmosClientException) { | ||
|
|
@@ -160,12 +171,12 @@ private Mono<CosmosDatabaseResponse> createDatabaseIfNotExistsInternal(CosmosDat | |
|
|
||
| /** | ||
| * Creates a database. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * created database. | ||
| * In case of failure the {@link Mono} will error. | ||
| * | ||
| * | ||
| * @param databaseSettings {@link CosmosDatabaseProperties} | ||
| * @param options {@link CosmosDatabaseRequestOptions} | ||
| * @return an {@link Mono} containing the single cosmos database response with the created database or an error. | ||
|
|
@@ -183,12 +194,12 @@ public Mono<CosmosDatabaseResponse> createDatabase(CosmosDatabaseProperties data | |
|
|
||
| /** | ||
| * Creates a database. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * created database. | ||
| * In case of failure the {@link Mono} will error. | ||
| * | ||
| * | ||
| * @param databaseSettings {@link CosmosDatabaseProperties} | ||
| * @return an {@link Mono} containing the single cosmos database response with the created database or an error. | ||
| */ | ||
|
|
@@ -198,12 +209,12 @@ public Mono<CosmosDatabaseResponse> createDatabase(CosmosDatabaseProperties data | |
|
|
||
| /** | ||
| * Creates a database. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Mono} upon successful completion will contain a single resource response with the | ||
| * created database. | ||
| * In case of failure the {@link Mono} will error. | ||
| * | ||
| * | ||
| * @param id id of the database | ||
| * @return a {@link Mono} containing the single cosmos database response with the created database or an error. | ||
| */ | ||
|
|
@@ -275,27 +286,27 @@ public Mono<CosmosDatabaseResponse> createDatabase(String id, int throughput) { | |
|
|
||
| /** | ||
| * Reads all databases. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Flux} will contain one or several feed response of the read databases. | ||
| * In case of failure the {@link Flux} will error. | ||
| * | ||
| * | ||
| * @param options {@link FeedOptions} | ||
| * @return a {@link Flux} containing one or several feed response pages of read databases or an error. | ||
| */ | ||
| public Flux<FeedResponse<CosmosDatabaseProperties>> readAllDatabases(FeedOptions options) { | ||
| return getDocClientWrapper().readDatabases(options) | ||
| .map(response-> BridgeInternal.createFeedResponse(CosmosDatabaseProperties.getFromV2Results(response.results()), | ||
| .map(response-> BridgeInternal.createFeedResponse(CosmosDatabaseProperties.getFromV2Results(response.results()), | ||
| response.responseHeaders())); | ||
| } | ||
|
|
||
| /** | ||
| * Reads all databases. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * | ||
| * After subscription the operation will be performed. | ||
| * The {@link Flux} will contain one or several feed response of the read databases. | ||
| * In case of failure the {@link Flux} will error. | ||
| * | ||
| * | ||
| * @return a {@link Flux} containing one or several feed response pages of read databases or an error. | ||
| */ | ||
| public Flux<FeedResponse<CosmosDatabaseProperties>> readAllDatabases() { | ||
|
|
@@ -331,7 +342,7 @@ public Flux<FeedResponse<CosmosDatabaseProperties>> queryDatabases(String query, | |
| */ | ||
| public Flux<FeedResponse<CosmosDatabaseProperties>> queryDatabases(SqlQuerySpec querySpec, FeedOptions options){ | ||
| return getDocClientWrapper().queryDatabases(querySpec, options) | ||
| .map(response-> BridgeInternal.createFeedResponse( | ||
| .map(response-> BridgeInternal.createFeedResponse( | ||
| CosmosDatabaseProperties.getFromV2Results(response.results()), | ||
| response.responseHeaders())); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||||||||
| // Licensed under the MIT License. | ||||||||
| package com.azure.data.cosmos; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Cosmos Key Credential is used to store key credentials, in order to support dynamic key rotation. | ||||||||
| * Singleton instance should be used to support multiple keys. | ||||||||
| * Azure client library for Cosmos ensures to use the updated key provided in the same singleton instance | ||||||||
| * which was used when building {@link CosmosClient} | ||||||||
| */ | ||||||||
| public class CosmosKeyCredential { | ||||||||
kushagraThapar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| private String key; | ||||||||
|
|
||||||||
| // Stores key's hashcode for performance improvements | ||||||||
| private int keyHashCode; | ||||||||
|
|
||||||||
| public CosmosKeyCredential(String key) { | ||||||||
| this.key = key; | ||||||||
| this.keyHashCode = key.hashCode(); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns the key stored in Cosmos Key Credential | ||||||||
| * @return key | ||||||||
| */ | ||||||||
| public String key() { | ||||||||
kushagraThapar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| return key; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Sets the key to be used in CosmosKeyCredential | ||||||||
| * @param key key to be used in CosmosKeyCredential | ||||||||
| * @return current CosmosKeyCredential | ||||||||
| */ | ||||||||
| public CosmosKeyCredential key(String key) { | ||||||||
kushagraThapar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| this.key = key; | ||||||||
| this.keyHashCode = key.hashCode(); | ||||||||
| return this; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * CosmosKeyCredential stores the computed hashcode of the key for performance improvements. | ||||||||
| * @return hashcode of the key | ||||||||
| */ | ||||||||
| public int keyHashCode() { | ||||||||
kushagraThapar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| return this.keyHashCode; | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.