21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / api / lib / tasks / manage_long_lived_tokens.rake
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 # Tasks that can be useful when changing token expiration policies by assigning
6 # a non-zero value to Login.TokenLifetime config.
7
8 require 'set'
9 require 'current_api_client'
10
11 namespace :db do
12   desc "Apply expiration policy on long lived tokens"
13   task fix_long_lived_tokens: :environment do
14     lifetime = Rails.configuration.API.MaxTokenLifetime
15     if lifetime.nil? or lifetime == 0
16       lifetime = Rails.configuration.Login.TokenLifetime
17     end
18     if lifetime.nil? or lifetime == 0
19       puts("No expiration policy set (API.MaxTokenLifetime nor Login.TokenLifetime is set), nothing to do.")
20       # abort the rake task
21       next
22     end
23     exp_date = Time.now + lifetime
24     puts("Setting token expiration to: #{exp_date}")
25     token_count = 0
26     ll_tokens(lifetime).each do |auth|
27       if auth.user.nil?
28         printf("*** WARNING, found ApiClientAuthorization with invalid user: auth id: %d, user id: %d\n", auth.id, auth.user_id)
29         # skip this token
30         next
31       end
32       if (auth.user.uuid =~ /-tpzed-000000000000000/).nil? and (auth.user.uuid =~ /-tpzed-anonymouspublic/).nil?
33         CurrentApiClientHelper.act_as_system_user do
34           auth.update!(expires_at: exp_date)
35         end
36         token_count += 1
37       end
38     end
39     puts("#{token_count} tokens updated.")
40   end
41
42   desc "Show users with long lived tokens"
43   task check_long_lived_tokens: :environment do
44     lifetime = Rails.configuration.API.MaxTokenLifetime
45     if lifetime.nil? or lifetime == 0
46       lifetime = Rails.configuration.Login.TokenLifetime
47     end
48     if lifetime.nil? or lifetime == 0
49       puts("No expiration policy set (API.MaxTokenLifetime nor Login.TokenLifetime is set), nothing to do.")
50       # abort the rake task
51       next
52     end
53     user_ids = Set.new()
54     token_count = 0
55     ll_tokens(lifetime).each do |auth|
56       if auth.user.nil?
57         printf("*** WARNING, found ApiClientAuthorization with invalid user: auth id: %d, user id: %d\n", auth.id, auth.user_id)
58         # skip this token
59         next
60       end
61       if not auth.user.nil? and (auth.user.uuid =~ /-tpzed-000000000000000/).nil? and (auth.user.uuid =~ /-tpzed-anonymouspublic/).nil?
62         user_ids.add(auth.user_id)
63         token_count += 1
64       end
65     end
66
67     if user_ids.size > 0
68       puts("Found #{token_count} long-lived tokens from users:")
69       user_ids.each do |uid|
70         u = User.find(uid)
71         puts("#{u.username},#{u.email},#{u.uuid}") if !u.nil?
72       end
73     else
74       puts("No long-lived tokens found.")
75     end
76   end
77
78   def ll_tokens(lifetime)
79     query = ApiClientAuthorization.where(expires_at: nil)
80     query = query.or(ApiClientAuthorization.where("expires_at > ?", Time.now + lifetime))
81     query
82   end
83 end