clerk-rails 0.1.8 → 0.1.9

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: 5ebd7434ec6b1e5d225ac7de149693f061e1204ca473ad4e17a5bfea5868a489
4
- data.tar.gz: 5c75c18dad6e96ae75ef990eea3a1f37673153d91bc7c47b6885cc6d2a0ef885
3
+ metadata.gz: e595682c8db22e0199602650c5172b9e038576a93a00a86263a68b2bc3663d30
4
+ data.tar.gz: e61a165746679c0e148cd267e8a30acfc3f2de59c86e17829a0101e44eaf4874
5
5
  SHA512:
6
- metadata.gz: 9b10930865c7a58a193845a2e5108915801142fb0172ea877e575481f9d44cfa8fd440bc30f7d15b8209166d06f1db6286f0721818a3f5ef39fb13b2904d1840
7
- data.tar.gz: ec883cfa162d37fbf9aace0db5a43d011dd21ddc2dfd739a23b4e829054e1cb5bf54aeda39e865ba37ba84cc285c01635557a729a7282eb1cb507155f37438a7
6
+ metadata.gz: 2eeadea3edb895b7cc7e5ef12c4d1b82f605ee35429f860a968a73598d214b40349b971044bde01f7b38b44059147986cc2523987d4e97470bbe1c0b6f22a5e4
7
+ data.tar.gz: 5be6344c1ae185beeca74194a4d207fe35a58cec638fbfeab07cec18efd2660c08d01c0b81c3b84636864169fc545458b380e53ddb442d359d65d6e6436e7678
@@ -4,7 +4,7 @@ module Clerk
4
4
  establish_connection Clerk.database_connection_url
5
5
  def self.clerk_table_name(table_name)
6
6
  version = Clerk::VERSION.split(".").map.with_index{|x, i| (i==2 ? "00" : x.rjust(2, '0')) }.join
7
- "client.#{table_name}_#{version}_#{Clerk.key_secret}"
7
+ "instance.#{table_name}_#{version}_#{Clerk.key_secret}"
8
8
  end
9
9
 
10
10
  def self.clerk_table_name_nc(table_name)
@@ -37,5 +37,37 @@
37
37
  else
38
38
  _original_create_record(*args, &block)
39
39
  end
40
- end
40
+ end
41
+
42
+ alias_method :_original_update_row, :_update_row
43
+
44
+ def _clerk_update_row(attribute_names, attempted_action = "update")
45
+ response = self.class.clerk_persistence_api.patch(
46
+ "#{self.class.clerk_persistence_path}/#{id_in_database}",
47
+ attributes_with_values(attribute_names).except("updated_at")
48
+ )
49
+
50
+ if response.status == 200
51
+ 1 # Affected rows
52
+ elsif response.status == 422
53
+ response.data.each do |k, vs|
54
+ vs.each do |v|
55
+ self.errors.add(k, v)
56
+ end
57
+ end
58
+ raise ActiveRecord::RecordInvalid.new(self)
59
+ else
60
+ Rails.logger.fatal "Server Failed: #{response.data[:message]}"
61
+ raise ActiveRecord::RecordNotSaved.new("Failed to save the server record", self)
62
+ end
63
+ end
64
+
65
+ def _update_row(*args, &block)
66
+ if defined?(self.class.clerk_persistence_path)
67
+ _clerk_update_row(*args, &block)
68
+ else
69
+ _original_update_row(*args, &block)
70
+ end
71
+ end
72
+
41
73
  end
data/lib/clerk-rails.rb CHANGED
@@ -51,6 +51,6 @@ module Clerk
51
51
  end
52
52
 
53
53
  class Configuration
54
- attr_accessor :app_host, :accounts_host, :database_url, :tunnel_key, :environment, :session_mode, :last_account, :cookie_host
54
+ attr_accessor :app_host, :accounts_host, :database_url, :ngrok_authtoken, :environment, :session_mode, :last_account, :cookie_host, :tunnel_cert, :tunnel_key
55
55
  end
56
56
  end
data/lib/clerk/engine.rb CHANGED
@@ -13,13 +13,15 @@ module Clerk
13
13
  begin
14
14
  config_data = Clerk.api.get('/api/environment').data
15
15
  Clerk.configure do |config|
16
- config.app_host = config_data[:APP_HOST]
17
- config.accounts_host = config_data[:ACCOUNTS_HOST]
18
- config.database_url = config_data[:DATABASE_URL]
19
- config.tunnel_key = config_data[:TUNNEL_KEY]
20
- end
16
+ config.app_host = config_data[:APP_HOST]
17
+ config.accounts_host = config_data[:ACCOUNTS_HOST]
18
+ config.database_url = config_data[:DATABASE_URL]
19
+ config.ngrok_authtoken = config_data[:NGROK_AUTHTOKEN]
20
+ config.tunnel_cert = config_data[:TUNNEL_CERT]
21
+ config.tunnel_key = config_data[:TUNNEL_KEY]
22
+ end
21
23
  rescue => e
22
- raise "Clerk could not be loaded. #{config_data}"
24
+ raise "Clerk could not be loaded. #{e} #{config_data}"
23
25
  end
24
26
 
25
27
  if defined?(Rails::Server) and Rails.env.development?
data/lib/clerk/tunnel.rb CHANGED
@@ -15,6 +15,7 @@ module Clerk
15
15
  def self.ngrok_path
16
16
  data_dir.join("clerk_#{executable_type}")
17
17
  end
18
+
18
19
  def self.ngrok_zip_path
19
20
  data_dir.join("clerk_#{executable_type}.zip")
20
21
  end
@@ -23,10 +24,6 @@ module Clerk
23
24
  data_dir.join("account.key")
24
25
  end
25
26
 
26
- def self.ready?
27
- crt_ready? and ngrok_ready?
28
- end
29
-
30
27
  def self.crt_ready?
31
28
  File.exist? key_path and File.exist? crt_path
32
29
  end
@@ -35,12 +32,6 @@ module Clerk
35
32
  File.exist? ngrok_path
36
33
  end
37
34
 
38
- def self.setup!
39
- setup_crt! unless crt_ready?
40
- setup_ngrok! unless ngrok_ready?
41
- puts "=> [Clerk] Setup done."
42
- end
43
-
44
35
  def self.setup_ngrok!
45
36
  ngrok_paths = {
46
37
  darwin_amd64: "/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip",
@@ -76,76 +67,20 @@ module Clerk
76
67
  File.delete(ngrok_zip_path)
77
68
  end
78
69
 
79
- def self.setup_crt!
80
- puts "=> [Clerk] First boot detected. Initializing environment."
81
- require 'acme-client'
82
- require 'openssl'
83
-
84
- Dir.mkdir(data_dir) unless Dir.exist? data_dir
85
-
86
- puts "=> [Clerk] Authenticating with Let's Encrypt."
87
- if File.exist? acc_path
88
- acc_private_key = OpenSSL::PKey::RSA.new(File.read(acc_path))
89
- client = ::Acme::Client.new(private_key: acc_private_key, directory: 'https://acme-v02.api.letsencrypt.org/directory')
90
- client.kid #Load existing account
91
- else
92
- acc_private_key = OpenSSL::PKey::RSA.new(4096)
93
-
94
- client = ::Acme::Client.new(private_key: acc_private_key, directory: 'https://acme-v02.api.letsencrypt.org/directory')
95
-
96
- account = client.new_account(contact: 'mailto:sidot3291@gmail.com', terms_of_service_agreed: true)
97
-
98
- File.write(acc_path, acc_private_key.to_pem)
99
- end
100
-
101
- puts "=> [Clerk] Beginning new certificate order for #{Clerk.config.app_host}."
102
- order = client.new_order(identifiers: [Clerk.config.app_host])
103
- challenge = order.authorizations.first.dns
104
-
105
- puts "=> [Clerk] Completing DNS challenge."
106
- record_name = "#{challenge.record_name}.#{Clerk.config.app_host.gsub(".clerkdev.com", "")}"
107
- config_data = Clerk.api.post('/api/acme_challenge', {
108
- name: record_name,
109
- type: challenge.record_type,
110
- content: challenge.record_content
111
- })
112
-
113
- puts "=> [Clerk] Waiting for DNS to process. This may take a few minutes."
114
- confirm_url = URI.parse("https://dns.google.com/resolve?name=#{challenge.record_name}.#{Clerk.config.app_host}&type=TXT")
115
- while true
116
- confirm_check = JSON.parse(Net::HTTP.get(confirm_url))
117
- break if confirm_check.dig("Answer", 0, "data")=="\"#{challenge.record_content}\""
118
- sleep(5)
119
- puts "=> [Clerk] Checking processing status..."
120
- end
121
-
122
- sleep(5) # Sometimes google returns correctly before. Add an extra 10 seconds.
123
- puts "=> [Clerk] Checking processing status..."
124
- sleep(5)
125
-
126
- puts "=> [Clerk] Validating DNS challenge."
127
- challenge.request_validation
70
+ def self.save_tunnel_cert_locally!
71
+ File.write(crt_path, Clerk.config.tunnel_cert)
72
+ File.write(key_path, Clerk.config.tunnel_key)
73
+ end
128
74
 
129
- while challenge.status == 'pending'
130
- sleep(2)
131
- puts "=> [Clerk] Checking validation status..."
132
- challenge.reload
75
+ def self.start!
76
+ if Clerk.config.tunnel_cert == nil || Clerk.config.tunnel_key == nil
77
+ raise "=> Sorry, your dev certificate isn't ready yet. It may take up to 30 minutes to create."
133
78
  end
134
79
 
135
- puts "=> [Clerk] Validation complete. Requesting certificate."
136
- crt_private_key = OpenSSL::PKey::RSA.new(4096)
137
- File.write(key_path, crt_private_key.to_pem)
138
-
139
- csr = Acme::Client::CertificateRequest.new(private_key: crt_private_key, subject: { common_name: Clerk.config.app_host })
140
- order.finalize(csr: csr)
141
- sleep(1) while order.status == 'processing'
80
+ save_tunnel_cert_locally!
142
81
 
143
- File.write(crt_path, order.certificate)
144
- puts "=> [Clerk] Certificate created."
145
- end
146
-
147
- def self.start!
148
- setup! unless ready?
82
+ setup_ngrok! unless ngrok_ready?
83
+ puts "=> [Clerk] Setup done."
149
84
 
150
85
  self.patch_rack_requests
151
86
 
@@ -167,7 +102,7 @@ module Clerk
167
102
  puts "=> Booting https://#{Clerk.config.app_host} with Clerk"
168
103
  options = {
169
104
  addr: server_options[:Port],
170
- authtoken: Clerk.config.tunnel_key,
105
+ authtoken: Clerk.config.ngrok_authtoken,
171
106
  hostname: Clerk.config.app_host,
172
107
  region: "us",
173
108
  crt: Rails.root.join(".clerk/dev.crt"),
data/lib/clerk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clerk
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clerk-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clerk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-26 00:00:00.000000000 Z
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails