Skip to content

Commit

Permalink
JCLOUDS-617: Use the configured JCE provider in the Cipher payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx committed Jul 2, 2014
1 parent c0d16e7 commit 7392994
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.hash.Hashing.sha1;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.toByteArray;
Expand All @@ -33,6 +34,7 @@
import javax.inject.Singleton;

import org.jclouds.Constants;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.TimeStamp;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpException;
Expand All @@ -50,6 +52,7 @@
import org.jclouds.util.Strings2;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
Expand All @@ -74,20 +77,22 @@ public class SignedHeaderAuth implements HttpRequestFilter {
private final Provider<String> timeStampProvider;
private final String emptyStringHash;
private final HttpUtils utils;
private final Crypto crypto;

@Resource
@Named(Constants.LOGGER_SIGNATURE)
Logger signatureLog = Logger.NULL;

@Inject
public SignedHeaderAuth(SignatureWire signatureWire, @org.jclouds.location.Provider Supplier<Credentials> creds,
Supplier<PrivateKey> supplyKey, @TimeStamp Provider<String> timeStampProvider, HttpUtils utils) {
this.signatureWire = signatureWire;
this.creds = creds;
this.supplyKey = supplyKey;
this.timeStampProvider = timeStampProvider;
Supplier<PrivateKey> supplyKey, @TimeStamp Provider<String> timeStampProvider, HttpUtils utils, Crypto crypto) {
this.signatureWire = checkNotNull(signatureWire, "signatureWire");
this.creds = checkNotNull(creds, "creds");
this.supplyKey = checkNotNull(supplyKey, "supplyKey");
this.timeStampProvider = checkNotNull(timeStampProvider, "timeStampProvider");
this.emptyStringHash = hashBody(Payloads.newStringPayload(""));
this.utils = utils;
this.utils = checkNotNull(utils, "utils");
this.crypto = checkNotNull(crypto, "crypto");
}

public HttpRequest filter(HttpRequest input) throws HttpException {
Expand Down Expand Up @@ -186,7 +191,7 @@ public boolean apply(Part input) {

public String sign(String toSign) {
try {
byte[] encrypted = toByteArray(new RSAEncryptingPayload(Payloads.newStringPayload(toSign), supplyKey.get()));
byte[] encrypted = toByteArray(new RSAEncryptingPayload(crypto, Payloads.newStringPayload(toSign), supplyKey.get()));
return base64().encode(encrypted);
} catch (IOException e) {
throw new HttpException("error signing request", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.jclouds.chef.strategy.ListEnvironments;
import org.jclouds.chef.strategy.ListNodes;
import org.jclouds.chef.strategy.UpdateAutomaticAttributesOnNode;
import org.jclouds.crypto.Crypto;
import org.jclouds.domain.JsonBall;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.RSADecryptingPayload;
Expand Down Expand Up @@ -90,6 +91,8 @@ public class BaseChefService implements ChefService {
private final ListEnvironments listEnvironments;
private final ListNodesInEnvironment listNodesInEnvironment;
private final Json json;
private final Crypto crypto;

@Resource
@Named(ChefProperties.CHEF_LOGGER)
protected Logger logger = Logger.NULL;
Expand All @@ -104,7 +107,7 @@ protected BaseChefService(ChefContext chefContext, ChefApi api,
@Named(CHEF_BOOTSTRAP_DATABAG) String databag, GroupToBootScript groupToBootScript,
BootstrapConfigForGroup bootstrapConfigForGroup, RunListForGroup runListForGroup,
ListEnvironments listEnvironments, ListNodesInEnvironment listNodesInEnvironment,
ListCookbookVersionsInEnvironment listCookbookVersionsInEnvironment, Json json) {
ListCookbookVersionsInEnvironment listCookbookVersionsInEnvironment, Json json, Crypto crypto) {
this.chefContext = checkNotNull(chefContext, "chefContext");
this.api = checkNotNull(api, "api");
this.cleanupStaleNodesAndClients = checkNotNull(cleanupStaleNodesAndClients, "cleanupStaleNodesAndClients");
Expand All @@ -126,6 +129,7 @@ protected BaseChefService(ChefContext chefContext, ChefApi api,
this.listNodesInEnvironment = checkNotNull(listNodesInEnvironment, "listNodesInEnvironment");
this.listCookbookVersionsInEnvironment = checkNotNull(listCookbookVersionsInEnvironment, "listCookbookVersionsInEnvironment");
this.json = checkNotNull(json, "json");
this.crypto = checkNotNull(crypto, "crypto");
}

@Override
Expand All @@ -135,13 +139,13 @@ public ChefContext getContext() {

@Override
public byte[] encrypt(InputSupplier<? extends InputStream> supplier) throws IOException {
return ByteStreams.toByteArray(new RSAEncryptingPayload(Payloads.newPayload(supplier.getInput()), privateKey
return ByteStreams.toByteArray(new RSAEncryptingPayload(crypto, Payloads.newPayload(supplier.getInput()), privateKey
.get()));
}

@Override
public byte[] decrypt(InputSupplier<? extends InputStream> supplier) throws IOException {
return ByteStreams.toByteArray(new RSADecryptingPayload(Payloads.newPayload(supplier.getInput()), privateKey
return ByteStreams.toByteArray(new RSADecryptingPayload(crypto, Payloads.newPayload(supplier.getInput()), privateKey
.get()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.jclouds.ContextBuilder;
import org.jclouds.chef.ChefApiMetadata;
import org.jclouds.crypto.Crypto;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpUtils;
Expand Down Expand Up @@ -193,6 +194,7 @@ protected void createFilter() throws IOException {
.modules(ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule())).buildInjector();

HttpUtils utils = injector.getInstance(HttpUtils.class);
Crypto crypto = injector.getInstance(Crypto.class);

Supplier<PrivateKey> privateKey = injector.getInstance(Key.get(new TypeLiteral<Supplier<PrivateKey>>() {
}));
Expand All @@ -205,7 +207,7 @@ public String get() {
return TIMESTAMP_ISO8601;
}

}, utils);
}, utils, crypto);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Expand All @@ -29,6 +30,7 @@
import org.jclouds.chef.domain.Client;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.Pems;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
import org.jclouds.io.Payloads;
Expand Down Expand Up @@ -77,16 +79,16 @@ protected void configure() {
privateKey = crypto.rsaKeyFactory().generatePrivate(Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8))));
}

public void test() throws IOException {
public void test() throws IOException, CertificateException, NoSuchAlgorithmException {

Client user = Client.builder().certificate(certificate).orgname("jclouds").clientname("adriancole-jcloudstest")
.name("adriancole-jcloudstest").isValidator(false).privateKey(privateKey).build();

byte[] encrypted = ByteStreams.toByteArray(new RSAEncryptingPayload(Payloads.newPayload("fooya"), user
byte[] encrypted = ByteStreams.toByteArray(new RSAEncryptingPayload(new JCECrypto(), Payloads.newPayload("fooya"), user
.getCertificate().getPublicKey()));

assertEquals(
ByteStreams.toByteArray(new RSADecryptingPayload(Payloads.newPayload(encrypted), user.getPrivateKey())),
ByteStreams.toByteArray(new RSADecryptingPayload(new JCECrypto(), Payloads.newPayload(encrypted), user.getPrivateKey())),
"fooya".getBytes());

assertEquals(
Expand Down

0 comments on commit 7392994

Please sign in to comment.