|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Travis::API::V3 |
| 4 | + class InsightsClient |
| 5 | + class ConfigurationError < StandardError; end |
| 6 | + |
| 7 | + def initialize(user_id) |
| 8 | + @user_id = user_id |
| 9 | + end |
| 10 | + |
| 11 | + def user_notifications(filter, page, active, sort_by, sort_direction) |
| 12 | + query_string = query_string_from_params( |
| 13 | + value: filter, |
| 14 | + page: page || '1', |
| 15 | + active: active, |
| 16 | + order: sort_by, |
| 17 | + order_dir: sort_direction |
| 18 | + ) |
| 19 | + response = connection.get("/user_notifications?#{query_string}") |
| 20 | + |
| 21 | + handle_errors_and_respond(response) do |body| |
| 22 | + notifications = body['data'].map do |notification| |
| 23 | + Travis::API::V3::Models::InsightsNotification.new(notification) |
| 24 | + end |
| 25 | + |
| 26 | + Travis::API::V3::Models::InsightsCollection.new(notifications, body.fetch('total_count')) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + def toggle_snooze_user_notifications(notification_ids) |
| 31 | + response = connection.put('/user_notifications/toggle_snooze', snooze_ids: notification_ids) |
| 32 | + |
| 33 | + handle_errors_and_respond(response) |
| 34 | + end |
| 35 | + |
| 36 | + def user_plugins(filter, page, active, sort_by, sort_direction) |
| 37 | + query_string = query_string_from_params( |
| 38 | + value: filter, |
| 39 | + page: page || '1', |
| 40 | + active: active, |
| 41 | + order: sort_by, |
| 42 | + order_dir: sort_direction |
| 43 | + ) |
| 44 | + response = connection.get("/user_plugins?#{query_string}") |
| 45 | + |
| 46 | + handle_errors_and_respond(response) do |body| |
| 47 | + plugins = body['data'].map do |plugin| |
| 48 | + Travis::API::V3::Models::InsightsPlugin.new(plugin) |
| 49 | + end |
| 50 | + |
| 51 | + Travis::API::V3::Models::InsightsCollection.new(plugins, body.fetch('total_count')) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def create_plugin(params) |
| 56 | + response = connection.post("/user_plugins", user_plugin: params) |
| 57 | + handle_errors_and_respond(response) do |body| |
| 58 | + Travis::API::V3::Models::InsightsPlugin.new(body['plugin']) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def toggle_active_plugins(plugin_ids) |
| 63 | + response = connection.put('/user_plugins/toggle_active', toggle_ids: plugin_ids) |
| 64 | + |
| 65 | + handle_errors_and_respond(response) do |body| |
| 66 | + Travis::API::V3::Models::InsightsCollection.new([], 0) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def delete_many_plugins(plugin_ids) |
| 71 | + response = connection.delete('/user_plugins/delete_many', delete_ids: plugin_ids) |
| 72 | + |
| 73 | + handle_errors_and_respond(response) do |body| |
| 74 | + Travis::API::V3::Models::InsightsCollection.new([], 0) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def generate_key(plugin_name, plugin_type) |
| 79 | + response = connection.get('/user_plugins/generate_key', name: plugin_name, plugin_type: plugin_type) |
| 80 | + |
| 81 | + handle_errors_and_respond(response) do |body| |
| 82 | + body |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def authenticate_key(params) |
| 87 | + response = connection.post('/user_plugins/authenticate_key', params) |
| 88 | + |
| 89 | + handle_errors_and_respond(response) do |body| |
| 90 | + body |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def template_plugin_tests(plugin_type) |
| 95 | + response = connection.get("/user_plugins/#{plugin_type}/template_plugin_tests") |
| 96 | + |
| 97 | + handle_errors_and_respond(response) do |body| |
| 98 | + body |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def get_scan_logs(plugin_id, last_id) |
| 103 | + params = last_id ? { last: last_id, poll: true } : {} |
| 104 | + response = connection.get("/user_plugins/#{plugin_id}/get_scan_logs", params) |
| 105 | + |
| 106 | + handle_errors_and_respond(response) do |body| |
| 107 | + body |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + def probes(filter, page, active, sort_by, sort_direction) |
| 112 | + query_string = query_string_from_params( |
| 113 | + value: filter, |
| 114 | + page: page || '1', |
| 115 | + active: active, |
| 116 | + order: sort_by, |
| 117 | + order_dir: sort_direction |
| 118 | + ) |
| 119 | + response = connection.get("/probes?#{query_string}") |
| 120 | + |
| 121 | + handle_errors_and_respond(response) do |body| |
| 122 | + probes = body['data'].map do |probe| |
| 123 | + Travis::API::V3::Models::InsightsProbe.new(probe) |
| 124 | + end |
| 125 | + |
| 126 | + Travis::API::V3::Models::InsightsCollection.new(probes, body.fetch('total_count')) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def create_probe(params) |
| 131 | + response = connection.post("/probes", test_template: params) |
| 132 | + handle_errors_and_respond(response) do |body| |
| 133 | + Travis::API::V3::Models::InsightsProbe.new(body) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + def update_probe(params) |
| 138 | + response = connection.patch("/probes/#{params['probe_id']}", params) |
| 139 | + handle_errors_and_respond(response) do |body| |
| 140 | + Travis::API::V3::Models::InsightsProbe.new(body) |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + def get_probe(params) |
| 145 | + response = connection.get("/probes/#{params['probe_id']}/template_test", params) |
| 146 | + handle_errors_and_respond(response) do |body| |
| 147 | + Travis::API::V3::Models::InsightsProbe.new(body) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + def toggle_active_probes(probe_ids) |
| 152 | + response = connection.put('/probes/toggle_active', toggle_ids: probe_ids) |
| 153 | + |
| 154 | + handle_errors_and_respond(response) do |body| |
| 155 | + Travis::API::V3::Models::InsightsCollection.new([], 0) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + def delete_many_probes(probe_ids) |
| 160 | + response = connection.delete('/probes/delete_many', delete_ids: probe_ids) |
| 161 | + |
| 162 | + handle_errors_and_respond(response) do |body| |
| 163 | + Travis::API::V3::Models::InsightsCollection.new([], 0) |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + def sandbox_plugins(plugin_type) |
| 168 | + response = connection.post('/sandbox/plugins', plugin_type: plugin_type) |
| 169 | + |
| 170 | + handle_errors_and_respond(response) do |body| |
| 171 | + body |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + def sandbox_plugin_data(plugin_id) |
| 176 | + response = connection.post('/sandbox/plugin_data', plugin_id: plugin_id) |
| 177 | + |
| 178 | + handle_errors_and_respond(response) do |body| |
| 179 | + body |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + def sandbox_run_query(plugin_id, query) |
| 184 | + response = connection.post('/sandbox/run_query', plugin_id: plugin_id, query: query) |
| 185 | + |
| 186 | + handle_errors_and_respond(response) do |body| |
| 187 | + body |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + def public_key |
| 192 | + response = connection.get('/api/v1/public_keys/latest.json') |
| 193 | + |
| 194 | + handle_errors_and_respond(response) do |body| |
| 195 | + Travis::API::V3::Models::InsightsPublicKey.new(body) |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | + def search_tags |
| 200 | + response = connection.get('/tags') |
| 201 | + |
| 202 | + handle_errors_and_respond(response) do |body| |
| 203 | + tags = body.map do |tag| |
| 204 | + Travis::API::V3::Models::InsightsTag.new(tag) |
| 205 | + end |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | + private |
| 210 | + |
| 211 | + def handle_errors_and_respond(response) |
| 212 | + case response.status |
| 213 | + when 200, 201 |
| 214 | + yield(response.body) if block_given? |
| 215 | + when 202 |
| 216 | + true |
| 217 | + when 204 |
| 218 | + true |
| 219 | + when 400 |
| 220 | + raise Travis::API::V3::ClientError, response.body.fetch('error', '') |
| 221 | + when 403 |
| 222 | + raise Travis::API::V3::InsufficientAccess, response.body['rejection_code'] |
| 223 | + when 404 |
| 224 | + raise Travis::API::V3::NotFound, response.body.fetch('error', '') |
| 225 | + when 422 |
| 226 | + raise Travis::API::V3::UnprocessableEntity, response.body.fetch('error', '') |
| 227 | + else |
| 228 | + raise Travis::API::V3::ServerError, 'Insights API failed' |
| 229 | + end |
| 230 | + end |
| 231 | + |
| 232 | + def connection(timeout: 20) |
| 233 | + @connection ||= Faraday.new(url: insights_url, ssl: { ca_path: '/usr/lib/ssl/certs' }) do |conn| |
| 234 | + conn.headers[:Authorization] = "Token token=\"#{insights_auth_token}\"" |
| 235 | + conn.headers['X-Travis-User-Id'] = @user_id.to_s |
| 236 | + conn.headers['Content-Type'] = 'application/json' |
| 237 | + conn.request :json |
| 238 | + conn.response :json |
| 239 | + conn.options[:open_timeout] = timeout |
| 240 | + conn.options[:timeout] = timeout |
| 241 | + conn.use OpenCensus::Trace::Integrations::FaradayMiddleware if Travis::Api::App::Middleware::OpenCensus.enabled? |
| 242 | + conn.adapter :net_http |
| 243 | + end |
| 244 | + end |
| 245 | + |
| 246 | + def insights_url |
| 247 | + Travis.config.new_insights.insights_url || raise(ConfigurationError, 'No Insights API URL configured!') |
| 248 | + end |
| 249 | + |
| 250 | + def insights_auth_token |
| 251 | + Travis.config.new_insights.insights_auth_token || raise(ConfigurationError, 'No Insights Auth Token configured!') |
| 252 | + end |
| 253 | + |
| 254 | + def query_string_from_params(params) |
| 255 | + params.delete_if { |_, v| v.nil? || v.empty? }.to_query |
| 256 | + end |
| 257 | + end |
| 258 | +end |
0 commit comments