Skip to content

Commit

Permalink
mgmt fix todo (#12798)
Browse files Browse the repository at this point in the history
* support key curve name for EC key

* use flux for inputsteam
  • Loading branch information
weidongxu-microsoft authored Jul 6, 2020
1 parent e2e242c commit 6410184
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import com.azure.resourcemanager.appservice.models.KuduAuthenticationPolicy;
import com.azure.resourcemanager.appservice.models.WebAppBase;
import com.fasterxml.jackson.core.JsonParseException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -140,7 +143,9 @@ Mono<Void> warDeploy(
"x-ms-body-logging: false"
})
@Post("api/zipdeploy")
Mono<Void> zipDeploy(@HostParam("$host") String host, @BodyParam("application/octet-stream") byte[] zipFile);
Mono<Void> zipDeploy(
@HostParam("$host") String host,
@BodyParam("application/octet-stream") byte[] zipFile);

@Headers({
"Content-Type: application/octet-stream",
Expand Down Expand Up @@ -243,7 +248,12 @@ private static int findByte(ByteBuffer byteBuffer, byte b) {
}

Mono<Void> warDeployAsync(InputStream warFile, String appName) {
return withRetry(service.warDeploy(host, byteArrayFromInputStream(warFile), appName));
InputStreamFlux flux = fluxFromInputStream(warFile);
if (flux.flux != null) {
return withRetry(service.warDeploy(host, flux.flux, flux.size, appName));
} else {
return withRetry(service.warDeploy(host, flux.bytes, appName));
}
}

Mono<Void> warDeployAsync(File warFile, String appName) throws IOException {
Expand All @@ -259,7 +269,12 @@ Mono<Void> warDeployAsync(File warFile, String appName) throws IOException {
}

Mono<Void> zipDeployAsync(InputStream zipFile) {
return withRetry(service.zipDeploy(host, byteArrayFromInputStream(zipFile)));
InputStreamFlux flux = fluxFromInputStream(zipFile);
if (flux.flux != null) {
return withRetry(service.zipDeploy(host, flux.flux, flux.size));
} else {
return withRetry(service.zipDeploy(host, flux.bytes));
}
}

Mono<Void> zipDeployAsync(File zipFile) throws IOException {
Expand All @@ -274,21 +289,37 @@ Mono<Void> zipDeployAsync(File zipFile) throws IOException {
}));
}

private byte[] byteArrayFromInputStream(InputStream inputStream) {
// TODO (weidxu) core does not yet support InputStream as @BodyParam
private InputStreamFlux fluxFromInputStream(InputStream inputStream) {
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
InputStreamFlux inputStreamFlux = new InputStreamFlux();
if (inputStream instanceof FileInputStream) {
inputStreamFlux.size = ((FileInputStream) inputStream).getChannel().size();
inputStreamFlux.flux = FluxUtil.toFluxByteBuffer(inputStream);
} else if (inputStream instanceof ByteArrayInputStream) {
inputStreamFlux.size = inputStream.available();
inputStreamFlux.flux = FluxUtil.toFluxByteBuffer(inputStream);
} else {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
inputStreamFlux.bytes = buffer.toByteArray();
inputStreamFlux.size = inputStreamFlux.bytes.length;
}
return buffer.toByteArray();
return inputStreamFlux;
} catch (IOException e) {
throw logger.logExceptionAsError(new IllegalStateException(e));
}
}

private static class InputStreamFlux {
private Flux<ByteBuffer> flux;
private byte[] bytes;
private long size;
}

private Mono<Void> withRetry(Mono<Void> observable) {
return observable
.retryWhen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.azure.resourcemanager.appservice.models.WebContainer;
import com.azure.resourcemanager.resources.fluentcore.arm.Region;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.azure.resourcemanager.resources.fluentcore.profile.AzureProfile;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -75,7 +77,9 @@ public void canDeployMultipleWars() throws Exception {

if (!isPlaybackMode()) {
webApp.warDeploy(new File(WarDeployTests.class.getResource("/helloworld.war").getPath()));
webApp.warDeploy(new File(WarDeployTests.class.getResource("/helloworld.war").getPath()), "app2");
try (InputStream is = new FileInputStream(new File(WarDeployTests.class.getResource("/helloworld.war").getPath()))) {
webApp.warDeploy(is, "app2");
}

Response<String> response = curl("http://" + webappName + "." + "azurewebsites.net");
Assertions.assertEquals(200, response.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.azure.security.keyvault.keys.models.CreateRsaKeyOptions;
import com.azure.security.keyvault.keys.models.ImportKeyOptions;
import com.azure.security.keyvault.keys.models.JsonWebKey;
import com.azure.security.keyvault.keys.models.KeyCurveName;
import com.azure.security.keyvault.keys.models.KeyOperation;
import com.azure.security.keyvault.keys.models.KeyProperties;
import com.azure.security.keyvault.keys.models.KeyType;
Expand Down Expand Up @@ -345,13 +346,17 @@ public KeyImpl withHsm(boolean isHsm) {

@Override
public KeyImpl withKeySize(int size) {
// TODO (weidxu) currently no setKeySize in CreateEcKeyOptions
/*if (createKeyRequest instanceof CreateEcKeyOptions) {
((CreateEcKeyOptions) createKeyRequest).setKeySize(size);
} else */
if (createKeyRequest instanceof CreateRsaKeyOptions) {
((CreateRsaKeyOptions) createKeyRequest).setKeySize(size);
}
return this;
}

@Override
public DefinitionStages.WithCreate withKeyCurveName(KeyCurveName keyCurveName) {
if (createKeyRequest instanceof CreateEcKeyOptions) {
((CreateEcKeyOptions) createKeyRequest).setCurveName(keyCurveName);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.azure.security.keyvault.keys.cryptography.models.KeyWrapAlgorithm;
import com.azure.security.keyvault.keys.cryptography.models.SignatureAlgorithm;
import com.azure.security.keyvault.keys.models.JsonWebKey;
import com.azure.security.keyvault.keys.models.KeyCurveName;
import com.azure.security.keyvault.keys.models.KeyOperation;
import com.azure.security.keyvault.keys.models.KeyProperties;
import com.azure.security.keyvault.keys.models.KeyType;
Expand Down Expand Up @@ -198,12 +199,20 @@ interface WithKey {
/** The stage of a key definition allowing to specify the key size. */
interface WithKeySize {
/**
* Specifies the size of the key to create.
* Specifies the size of the RSA key to create.
*
* @param size the size of the key in integer
* @return the next stage of the definition
*/
WithCreate withKeySize(int size);

/**
* Specifies the name of the key curve for elliptic-curve key to create.
*
* @param keyCurveName name of the key curve
* @return the next stage of the definition
*/
WithCreate withKeyCurveName(KeyCurveName keyCurveName);
}

/** The stage of a key definition allowing to specify the allowed operations for the key. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.azure.security.keyvault.keys.cryptography.models.KeyWrapAlgorithm;
import com.azure.security.keyvault.keys.cryptography.models.SignatureAlgorithm;
import com.azure.security.keyvault.keys.models.JsonWebKey;
import com.azure.security.keyvault.keys.models.KeyCurveName;
import com.azure.security.keyvault.keys.models.KeyOperation;
import com.azure.security.keyvault.keys.models.KeyType;
import java.security.KeyPair;
Expand Down Expand Up @@ -74,6 +75,33 @@ public void canCRUDKey() throws Exception {
// List versions
Iterable<Key> keys = key.listVersions();
Assertions.assertEquals(2, TestUtilities.getSize(keys));

// Create RSA key with size
key = vault
.keys()
.define(keyName)
.withKeyTypeToCreate(KeyType.RSA)
.withKeyOperations(KeyOperation.SIGN, KeyOperation.VERIFY)
.withKeySize(2048)
.create();

Assertions.assertNotNull(key);
Assertions.assertNotNull(key.id());
Assertions.assertEquals(KeyType.RSA, key.getJsonWebKey().getKeyType());

// Create EC key with curve
key = vault
.keys()
.define(keyName)
.withKeyTypeToCreate(KeyType.EC)
.withKeyOperations(KeyOperation.SIGN, KeyOperation.VERIFY)
.withKeyCurveName(KeyCurveName.P_521)
.create();

Assertions.assertNotNull(key);
Assertions.assertNotNull(key.id());
Assertions.assertEquals(KeyType.EC, key.getJsonWebKey().getKeyType());
Assertions.assertEquals(KeyCurveName.P_521, key.getJsonWebKey().getCurveName());
}

@Test
Expand Down

0 comments on commit 6410184

Please sign in to comment.