1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 # At the time we introduced scopes everywhere, VirtualMachinesController
6 # recognized scopes that gave the URL for a VM to grant access to that VM's
7 # login list. This migration converts those VM-specific scopes to the new
8 # general format, and back.
10 class NewScopeFormat < ActiveRecord::Migration[4.2]
11 include CurrentApiClient
14 %r{(/arvados/v1/virtual_machines/[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})}
15 OLD_SCOPE_REGEX = %r{^https?://[^/]+#{VM_PATH_REGEX.source}$}
16 NEW_SCOPE_REGEX = %r{^GET #{VM_PATH_REGEX.source}/logins$}
18 def fix_scopes_matching(regex)
20 ApiClientAuthorization.find_each do |auth|
21 auth.scopes = auth.scopes.map do |scope|
22 if match = regex.match(scope)
33 fix_scopes_matching(OLD_SCOPE_REGEX) do |match|
34 "GET #{match[1]}/logins"
41 hostname = 'www.example.com'
44 hostname = Socket.gethostname
46 fix_scopes_matching(NEW_SCOPE_REGEX) do |match|
47 Rails.application.routes.url_for(controller: 'virtual_machines',
48 uuid: match[1].split('/').last,
49 host: hostname, protocol: 'https')