|
15 | 15 | # See the License for the specific language governing permissions and |
16 | 16 | # limitations under the License. |
17 | 17 |
|
18 | | -require 'java_buildpack/logging/logger_factory' |
19 | 18 | require 'java_buildpack/component/base_component' |
20 | 19 | require 'java_buildpack/framework' |
21 | | -require 'fileutils' |
22 | | -require 'net/http' |
23 | | -require 'json' |
24 | | -require 'date' |
25 | | -require 'cgi' |
| 20 | +require 'java_buildpack/util/dash_case' |
26 | 21 |
|
27 | 22 | module JavaBuildpack |
28 | 23 | module Framework |
29 | 24 |
|
30 | 25 | # Encapsulates the functionality for enabling zero-touch Seeker support. |
31 | 26 | class SeekerSecurityProvider < JavaBuildpack::Component::BaseComponent |
| 27 | + |
32 | 28 | # Creates an instance |
33 | 29 | # |
34 | 30 | # @param [Hash] context a collection of utilities used the component |
35 | 31 | def initialize(context) |
36 | 32 | super(context) |
37 | | - @logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger SeekerSecurityProvider |
| 33 | + |
| 34 | + @uri = download_url(credentials) if supports? |
38 | 35 | end |
39 | 36 |
|
40 | 37 | # (see JavaBuildpack::Component::BaseComponent#detect) |
41 | 38 | def detect |
42 | | - @application.services.one_service? FILTER |
| 39 | + @uri ? self.class.to_s.dash_case : nil |
43 | 40 | end |
44 | 41 |
|
45 | 42 | # (see JavaBuildpack::Component::BaseComponent#compile) |
46 | | - |
47 | 43 | def compile |
48 | | - @logger.info { 'Seeker buildpack compile stage start' } |
49 | | - credentials = fetch_credentials |
50 | | - @logger.info { "Credentials #{credentials}" } |
51 | | - assert_configuration_valid(credentials) |
52 | | - if should_download_sensor(credentials[ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY]) |
53 | | - fetch_agent_within_sensor(credentials) |
54 | | - else |
55 | | - fetch_agent_direct(credentials) |
| 44 | + JavaBuildpack::Util::Cache::InternetAvailability.instance.available( |
| 45 | + true, 'Downloading from Synopsys Seeker Server' |
| 46 | + ) do |
| 47 | + download_zip('', @uri, false, @droplet.sandbox, @component_name) |
56 | 48 | end |
57 | 49 | @droplet.copy_resources |
58 | | - end |
59 | | - |
60 | | - # extract seeker relevant configuration as map |
61 | | - def fetch_credentials |
62 | | - service = @application.services.find_service FILTER |
63 | | - service['credentials'] |
64 | | - end |
65 | | - |
66 | | - # verify required agent configuration is present |
67 | | - def assert_configuration_valid(credentials) |
68 | | - mandatory_config_keys = |
69 | | - [ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY, SENSOR_HOST_SERVICE_CONFIG_KEY, |
70 | | - SENSOR_PORT_SERVICE_CONFIG_KEY, SEEKER_SERVER_URL_CONFIG_KEY] |
71 | | - mandatory_config_keys.each do |config_key| |
72 | | - raise "'#{config_key}' credential must be set" unless credentials[config_key] |
73 | | - end |
| 50 | + rescue StandardError => e |
| 51 | + raise "Synopsys Seeker download failed: #{e}" |
74 | 52 | end |
75 | 53 |
|
76 | 54 | # (see JavaBuildpack::Component::BaseComponent#release) |
77 | 55 | def release |
78 | | - @logger.info { 'Seeker buildpack release stage start' } |
79 | | - credentials = fetch_credentials |
| 56 | + c = credentials |
| 57 | + |
80 | 58 | @droplet.java_opts.add_javaagent(@droplet.sandbox + 'seeker-agent.jar') |
81 | 59 | @droplet.environment_variables |
82 | | - .add_environment_variable('SEEKER_SENSOR_HOST', credentials[SENSOR_HOST_SERVICE_CONFIG_KEY]) |
83 | | - .add_environment_variable('SEEKER_SENSOR_HTTP_PORT', credentials[SENSOR_PORT_SERVICE_CONFIG_KEY]) |
84 | | - .add_environment_variable('SEEKER_SERVER_URL', credentials[SEEKER_SERVER_URL_CONFIG_KEY]) |
| 60 | + .add_environment_variable('SEEKER_SERVER_URL', c[SEEKER_SERVER_URL_CONFIG_KEY]) |
85 | 61 | end |
86 | 62 |
|
87 | | - # JSON key for the host of the seeker sensor |
88 | | - SENSOR_HOST_SERVICE_CONFIG_KEY = 'sensor_host' |
89 | | - |
90 | | - # JSON key for the port of the seeker sensor |
91 | | - SENSOR_PORT_SERVICE_CONFIG_KEY = 'sensor_port' |
92 | | - # JSON key for the address of seeker sensor |
93 | | - SEEKER_SERVER_URL_CONFIG_KEY = 'seeker_server_url' |
94 | | - |
95 | | - # Enterprise server url, for example: `https://seeker-server.com:8082` |
96 | | - ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY = 'enterprise_server_url' |
97 | | - |
98 | | - # Relative path of the sensor zip |
99 | | - SENSOR_ZIP_RELATIVE_PATH_AT_ENTERPRISE_SERVER = 'rest/ui/installers/binaries/LINUX' |
100 | | - |
101 | | - # Relative path of the Java agent jars after Sensor extraction |
102 | | - AGENT_JARS_PATH = 'inline/agents/java/*' |
| 63 | + private |
103 | 64 |
|
104 | 65 | # Relative path of the agent zip |
105 | 66 | AGENT_PATH = '/rest/api/latest/installers/agents/binaries/JAVA' |
106 | 67 |
|
107 | | - # Version details of Seekers server REST API path |
108 | | - SEEKER_VERSION_API = '/rest/api/version' |
109 | | - |
110 | 68 | # seeker service name identifier |
111 | | - FILTER = /seeker/.freeze |
112 | | - |
113 | | - private_constant :SENSOR_HOST_SERVICE_CONFIG_KEY, :SENSOR_PORT_SERVICE_CONFIG_KEY, |
114 | | - :ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY, :SENSOR_ZIP_RELATIVE_PATH_AT_ENTERPRISE_SERVER, |
115 | | - :AGENT_JARS_PATH, :AGENT_PATH, :SEEKER_VERSION_API |
116 | | - |
117 | | - private |
118 | | - |
119 | | - def should_download_sensor(server_base_url) |
120 | | - json_response = get_seeker_version_details(server_base_url) |
121 | | - @logger.debug { "Seeker server response for version WS: #{json_response}" } |
122 | | - seeker_version_response = JSON.parse(json_response) |
123 | | - seeker_version = seeker_version_response['version'] |
124 | | - version_prefix = seeker_version[0, 7] |
125 | | - last_seeker_version_without_agent_direct_download_date = Date.parse('2018.05.01') |
126 | | - @logger.info { "Current Seeker version #{version_prefix}" } |
127 | | - current_seeker_version = Date.parse(version_prefix + '.01') |
128 | | - current_seeker_version <= last_seeker_version_without_agent_direct_download_date |
129 | | - end |
| 69 | + FILTER = /seeker/i.freeze |
130 | 70 |
|
131 | | - def get_seeker_version_details(server_base_url) |
132 | | - uri = URI.parse(server_base_url) |
133 | | - http = Net::HTTP.new(uri.host, uri.port) |
134 | | - if uri.scheme == 'https' |
135 | | - http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
136 | | - http.use_ssl = true |
137 | | - end |
138 | | - http_response = http.request_get(SEEKER_VERSION_API) |
139 | | - http_response.body |
140 | | - end |
| 71 | + # JSON key for the address of seeker sensor |
| 72 | + SEEKER_SERVER_URL_CONFIG_KEY = 'seeker_server_url' |
141 | 73 |
|
142 | | - def agent_direct_link(credentials) |
143 | | - URI.join(credentials[ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY], AGENT_PATH).to_s |
144 | | - end |
| 74 | + private_constant :AGENT_PATH, :FILTER, :SEEKER_SERVER_URL_CONFIG_KEY |
145 | 75 |
|
146 | | - def fetch_agent_direct(credentials) |
147 | | - @logger.info { 'Trying to download agent directly...' } |
148 | | - java_agent_zip_uri = agent_direct_link(credentials) |
149 | | - download_agent(java_agent_zip_uri) |
| 76 | + def credentials |
| 77 | + @application.services.find_service(FILTER, SEEKER_SERVER_URL_CONFIG_KEY)['credentials'] |
150 | 78 | end |
151 | 79 |
|
152 | | - def download_agent(java_agent_zip_uri) |
153 | | - @logger.debug { "Before downloading Agent from: #{java_agent_zip_uri}" } |
154 | | - download_zip('', java_agent_zip_uri, false, @droplet.sandbox) |
| 80 | + def download_url(credentials) |
| 81 | + "#{credentials[SEEKER_SERVER_URL_CONFIG_KEY]}#{AGENT_PATH}" |
155 | 82 | end |
156 | 83 |
|
157 | | - def fetch_agent_within_sensor(credentials) |
158 | | - @logger.info { 'Trying to download sensor...' } |
159 | | - seeker_tmp_dir = @droplet.sandbox + 'seeker_tmp_sensor' |
160 | | - shell "rm -rf #{seeker_tmp_dir}" |
161 | | - sensor_direct_link = sensor_direct_link(credentials) |
162 | | - @logger.debug { "Before downloading Sensor from: #{sensor_direct_link}" } |
163 | | - download_zip('', sensor_direct_link, |
164 | | - false, seeker_tmp_dir, 'SensorInstaller.zip') |
165 | | - inner_jar_file = seeker_tmp_dir + 'SeekerInstaller.jar' |
166 | | - # Unzip only the java agent - to save time |
167 | | - shell "unzip -j #{inner_jar_file} #{AGENT_JARS_PATH} -d #{@droplet.sandbox} 2>&1" |
168 | | - shell "rm -rf #{seeker_tmp_dir}" |
| 84 | + def supports? |
| 85 | + @application.services.one_service?(FILTER, SEEKER_SERVER_URL_CONFIG_KEY) |
169 | 86 | end |
170 | 87 |
|
171 | | - def sensor_direct_link(credentials) |
172 | | - enterprise_server_uri = URI.parse(credentials[ENTERPRISE_SERVER_URL_SERVICE_CONFIG_KEY].strip) |
173 | | - URI.join(enterprise_server_uri, SENSOR_ZIP_RELATIVE_PATH_AT_ENTERPRISE_SERVER).to_s |
174 | | - end |
175 | 88 | end |
| 89 | + |
176 | 90 | end |
| 91 | + |
177 | 92 | end |
0 commit comments