thecore_auth_commons 3.6.0 → 3.6.2

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
  SHA256:
3
- metadata.gz: b77d8d01f744d28bf89e6901dc7061352926f7f70e386cdd77b90267d7665dfd
4
- data.tar.gz: f9b35c8d3983fe50ac88d0ac1e73fa61e35f5b592fbe88bfbdaaf8aaab45dd91
3
+ metadata.gz: a2e252301334fb7245c39abbf78f6f999e3025ba309ff34167ba9cde0fba26db
4
+ data.tar.gz: 962f5fb41a22bccc567bcc1a10094a705ae488af244bb84af3f9cb330de2caa5
5
5
  SHA512:
6
- metadata.gz: ad45aad725755827751f9f78588cf7ccc1567f89a07eb1309b6d7c286e3e64e7975725c2b730435c08087026859689bea47bc37888e8c6aa4191fe0a99310e2e
7
- data.tar.gz: 5853e1509cbd870f63e5e4a2d02c7ab7bd2fe61552760187dcf695306b2d04f27c3a46882f9e4e9cf8166727842736cd8ac7baedb8ff1f5bfeb7fa7f390dd7f0
6
+ metadata.gz: 3e10fd5b4b36f478eeb6612c9336400e3cf9a49b0ce8938a74b8bb77e9108c464b96478e6d45c1a38fa4acf947675e6246878dd668e86d94a86e621856d0edf1
7
+ data.tar.gz: d50a7f9e96935300394691c04da037ded09759e5268c840ae2817172ecb550873b133ac0de3d517ff88711df08ae207c870f2c5f4857ff608240b061e4b6870b
data/README.md CHANGED
@@ -23,7 +23,7 @@ Provides authentication and role-based authorization for Rails applications in t
23
23
  | `GOOGLE_CLIENT_ID` | — | Google OAuth2 client ID |
24
24
  | `GOOGLE_CLIENT_SECRET` | — | Google OAuth2 client secret |
25
25
  | `BASE_DOMAIN` | `example.com` | Used to build the default admin email at seed time |
26
- | `ADMIN_PASSWORD` | `Change#1` | Default admin password at seed time |
26
+ | `ADMIN_PASSWORD` | — (random) | First admin's password at seed time; when unset the seed generates a random one and writes it to `storage/admin_initial_password` (see Seeding) |
27
27
 
28
28
  OmniAuth providers are registered only when their respective env vars are all present.
29
29
 
@@ -73,7 +73,7 @@ actions: [:manage, :create, :read, :update, :destroy]
73
73
  targets: ["all"] + ApplicationRecord.subclasses.map { |m| m.to_s.underscore }
74
74
  ```
75
75
 
76
- It also creates the first admin user (`admin@BASE_DOMAIN` / `ADMIN_PASSWORD`) if no admin exists yet.
76
+ It also creates the first admin user, `admin@BASE_DOMAIN`, only if the database has no admin yet (existing admins are never touched). Its password is `ADMIN_PASSWORD` when set. Otherwise the seed generates a random one and writes it to `<Rails.root>/storage/admin_initial_password` with mode `0600`; the log gets one line saying where it is, never the password. In a `thecore_devcontainer` deployment that's `/root/persistence/${COMPOSE_PROJECT_NAME}/storage/admin_initial_password` on the host, or `docker compose exec backend cat storage/admin_initial_password`. After the first login change the password and delete the file. An existing file is never overwritten: if it's there, the admin is not created and the log says to delete the file and seed again. Until 3.6.1 the fallback was the fixed `Change#1`.
77
77
 
78
78
  To add a permission via console:
79
79
 
@@ -104,7 +104,7 @@ Multiple servers are supported, ordered by `priority` (ascending). Each server c
104
104
 
105
105
  On LDAP login or import, `memberOf` groups are read; matching group names become `Role` records assigned to the user. Groups named `Administrators`, `Domain Admins`, `Schema Admins`, `Enterprise Admins`, `admins`, or `administrators` also grant `user.admin = true`.
106
106
 
107
- Deleting a `LdapServer` record destroys all users whose `auth_source` is `"ldap #{id}"`.
107
+ Deleting a `LdapServer` record keeps all users whose `auth_source` is `"ldap #{id}"` — same record, same roles — and turns them into local users (`auth_source = "local"`). To log in again they need a local password, set by hand.
108
108
 
109
109
  ### Background LDAP import
110
110
 
@@ -10,8 +10,9 @@ class LdapServer < ApplicationRecord
10
10
  validates :base_dn, presence: true
11
11
 
12
12
  # Callbacks
13
- # After the record is actually deleted from the DB, remove all associated users which have the same auth_source with value "ldap #{id}"
14
- after_destroy :remove_users_with_auth_source
13
+ # Users imported from this server outlive it: they become local users, same record and roles,
14
+ # and need a local password set by hand to log in again.
15
+ after_destroy :turn_users_into_local_users
15
16
 
16
17
  def test_connection
17
18
  # Test the connection to the LDAP server
@@ -37,10 +38,10 @@ class LdapServer < ApplicationRecord
37
38
 
38
39
  private
39
40
 
40
- # This method is called after the record is destroyed
41
- def remove_users_with_auth_source
42
- User.where(auth_source: "ldap #{id}").find_each do |user|
43
- user.destroy
44
- end
41
+ # Destroying the users here used to strip their roles even when the destroy was refused: a
42
+ # User's dependents are destroyed before any later before_destroy guard aborts, and the
43
+ # abort's rollback is swallowed inside this record's own transaction, so it never undid them.
44
+ def turn_users_into_local_users
45
+ User.where(auth_source: "ldap #{id}").update_all(auth_source: "local", updated_at: Time.current)
45
46
  end
46
47
  end
data/db/seeds.rb CHANGED
@@ -3,15 +3,7 @@ puts "Loading ThecoreAuthCommons seeds"
3
3
  # Needing this since in the rake task is not aeger loaded
4
4
  Zeitwerk::Loader.eager_load_all
5
5
 
6
- email = "admin@#{ENV["BASE_DOMAIN"].presence || "example.com"}"
7
- psswd = ENV["ADMIN_PASSWORD"].presence || "Change#1"
8
-
9
- unless User.where(admin: true).exists?
10
- u = User.find_or_initialize_by(email: email)
11
- u.password = u.password_confirmation = psswd
12
- u.admin = true
13
- u.save(validate: false)
14
- end
6
+ ThecoreAuthCommons.seed_first_admin
15
7
 
16
8
  @values = {
17
9
  predicates: %i[can cannot],
@@ -1,3 +1,3 @@
1
1
  module ThecoreAuthCommons
2
- VERSION = "3.6.0".freeze
2
+ VERSION = "3.6.2".freeze
3
3
  end
@@ -44,6 +44,42 @@ module ThecoreAuthCommons
44
44
  u
45
45
  end
46
46
 
47
+ INITIAL_ADMIN_PASSWORD_FILE = "admin_initial_password".freeze
48
+
49
+ # Crea il primo admin, solo se nel DB non ce n'è nessuno. La password è ADMIN_PASSWORD,
50
+ # oppure una casuale scritta in <storage_dir>/admin_initial_password (0600) e mai nel log.
51
+ # Un file già presente non viene mai sovrascritto: in quel caso l'admin non viene creato.
52
+ # Se il salvataggio fallisce il file viene tolto, così il seed successivo può riprovare.
53
+ def self.seed_first_admin(storage_dir: Rails.root.join("storage"))
54
+ return if User.where(admin: true).exists?
55
+
56
+ email = "admin@#{ENV["BASE_DOMAIN"].presence || "example.com"}"
57
+ password = ENV["ADMIN_PASSWORD"].presence
58
+ unless password
59
+ generated_password_file = File.join(storage_dir.to_s, INITIAL_ADMIN_PASSWORD_FILE)
60
+ password = generate_secure_password
61
+ begin
62
+ FileUtils.mkdir_p(storage_dir.to_s)
63
+ # EXCL: mai sovrascrivere, anche con backend e worker che lanciano il seed insieme
64
+ File.open(generated_password_file, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |f| f.puts password }
65
+ rescue Errno::EEXIST
66
+ puts "Admin #{email} NON creato: #{generated_password_file} esiste già. Cancellalo e rilancia il seed."
67
+ return
68
+ end
69
+ end
70
+
71
+ begin
72
+ u = User.find_or_initialize_by(email: email)
73
+ u.password = u.password_confirmation = password
74
+ u.admin = true
75
+ u.save!(validate: false)
76
+ rescue StandardError
77
+ File.delete(generated_password_file) if generated_password_file
78
+ raise
79
+ end
80
+ puts(generated_password_file ? "Admin #{email} creato: password in #{generated_password_file}" : "Admin #{email} creato con la password di ADMIN_PASSWORD")
81
+ end
82
+
47
83
  def self.import_ldap_users_task
48
84
  puts "== Avvio sincronizzazione utenti da LDAP =="
49
85
 
@@ -127,17 +163,17 @@ module ThecoreAuthCommons
127
163
 
128
164
  # Obbliga almeno un carattere da ogni gruppo
129
165
  password = [
130
- lowercase.sample,
131
- uppercase.sample,
132
- numbers.sample,
133
- symbols.sample,
166
+ lowercase.sample(random: SecureRandom),
167
+ uppercase.sample(random: SecureRandom),
168
+ numbers.sample(random: SecureRandom),
169
+ symbols.sample(random: SecureRandom),
134
170
  ]
135
171
 
136
172
  # Caratteri restanti scelti a caso tra tutti
137
173
  all_characters = lowercase + uppercase + numbers + symbols
138
- (length - 4).times { password << all_characters.sample }
174
+ (length - 4).times { password << all_characters.sample(random: SecureRandom) }
139
175
 
140
176
  # Mischia per evitare ordine prevedibile
141
- password.shuffle.join
177
+ password.shuffle(random: SecureRandom).join
142
178
  end
143
179
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_auth_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.0
4
+ version: 3.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni