raas-client-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94c1331c5b736d7cec268742acd323958041d1a1ee484a5064594e8329ac9898
4
+ data.tar.gz: aaef60ac317c3ede890f2558f22e1887ddc209390856d54231c180239acf1013
5
+ SHA512:
6
+ metadata.gz: 0b3b5c343c7b6be739cb2ebadeca642767370e0daa73820c9f6784ea23a21d94779b061674ec8a97c22ef2c1f582b44df85512718f4e621d840972a5f8d1b943
7
+ data.tar.gz: 5c2e4dd67fd2d05ceb8c82df9016ed6c4d60162db928027f4ac8adff044c5646c6d22ef3ecc8c62d590b3dce7bd00c53bdf80a50ad302e34854382a987c38460
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Yasuhiro Miyake
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Raas::Client::Rails
2
+
3
+ Adding extra endpoints to access RaaS. These endpoints allows you to fetch external token to access RaaS or enables you to invoke specific functions through them.
4
+
5
+ ## Usage
6
+
7
+ ### Set your Credentials
8
+
9
+ `landscape`, `application`, `token` should be set correctly.
10
+ You can manage these variables by adding external files such as **RaaS.yml** on your parents app. Of course, you can directly hard code these values on you app as follows.
11
+ These credentials are provided by SuTech. Please, ask [SuTech](https://www.sutech.co.jp/) adminisators for further inquiries.
12
+
13
+ ```ruby
14
+ Raas::Client::Rails::Engine.config.raas_client_rails.landscape = 'LANDSCAPE'
15
+ Raas::Client::Rails::Engine.config.raas_client_rails.application = 'APP'
16
+ Raas::Client::Rails::Engine.config.raas_client_rails.token = 'TOKEN VALUE'
17
+ ```
18
+
19
+ This condifuration is **mandatory** please, do not forget before you start interacting with APIs below.
20
+
21
+ ### APIs
22
+
23
+ Once you install your gem, your application has several extra endpoints to interact with RaaS.
24
+
25
+ - `/raas/report/session` : Generate external session by using your credentials for report
26
+ - `/raas/datatraveler/session` : Generate external session by using your credentials for report
27
+ - `/raas/health` : Ask RaaS to get current health condition
28
+
29
+ ## Installation
30
+
31
+ Add this line to your application's Gemfile:
32
+
33
+ ```ruby
34
+ gem "raas-client-rails"
35
+ ```
36
+
37
+ And then execute:
38
+ ```bash
39
+ $ bundle
40
+ ```
41
+
42
+ Or install it yourself as:
43
+ ```bash
44
+ $ gem install raas-client-rails
45
+ ```
46
+
47
+ ## License
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/raas/client/rails .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,8 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ class ApplicationController < ActionController::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,135 @@
1
+ module Raas::Client::Rails
2
+
3
+ require_relative 'rest_client'
4
+
5
+ class RaasController < ApplicationController
6
+
7
+ MSA_REPORT = "report";
8
+ MSA_DT = "datatraveler";
9
+
10
+ #/raas/health
11
+ def health()
12
+ prepare(params)
13
+ render plain: "Moving #{@tenant}/#{@subsystem}";
14
+ end
15
+
16
+ def prepare(params)
17
+ # seven arguments are needed to make this work
18
+ application = Raas::Client::Rails::Engine.config.raas_client_rails.landscape
19
+ landscape = Raas::Client::Rails::Engine.config.raas_client_rails.application
20
+ token = Raas::Client::Rails::Engine.config.raas_client_rails.token
21
+ @backUrl = params[:backUrl]
22
+ @subUrl = params[:subUrl]
23
+ @tenant = params[:tenant]
24
+ @subsystem = params[:sub]
25
+ @restClient = RestClient.new(application,landscape,token)
26
+ end
27
+
28
+ def report_session()
29
+ Rails.logger.warn("session parameter=#{params}");
30
+ prepare(params)
31
+ response = @restClient.createExternalSession(MSA_REPORT,@backUrl,@subUrl,@tenant,@subsystem)
32
+ render json: response.body
33
+ end
34
+
35
+ def datatraveler_session()
36
+ Rails.logger.warn("session parameter=#{params}")
37
+ prepare(params)
38
+ response = @restClient.createExternalSession(MSA_DT,@backUrl,@subUrl,@tenant,@subsystem)
39
+ render json: response.body
40
+ end
41
+
42
+ MIME_TYPE_HTML = "html"
43
+ MIME_TYPE_FILE = "file"
44
+ MIME_TYPE_JSON = "json"
45
+
46
+ #/raas/get
47
+ def get()
48
+ prepare(params)
49
+ path = params[:path]
50
+ parameters = params[:parameters]
51
+ mimeType = params[:mimeType]
52
+ fileName = params[:fileName]
53
+ type = params[:type]
54
+ #by default return HTML
55
+ if mimeType.nil?
56
+ mimeType = MIME_TYPE_HTML
57
+ end
58
+ #by default return download.pdf
59
+ if fileName.nil?
60
+ fileName = "download"
61
+ end
62
+ if type.nil?
63
+ type = "application/pdf"
64
+ end
65
+ #by default empty Hash if nothing is assigned
66
+ if parameters.nil?
67
+ payload = Hash.new
68
+ else
69
+ payload = parameters
70
+ end
71
+ Rails.logger.warn("get path=#{path} parameters=#{payload}");
72
+ response = @restClient.get(path,payload,@tenant,@subsystem)
73
+ handleMime(mimeType, fileName, type, response)
74
+ end
75
+
76
+ # How you return the value? This can be changed by passing mimeType through parameter
77
+ def handleMime(mimeType, fileName,type, response)
78
+ case mimeType
79
+ when MIME_TYPE_HTML
80
+ #sanitize response automatically
81
+ render html: response.body
82
+ when MIME_TYPE_JSON
83
+ render json: response.body
84
+ when MIME_TYPE_FILE
85
+ send_data(response, filename: fileName, type: type)
86
+ end
87
+ # p "#{fileName} #{type} #{mimeType}"
88
+ end
89
+
90
+ #/raas/delete
91
+ def delete()
92
+ prepare(params)
93
+ path = params[:path]
94
+ parameters = params[:parameters]
95
+ if parameters.nil?
96
+ payload = nil
97
+ else
98
+ payload = JSON.parse(parameters)
99
+ end
100
+ Rails.logger.warn("delete path=#{path} parameters=#{parameters}");
101
+ return @restClient.delete(path,payload,@tenant,@subsystem)
102
+ end
103
+
104
+ #/raas/post
105
+ def post()
106
+ prepare(params)
107
+ path = params[:path]
108
+ requestBody = params[:requestbody]
109
+ if requestBody.nil?
110
+ payload = nil
111
+ else
112
+ payload = JSON.parse(requestBody)
113
+ end
114
+ Rails.logger.warn("post path=#{path} requestBody=#{requestBody}");
115
+ response = @restClient.post(path,payload,@tenant,@subsystem)
116
+ return response.body;
117
+ end
118
+
119
+ #/raas/put
120
+ def put()
121
+ prepare(params)
122
+ path = params[:path]
123
+ requestBody = params[:requestbody]
124
+ if requestBody.nil?
125
+ payload = nil
126
+ else
127
+ payload = JSON.parse(requestBody)
128
+ end
129
+ Rails.logger.warn("post path=#{path} requestBody=#{requestBody}");
130
+ response = @restClient.put(path,payload,@tenant,@subsystem)
131
+ return response.body;
132
+ end
133
+
134
+ end
135
+ end
@@ -0,0 +1,160 @@
1
+ module Raas::Client::Rails
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'securerandom'
6
+
7
+ # Rest Client for RaaS
8
+ class RestClient
9
+ GET = "get"
10
+ POST = "post"
11
+ PUT = "put"
12
+ DELETE = "delete"
13
+ APEX_DOMAIN = "functions.asaservice.inc";
14
+
15
+ def initialize(application,landscape,token)
16
+ @application = application
17
+ @landscape = landscape
18
+ @token = token
19
+ end
20
+
21
+ # ResponseEntity<String> resp = raasClient.createExternalSession(Constant.MSA_REPORT,req.getBackUrl(),req.getSubUrl(), saasContextProvider.getTenant(), saasContextProvider.getSub());
22
+
23
+
24
+ def createExternalSession(msa,backUrl,subUrl,tenant,subsystem)
25
+ body = Hash.new
26
+ body["backUrl"] = backUrl
27
+ body["subUrl"] = subUrl
28
+ body["tenant"] = tenant
29
+ body["sub"] = subsystem
30
+ return proxy(body,POST,"/#{msa}/external/session",nil,@token)
31
+ end
32
+
33
+ def getUserToken()
34
+ #No trace level... so that adding [TRACE] for filtering
35
+ Rails.logger.debug("[TRACE] getUserToken is started");
36
+ #Use Rails default cache system, this simply use cache otherwise fetch and store
37
+ Rails.logger.debug("Attempt to recycle token");
38
+ cacheName = "#{@tenant}+++#{@sub}"
39
+ @@t = @tenant
40
+ @@s = @sub
41
+ Rails.logger.debug("Cache name is #{cacheName}")
42
+ # p cacheName
43
+ return token = Rails.cache.fetch(cacheName) do
44
+ Rails.logger.debug("No cache found, fetch it by api");
45
+ Rails.logger.debug("tenant #=> #{@@t} / sub #=> #{@@s}")
46
+ response = getUserTokenImpl( "report" , @@t , @@s);
47
+ Rails.logger.debug(response.body)
48
+ return JSON.parse(response.body);
49
+ end
50
+ end
51
+
52
+ # fetch token from RaaS because no cache
53
+ def getUserTokenImpl(msa,tenant,sub)
54
+ body = Hash.new
55
+ body["tenant"] = tenant
56
+ body["sub"] = sub
57
+ response = proxy(body,POST,"/#{msa}/external/token",nil,@token);
58
+ end
59
+
60
+ def get(requestUrl, params,tenant,sub)
61
+ @tenant = tenant
62
+ @sub = sub
63
+ json = getUserToken()
64
+ return proxy(nil, GET,requestUrl,params,json["token"]);
65
+ end
66
+
67
+ def delete(requestUrl, params,tenant,sub)
68
+ @tenant = tenant
69
+ @sub = sub
70
+ json = getUserToken()
71
+ return proxy(nil, DELETE,requestUrl,params,json["token"]);
72
+ end
73
+
74
+ def put(requestUrl,body,tenant,sub)
75
+ @tenant = tenant
76
+ @sub = sub
77
+ json = getUserToken()
78
+ return proxy(body , PUT,requestUrl,nil,json["token"]);
79
+ end
80
+
81
+ def post(requestUrl,body,tenant,sub)
82
+ @tenant = tenant
83
+ @sub = sub
84
+ json = getUserToken()
85
+ return proxy(body , POST,requestUrl,nil,json["token"]);
86
+ end
87
+
88
+ def proxy(body,method,requestUrl,params,token)
89
+ Rails.logger.warn("method:#{method} url:#{requestUrl} token:#{token} params:#{params}" );
90
+ #TraceID seems to be different... is it OK!?
91
+ traceId = SecureRandom.uuid;
92
+ Rails.logger.debug("[TRACE] traceId=#{traceId}")
93
+ # p "token: #{token}"
94
+ domain = decideDomain();
95
+ uri = "https://#{domain}#{requestUrl}"
96
+ url = URI.parse(uri)
97
+ unless params.nil?
98
+ #should I do like this!?
99
+ parameters_hash = params.to_unsafe_h
100
+ url.query = URI.encode_www_form(parameters_hash) unless params.nil?
101
+ end
102
+ # replacing context path form urI to match actual gateway URI
103
+ headers = {
104
+ 'TRACE' => traceId,
105
+ 'Content-Type' => 'application/json',
106
+ 'Accept' => 'application/json',
107
+ 'Authorization' => "Bearer #{token}"
108
+ }
109
+ #restTemplate.exchange alternative
110
+ case method
111
+ when GET
112
+ httpEntity = Net::HTTP::Get.new(url.to_s,headers)
113
+ when POST
114
+ httpEntity = Net::HTTP::Post.new(url.to_s,headers)
115
+ when DELETE
116
+ httpEntity = Net::HTTP::Delete.new(url.to_s,headers)
117
+ when PUT
118
+ httpEntity = Net::HTTP::Put.new(url.to_s,headers)
119
+ else
120
+ puts "method is not be defined..."
121
+ end
122
+ httpEntity['Accept-Encoding'] = '' #delete acceptEncoding here
123
+ httpEntity.body = body.to_json if body
124
+ retry_count = 0
125
+ max_retries = 4
126
+ host = URI(url.to_s).host
127
+ port = URI(url.to_s).port
128
+ begin
129
+ Rails.logger.debug(url.to_s);
130
+ Rails.logger.debug(body);
131
+ Rails.logger.debug(httpEntity);
132
+ http = Net::HTTP.new(host,port)
133
+ http.use_ssl = true
134
+ # p url.to_s
135
+ response = http.request(httpEntity)
136
+ Rails.logger.debug(response);
137
+ return response;
138
+ rescue => e
139
+ retry_count += 1
140
+ if retry_count <= max_retries
141
+ Rails.logger.debug("Retry attempt #{retry_count}")
142
+ sleep(4) #wait 4 seconds
143
+ retry
144
+ else
145
+ Rails.logger.error("retry method for the following url #{uri} has failed #{e}");
146
+ Rails.logger.error(e.backtrace);
147
+ raise e
148
+ end
149
+ end
150
+ end
151
+
152
+ def decideDomain()
153
+ if "prod" == @landscape then
154
+ return "#{@application}.#{APEX_DOMAIN}"
155
+ else
156
+ return "#{@application}.#{@landscape}.#{APEX_DOMAIN}"
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,8 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ module ApplicationHelper
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Raas::Client::Rails
2
+ module RaaSHelper
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ class ApplicationJob < ActiveJob::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ class ApplicationMailer < ActionMailer::Base
5
+ default from: "from@example.com"
6
+ layout "mailer"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Raas client rails</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "raas/client/rails/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ Raas::Client::Rails::Engine.routes.draw do
2
+ get '/raas/health', to: 'raas#health'
3
+ post '/raas/report/session', to: 'raas#report_session'
4
+ post '/raas/datatraveler/session', to: 'raas#datatraveler_session'
5
+ get '/raas', to: 'raas#get'
6
+ delete '/raas', to: 'raas#delete'
7
+ post '/raas', to: 'raas#post'
8
+ put '/raas', to: 'raas#put'
9
+ end
@@ -0,0 +1,16 @@
1
+ puts "Loading Raas::Client::Rails::Engine..."
2
+ module Raas
3
+ module Client
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Raas::Client::Rails
7
+ config.raas_client_rails = Configuration.new
8
+ config.autoload_paths << File.expand_path('lib', __dir__)
9
+ end
10
+
11
+ class Configuration
12
+ attr_accessor :landscape, :application, :token
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Raas
2
+ module Client
3
+ module Rails
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require "raas/client/rails/version"
2
+ require "raas/client/rails/engine"
3
+ module Raas
4
+ module Client
5
+ module Rails
6
+ # Your code goes here...
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :raas_client_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raas-client-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sunao Suzuki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.7
27
+ description: Enable your app to interact with RaaS
28
+ email:
29
+ - sunao@sutech.co.jp
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/raas_client_rails_manifest.js
38
+ - app/assets/stylesheets/raas/client/rails/application.css
39
+ - app/controllers/raas/client/rails/application_controller.rb
40
+ - app/controllers/raas/client/rails/raas_controller.rb
41
+ - app/controllers/raas/client/rails/rest_client.rb
42
+ - app/helpers/raas/client/rails/application_helper.rb
43
+ - app/helpers/raas/client/rails/raa_s_helper.rb
44
+ - app/jobs/raas/client/rails/application_job.rb
45
+ - app/mailers/raas/client/rails/application_mailer.rb
46
+ - app/models/raas/client/rails/application_record.rb
47
+ - app/views/layouts/raas/client/rails/application.html.erb
48
+ - config/routes.rb
49
+ - lib/raas/client/rails.rb
50
+ - lib/raas/client/rails/engine.rb
51
+ - lib/raas/client/rails/version.rb
52
+ - lib/tasks/raas/client/rails_tasks.rake
53
+ homepage: https://www.sutech.co.jp/
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://www.sutech.co.jp/
58
+ source_code_uri: https://github.com/SuTechDevelop/raas-client-for-rails
59
+ changelog_uri: https://github.com/SuTechDevelop/raas-client-for-rails/activity
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.2.15
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Summary
79
+ test_files: []