a.k.a A Really Complicated Slice Use-Case
The ability the startup the application with certain slices switched off is a really neat feature, one that I was going to build myself right before discovering this in Hanami.
Some Background
I work on my company’s identity team responsible for IAM infrastructure for our customers, so our security requirements are pretty stringent. We also want to go above and beyond where possible.
We have successfully moved our SAML integration API over to Hanami (from dry-system), and now we want to introduce a new slice for OAuth. A future increment will probably introduce a SCIM API.
These are all different, but sort-of related concerns that share a lot of dependencies, we want to unify it as multiple slices in one project.
Our app is intended to run as different “personas” in production, as k8s deployments. So the SAML API is one deployment, and we will have two copies of the OAuth deployment (two isolated use-cases with different security concerns).
So, we want to deploy our Hanami app with different HANAMI_SLICES
per deployment to only enable the functionality for that pod.
The Problem
The slice
route helper raises SliceLoadError
when the referenced slice is not present in SliceRegistrar
. However, this is the intended state of affairs when it is being excluded by HANAMI_SLICES
.
This seems to make this feature considerably less useful. I think there needs to be some middle ground between an incorrect slice name, and one that is being intentionally switched off.
I’m a little unsure how this would work best, however. I could simply swallow SliceLoadError
in slice
, but that would silently fail if you typo a slice name. I think the exception is useful, but I’m not certain how this should be avoided.
Doing something like
if Hanami.slice?(:foo)
slice :foo, at: '/foo'
end
doesn’t work, because the Routes class is loaded before the slices are registered. It would need to be a lazy check, perhaps something like
slice :foo, at: '/foo', if: -> { Hanami.slice?(:foo) }
Another possibility is registering a null slice in SliceRegistrar
that does nothing, but passes the presence check. That would need to be excluded from SliceRegistrar#each
.
I’m betting it’s pretty unlikely anybody has run into this before, but maybe I’m not alone and someone has solved this for themselves?
Otherwise, I’m curious what everybody thinks.