|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use LWP::UserAgent; |
| 4 | +use LWP::Protocol::https; |
| 5 | +print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n"; |
| 6 | +print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n"; |
| 7 | +use JSON; |
| 8 | + |
| 9 | +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; |
| 10 | + |
| 11 | +# |
| 12 | +# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token |
| 13 | +# and use this token in your Authorization header for all subsequent requests. Token validity |
| 14 | +# is fixed at 24 hours |
| 15 | +# |
| 16 | +my $token; |
| 17 | + |
| 18 | +#change this as per your host name |
| 19 | +$fqdn_hostname = "localhost"; |
| 20 | + |
| 21 | +# |
| 22 | +# This script will use the "Enhanced Auditing" feature in NetBackup to create a non-root admin |
| 23 | +# account in NetBackup. This admin account can then be used to invoke REST API requests. |
| 24 | +# |
| 25 | + |
| 26 | +print "\n\n Adding the user\n\n"; |
| 27 | +system q["/usr/openv/netbackup/bin/bpnbat" -addUser testuser Test1234 vx]; |
| 28 | + |
| 29 | +print "Enabling enhanced auditing...\n\n"; |
| 30 | +system q[echo y|"/usr/openv/netbackup/bin/admincmd/bpnbaz" -SetupExAudit]; |
| 31 | + |
| 32 | +print "Granting VxSS user administrator privileges...\n\n"; |
| 33 | +system q["/usr/openv/netbackup/bin/admincmd/bpnbaz" -AddUser vx:vx:testuser]; |
| 34 | + |
| 35 | +print "Add the new user to the EA user list...\n\n"; |
| 36 | +my $auth_file = '/usr/openv/java/auth.conf'; |
| 37 | +open(my $fh, '>>', $auth_file) or die "Could not open auth.conf"; |
| 38 | + say $fh "testuser ADMIN=All JBP=ALL"; |
| 39 | +close $fh; |
| 40 | + |
| 41 | +print "Restarting services..."; |
| 42 | +system q["/usr/openv/netbackup/bin/bp.kill_all"]; |
| 43 | +system q["/usr/openv/netbackup/bin/bp.start_all"]; |
| 44 | + |
| 45 | +# |
| 46 | +# for the sake of this test, ignore ssl certificate |
| 47 | +# |
| 48 | +my $ua = LWP::UserAgent->new( |
| 49 | + timeout => 500, |
| 50 | + ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE }, |
| 51 | +); |
| 52 | + |
| 53 | +my $token_url = "https://$fqdn_hostname:1556/netbackup/login"; |
| 54 | + |
| 55 | +my $req = HTTP::Request->new(POST => $token_url); |
| 56 | +$req->header('content-type' => 'application/json'); |
| 57 | + |
| 58 | +my $post_data = '{ "domainType": "vx", "domainName": "vx", "userName": "testuser", "password": "Test1234" }'; |
| 59 | +$req->content($post_data); |
| 60 | + |
| 61 | +print "**************************************************************"; |
| 62 | +print "\n\n Making Post Request to login to get token \n\n"; |
| 63 | +my $resp = $ua->request($req); |
| 64 | +if ($resp->is_success) { |
| 65 | + my $message = decode_json($resp->content); |
| 66 | + $token = $message->{"token"}; |
| 67 | + print "Received token: $token\n"; |
| 68 | +} |
| 69 | +else { |
| 70 | + print "HTTP POST error code: ", $resp->code, "\n"; |
| 71 | + print "HTTP POST error message: ", $resp->message, "\n"; |
| 72 | + #die; let this fall through to cleanup code |
| 73 | +} |
| 74 | + |
| 75 | +# |
| 76 | +# Here we use the Front End Data report as an example of how to use the token to invoke a |
| 77 | +# REST API request |
| 78 | +# |
| 79 | +my $url = "https://$fqdn_hostname:1556/netbackup/catalog/frontenddata"; |
| 80 | +my $catalog_req = HTTP::Request->new(GET => $url); |
| 81 | +$catalog_req->header('Authorization' => $token); |
| 82 | + |
| 83 | +my $response = $ua->request($catalog_req); |
| 84 | + |
| 85 | +print "**************************************************************"; |
| 86 | +print "\n\n Making Get Request to Catalog/FrontendData with token \n\n"; |
| 87 | +if ($response->is_success) { |
| 88 | + print "/Catalog/frontenddata request was succesful \n\n"; |
| 89 | + |
| 90 | + $data = decode_json($response->content); |
| 91 | + my $pretty = JSON->new->pretty->encode($data); |
| 92 | + print "Received data \n$pretty\n\n\n"; |
| 93 | +} |
| 94 | +else { |
| 95 | + print "HTTP GET error code: ", $response->code, "\n"; |
| 96 | + print "HTTP GET error message: ", $response->message, "\n"; |
| 97 | + #die; let this fall through to cleanup code |
| 98 | +} |
| 99 | + |
| 100 | +# |
| 101 | +# Another example, list jobs... |
| 102 | +# |
| 103 | +my $url = "https://$fqdn_hostname:1556/netbackup/admin/jobs"; |
| 104 | +my $jobs_req = HTTP::Request->new(GET => $url); |
| 105 | +$jobs_req->header('Authorization' => $token); |
| 106 | + |
| 107 | +my $response = $ua->request($jobs_req); |
| 108 | + |
| 109 | +print "**************************************************************"; |
| 110 | +print "\n\n Making Get Request to list all jobs \n\n"; |
| 111 | +if ($response->is_success) { |
| 112 | + print "List jobs request was succesful \n\n"; |
| 113 | + |
| 114 | + $data = decode_json($response->content); |
| 115 | + my $pretty = JSON->new->pretty->encode($data); |
| 116 | + print "Received data \n$pretty\n\n\n"; |
| 117 | +} |
| 118 | +else { |
| 119 | + print "HTTP GET error code: ", $response->code, "\n"; |
| 120 | + print "HTTP GET error message: ", $response->message, "\n"; |
| 121 | + #die; let this fall through to cleanup code |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +# |
| 127 | +# Logging out will cleanup the session and invalidate the token immediately. |
| 128 | +# If you do not log out, the session expires after 24 hours. |
| 129 | +# |
| 130 | +print "**************************************************************"; |
| 131 | +print "\n\nLogout of the REST APIs and cleanup the session(optional)\n"; |
| 132 | + |
| 133 | +my $logout_url = "https://$fqdn_hostname:1556/netbackup/logout"; |
| 134 | +my $logout_req = HTTP::Request->new(POST => $logout_url); |
| 135 | +$logout_req->header('content-type' => $content_type); |
| 136 | +$logout_req->header('Authorization' => $token); |
| 137 | + |
| 138 | +my $resp = $ua->request($logout_req); |
| 139 | +if ($resp->is_success) { |
| 140 | + print "Successfully logged out\n\n"; |
| 141 | +} else { |
| 142 | + print "Failed to logout of the current session\n"; |
| 143 | + print "HTTP POST error code: ", $resp->code, "\n"; |
| 144 | + print "HTTP POST error message: ", $resp->message, "\n"; |
| 145 | + die; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +print "Revoking the user's administrator privileges...\n\n"; |
| 150 | +system q["/usr/openv/netbackup/bin/admincmd/bpnbaz" -DelUser vx:vx:testuser]; |
| 151 | + |
| 152 | +print "Disabling enhanced auditing...\n\n"; |
| 153 | +system q[echo y|"/usr/openv/netbackup/bin/admincmd/bpnbaz" -DisableExAudit]; |
| 154 | + |
| 155 | +print "\n\n Deleting the user\n\n"; |
| 156 | +system q["/usr/openv/netbackup/bin/bpnbat" -RemoveUser testuser vx]; |
| 157 | + |
| 158 | + |
0 commit comments