clowder-common-ruby 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 327fd0920abf084c7289527fa3d9e98eeb8ccb15bc11251e86d85468c234f804
4
- data.tar.gz: 454f639a092d6c835d1579f72b33e4dac82ecfa2ae4878b4949be508afa0af3f
3
+ metadata.gz: b5a31730c5274d307ff7d9ef22f75d2e70a6c5ca44fb66d26f8b7eee4affdd7f
4
+ data.tar.gz: a70f87a031e547234e2f4d4049702147624127b6ff8e4824e320b1dea69dd7b2
5
5
  SHA512:
6
- metadata.gz: 9acccd0a8be9c9bb6b74dcb3ae04c66adf53ddf0186a2d2fa45c82774b272615d05672e952c9d2e82bad9a2a066d7bd1786df85399a961ef40b24bddffe737aa
7
- data.tar.gz: bc0f1cbf3c2c15853b708340a273ee649220c625d5947fbff06a2603412e25f335e6c8ea42edf280e35ee1894d0c72f28ec253bb0e39793dd80776e64072a4f6
6
+ metadata.gz: a50970cfbc1d9dc96547f358c598043db05a71be2420e1a4094f7b437b0af49666a7480aac53566df579230859158b567ba6e7a98b007bb5aa8ae28e2d86a5ae
7
+ data.tar.gz: 8583447351853c20a44d0d8eacc0a31e6f93c1a2db24f83caa2b24f0caf6da68ced490aedce016aa0256d544f29351e890933f66ff811d92a9bbca3b643c4319
data/README.md CHANGED
@@ -47,6 +47,10 @@ The ``clowder`` library also comes with several other helpers
47
47
 
48
48
  See [test.json](test.json) for all available values
49
49
 
50
+ ### Usage in Rails
51
+
52
+ In Rails applications, requiring the ```clowder-common-ruby/engine``` will run the initializer, the output of which will be accessible under ```Settings```.
53
+
50
54
  ### Kafka Topics
51
55
 
52
56
  Topics are structured as a hash `<requested_name> => <name>`
@@ -0,0 +1,7 @@
1
+ require 'rails/engine'
2
+
3
+ module ClowderCommonRuby
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace(ClowderCommonRuby)
6
+ end
7
+ end
@@ -0,0 +1,176 @@
1
+ require 'clowder-common-ruby'
2
+
3
+ module ClowderCommonRuby
4
+ class RailsConfig
5
+ class << self
6
+ # Rails configuration hash
7
+ def to_h
8
+ config = ClowderCommonRuby::Config.load
9
+
10
+ {
11
+ kafka: configure_kafka(config),
12
+ logging: configure_cloudwatch(config),
13
+ redis: configure_redis(config),
14
+ endpoints: configure_endpoints(config, :dependency_endpoints),
15
+ private_endpoints: configure_endpoints(config, :private_dependency_endpoints),
16
+ database: configure_database(config),
17
+ prometheus_exporter_port: config&.metricsPort,
18
+ tls_ca_path: config.tlsCAPath,
19
+ web_port: config.webPort
20
+ }
21
+ end
22
+
23
+ # Short path for db config
24
+ def db_config
25
+ config = ClowderCommonRuby::Config.load
26
+
27
+ configure_database(config)
28
+ end
29
+
30
+ private
31
+
32
+ # Kafka configuration hash
33
+ def configure_kafka(config)
34
+ # In case there are no kafka brokers, an empty string is ensured in place of ':'
35
+ build_kafka_security(config).merge(
36
+ brokers: build_kafka_brokers(config) || '',
37
+ topics: build_kafka_topics(config)
38
+ )
39
+ end
40
+
41
+ # Kafka security settings hash
42
+ def build_kafka_security(config)
43
+ server_config = config.kafka.brokers[0]
44
+ authtype = server_config&.dig('authtype')
45
+ cacert = server_config&.dig('cacert')
46
+
47
+ return { security_protocol: 'plaintext' } unless authtype
48
+
49
+ unless authtype == 'sasl'
50
+ raise "Unsupported Kafka security protocol '#{authtype}'"
51
+ end
52
+
53
+ {
54
+ sasl_username: server_config&.dig('sasl', 'username'),
55
+ sasl_password: server_config&.dig('sasl', 'password'),
56
+ sasl_mechanism: server_config&.dig('sasl', 'saslMechanism'),
57
+ security_protocol: server_config&.dig('sasl', 'securityProtocol'),
58
+ ssl_ca_location: build_kafka_certificate(cacert)
59
+ }.compact
60
+ # In case build_kafka_certificate returns nil, ssl_ca_location is removed from the hash with compact
61
+ end
62
+
63
+ # Gives location of ssl certificate and makes sure it exists
64
+ def build_kafka_certificate(cacert)
65
+ return unless cacert.present?
66
+
67
+ kafka_config[:ssl_ca_location] = Rails.root.join('tmp', 'kafka_ca.crt')
68
+
69
+ write_temporary_file(kafka_config[:ssl_ca_location], cacert)
70
+
71
+ kafka_config[:ssl_ca_location]
72
+ end
73
+
74
+ # Kafka brokers list
75
+ def build_kafka_brokers(config)
76
+ config.kafka.brokers&.map do |broker|
77
+ "#{broker.hostname}:#{broker.port}"
78
+ end&.join(',')
79
+ end
80
+
81
+ # Kafka topics hash
82
+ def build_kafka_topics(config)
83
+ config.kafka.topics&.each_with_object({}) do |topic, obj|
84
+ k = "#{topic.name.sub(/^platform./, '')}".parameterize.underscore.to_sym
85
+ obj[k] = topic.name
86
+ end
87
+ end
88
+
89
+ # Redis configuration hash
90
+ def configure_redis(config)
91
+ inMemoryDb = config&.dig('inMemoryDb')
92
+ redis_url = "redis://#{inMemoryDb.dig('hostname')}:#{inMemoryDb.dig('port')}"
93
+
94
+ {
95
+ url: redis_url,
96
+ password: inMemoryDb.dig('password')
97
+ }
98
+ end
99
+
100
+ # Cloudwatch configuration hash
101
+ def configure_cloudwatch(config)
102
+ cloudwatch = config.logging&.cloudwatch
103
+
104
+ {
105
+ region: cloudwatch&.region,
106
+ log_group: cloudwatch&.logGroup,
107
+ log_stream: Socket.gethostname,
108
+ type: config.logging&.type,
109
+ credentials: {
110
+ access_key_id: cloudwatch&.accessKeyId,
111
+ secret_access_key: cloudwatch&.secretAccessKey
112
+ }
113
+ }
114
+ end
115
+
116
+ # All endpoints' configuration hash
117
+ def configure_endpoints(config, field)
118
+ config.try(field)&.each_with_object({}) do |(name, endpoint), obj|
119
+ service = endpoint.dig('service')
120
+
121
+ next unless service
122
+
123
+ key = name.underscore.to_sym
124
+ obj[key] = build_endpoint(config, service)
125
+ end
126
+ end
127
+
128
+ # Endpoint configuration hash
129
+ def build_endpoint(config, endpoint)
130
+ scheme = config.tlsCAPath ? 'https' : 'http'
131
+ port = config.tlsCAPath ? endpoint.tlsPort : endpoint.port
132
+ host = "#{endpoint.hostname}:#{port}"
133
+
134
+ {
135
+ scheme: scheme,
136
+ host: host,
137
+ url: "#{scheme}://#{host}"
138
+ }
139
+ end
140
+
141
+ # Database configuration hash
142
+ def configure_database(config)
143
+ {
144
+ database: config.database.name,
145
+ username: config.database.username,
146
+ password: config.database.password,
147
+ host: config.database.hostname,
148
+ port: config.database.port,
149
+ admin_username: config.database.adminUsername,
150
+ admin_password: config.database.adminPassword,
151
+ rds_ca: config.database.rdsCa,
152
+ ssl_mode: config.database.sslMode,
153
+ ssl_root_cert: build_database_certificate(config.database.rdsCa)
154
+ }
155
+ end
156
+
157
+ # Gives location of database ssl certificate and makes sure it exists
158
+ def build_database_certificate(rdsca)
159
+ return unless rdsca.present?
160
+
161
+ db_config = Rails.root.join('tmp', 'rdsCa')
162
+
163
+ write_temporary_file(db_config, rdsca)
164
+
165
+ db_config
166
+ end
167
+
168
+ # Creates and writes file if it doesn't exist
169
+ def write_temporary_file(path, content)
170
+ File.open(path, 'w') do |f|
171
+ f.write(content)
172
+ end unless File.exist?(path)
173
+ end
174
+ end
175
+ end
176
+ end
@@ -1,3 +1,3 @@
1
1
  module ClowderCommonRuby
2
- VERSION = '0.2.8'.freeze # Patch version is being automatically bumped
2
+ VERSION = '0.3.0'.freeze # Patch version is being automatically bumped
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clowder-common-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Red Hat Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-22 00:00:00.000000000 Z
11
+ date: 2023-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This is a ruby interface for preparing Clowder variables.
14
14
  email:
@@ -25,6 +25,8 @@ files:
25
25
  - bin/schema.json
26
26
  - lib/clowder-common-ruby.rb
27
27
  - lib/clowder-common-ruby/config.rb
28
+ - lib/clowder-common-ruby/engine.rb
29
+ - lib/clowder-common-ruby/rails_config.rb
28
30
  - lib/clowder-common-ruby/test.rb
29
31
  - lib/clowder-common-ruby/types.rb
30
32
  - lib/clowder-common-ruby/version.rb