forty 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2dc92f794265aad44394529187b019718c6ea1df
4
- data.tar.gz: 3deb3ea465ad648c24b890dfdfa3e7b537049d1c
3
+ metadata.gz: 3100804368cd3b2645003487a70b7f7b20933482
4
+ data.tar.gz: ebbc10ef2bc8da5cc56dbc1a10c924765bd39f87
5
5
  SHA512:
6
- metadata.gz: 8e74a9e2934e0fa3cd229f36a29e3dff7c64947fc91eec48085e5b353a27e855133a01116a82d9a2b1d1336bc3e2134791ea29e103e69c927944ab489b22efdb
7
- data.tar.gz: 4a1d220596760596de4d9df3a759ba99291d77c9e2132d23a6b02bf3e4fa856c6e013e0d9ab92c1fd760993d13cbc705e6a16659c0a16f0bfc38e4e955af4e20
6
+ metadata.gz: 20f03889ad1ea334ec713ecea2f7b24647f351f36ff6012d5877a296f08447bfeaf69f933d799d8b6316ab2b7f639ae0e349acc61a6cff2940f7e75c38443e86
7
+ data.tar.gz: f8a78479552e2e2b75458d1519a0a552d9f28ee509d77f6935c5a6b172d6eb646ee05ccac655edef68fc45d4a246b79b569bb42d78dba1def1b54a5f59c8e868
@@ -1,4 +1,6 @@
1
1
  require 'logger'
2
+ require 'mail'
3
+ require 'erb'
2
4
 
3
5
  module Forty
4
6
  class Configuration
@@ -18,6 +20,7 @@ module Forty
18
20
  end
19
21
 
20
22
  class Database
23
+ attr_accessor :name
21
24
  attr_accessor :host
22
25
  attr_accessor :port
23
26
  attr_accessor :user
@@ -25,9 +28,55 @@ module Forty
25
28
  attr_accessor :database
26
29
  end
27
30
 
31
+ class Mailer
32
+ attr_accessor :smtp_address
33
+ attr_accessor :smtp_host
34
+ attr_accessor :smtp_port
35
+ attr_accessor :smtp_username
36
+ attr_accessor :smtp_password
37
+ attr_accessor :smtp_authentication
38
+ attr_accessor :smtp_encryption
39
+ attr_accessor :enabled
40
+ attr_accessor :templates
41
+
42
+ def send_welcome(recipient, username, password)
43
+ mail = ::Mail.new
44
+ mail.delivery_method :smtp, {
45
+ smtp_envelope_from: @smtp_address,
46
+ address: @smtp_host,
47
+ port: @smtp_port.to_i,
48
+ user_name: @smtp_username,
49
+ password: @smtp_password,
50
+ authentication: @smtp_authentication,
51
+ encryption: @smtp_encryption,
52
+ }
53
+ mail.from @smtp_address
54
+ mail.to recipient
55
+ mail.subject "#{Forty.database_configuration.name.to_s.length == 0 ? '' : Forty.database_configuration.name + ' '}DB Credentials (User: #{username})"
56
+
57
+ parameters = binding
58
+ parameters.local_variable_set(:database_name, Forty.database_configuration.name)
59
+ parameters.local_variable_set(:username, username)
60
+ parameters.local_variable_set(:password, password)
61
+ parameters.local_variable_set(:host, Forty.database_configuration.host)
62
+ parameters.local_variable_set(:port, Forty.database_configuration.port)
63
+ parameters.local_variable_set(:database, Forty.database_configuration.database)
64
+
65
+ if @enabled
66
+ mail.body ERB.new(File.read(@templates[:user_created])).result(parameters)
67
+ Forty.configuration.logger.info('Sending \'user_created\' email to ' + recipient)
68
+ mail.deliver
69
+ Forty.configuration.logger.info('Sent \'user_created\' email successfully')
70
+ else
71
+ Forty.configuration.logger.warn('Mail not enabled, skipped sending welcome email. You will need to regenerate a password for user ' + username + '.')
72
+ end
73
+ end
74
+ end
75
+
28
76
  class << self
29
77
  attr_writer :configuration
30
78
  attr_writer :database_configuration
79
+ attr_writer :mailer_configuration
31
80
  end
32
81
 
33
82
  def self.configuration
@@ -45,4 +94,12 @@ module Forty
45
94
  def self.database
46
95
  yield(database_configuration)
47
96
  end
97
+
98
+ def self.mailer_configuration
99
+ @mailer ||= Forty::Mailer.new
100
+ end
101
+
102
+ def self.mailer
103
+ yield(mailer_configuration)
104
+ end
48
105
  end
data/lib/forty/sync.rb CHANGED
@@ -1,5 +1,3 @@
1
- # require_relative 'configuration'
2
-
3
1
  module Forty
4
2
 
5
3
  def self.sync(dry_run=true)
@@ -9,6 +7,7 @@ module Forty
9
7
  Forty.configuration.schemas,
10
8
  Forty::ACL.new(Forty.configuration.acl_file),
11
9
  Forty.instance_variable_get(:@database),
10
+ Forty.instance_variable_get(:@mailer),
12
11
  dry_run
13
12
  ).run
14
13
  end
@@ -16,7 +15,7 @@ module Forty
16
15
  class Sync
17
16
  class Error < StandardError; end
18
17
 
19
- def initialize(logger, master_username, production_schemas, acl_config, executor, dry_run=true)
18
+ def initialize(logger, master_username, production_schemas, acl_config, executor, mailer, dry_run=true)
20
19
  @logger = logger or raise Error, 'No logger provided'
21
20
  @master_username = master_username or raise Error, 'No master username provided'
22
21
  @production_schemas = production_schemas or raise Error, 'No production schemas provided'
@@ -26,7 +25,8 @@ module Forty
26
25
  @acl_config['users'] ||= {}
27
26
  @acl_config['groups'] ||= {}
28
27
 
29
- @executor = executor or raise Error, 'No dwh executor provided'
28
+ @executor = executor or raise Error, 'No database executor provided'
29
+ @mailer = mailer or raise Error, 'No mailer provided'
30
30
  @dry_run = dry_run
31
31
 
32
32
  @logger.warn('Dry mode disabled, executing on production') unless @dry_run
@@ -49,7 +49,7 @@ Starting sync...
49
49
  / /_/ __ \\/ ___/ __/ / / /
50
50
  / __/ /_/ / / / /_/ /_/ /
51
51
  /_/ \\____/_/ \\__/\\__, / Database ACL Sync
52
- /____/ v0.2.1
52
+ /____/ v0.3.0
53
53
 
54
54
  ===============================================================================
55
55
 
@@ -80,8 +80,9 @@ BANNER
80
80
  roles = @acl_config['users'][user]['roles'] || []
81
81
  password = @acl_config['users'][user]['password']
82
82
  search_path = @production_schemas.join(',')
83
+ owner = @acl_config['users'][user]['email']
83
84
 
84
- _create_user(user, password, roles, search_path)
85
+ _create_user(user, password, roles, search_path, owner)
85
86
  end
86
87
 
87
88
  @logger.info('All users are in sync') if (undefined_users.count + missing_users.count) == 0
@@ -308,9 +309,19 @@ BANNER
308
309
  _execute_statement("drop group #{group};")
309
310
  end
310
311
 
311
- def _create_user(user, password, roles=[], search_path=nil)
312
+ def _create_user(user, password, roles=[], search_path=nil, owner='')
313
+ if password.to_s.length == 0
314
+ password = _generate_password()
315
+ end
316
+
312
317
  _execute_statement("create user #{user} with password '#{password}' #{roles.join(' ')};")
313
318
 
319
+ if owner.to_s.length > 0 and owner.include?('@')
320
+ @mailer.send_welcome(owner, user, password)
321
+ else
322
+ @logger.warn("Email address of user '#{user}' is empty or malformed: '#{owner}'")
323
+ end
324
+
314
325
  unless search_path.nil? or search_path.empty?
315
326
  _execute_statement("alter user #{user} set search_path to #{search_path};")
316
327
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefanie Grunwald
@@ -70,6 +70,26 @@ dependencies:
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
72
  version: '12.0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: mail
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.6.0
80
+ - - "<"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.0
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '3.0'
73
93
  - !ruby/object:Gem::Dependency
74
94
  name: rspec
75
95
  requirement: !ruby/object:Gem::Requirement
@@ -146,5 +166,5 @@ rubyforge_project:
146
166
  rubygems_version: 2.4.5
147
167
  signing_key:
148
168
  specification_version: 3
149
- summary: Manage users, groups and ACL (access control lists) for AWS Redshift databases
169
+ summary: Manage users, groups and ACL (access control lists) for Postgres databases
150
170
  test_files: []