Metadata-Version: 2.5
Name: trytond_orclasstra_licensing
Version: 8.0.0.13
Summary: Orclasstra licensing core module for Tryton
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@tryton-erp.eu>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: licensing,orclasstra,plan,quota,tryton
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: cryptography
Requires-Dist: trytond-party<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Requires-Dist: proteus<8.1,>=8.0; extra == 'test'
Description-Content-Type: text/x-rst

Orclasstra Licensing
====================

The ``orclasstra_licensing`` module is the product licensing core of
the Orclasstra suite: it holds the plan catalogue that every consumer
module of the suite is measured against - the plans, the modules they
allow and the quotas they carry, and the registry of the limit kinds a
quota is measured in - and the licences issued from it, and it is the
engine that measures the consumer modules against them.

Programme Documentation
-----------------------

This module is the Level 0 core of the licensing family.  The module
map, the layering, the dependency rules and the plan-to-group matrix of
the whole programme are in ``modules-cch/LICENSING.md`` of the
repository; every provider module of the family links back to it.

Features
--------

- **Plan catalogue**: a plan names a product edition with a unique
  code, a name, an optional description and an active flag.
- **Module allow set**: a plan lists the modules it allows, by module
  name, so an edition can be described whether or not the module is
  installed and activated in the deployment.
- **Plan quotas**: a plan carries one quota per limit kind, with the
  amount the edition allows, the amount it warns from and whether
  reaching the limit warns (soft) or refuses the operation (hard).
  ``-1`` names an unlimited amount.
- **Limit kinds registry**: the unit a module group is counted in, such
  as ``cmdb.item.computer`` or ``portal.board``, declared by the
  provider of the group with its unit - a number of objects, of bytes or
  of seats.  The core owns the registry, the provider owns the kinds.
- **Licence record**: a plan bound to a customer, an installation and a
  validity window, with a code drawn from a sequence, the lifecycle in a
  ``status`` and the searchable ``state`` it computes - ``draft``,
  ``active``, ``suspended``, ``expired`` or ``revoked``.  A licence
  whose end date has passed is expired and never active, the *Renew*
  button extends it, the *Suspend* and *Revoke* buttons move it, and
  every one of those transitions is written to an audit trail.
- **Offline licence keys**: an installation is often offline - a
  customer network, an air-gapped laboratory - so an activation never
  calls home: a key is a self-contained blob, a versioned header, a
  signed payload and a signature, that the module parses and verifies
  with the public key it ships.  The key names the licence it activates,
  the product it was issued for, its own validity window and the
  fingerprint of the installation it is bound to, and a key that is
  malformed, whose payload was edited after it was signed, that names
  another product, is outside its window, is bound to another
  installation or activates a licence this installation does not carry
  is refused with a message of its own.
- **Installation binding**: the *fingerprint* of a deployment is a
  stable, non-reversible digest of the machine it runs on and of the
  name of its database, and it is what binds a licence to one
  installation: the activation writes it on the licence, and a key
  issued for another installation - or a licence bound to one - is
  refused.  Only the digest is ever published, never the identity it is
  computed from.
- **Activation state machine**: a licence is activated (with a key),
  suspended, revoked and renewed, each transition is refused with an
  ``ir.message``-backed ``UserError`` when the licence can not make it -
  a revoked licence is neither activated nor renewed, a draft is never
  suspended - and each one is recorded in the audit trail of
  ``orclasstra.licensing.license.activation``.
- **Enforcement engine**: the single entry point a consumer module
  calls before it writes.  The consumer counts its own amounts and hands
  the count to ``orclasstra.licensing.enforcement``, which answers with
  a decision - within the plan, over the soft limit or over the limit -
  and raises an error or a warning according to the policy.  The core
  reaches the consumer only through the pool, so the direction of the
  dependency is never reversed.
- **Module gate**: ``ir.module`` is extended so that the form and the
  tree of a module show the state of the module against the active plan
  - ``licensing_allowed`` and ``licensing_reason`` - and so that the
  *Activate* button asks the engine before it writes any state.  A
  module the plan does not allow is refused with an
  ``ir.message``-backed ``UserError``, warned about or only recorded -
  per the policy of the deployment - while the licensing layer and the
  modules it depends on are never refused, so that a plan can never
  lock a deployment out of its own licensing layer.
- **Deployment policy and grace period**:
  ``orclasstra.licensing.configuration`` carries the ``policy`` of the
  deployment - ``enforce`` lets every quota decide for itself, ``warn``
  downgrades every hard quota to a warning and ``log`` refuses nothing -
  and the ``grace_days`` a licence remains effective after its end date.
  Neither can turn a quota on: the engine gates less than the plans ask
  for and never more, and a configuration that can not be read falls
  back to its safe defaults.
- **Audit log**: ``orclasstra.licensing.enforcement.log`` records every
  gated decision, the ones that let the operation through as well as the
  ones that refuse it, so that the licensing layer can be audited
  afterwards and a late renewal shows up before it interrupts the work.
- **Metering**: ``orclasstra.licensing.usage`` keeps one counter per
  plan and limit kind and ``orclasstra.licensing.usage.snapshot`` an
  append-only history of what every counter measured.  The amount is
  read from the provider registry of the kind's code, and the state of a
  counter - ``ok``, ``warning`` or ``exceeded`` - is read against the
  quota of its plan, with an unlimited quota always ``ok``.  The daily
  ``check_usage`` job measures every counter of the plan of the
  effective licence, appends one snapshot per counter and mails the
  headroom alerts of the deployment.
- **Compliance checks**: ``orclasstra.licensing.compliance_check``
  records the audit of a licence at one moment - the licence, the day it
  was checked, the result and the details the notification reports - and
  the *Check Compliance* button and the daily ``check_expiration`` job
  write one check per licence that is in force and per day, so a licence
  is never checked twice a day.  The result is the first answer that
  applies: a licence whose validity window has passed is ``expired``, a
  licence whose plan is over a limit is ``over_limit``, a licence whose
  end date is inside the lookahead window a renewal is planned with is
  ``expiring``, and everything else is ``compliant``.  The run mails the
  report to the licence administrators and raises the headroom warnings
  of the metering from the same measurement, so that a limit is seen
  coming the day the licence that carries it is watched.  The
  *Compliance Dashboard* report reads the licences that are in force,
  the recent checks and the ones that are expiring or have lapsed.
- **Administration screen**: the catalogue, the registry of the limit
  kinds, the licences and the configuration are maintained under
  *Administration → Licensing* by the administrators of the deployment
  and of the licensing layer, the log and the compliance checks are read
  by them, and the catalogue and the licences are read by every user.

Dependencies
------------

- Tryton modules: ``ir``, ``res``, ``party``.

The core deliberately depends on nothing else.  ``party`` is a core
Tryton module, like ``company``: a licence has to name the customer that
holds it, and depending on a core module is not depending on a consumer
of the suite.  It is the licensing layer that gates access *to* the
consumer modules, so a consumer module depends on it and never the
reverse.  The providers of the module groups
(``orclasstra_licensing_<group>``) carry the group dependency, their
limit kinds and their provider class.

Models
------

- ``orclasstra.licensing.plan``: a catalogue entry.
- ``orclasstra.licensing.plan.module``: one module a plan allows.
- ``orclasstra.licensing.plan.limit``: one quota a plan carries.
- ``orclasstra.licensing.limit_kind``: one unit a group is counted in.
- ``orclasstra.licensing.license``: a plan bound to a customer, an
  installation and a validity window.
- ``orclasstra.licensing.license.activation``: the audit trail of a
  licence, one entry per activation, suspension, revocation and renewal.
- ``orclasstra.licensing.configuration``: the ``policy`` and the
  ``grace_days`` of the deployment (a singleton).
- ``orclasstra.licensing.enforcement``: the enforcement engine the
  consumer modules call (a plain model, no table).
- ``orclasstra.licensing.enforcement.log``: the audit log of the
  decisions of the engine.
- ``orclasstra.licensing.usage``: one counter per plan and limit kind,
  measured through the provider registry of the kind's code.
- ``orclasstra.licensing.usage.snapshot``: one measurement of a counter,
  appended to an append-only history.
- ``orclasstra.licensing.compliance_check``: the audit of one licence at
  one moment - the result of the check and the details the notification
  reports - written by the layer and never typed.
- ``ir.module`` (extended): the gate on the activation of a module,
  with the ``licensing_allowed`` and ``licensing_reason`` fields it
  carries.

Conventions
-----------

- The code of a plan is unique and is the name a licence, an order or a
  support case refers to.
- The code of a licence is drawn from its own sequence (``LIC-``) and is
  unique; it is what a support case, an order or a key refers to, so it
  is never typed by hand.
- A licence carries its lifecycle in ``status`` - ``draft``, ``active``,
  ``suspended`` or ``revoked`` - and exposes it, the validity window
  included, as the searchable ``state``.  A licence that is active and
  whose end date has passed reads ``expired``, never ``active``; an
  empty end date is a perpetual licence.
- A licence is renewed through the *Renew* button, which extends it by
  one year from its own end date, or from today when it is perpetual or
  has lapsed.  A draft was never activated and a revoked licence was
  withdrawn, so neither of them can be renewed.
- A licence is never moved by editing its ``status``: the activation,
  the suspension, the revocation and the renewal are the transitions,
  each one refuses with an ``ir.message``-backed ``UserError`` what the
  licence can not make, and each one is written to the audit trail of
  ``orclasstra.licensing.license.activation``.
- A licence is activated with an *offline key* and never calls home:
  the key is a self-contained blob signed by the issuer, and the module
  only ever *verifies* - it ships the public verification key of the
  issuer (``keys/public_key.pem``) and holds no private material, so a
  key signed by another private key does not answer to the key the
  deployment holds and is refused, and a deployment that replaces the
  file verifies its own keys.
- A key binds a licence to one installation: the *fingerprint* is a
  stable, non-reversible digest of the machine identity and the database
  name, and only the digest is ever published, never the identity it is
  computed from.  A key issued for another installation is refused, and
  a licence already bound to another installation refuses a key issued
  for this one.
- A module of a plan is a module name and not a relation to
  ``ir.module``: a plan can name a module that is not installed, not
  activated or not part of the deployment at all.
- A plan allows a module once; the pair is unique.
- A quota names its limit kind by the kind's code and not by a
  relation: a plan can quota a kind whose provider module is not
  installed, not activated or not part of the deployment at all (see
  "Dependency Rules" of ``modules-cch/LICENSING.md``).
- A plan quotas a kind once; the pair is unique.  ``-1`` names an
  unlimited amount and is no amount at all below that, and the soft
  limit is the warn threshold of the limit: it is at most the limit, and
  an unlimited quota has none.
- A consumer module asks the engine before it writes and never enforces
  the plan itself: it declares its limit kinds and counts them in its
  own provider, and the engine decides, audits and raises.  A missing or
  inactive quota, a licence that is not effective and a configuration
  that can not be read all let the operation through, so an installation
  can never lock itself out because licensing data is missing.
- A licence inside the ``grace_days`` of the deployment is still
  effective, but a hard quota of it warns instead of refusing, so that a
  late renewal does not interrupt the work.
- Activating a module is gated against the allow-set of the plan of the
  effective licence, through the same engine and the same policy as a
  quota.  The licensing layer itself and the modules it depends on are
  exempt, a plan that names no module gates nothing, and a deployment
  without an effective licence gates nothing: the gate reports and
  refuses, it never locks a deployment out of its own licensing layer.
- Every gated decision is written to the audit log, and a failure of
  that write never fails the operation the audit serves.
- A counter is kept per plan and limit kind and is measured through the
  provider registry of the kind's code: a kind no provider counts, a
  counter that raises and a counter that measures nothing all measure
  nothing, and none of them fails the run.  The ``state`` of a counter
  reads the amount against the quota of its plan - ``ok`` under the soft
  limit, ``warning`` from it up to the limit and ``exceeded`` over it -
  and an unlimited quota is always ``ok`` and has no headroom and no
  percentage.
- The history of a counter is append-only: the layer writes the
  snapshots outside the access rights, every licensing group reads them
  and the licensing administration alone deletes them, so a measurement
  is never rewritten.
- A counter alerts over a limit of its plan whatever the
  ``warning_threshold`` of the deployment says, from that threshold up
  below it, and never when its quota is unlimited.  A deployment that
  configures no recipient mails the licensing administrators that carry
  an email address, and a deployment that has none mails nothing and
  does not fail.  The compliance report of the layer is mailed to the
  same recipients.
- The compliance of a licence is the first answer that applies: a
  licence whose window has passed is ``expired``, a licence whose plan
  is over a limit is ``over_limit``, a licence whose end date is inside
  the lookahead window is ``expiring``, and everything else is
  ``compliant``.  A licence that is a draft, that is suspended or that
  was revoked is not in force and is not checked at all.
- A check is written once per licence that is in force and per day, by
  the layer and outside its access rights: every licensing group reads
  it, the licensing administration alone deletes it and nobody edits it,
  so a check is never rewritten and the run is idempotent for the day.

Cron Jobs
---------

- ``cron_check_usage``: runs once a day and calls
  ``orclasstra.licensing.usage|check_usage`` to measure every counter of
  the plan of the effective licence, append one snapshot per counter and
  mail the headroom alerts.  A deployment without an effective licence
  measures nothing and appends nothing.
- ``cron_check_expiration``: runs once a day and calls
  ``orclasstra.licensing.license|check_expiration`` to check every
  licence that is in force, record one check per licence and per day,
  mail the compliance report to the licence administrators and raise the
  headroom warnings of the metering from the same measurement.  A
  licence that carries a check of today is left alone, so the run is
  idempotent for the day, and a deployment without a licence in force
  checks nothing and mails nothing.

Wizards
-------

- ``orclasstra.licensing.import_key``: the *Import Licence Key* wizard.
  It reads the offline key an administrator pastes or uploads, shows
  what the key and the licence it names say without writing anything
  (the *Preview* step), and activates the licence on *Import* through
  the transition of the licence itself - never through the status field.
  The start screen also shows the fingerprint of the installation, the
  value a customer sends to the issuer to have a key bound to it.
