com.microsoft.azure.management.keyvault.implementation.KeyImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-mgmt-keyvault Show documentation
Show all versions of azure-mgmt-keyvault Show documentation
This package contains Microsoft Azure Key Vault Management SDK. This package has been deprecated. A replacement package com.azure.resourcemanager:azure-resourcemanager-keyvault is available as of 31-March-2022. We strongly encourage you to upgrade to continue receiving updates. See Migration Guide https://aka.ms/java-track2-migration-guide for guidance on upgrading. Refer to our deprecation policy https://azure.github.io/azure-sdk/policies_support.html for more details.
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.management.keyvault.implementation;
import com.microsoft.azure.ListOperationCallback;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.keyvault.KeyIdentifier;
import com.microsoft.azure.keyvault.models.Attributes;
import com.microsoft.azure.keyvault.models.BackupKeyResult;
import com.microsoft.azure.keyvault.models.KeyAttributes;
import com.microsoft.azure.keyvault.models.KeyBundle;
import com.microsoft.azure.keyvault.models.KeyItem;
import com.microsoft.azure.keyvault.models.KeyOperationResult;
import com.microsoft.azure.keyvault.models.KeyVerifyResult;
import com.microsoft.azure.keyvault.requests.CreateKeyRequest;
import com.microsoft.azure.keyvault.requests.ImportKeyRequest;
import com.microsoft.azure.keyvault.requests.UpdateKeyRequest;
import com.microsoft.azure.keyvault.webkey.JsonWebKey;
import com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm;
import com.microsoft.azure.keyvault.webkey.JsonWebKeyOperation;
import com.microsoft.azure.keyvault.webkey.JsonWebKeySignatureAlgorithm;
import com.microsoft.azure.keyvault.webkey.JsonWebKeyType;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.keyvault.Key;
import com.microsoft.azure.management.keyvault.Vault;
import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl;
import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter;
import com.microsoft.azure.management.resources.fluentcore.utils.Utils;
import com.microsoft.rest.ServiceFuture;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func0;
import rx.functions.Func1;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Implementation for Vault and its parent interfaces.
*/
@LangDefinition
class KeyImpl
extends CreatableUpdatableImpl<
Key,
KeyBundle,
KeyImpl>
implements
Key,
Key.Definition,
Key.UpdateWithCreate,
Key.UpdateWithImport {
private final Vault vault;
private CreateKeyRequest.Builder createKeyRequest;
private UpdateKeyRequest.Builder updateKeyRequest;
private ImportKeyRequest.Builder importKeyRequest;
KeyImpl(String name, KeyBundle innerObject, Vault vault) {
super(name, innerObject);
this.vault = vault;
this.updateKeyRequest = new UpdateKeyRequest.Builder(vault.vaultUri(), name);
}
@Override
public String id() {
if (inner().keyIdentifier() == null) {
return null;
}
return inner().keyIdentifier().identifier();
}
@Override
public JsonWebKey jsonWebKey() {
return inner().key();
}
@Override
public KeyAttributes attributes() {
return inner().attributes();
}
@Override
public Map tags() {
return inner().tags();
}
@Override
public boolean managed() {
return Utils.toPrimitiveBoolean(inner().managed());
}
@Override
public PagedList listVersions() {
return new PagedListConverter() {
@Override
public Observable typeConvertAsync(final KeyItem keyItem) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().getKeyAsync(keyItem.identifier().identifier(), null);
}
@Override
protected Key wrapModel(KeyBundle keyBundle) {
return new KeyImpl(keyBundle.keyIdentifier().name(), keyBundle, vault);
}
}.toObservable();
}
}.convert(vault.client().listKeyVersions(vault.vaultUri(), name()));
}
@Override
public Observable listVersionsAsync() {
return new KeyVaultFutures.ListCallbackObserver() {
@Override
protected void list(ListOperationCallback callback) {
vault.client().listKeyVersionsAsync(vault.vaultUri(), name(), callback);
}
@Override
protected Observable typeConvertAsync(KeyItem o) {
return Observable.just(o.identifier());
}
}.toObservable()
.flatMap(new Func1>() {
@Override
public Observable call(final KeyIdentifier keyIdentifier) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().getKeyAsync(keyIdentifier.identifier(), null);
}
@Override
protected Key wrapModel(KeyBundle keyBundle) {
return new KeyImpl(keyIdentifier.name(), keyBundle, vault);
}
}.toObservable();
}
});
}
@Override
public byte[] backup() {
return vault.client().backupKey(vault.vaultUri(), name()).value();
}
@Override
public Observable backupAsync() {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().backupKeyAsync(vault.vaultUri(), name(), null);
}
@Override
protected byte[] wrapModel(BackupKeyResult backupKeyResult) {
return backupKeyResult.value();
}
}.toObservable();
}
@Override
public byte[] encrypt(JsonWebKeyEncryptionAlgorithm algorithm, byte[] content) {
return vault.client().encrypt(inner().keyIdentifier().identifier(), algorithm, content).result();
}
@Override
public Observable encryptAsync(final JsonWebKeyEncryptionAlgorithm algorithm, final byte[] content) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().encryptAsync(inner().keyIdentifier().identifier(), algorithm, content, null);
}
@Override
protected byte[] wrapModel(KeyOperationResult keyOperationResult) {
return keyOperationResult.result();
}
}.toObservable();
}
@Override
public byte[] decrypt(JsonWebKeyEncryptionAlgorithm algorithm, byte[] content) {
return vault.client().decrypt(inner().keyIdentifier().identifier(), algorithm, content).result();
}
@Override
public Observable decryptAsync(final JsonWebKeyEncryptionAlgorithm algorithm, final byte[] content) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().decryptAsync(inner().keyIdentifier().identifier(), algorithm, content, null);
}
@Override
protected byte[] wrapModel(KeyOperationResult keyOperationResult) {
return keyOperationResult.result();
}
}.toObservable();
}
@Override
public byte[] sign(JsonWebKeySignatureAlgorithm algorithm, byte[] digest) {
return vault.client().sign(inner().keyIdentifier().identifier(), algorithm, digest).result();
}
@Override
public Observable signAsync(final JsonWebKeySignatureAlgorithm algorithm, final byte[] digest) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().signAsync(inner().keyIdentifier().identifier(), algorithm, digest, null);
}
@Override
protected byte[] wrapModel(KeyOperationResult keyOperationResult) {
return keyOperationResult.result();
}
}.toObservable();
}
@Override
public boolean verify(JsonWebKeySignatureAlgorithm algorithm, byte[] digest, byte[] signature) {
return vault.client().verify(inner().keyIdentifier().identifier(), algorithm, digest, signature).value();
}
@Override
public Observable verifyAsync(final JsonWebKeySignatureAlgorithm algorithm, final byte[] digest, final byte[] signature) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().verifyAsync(inner().keyIdentifier().identifier(), algorithm, digest, signature, null);
}
@Override
protected Boolean wrapModel(KeyVerifyResult keyOperationResult) {
return keyOperationResult.value();
}
}.toObservable();
}
@Override
public byte[] wrapKey(JsonWebKeyEncryptionAlgorithm algorithm, byte[] key) {
return vault.client().wrapKey(inner().keyIdentifier().identifier(), algorithm, key).result();
}
@Override
public Observable wrapKeyAsync(final JsonWebKeyEncryptionAlgorithm algorithm, final byte[] key) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().wrapKeyAsync(inner().keyIdentifier().identifier(), algorithm, key, null);
}
@Override
protected byte[] wrapModel(KeyOperationResult keyOperationResult) {
return keyOperationResult.result();
}
}.toObservable();
}
@Override
public byte[] unwrapKey(JsonWebKeyEncryptionAlgorithm algorithm, byte[] key) {
return vault.client().unwrapKey(inner().keyIdentifier().identifier(), algorithm, key).result();
}
@Override
public Observable unwrapKeyAsync(final JsonWebKeyEncryptionAlgorithm algorithm, final byte[] key) {
return new KeyVaultFutures.ServiceFutureConverter() {
@Override
protected ServiceFuture callAsync() {
return vault.client().unwrapKeyAsync(inner().keyIdentifier().identifier(), algorithm, key, null);
}
@Override
protected byte[] wrapModel(KeyOperationResult keyOperationResult) {
return keyOperationResult.result();
}
}.toObservable();
}
@Override
protected Observable getInnerAsync() {
return Observable.from(vault.client().getKeyAsync(id(), null));
}
@Override
public KeyImpl withTags(Map tags) {
if (isInCreateMode()) {
if (createKeyRequest != null) {
createKeyRequest.withTags(tags);
} else {
importKeyRequest.withTags(tags);
}
} else {
updateKeyRequest.withTags(tags);
}
return this;
}
@Override
public boolean isInCreateMode() {
return id() == null;
}
@Override
public Observable createResourceAsync() {
return Observable.defer(new Func0>() {
@Override
public Observable call() {
if (createKeyRequest != null) {
return Observable.from(vault.client().createKeyAsync(createKeyRequest.build(), null))
.map(innerToFluentMap(KeyImpl.this))
.doOnCompleted(new Action0() {
@Override
public void call() {
createKeyRequest = null;
updateKeyRequest = new UpdateKeyRequest.Builder(vault.vaultUri(), name());
}
});
} else {
return Observable.from(vault.client().importKeyAsync(importKeyRequest.build(), null))
.map(innerToFluentMap(KeyImpl.this))
.doOnCompleted(new Action0() {
@Override
public void call() {
importKeyRequest = null;
updateKeyRequest = new UpdateKeyRequest.Builder(vault.vaultUri(), name());
}
});
}
}
});
}
@Override
public Observable updateResourceAsync() {
Observable set = Observable.just((Key) this);
if (createKeyRequest != null || importKeyRequest != null) {
set = createResourceAsync();
}
return set.flatMap(new Func1>() {
@Override
public Observable call(Key secret) {
return Observable.from(vault.client().updateKeyAsync(updateKeyRequest.build(), null));
}
}).flatMap(new Func1>() {
@Override
public Observable call(KeyBundle secretBundle) {
return refreshAsync();
}
}).doOnCompleted(new Action0() {
@Override
public void call() {
createKeyRequest = null;
importKeyRequest = null;
updateKeyRequest = new UpdateKeyRequest.Builder(vault.vaultUri(), name());
}
});
}
@Override
public KeyImpl withAttributes(Attributes attributes) {
if (isInCreateMode()) {
if (createKeyRequest != null) {
createKeyRequest.withAttributes(attributes);
} else {
importKeyRequest.withAttributes(attributes);
}
} else {
updateKeyRequest.withAttributes(attributes);
}
return this;
}
@Override
public KeyImpl withKeyTypeToCreate(JsonWebKeyType keyType) {
createKeyRequest = new CreateKeyRequest.Builder(vault.vaultUri(), name(), keyType);
return this;
}
@Override
public KeyImpl withLocalKeyToImport(JsonWebKey key) {
importKeyRequest = new ImportKeyRequest.Builder(vault.vaultUri(), name(), key);
return this;
}
@Override
public KeyImpl withKeyOperations(List keyOperations) {
if (isInCreateMode()) {
createKeyRequest.withKeyOperations(keyOperations);
} else {
updateKeyRequest.withKeyOperations(keyOperations);
}
return this;
}
@Override
public KeyImpl withKeyOperations(JsonWebKeyOperation... keyOperations) {
return withKeyOperations(Arrays.asList(keyOperations));
}
@Override
public KeyImpl withHsm(boolean isHsm) {
importKeyRequest.withHsm(isHsm);
return this;
}
@Override
public KeyImpl withKeySize(int size) {
createKeyRequest.withKeySize(size);
return this;
}
}