-
Notifications
You must be signed in to change notification settings - Fork 258
Add multi-key JWKS rotation for LTI 1.3 signing keys #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
donny-wong
wants to merge
4
commits into
MarkUsProject:master
Choose a base branch
from
donny-wong:quercus_integration_lti_jwks_key_rotation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Rotates the LTI signing key when due and prunes retired keys past the | ||
| # overlap window. Scheduled via Settings.resque_scheduler; only runs when | ||
| # Settings.lti.rotation.enabled is true. | ||
| class LtiKeyMaintenanceJob < ApplicationJob | ||
| def perform | ||
| return unless Settings.lti&.rotation&.enabled | ||
|
|
||
| LtiKeyStore.rotate_if_due! | ||
| LtiKeyStore.prune! | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'time' | ||
|
|
||
| # Manages the RSA key material MarkUs uses to sign LTI 1.3 client_credentials | ||
| # assertions to Canvas, and to publish its public JWKS. | ||
| # | ||
| # Rotation model (zero-downtime): | ||
| # * Keys live as PEM files in key_dir, named lti_key_<UTC-timestamp>.pem. | ||
| # * The "current" signer is the newest file (paths sorted lexicographically, | ||
| # which matches chronological order given the zero-padded UTC timestamp), | ||
| # overridable via Settings.lti.rotation.current_key. | ||
| # * public_jwks publishes ALL keys present, so Canvas can still verify | ||
| # assertions signed by an outgoing key until they expire and its JWKS | ||
| # cache refreshes. | ||
| # * rake markus:rotate_if_due adds a new key when the current one is past | ||
| # its max age; rake markus:prune_keys removes keys past the overlap window. | ||
| module LtiKeyStore | ||
| module_function | ||
|
|
||
| KEY_GLOB = 'lti_key_*.pem' | ||
|
|
||
| def key_dir | ||
| Settings.lti&.rotation&.key_dir || | ||
| File.join(Settings.file_storage.default_root_path, 'lti', 'keys') | ||
| end | ||
|
|
||
| # All private-key PEM paths, newest first. If the rotation dir is empty, | ||
| # fall back to the legacy single key.pem (LtiClient::KEY_PATH). | ||
| def key_paths | ||
| paths = Dir.glob(File.join(key_dir, KEY_GLOB)).sort.reverse | ||
| return paths if paths.any? | ||
|
|
||
| File.exist?(LtiClient::KEY_PATH) ? [LtiClient::KEY_PATH] : [] | ||
| end | ||
|
|
||
| # The RSA key MarkUs signs NEW assertions with. | ||
| def current_key | ||
| path = explicit_current || key_paths.first | ||
| raise 'No LTI signing key found' if path.nil? | ||
|
|
||
| OpenSSL::PKey::RSA.new(File.read(path)) | ||
| end | ||
|
|
||
| # JWK wrapper for the current signer (used to set the `kid` header). | ||
| def current_jwk | ||
| JWT::JWK.new(current_key) | ||
| end | ||
|
|
||
| # Public JWKS: every published key, exported public-only. | ||
| # NB: JWT::JWK#export returns public members only unless include_private: true. | ||
| def public_jwks | ||
| { keys: key_paths.map { |p| JWT::JWK.new(OpenSSL::PKey::RSA.new(File.read(p))).export } } | ||
| end | ||
|
|
||
| # Optional explicit override, e.g. Settings.lti.rotation.current_key = 'lti_key_20260101T000000Z.pem' | ||
| # Raises rather than silently falling back to the newest key: pinning is a | ||
| # deliberate operator action (typically compromise response), so quietly | ||
| # signing with a different key would defeat the point and hide the mistake. | ||
| def explicit_current | ||
| name = Settings.lti&.rotation&.current_key | ||
| return if name.nil? | ||
|
|
||
| path = File.join(key_dir, name) | ||
| raise "Pinned LTI signing key not found: #{path} (check Settings.lti.rotation.current_key)" unless File.exist?(path) | ||
|
|
||
| path | ||
| end | ||
|
|
||
| # Creation time encoded in the filename (UTC); falls back to mtime. | ||
| def created_at(path) | ||
| ts = File.basename(path)[/\d{8}T\d{6}Z/] | ||
| ts ? Time.parse(ts).utc : File.mtime(path).utc | ||
| end | ||
|
|
||
| # Mint a new key; it becomes the current signer. Returns its path. | ||
| def rotate! | ||
| FileUtils.mkdir_p(key_dir) | ||
| key = OpenSSL::PKey::RSA.new(2048) | ||
| path = File.join(key_dir, "lti_key_#{Time.now.utc.strftime('%Y%m%dT%H%M%SZ')}.pem") | ||
| File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |f| f.write(key.to_pem) } | ||
| Rails.logger.info("LTI key rotated: #{File.basename(path)} (kid=#{JWT::JWK.new(key).kid})") | ||
| path | ||
| end | ||
|
|
||
| # Rotate only if the current signer is past its max age (or there is none). | ||
| def rotate_if_due! | ||
| max_age = Settings.lti.rotation.max_age_days.days | ||
| current = key_paths.first | ||
| age = current && (Time.now.utc - created_at(current)) | ||
| return rotate! if age.nil? || age > max_age | ||
|
|
||
| Rails.logger.info("LTI key #{(age / 1.day).round(1)}d old; no rotation (threshold #{max_age / 1.day}d)") | ||
| nil | ||
| end | ||
|
|
||
| # Delete retired keys past the overlap window. A key is retired when its | ||
| # successor was created. Neither the newest key nor a key pinned via | ||
| # Settings.lti.rotation.current_key is ever pruned -- deleting the current | ||
| # signer would leave MarkUs unable to sign. | ||
| def prune! | ||
|
donny-wong marked this conversation as resolved.
|
||
| overlap = Settings.lti.rotation.overlap_days.days | ||
| paths = key_paths | ||
| pinned = explicit_current | ||
| now = Time.now.utc | ||
|
|
||
| paths.each_with_index.filter_map do |path, i| | ||
| next if i.zero? | ||
| next if pinned && path == pinned | ||
|
|
||
| age = now - created_at(paths[i - 1]) | ||
| next unless age > overlap | ||
|
|
||
| File.delete(path) | ||
| Rails.logger.info("Pruned LTI key #{File.basename(path)} (retired #{(age / 1.day).round(1)}d ago)") | ||
| path | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| namespace :markus do | ||
| desc 'Rotate LTI private key' | ||
| desc 'Generate a new LTI signing key and make it the current signer' | ||
| task lti_key: :environment do | ||
| print('Creating new private key') | ||
| key = OpenSSL::PKey::RSA.new(2048) | ||
| FileUtils.mkdir_p(File.dirname(LtiClient::KEY_PATH)) | ||
| f = File.new(LtiClient::KEY_PATH, 'w') | ||
| f.write(key.to_s) | ||
| f.close | ||
| puts "New current LTI key: #{LtiKeyStore.rotate!}" | ||
| end | ||
|
|
||
| desc 'Rotate the LTI signing key if the current one is past Settings.lti.rotation.max_age_days' | ||
| task rotate_if_due: :environment do | ||
| LtiKeyStore.rotate_if_due! | ||
| end | ||
|
|
||
| desc 'Remove retired LTI keys past Settings.lti.rotation.overlap_days' | ||
| task prune_keys: :environment do | ||
| LtiKeyStore.prune! | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| describe LtiKeyMaintenanceJob do | ||
| describe '#perform' do | ||
| context 'when rotation is disabled' do | ||
| before { allow(Settings.lti.rotation).to receive(:enabled).and_return(false) } | ||
|
|
||
| it 'does not rotate' do | ||
| expect(LtiKeyStore).not_to receive(:rotate_if_due!) | ||
| LtiKeyMaintenanceJob.perform_now | ||
| end | ||
|
|
||
| it 'does not prune' do | ||
| expect(LtiKeyStore).not_to receive(:prune!) | ||
| LtiKeyMaintenanceJob.perform_now | ||
| end | ||
| end | ||
|
|
||
| context 'when rotation is enabled' do | ||
| before { allow(Settings.lti.rotation).to receive(:enabled).and_return(true) } | ||
|
|
||
| it 'rotates when due' do | ||
| allow(LtiKeyStore).to receive(:prune!) | ||
| expect(LtiKeyStore).to receive(:rotate_if_due!) | ||
| LtiKeyMaintenanceJob.perform_now | ||
| end | ||
|
|
||
| it 'prunes retired keys' do | ||
| allow(LtiKeyStore).to receive(:rotate_if_due!) | ||
| expect(LtiKeyStore).to receive(:prune!) | ||
| LtiKeyMaintenanceJob.perform_now | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.