@BeforeClass public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException { KeyFactory keyfactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyfactory.generatePrivate( Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8)))); PublicKey publicKey = keyfactory.generatePublic( Pems.publicKeySpec(ByteSource.wrap(PUBLIC_KEY.getBytes(Charsets.UTF_8)))); keyPair = new KeyPair(publicKey, privateKey); openSshKey = SshKeys.encodeAsOpenSSH(RSAPublicKey.class.cast(publicKey)); }
@Override public Statement apply(String group) { checkNotNull(group, "group"); String validatorClientName = validatorName.get(); PrivateKey validatorKey = validatorCredential.get(); JsonBall bootstrapConfig = null; try { bootstrapConfig = bootstrapConfigForGroup.load(group); } catch (Exception e) { throw propagate(e); } Map<String, JsonBall> config = json.fromJson(bootstrapConfig.toString(), BootstrapConfigForGroup.BOOTSTRAP_CONFIG_TYPE); Optional<JsonBall> environment = Optional.fromNullable(config.get("environment")); String chefConfigDir = "{root}etc{fs}chef"; Statement createChefConfigDir = exec("{md} " + chefConfigDir); Statement createClientRb = appendFile( chefConfigDir + "{fs}client.rb", ImmutableList.of( "require 'rubygems'", "require 'ohai'", "o = Ohai::System.new", "o.all_plugins", String.format("node_name \"%s-\" + o[:ipaddress]", group), "log_level :info", "log_location STDOUT", String.format("validation_client_name \"%s\"", validatorClientName), String.format("chef_server_url \"%s\"", endpoint.get()))); Statement createValidationPem = appendFile( chefConfigDir + "{fs}validation.pem", Splitter.on(newLinePattern).split(Pems.pem(validatorKey))); String chefBootFile = chefConfigDir + "{fs}first-boot.json"; Statement createFirstBoot = appendFile(chefBootFile, Collections.singleton(json.toJson(bootstrapConfig))); ImmutableMap.Builder<String, String> options = ImmutableMap.builder(); options.put("-j", chefBootFile); if (environment.isPresent()) { options.put("-E", environment.get().toString()); } String strOptions = Joiner.on(' ').withKeyValueSeparator(" ").join(options.build()); Statement runChef = exec("chef-client " + strOptions); return newStatementList( new ExitInsteadOfReturn(installChef), createChefConfigDir, createClientRb, createValidationPem, createFirstBoot, runChef); }
@Override public X509Certificate deserialize( JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String keyText = json.getAsString().replaceAll("\\n", "\n"); try { return Pems.x509Certificate( ByteSource.wrap(keyText.getBytes(Charsets.UTF_8)), crypto.certFactory()); } catch (UnsupportedEncodingException e) { Throwables.propagate(e); return null; } catch (IOException e) { Throwables.propagate(e); return null; } catch (CertificateException e) { Throwables.propagate(e); return null; } }
@Override public PublicKey deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String keyText = json.getAsString().replaceAll("\\n", "\n"); try { return crypto .rsaKeyFactory() .generatePublic(Pems.publicKeySpec(ByteSource.wrap(keyText.getBytes(Charsets.UTF_8)))); } catch (UnsupportedEncodingException e) { Throwables.propagate(e); return null; } catch (InvalidKeySpecException e) { Throwables.propagate(e); return null; } catch (IOException e) { Throwables.propagate(e); return null; } }