EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [WindowsLoginCredentialsFromEncryptedData.java]

nameclass, %method, %block, %line, %
WindowsLoginCredentialsFromEncryptedData.java100% (1/1)100% (2/2)90%  (53/59)81%  (13/16)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class WindowsLoginCredentialsFromEncryptedData100% (1/1)100% (2/2)90%  (53/59)81%  (13/16)
apply (PasswordDataAndPrivateKey): LoginCredentials 100% (1/1)89%  (47/53)77%  (10/13)
WindowsLoginCredentialsFromEncryptedData (Crypto): void 100% (1/1)100% (6/6)100% (3/3)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.ec2.compute.functions;
20 
21import java.nio.charset.Charset;
22import java.security.KeyFactory;
23import java.security.PrivateKey;
24import java.security.spec.KeySpec;
25 
26import javax.annotation.Nullable;
27import javax.crypto.Cipher;
28import javax.inject.Inject;
29 
30import org.jclouds.crypto.Crypto;
31import org.jclouds.crypto.Pems;
32import org.jclouds.domain.LoginCredentials;
33import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
34import org.jclouds.encryption.internal.Base64;
35 
36import com.google.common.base.Function;
37import com.google.common.base.Throwables;
38import com.google.inject.Singleton;
39 
40/**
41 * Given an encrypted Windows Administrator password and the decryption key, return a LoginCredentials instance.
42 *
43 * @author Richard Downer
44 */
45@Singleton
46public class WindowsLoginCredentialsFromEncryptedData implements Function<PasswordDataAndPrivateKey, LoginCredentials> {
47 
48   private final Crypto crypto;
49 
50   @Inject
51   public WindowsLoginCredentialsFromEncryptedData(Crypto crypto) {
52      this.crypto = crypto;
53   }
54 
55   @Override
56   public LoginCredentials apply(@Nullable PasswordDataAndPrivateKey dataAndKey) {
57      if(dataAndKey == null)
58         return null;
59 
60      try {
61         KeySpec keySpec = Pems.privateKeySpec(dataAndKey.getPrivateKey());
62         KeyFactory kf = crypto.rsaKeyFactory();
63         PrivateKey privKey = kf.generatePrivate(keySpec);
64 
65         Cipher cipher = crypto.cipher("RSA/NONE/PKCS1Padding");
66         cipher.init(Cipher.DECRYPT_MODE, privKey);
67         byte[] cipherText = Base64.decode(dataAndKey.getPasswordData().getPasswordData());
68         byte[] plainText = cipher.doFinal(cipherText);
69         String password = new String(plainText, Charset.forName("ASCII"));
70 
71         return LoginCredentials.builder()
72            .user("Administrator")
73            .password(password)
74            .noPrivateKey()
75            .build();
76      } catch(Exception e) {
77         throw Throwables.propagate(e);
78      }
79   }
80}

[all classes][org.jclouds.ec2.compute.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov