1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#!/usr/bin/env perl
# Copyright 2015 Red Hat
# Copyright 2015-2020 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
use strict;
use warnings;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";
use OpenQA::Schema::Result::ApiKeys;
use OpenQA::Schema;
use OpenQA::Utils 'random_hex';
use Getopt::Long;
my $email = 'admin@example.com';
my $nickname = 'admin';
my $fullname = 'Administrator';
my $key = "";
my $secret = "";
my $user = $ARGV[0];
my $help;
sub usage {
print "Usage: $0 [options] user \n\n";
print " --email : Email address.\n";
print " --nickname : Nickname.\n";
print " --fullname : Full name.\n";
print " --key : API key (will be randomly generated if not set).\n";
print " --secret : API secret (will be randomly generated if not set).\n";
print " user : User ID (e.g. OpenID URL).\n";
print "usage: $_[0]\n";
exit $_[0];
}
# need to count this *before* calling GetOptions
my $numargs = scalar @ARGV;
my $result = GetOptions(
"email=s" => \$email,
"nickname=s" => \$nickname,
"fullname=s" => \$fullname,
"key=s" => \$key,
"secret=s" => \$secret,
"help" => \$help,
);
usage 0 if $help;
usage 1 unless $result && $user && $numargs > 1;
if (($key || $secret)
&& !($key =~ /^[[:xdigit:]]{16}$/ && $secret =~ /^[[:xdigit:]]{16}$/))
{
die "--key and --secret must both be 16 digit hexadecimals.\n";
}
unless ($key) {
$key = random_hex();
$secret = random_hex();
print "Key: $key\n";
print "Secret: $secret\n";
}
my $schema = OpenQA::Schema::connect_db(deploy => 0, silent => 1, from_script => 1);
my @admins = $schema->resultset('Users')->search({is_admin => 1});
if (scalar @admins != 0) {
warn "An admin user already exists! Use client or web UI to create further users.\n";
exit 1;
}
my $account = $schema->resultset('Users')->create_user(
$user,
email => $email,
nickname => $nickname,
fullname => $fullname,
is_admin => 1
);
$schema->resultset("ApiKeys")->create({user_id => $account->id, key => $key, secret => $secret});
|