|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Cloud Foundry Java Buildpack |
| 4 | +# Copyright 2013-2020 the original author or authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# 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, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +require 'fileutils' |
| 19 | +require 'shellwords' |
| 20 | +require 'tempfile' |
| 21 | +require 'java_buildpack/component/versioned_dependency_component' |
| 22 | +require 'java_buildpack/framework' |
| 23 | +require 'java_buildpack/util/qualify_path' |
| 24 | + |
| 25 | +module JavaBuildpack |
| 26 | + module Framework |
| 27 | + |
| 28 | + # Encapsulates the functionality for enabling zero-touch Safenet ProtectApp Java Security Provider support. |
| 29 | + class CloudSqlSecurityProvider < JavaBuildpack::Component::VersionedDependencyComponent |
| 30 | + include JavaBuildpack::Util |
| 31 | + |
| 32 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 33 | + def compile |
| 34 | + download_zip false |
| 35 | + |
| 36 | + @droplet.copy_resources |
| 37 | + |
| 38 | + credentials = @application.services.find_service(FILTER, 'sslrootcert', 'sslcert', 'sslkey')['credentials'] |
| 39 | + |
| 40 | + pkcs12 = merge_client_credentials credentials |
| 41 | + add_client_credentials pkcs12 |
| 42 | + |
| 43 | + add_trusted_certificates credentials['sslrootcert'] |
| 44 | + end |
| 45 | + |
| 46 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 47 | + def release |
| 48 | + java_opts = @droplet.java_opts |
| 49 | + |
| 50 | + add_additional_properties(java_opts) |
| 51 | + end |
| 52 | + |
| 53 | + protected |
| 54 | + |
| 55 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) |
| 56 | + def supports? |
| 57 | + @application.services.one_service? FILTER, 'sslrootcert', 'sslcert', 'sslkey' |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + FILTER = /csb-google-mysql/.freeze |
| 63 | + |
| 64 | + private_constant :FILTER |
| 65 | + |
| 66 | + def add_additional_properties(java_opts) |
| 67 | + java_opts |
| 68 | + .add_system_property('javax.net.ssl.keyStore', keystore) |
| 69 | + .add_system_property('javax.net.ssl.keyStorePassword', password) |
| 70 | + end |
| 71 | + |
| 72 | + def add_client_credentials(pkcs12) |
| 73 | + shell "#{keytool} -importkeystore -noprompt -destkeystore #{keystore} -deststorepass #{password} " \ |
| 74 | + "-srckeystore #{pkcs12.path} -srcstorepass #{password} -srcstoretype pkcs12" \ |
| 75 | + " -alias #{File.basename(pkcs12)}" |
| 76 | + end |
| 77 | + |
| 78 | + def add_trusted_certificates(trusted_certificate) |
| 79 | + File.open("#{@droplet.root}/ssl/certs/ca-certificates.crt", 'a') do |f| |
| 80 | + f.write("#{trusted_certificate}\n") |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + def ext_dir |
| 85 | + @droplet.sandbox + 'ext' |
| 86 | + end |
| 87 | + |
| 88 | + def keystore |
| 89 | + @droplet.sandbox + 'cloud-sql-keystore.jks' |
| 90 | + end |
| 91 | + |
| 92 | + def keytool |
| 93 | + @droplet.java_home.root + 'bin/keytool' |
| 94 | + end |
| 95 | + |
| 96 | + def merge_client_credentials(credentials) |
| 97 | + certificate = write_certificate credentials['sslcert'] |
| 98 | + private_key = write_private_key credentials['sslkey'] |
| 99 | + |
| 100 | + pkcs12 = Tempfile.new('pkcs12-') |
| 101 | + pkcs12.close |
| 102 | + |
| 103 | + shell "openssl pkcs12 -export -in #{certificate.path} -inkey #{private_key.path} " \ |
| 104 | + "-name #{File.basename(pkcs12)} -out #{pkcs12.path} -passout pass:#{password}" |
| 105 | + |
| 106 | + pkcs12 |
| 107 | + end |
| 108 | + |
| 109 | + def password |
| 110 | + 'cloud-sql-keystore-password' |
| 111 | + end |
| 112 | + |
| 113 | + def write_certificate(certificate) |
| 114 | + Tempfile.open('certificate-') do |f| |
| 115 | + f.write "#{certificate}\n" |
| 116 | + f.sync |
| 117 | + f |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + def write_private_key(private_key) |
| 122 | + Tempfile.open('private-key-') do |f| |
| 123 | + f.write "#{private_key}\n" |
| 124 | + f.sync |
| 125 | + f |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments