Merge branch 'master' into 3177-collection-choose-files
[arvados.git] / services / api / script / create_superuser_token.rb
1 #!/usr/bin/env ruby
2
3 # Install the supplied string (or a randomly generated token, if none
4 # is given) as an API token that authenticates to the system user
5 # account.
6 #
7 # Print the token on stdout.
8
9 supplied_token = ARGV[0]
10
11 require File.dirname(__FILE__) + '/../config/boot'
12 require File.dirname(__FILE__) + '/../config/environment'
13
14 include ApplicationHelper
15 act_as_system_user
16
17 if supplied_token
18   api_client_auth = ApiClientAuthorization.
19     where(api_token: supplied_token).
20     first
21   if api_client_auth && !api_client_auth.user.uuid.match(/-000000000000000$/)
22     raise ActiveRecord::RecordNotUnique("Token already exists but is not a superuser token.")
23   end
24 end
25
26 if !api_client_auth
27   api_client_auth = ApiClientAuthorization.
28     new(user: system_user,
29         api_client_id: 0,
30         created_by_ip_address: '::1',
31         api_token: supplied_token)
32   api_client_auth.save!
33 end
34
35 puts api_client_auth.api_token