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