]> git.defcon.no Git - hermes/blob - guc-clients/add_apikey.pl
Merge branch 'master' of http://git.defcon.no/hermes
[hermes] / guc-clients / add_apikey.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use LWP;
5 use Data::Dumper;
6 use JSON;
7 use Getopt::Long;
8 use AppConfig;
9
10 my $help = 0;
11 my $host_ip = "";
12 my $level = "";
13 my $keyfile ="";
14 my $api_base = "";
15 my $api_key = "";
16 my $configfile;
17
18 my $config = AppConfig->new( { CREATE => 1 } );
19
20 $config->define("api_url=s");
21 $config->define("api_keyfile=s");
22
23 GetOptions(
24 "help" => \$help,
25 "host=s" => \$host_ip,
26 "access=s" => \$level,
27 "configfile=s" => \$configfile,
28 );
29
30 if (
31 (not $configfile) ||
32 (( $configfile ) && ( not -f $configfile ))
33 )
34 {
35 $help = 1;
36 }
37 else
38 {
39 $config->file( $configfile );
40 $api_base = $config->api_url;
41 $keyfile = $config->api_keyfile;
42 }
43 if (
44 ( ( $level ne "limited_read" ) &&
45 ( $level ne "full_read" ) &&
46 ( $level ne "read_write" ) ) ||
47 ( not $api_base ) ||
48 ( not $api_base =~ m/^http:\/\/\w+/ ) ||
49 ( not $keyfile ) ||
50 ( not -f $keyfile )
51 ) {
52 $help = 1;
53 }
54
55 if ( $help )
56 {
57 print <<END_HELP;
58 $0 invalid usage.
59 --help Show help
60
61 Required parameters:
62
63 --host=host_ip_addr
64 --access=limited_read|full_read|read_write
65 --configfile=/path/to/config/file (--config, -c)
66
67 (c) 2012, Gjøvik University College, jonl. Licence: MIT.
68 END_HELP
69 exit 0;
70 }
71
72
73 open KEY, "<" . $keyfile;
74 chomp( $api_key = <KEY> );
75 close KEY;
76
77 my ($g_ua, $session, $auth_key, $data);
78
79 $g_ua = LWP::UserAgent->new;
80 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
81
82 login_apikey();
83
84 $data = exec_apinode('auth/new_apikey', { 'host_ip' => $host_ip, 'access' => $level });
85 print Dumper($data);
86 undef $data;
87
88 logout();
89 ################################################################################################
90 sub exec_apinode($$)
91 {
92 my $node = shift;
93 my $param = shift;
94
95 my ( $response, $data );
96
97 $session = "" if not defined $session;
98 $auth_key = "" if not defined $auth_key;
99 my $url = $api_base . "/" . $node;
100
101 $param->{'session'} = $session;
102 $param->{'auth_key'} = $auth_key;
103
104 $response = $g_ua->post( $url, $param );
105 if ( $response->is_success )
106 {
107 if ( $response->content =~ m/\s*{/ )
108 {
109 $data = decode_json( $response->content);
110 }
111 else
112 {
113 $data = $response->content;
114 }
115
116 }
117 return $data;
118 }
119
120 sub login_apikey
121 {
122 my $response = $g_ua->post( $api_base . "/auth/login",
123 [ "api_key" => $api_key ] );
124
125 my $data = decode_json( $response->content) if $response->is_success;
126 die("HTTP error") unless $response->is_success;
127
128 if ( $data->{'response'} eq "ok" )
129 {
130 $session = $data->{'session'};
131 $auth_key = $data->{'auth_key'};
132 }
133 else
134 {
135 exit;
136 }
137 undef $data; undef $response;
138 }
139
140 sub logout
141 {
142 my $response = $g_ua->post( $api_base . "/auth/logout",
143 [ "session" => $session ] );
144 die("HTTP error") unless $response->is_success;
145 undef $session; undef $auth_key;
146 }