Commit Graph

373 Commits

Author SHA1 Message Date
Javi Martín
09eddec663 Remove unnecessary require statements
Since we autoload the `lib` folder, there's no need to manually require
the files inside it.
2024-04-11 19:08:01 +02:00
Javi Martín
fae29069d4 Ignore Sass warnings until we update our dependencies
We're getting one warning when compiling the assets due to the code we
use from font-awesome-sass, and a lot of warnings due to the code we use
from foundation.

Since these warnings are very annoying, and we get them both when
deploying and every time we change an SCSS file in development, we're
silencing them.

I haven't found the way to pass the `quiet_deps` option to the Sprockets
processor, so I'm monkey-patching the Sass engine instead.
2024-04-04 15:16:24 +02:00
Javi Martín
ec686a8d1a Ignore touch audited callbacks globally
This is possible since audited 5.4.3, and IMHO it's useful because
audited has added a callback on touch, which could result in strange
behavior since we might use the `touch` method to expire the cache
without doing any changes.

While this doesn't affect our application because we're using the `on:`
option of the `audited` method in the budget investment model, it'll
make it easier to enable audited for other classes in custom
installations without having to worry about the (usually undesired)
touch callbacks.
2024-03-27 16:51:29 +01:00
Javi Martín
d0f6e4396f Add missing options to invisible captcha initializer
These options had been added in the gem's initializer by version 2.0,
but we forgot to add them to our initializer file. So we're doing it
now.
2024-03-27 16:12:11 +01:00
Javi Martín
c585eb0e26 Merge pull request #5418 from consuldemocracy/dependabot/bundler/wicked_pdf-2.8.0
Bump wicked_pdf from 2.7.0 to 2.8.0
2024-03-27 15:39:43 +01:00
Javi Martín
65e14fd23f Add initializer included in acts-as-taggable-on 10.0.0
Similar to what we do with other gems, we add the commented code.
2024-03-23 03:38:13 +01:00
Javi Martín
8d2c2c473e Base Devise initializer on the latest version
We haven't updated this initializer for years, so here's the updated
version. The `expire_auth_token_on_timeout` doesn't seem to exist
anymore, and a few more options have been added.

Note that the default Devise initializer configures
`config.responder.error_status` and `config.responder.redirect_status`
so they follow Hotwire/Turbo conventions. For now, I'm commenting these
lines because we currently don't use Hotwire/Turbo.
2024-03-23 01:12:35 +01:00
Javi Martín
f8e7bf042d Update wicked_pdf initializer to the latest version
We haven't been updating this file for a long time so, and this version
uses the `configure` method instead of the `config` hash.

We're also adding a few comments already included in previous versions
of wicked pdf that we hadn't copied to our initializer.
2024-03-19 21:26:05 +01:00
Senén Rodero Rodríguez
1e46746d6a Filter airbrake parameters
As Rails does with the application log and other tools. We
are going to use the same filtering rules we use in Consul Democracy.

We are renaming the initializer file name `filter_parameter_logging.rb` so
it's loaded before the errbit initializer.
2023-11-23 18:21:29 +01:00
Senén Rodero Rodríguez
b7073691f1 Log successful and failed login attempts in a separate log file
We log the login parameter and the request IP address.

Quoting the ENS:

> [op.acc.5.r5.1] Se registrarán los accesos con éxito y los fallidos.
2023-10-25 10:13:03 +02:00
taitus
d54a5c2ae0 Allow define maximum_attemps and unlock_in 2023-10-24 20:21:03 +02:00
taitus
a1955531e1 Enable devise lockable module with default values
In order to the display a warn text on the last attempt
before the account is locked, we need update
config.paranoid to false as the devise documentation
explains.

Adding "config.paranoid: false" implies further changes
to the code, so for now we unncomment the default value
"config.last_attempt_warning = true" and update it to false.
2023-10-24 20:20:27 +02:00
taitus
7c771b28b5 Add password complexity 2023-10-24 19:00:43 +02:00
taitus
fe9da7988f Enable password_complexity
As it seems that adding complexity to the password is something that
might be wanted from the Consul applications, we added the necessary
changes to allow it.

In this version we simply:
- Uncomment the configuration variable "password_complexity"
- Set this variable without any restrictions
- Adapt the application so that everything still works normally.

One of the things that had to be done to adapt the application
was to remove the overwriting of the "self.included" method.

The original idea of overwriting the "self.included" method seems
to be the possibility of being able to overwrite the
:current_equal_password_validation validation.
The problem comes from the fact that by only calling that validation,
the rest of the validations that are defined (in this case
"password_complexity") are no longer applied.

It seems like a good idea to remove the overwrite of the "self.included"
method to allow all the defined validations to be applied and simply
overwrite the :current_equal_password_validation method so that
everything behaves the same.

:allow_passwords_equal_to_email configuration has been enabled too,
in order to allow existing records with this configuration.

Another change made was to uncomment the line:
and to keep everything working the same set the value to false:
config.email_validation = false.

This change has had to be made because in the documentation of
devise-security it says the following:
In other words, if we want to use the :secure_validatable module
we have to enable this configuration even if its value is "false".

If we kept the configuration variable commented out:
The following error appears:
"uninitialized constant Devise::Models::SecureValidatable::EmailValidator".

So it has been verified that if before making any change we
decommented the line and added the value of "false", the application
worked as normal.
2023-10-24 18:59:03 +02:00
taitus
56aadedc8c Update devise-security.rb
In these commits ffe9ac7 0d8def3 we updated the devise-security version.
In these versions the 'password_regex'  configuration key and some comments
were changed.

We update this file in order to use the new configuration key 'password_complexity' and keep comments updated.
2023-10-24 18:59:03 +02:00
Javi Martín
3527398db1 Remove no longer needed call to default_scoped
Automatically using `default_scoped` in this context is the default
behavior in Rails 6.1.
2023-09-11 23:40:37 +02:00
Javi Martín
633ccf80f6 Use Rails 6.1 defaults and overwrite them
We can remove the `new_framework_defaults_6_1` file by using Rails 6.1
default options and overwriting the one we haven't enabled.

We've experienced problems while running the tests (probably the same
would happen on production) when enabling the `has_many_inversing`
option. For example, after creating a legislation answer for a question
with no answers, calling `question.answers_count` would then return `2`
instead of `1`.

So we aren't enabling this option.
2023-09-11 23:40:37 +02:00
Javi Martín
b4da795f79 Preload CSS and JavaScript files
This is the default setting in Rails 6.1, and generates an extra tag in
the HTML which tells the browser to download and cache these files as
soon as possible, even before they're needed.

This might not be that relevant in our application, since on most pages
we only generate one CSS and one JS file. But it might make it easier to
move the `javascript_include_tag` statement to the bottom of the page in
the future if we detect that doing so increases performance.
2023-09-11 23:40:37 +02:00
Javi Martín
4984ac44d3 Use the default job queue to deliver emails
This is the default in Rails 6.1 for Action Mailer, just like it's the
default for Active Storage.
2023-09-11 23:40:37 +02:00
Javi Martín
50dc9dee69 Disable legacy database connection handling
Since we aren't using the old way to handle multiple databases (because
we don't use multiple databases), we can safely enable this option
without breaking anything.
2023-09-11 23:40:37 +02:00
Javi Martín
fe94d84e81 Use 308 as SSL default redirect status
This way user agents will know that the redirection from HTTP to HTTPS
is permanent and not temporary, which is the case if we activate the
`force_ssl` option (which we do by default).
2023-09-11 23:40:37 +02:00
Javi Martín
05eecaf46c Return UTC offests in the utc_to_local method
We don't use this method anywhere, so we might use the new default in
Rails 6.1, which was added when Rails started using tzinfo 2.
2023-09-11 23:40:37 +02:00
Javi Martín
4fe9d4cbcf Use URL-safe CSRF tokens and SameSite in cookies
These measures increase protection against CSRF ataks. The only reason
Rails provides them as a configuration option is there are complex
applications that run one version of the code in some servers while
running an old version of the code in other servers might run into
issues because the the old version won't handle the tokens or cookies
generated by the new version.

Since most Consul applications use just one server and the ones with
more servers would only face this issue for a few seconds (while
upgrading to a new version of Consul Democracy), we can safely enable
these configuration options.
2023-09-11 23:40:37 +02:00
Javi Martín
e8c8b00e94 Skip after callbacks if terminated in active job
Not sure this configuration option does anything, though, since it's
been removed in Rails 7.0 because it was not halting the callbacks.

But, if it does nothing, it's the same as disabling it, which is what we
were doing until now, so in the end using the Rails 6.1 default value
does no harm.
2023-09-11 23:40:37 +02:00
Javi Martín
8a6b9dcb46 Delete action mailbox options since we don't use it
Simply uncommeting them raised an exception.
2023-09-11 23:40:37 +02:00
Javi Martín
8e2b7e231f Configure "retry jitter" option in active job
This way, when several jobs fail at the same time, not all of them will
be retried at the same time.
2023-09-11 23:40:37 +02:00
Javi Martín
600bc793a1 Store Active Storage variants in the database
This mostly benefit people using external services, as now there's no
need to query the service to check whether a variant exists.

For most Consul Democracy installations, this will probably not be
relevant, so we're sticking wih the default value.
2023-09-11 23:40:37 +02:00
Javi Martín
1f67318f84 Delete configuration options that are now redundant
These options are the default options in Rails 6.1, so we no longer need
to customize them.
2023-09-11 23:40:37 +02:00
Javi Martín
13eebe4d7a Remove Wicked PDF monkey patch
This patch was added in commit baefc249f because both ViewComponent and
Wicked PDF monkey-patched the `render` method and so they were
incompatible.

However, Rails 6.1 includes the patch used by ViewComponent, meaning
ViewComponent doesn't monkey-patch the `render` method anymore, and so
it's compatible with Wicked PDF.
2023-09-11 23:40:37 +02:00
Javi Martín
6e9df3be5a Upgrade to Rails 6.1
Note that `Capybara.app_host` now returns `nil` by default and that
breaks tests using `lvh.me` or our custom `app_host` method, so we're
setting `Capybara.app_host` to the value it had in earlier versions of
Rails. I also haven't found a way to remove the code to set the
integration session host in relationable tests which I mentioned in
commit ffc14e499.

Also note that we now filter more parameters, and that they match
regular expressions, so filtering `:passw` means we're filtering
`passwd`, `password`, ...
2023-09-11 23:40:37 +02:00
Javi Martín
a1439d0790 Apply Layout/LineLength rubocop rule
Note we're excluding a few files:

* Configuration files that weren't generated by us
* Migration files that weren't generated by us
* The Gemfile, since it includes an important comment that must be on
  the same line as the gem declaration
* The Budget::Stats class, since the heading statistics are a mess and
  having shorter lines would require a lot of refactoring
2023-08-30 14:46:35 +02:00
Javi Martín
9491b20314 Extract method to get vote counts by votable type 2023-08-30 14:46:34 +02:00
Senén Rodero Rodríguez
b85df50a28 Configure airbrake as Errbit documentation says
Quoting `errbit/errbit/blob/main/app/views/apps/_configuration_instructions.html.erb`

> airbrake.io supports various features that are out of scope for Errbit.
2023-05-29 13:50:33 +02:00
Javi Martín
5c2ca27df1 Update Globalize monkey-patch to forward kw args
This method changed in Globalize version 6.0.1 [1], which we're using since
commit 6072372c9d.

We were getting a deprecation warning in Ruby 2.7:

```
config/initializers/globalize.rb:8: warning: Using the last argument as
keyword parameters is deprecated; maybe ** should be added to the call
```

So we're using the `(...)` arguments syntax to make it compatible with
Ruby 3.0, just like Globalize does when using Ruby 2.7.

[1] See pull request 778 in the globalize/globalize GitHub repo.
2023-01-26 17:19:15 +01:00
Senén Rodero Rodríguez
e08d287ef8 Do not track Apartment::TenantNotFound exceptions through Airbrake
As we do with `ActiveRecord::NotFound` exceptions. Otherwise, we
can quickly flood the third-party application in charge of tracking
exceptions.
2022-12-20 16:46:00 +01:00
Javi Martín
e38b860374 Use the right tenant after Devise redirections
This is something we had read about a long time ago, but didn't find how
to reproduce the issue until now.

As mentioned in the Apartment documentation:

> it's important to consider that you may want to maintain the
> "selected" tenant through different parts of the Rack application
> stack. For example, the Devise gem adds the Warden::Manager middleware
> at the end of the stack in the examples above, our
> Apartment::Elevators::Subdomain middleware would come after it.
> Trouble is, Apartment resets the selected tenant after the request is
> finished, so some redirects (e.g.  authentication) in Devise will be
> run in the context of the "public" tenant. The same issue would also
> effect a gem such as the better_errors gem which inserts a middleware
> quite early in the Rails middleware stack.
>
> To resolve this issue, consider adding the Apartment middleware at a
> location in the Rack stack that makes sense for your needs, e.g.:
>
> Rails.application.config.middleware.insert_before Warden::Manager,
> Apartment::Elevators::Subdomain
>
> Now work done in the Warden middleware is wrapped in the
> Apartment::Tenant.switch context started in the Generic elevator.
2022-11-19 17:49:27 +01:00
Javi Martín
58c9e8462d Only seed tenants when necessary in tests
On my machine, seeding a tenant takes about one second, so skipping this
action when it isn't necessary makes tests creating tenants faster
(although creating a tenant still takes about 3-4 seconds on my
machine).
2022-11-11 01:40:02 +01:00
Eduardo Vilar
0ea61b9b61 Allow different omniauth settings per tenant
Co-Authored-By: Javi Martín <javim@elretirao.net>
2022-11-11 01:39:30 +01:00
Eduardo Vilar
52ebeb7ba6 Consider the current tenant with delayed jobs 2022-11-09 18:19:20 +01:00
Javi Martín
5983006657 Use a custom method to detect the current tenant
The subdomain elevator we were using, which is included in apartment,
didn't work on hosts already including a subdomain (like
demo.consul.dev, for instance). In those cases, we would manually add
the subdomain to the list of excluded subdomains. Since these subdomains
will be different for different CONSUL installations, it meant each
installation had to customize the code. Furthermore, existing
installations using subdomains would stop working.

So we're using a custom method to find the current tenant, based on the
host defined in `default_url_options`.

In order to avoid any side-effects on single-tenant applications, we're
adding a new configuration option to enable multitenancy

We're enabling two ways to handle this configuration option:

a) Change the application_custom.rb file, which is under version control
b) Change the secrets.yml file, which is not under version control

This way people prefering to handle configuration options through
version control can do so, while people who prefer handling
configuration options through te secrets.yml file can do so as well.

We're also disabling the super-annoying warnings mentioning there are no
tenants which we got every time we run migrations on single-tenant
applications. These messages will only be enabled when the multitenancy
feature is enabled too. For this reason, we're also disabling the
multitenancy feature in the development environment by default.
2022-11-09 18:19:20 +01:00
Javi Martín
c483c6036a Install extensions in a shared schema
This way all tenants will be able to access them instead of just the
default one.

The apartment gem recommends using a rake task instead of a migration,
but that's a solution which is primarily meant for new installations.
Migrations are easier to execute on existing installations.

However, since this migration doesn't affect the `schema.rb` file, we
still need to make sure the shared schema is created in tasks which do
not execute migrations, like `db:schema:load` or `db:test:prepare`, just
like the apartment gem recommends. That's why we're enhancing these
tasks so they execute this migration.

Note that there might be cases where the database user isn't a superuser
(as it's usually the case on production environments), meaning commands
to create, alter or drop extensions will fail. There's also the case
where users don't have permissions to create schemas, which is needed in
order to create the shared extensions schema and the schemas used by the
tenants. For these reasons, we're minimizing the number of commands, and
so we only alter or create extensions when it is really necessary.

When users don't have permission, we aren't running the commands but
showing a warning with the steps needed to run the migration manually.
This is only necessary on installations which are going to use
multitenancy; single-tenant applications upgrading don't need to run
this migration, and that's why we aren't raising exceptions when we
can't run it.

For new installations, we'll change the CONSUL installer so extensions
are automatically created in the shared schema.

Also note the plpgsql extension is not handled here. This is a special
extension which must be installed on the pg_catalog schema, which is
always in the search path and so is shared by all tenants.

Finally, we also need to change the `database.yml` file in order to
search for shared extensions while running migrations or model tests,
since none of our enabled extensions are executed during migrations;
we're also adding a rake task for existing installations. Quoting the
apartment documentation:

> your database.yml file must mimic what you've set for your default and
> persistent schemas in Apartment. When you run migrations with Rails,
> it won't know about the extensions schema because Apartment isn't
> injected into the default connection, it's done on a per-request
> basis.
2022-11-09 17:53:31 +01:00
Eduardo Vilar
382abb3666 Add multitenancy with apartment
Co-Authored-By: Javi Martín <javim@elretirao.net>
2022-11-09 17:52:05 +01:00
Senén Rodero
9e0c09ebe1 Merge pull request #5004 from consul/omniauth-log
Configure OmniAuth to send log to Rails logger
2022-10-07 12:54:50 +02:00
Javi Martín
a729967e8a Make Devise::Mailer inherit from ApplicationMailer
This way we remove duplication in the `from:` proc, the helpers, and the
methods we're about to write.
2022-10-06 13:18:05 +02:00
Senén Rodero Rodríguez
ddc7b626f5 Configure OmniAuth to send log to Rails logger
Before this change log generated by OmniAuth was sent to the STDOUT. Now
developers will find those gems log within the `app/log` directory as expected.
2022-10-05 12:48:13 +02:00
Julian Herrero
24276a201e Remove useless answers show view
Until now, in order to edit an answer, we had to click on its title on
the table and then on the "Edit answer" link.

That was tedious and different from what we usually do in the admin
section. Furthermore, the code for the answers table was written twice
and when we modified it we forgot to update the one in the `show`
action, meaning the table here provided less information than the
information present in the answers tables.

Co-Authored-By: Javi Martín <javim@elretirao.net>
2022-09-14 14:45:35 +02:00
Javi Martín
5b844bf231 Use index_with to simplify hash generation
This method was introduced in Rails 6.0. It can be used to take an array
and create a hash where the elements of the array are the indexes of the
hash.
2022-08-24 18:19:02 +02:00
Javi Martín
0b613158da Update TranslationHelper monkey-patch
The `translate` method now receives keyword arguments instead of a hash
of options.
2022-08-24 14:36:49 +02:00
Javi Martín
7fe2309762 Use Rails 6.0 defaults and overwrite them
We can remove the `new_framework_defaults_6_0` file by using Rails 6.0
default options and overwriting the ones we haven't enabled.

We're still using the classic autoloader because we still haven't
checked how switching to zeitwerk will affect the way CONSUL
installations customize their code.

And we're using the default queues for Active Storage because we were
already using them and that will be the default option in Rails 6.1.
2022-08-24 14:36:49 +02:00
Javi Martín
1ea4988e52 Enable collection cache versioning
Similar to the way we enabled cache versioning in commit e01a94d7b. This
only affects caching `ActiveRecord::Relation` objects. I'm not even sure
we cache these objects, though, so we're just enabling the option
because it's the default one in Rails 6.0.
2022-08-24 14:36:49 +02:00