Skip to content

Commit 3241528

Browse files
committed
adding rbac filtering for policy
1 parent 29c9ff4 commit 3241528

File tree

5 files changed

+364
-11
lines changed

5 files changed

+364
-11
lines changed

recipes/perl/api_requests.pl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env perl
22
use LWP::UserAgent;
3+
use JSON;
34

45
my $content_type_v2 = "application/vnd.netbackup+json; version=2.0";
56

@@ -46,11 +47,13 @@ sub perform_login {
4647
if ($resp->is_success) {
4748
my $message = decode_json($resp->content);
4849
$token = $message->{"token"};
50+
print "Login succeeded with status code: ", $resp->code, "\n";
4951
}
5052
else {
5153
print "HTTP POST error code: ", $resp->code, "\n";
5254
print "HTTP POST error message: ", $resp->message, "\n";
5355
}
56+
return $token;
5457
}
5558

5659
# create VMWare policy with the name vmware_test_policy with default values
@@ -174,7 +177,7 @@ sub read_policy {
174177
}
175178

176179
# subroutine to read policy and extract generation number from response
177-
my $genertion;
180+
my $generation;
178181
sub read_policy_extract_Generation_Number_From_Response {
179182
my $policy_name = "vmware_test_policy";
180183
my $url = "$base_url/config/policies/$policy_name";
@@ -366,7 +369,21 @@ sub delete_schedule {
366369
# subroutine to delete policy
367370
sub delete_policy {
368371

369-
my $policy_name = "vmware_test_policy";
372+
my @argument_list = @_;
373+
my $policy_name = $argument_list[0];
374+
375+
# check if the user provides the token to use otherwise
376+
# use the default token created from the perform_login subroutine.
377+
if ($argument_list[1] ne "") {
378+
$token = $argument_list[1];
379+
}
380+
381+
# if the user provides the policyname use that to delete otherwise
382+
# delete the policy named "vmware_test_policy".
383+
if ($policy_name eq "") {
384+
$policy_name = "vmware_test_policy";
385+
}
386+
370387
my $url = "$base_url/config/policies/$policy_name";
371388

372389
my $req = HTTP::Request->new(DELETE => $url);
@@ -378,7 +395,7 @@ sub delete_policy {
378395

379396
my $resp = $ua->request($req);
380397
if ($resp->is_success) {
381-
print "Policy is deleted with status code: ", $resp->code, "\n";
398+
print "Policy [$policy_name] is deleted with status code: ", $resp->code, "\n";
382399
}
383400
else {
384401
print "HTTP DELETE error code: ", $resp->code, "\n";
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
#!/usr/bin/env perl
2+
use LWP::UserAgent;
3+
use JSON;
4+
5+
my $content_type_v2 = "application/vnd.netbackup+json; version=2.0";
6+
7+
my $json = JSON->new;
8+
my $ua = LWP::UserAgent->new(
9+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
10+
);
11+
12+
# create object group to access only VMware policies
13+
my $object_group_id;
14+
sub create_rbac_object_group_for_VMware_policy {
15+
16+
my @argument_list = @_;
17+
my $base_url = $argument_list[0];
18+
my $token = $argument_list[1];
19+
20+
my $url = "$base_url/rbac/object-groups";
21+
my $object_group_name = "VMwarePolicy";
22+
23+
my $req = HTTP::Request->new(POST => $url);
24+
$req->header('content-type' => $content_type_v2);
25+
$req->header('Authorization' => $token);
26+
27+
my $post_data = qq({ "data": { "type": "object-group", "attributes": {
28+
"name": "$object_group_name", "criteria": [
29+
{ "objectCriterion": "policyType eq 40", "objectType": "NBPolicy" } ]} } });
30+
$req->content($post_data);
31+
32+
print "\n\n**************************************************************";
33+
print "\n\n Making POST Request to create object group to access only VMware policies \n\n";
34+
35+
my $resp = $ua->request($req);
36+
if ($resp->is_success) {
37+
my $json_message = decode_json($resp->decoded_content);
38+
$object_group_id = $json_message->{"data"}{"id"};
39+
print "Object group [$object_group_name] is created with id [$object_group_id] to access only VMware policies with status code: ", $resp->code, "\n";
40+
}
41+
else {
42+
print "HTTP POST error code: ", $resp->code, "\n";
43+
print "HTTP POST error message: ", $resp->message, "\n";
44+
}
45+
}
46+
47+
# create access rule for a user with object group
48+
my $access_rule_id;
49+
sub create_rbac_access_rules {
50+
51+
my @argument_list = @_;
52+
my $base_url = $argument_list[0];
53+
my $token = $argument_list[1];
54+
my $user = $argument_list[2];
55+
my $domain = $argument_list[3];
56+
my $domainType = $argument_list[4];
57+
58+
my $url = "$base_url/rbac/access-rules";
59+
60+
my $req = HTTP::Request->new(POST => $url);
61+
$req->header('content-type' => $content_type_v2);
62+
$req->header('Authorization' => $token);
63+
64+
my $post_data = qq({ "data": { "type": "access-rule", "attributes": {
65+
"description": "adding VMwarePolicy object group"}, "relationships": {
66+
"userPrincipal": { "data": { "type" : "user-principal", "id": "$domain:$user:$domainType:$user" } },
67+
"objectGroup": { "data": { "type": "object-group", "id": "$object_group_id" } },
68+
"role": { "data": { "type": "role", "id": "3" } } } } });
69+
$req->content($post_data);
70+
71+
print "\n\n**************************************************************";
72+
print "\n\n Making POST Request to create access rule \n\n";
73+
74+
my $resp = $ua->request($req);
75+
if ($resp->is_success) {
76+
my $json_message = decode_json($resp->decoded_content);
77+
$access_rule_id = $json_message->{"data"}{"id"};
78+
print "Access rule is created with id [$access_rule_id] to access only VMware policies with status code: ", $resp->code, "\n";
79+
}
80+
else {
81+
print "HTTP POST error code: ", $resp->code, "\n";
82+
print "HTTP POST error message: ", $resp->message, "\n";
83+
}
84+
}
85+
86+
# subroutine to delete the object group
87+
sub delete_rbac_object_group_for_VMware_policy {
88+
89+
my @argument_list = @_;
90+
my $base_url = $argument_list[0];
91+
my $token = $argument_list[1];
92+
93+
my $url = "$base_url/rbac/object-groups/$object_group_id";
94+
95+
my $req = HTTP::Request->new(DELETE => $url);
96+
$req->header('content-type' => $content_type_v2);
97+
$req->header('Authorization' => $token);
98+
99+
print "\n\n**************************************************************";
100+
print "\n\n Making DELETE Request to remove the object group \n\n";
101+
102+
my $resp = $ua->request($req);
103+
if ($resp->is_success) {
104+
print "Policy is deleted with status code: ", $resp->code, "\n";
105+
}
106+
else {
107+
print "HTTP DELETE error code: ", $resp->code, "\n";
108+
print "HTTP DELETE error message: ", $resp->message, "\n";
109+
}
110+
}
111+
112+
# subroutine to delete the object group
113+
sub delete_rbac_access_rule {
114+
115+
my @argument_list = @_;
116+
my $base_url = $argument_list[0];
117+
my $token = $argument_list[1];
118+
119+
my $url = "$base_url/rbac/access-rules/$access_rule_id";
120+
121+
my $req = HTTP::Request->new(DELETE => $url);
122+
$req->header('content-type' => $content_type_v2);
123+
$req->header('Authorization' => $token);
124+
125+
print "\n\n**************************************************************";
126+
print "\n\n Making DELETE Request to remove the object group \n\n";
127+
128+
my $resp = $ua->request($req);
129+
if ($resp->is_success) {
130+
print "Policy is deleted with status code: ", $resp->code, "\n";
131+
}
132+
else {
133+
print "HTTP DELETE error code: ", $resp->code, "\n";
134+
print "HTTP DELETE error message: ", $resp->message, "\n";
135+
}
136+
}
137+
138+
# create VMWare policy with the name vmware_test_policy with default values
139+
sub create_bpnbat_user {
140+
141+
my @argument_list = @_;
142+
my $username = $argument_list[0];
143+
my $domainName = $argument_list[1];
144+
my $password = $argument_list[2];
145+
146+
print "\n\n**************************************************************";
147+
print "\n\n Creating user for RBAC filtering using bpnbat \n\n";
148+
149+
if ( $^O =~ /MSWin32/ ) {
150+
my $path = 'C:/\"Program Files\"/Veritas/NetBackup/bin/bpnbat.exe';
151+
my $rc = system(qq($path -AddUser $username $domainName $password)); # returns exit status values
152+
die "system() failed with status $rc" unless $rc == 0;
153+
} else {
154+
my $path = '/usr/openv/netbackup/bin/bpnbat';
155+
my $rc = system(qq($path -AddUser $username $domainName $password)); # returns exit status values
156+
die "system() failed with status $rc" unless $rc == 0;
157+
}
158+
print "\n\n";
159+
}
160+
161+
# create VMWare policy with the name vmware_test_policy with default values
162+
sub create_oracle_policy_with_defaults {
163+
164+
my @argument_list = @_;
165+
my $base_url = $argument_list[0];
166+
my $token = $argument_list[1];
167+
168+
my $url = "$base_url/config/policies";
169+
my $policy_name = "oracle_test_policy";
170+
171+
my $req = HTTP::Request->new(POST => $url);
172+
$req->header('content-type' => $content_type_v2);
173+
$req->header('Authorization' => $token);
174+
175+
my $post_data = qq({ "data": { "type": "policy", "id": "$policy_name", "attributes": {
176+
"policy": { "policyName": "$policy_name", "policyType": "Oracle" } } } });
177+
$req->content($post_data);
178+
179+
print "\n\n**************************************************************";
180+
print "\n\n Making POST Request to create Oracle policy with defaults \n\n";
181+
182+
my $resp = $ua->request($req);
183+
if ($resp->is_success) {
184+
print "Policy [$policy_name] with default values is create with status code: ", $resp->code, "\n";
185+
}
186+
else {
187+
print "HTTP POST error code: ", $resp->code, "\n";
188+
print "HTTP POST error message: ", $resp->message, "\n";
189+
}
190+
}
191+
192+
# create VMWare policy with the name vmware_test_policy with default values
193+
sub create_vmware_policy_with_defaults {
194+
195+
my @argument_list = @_;
196+
my $base_url = $argument_list[0];
197+
my $token = $argument_list[1];
198+
199+
my $url = "$base_url/config/policies";
200+
my $policy_name = "vmware_test_policy";
201+
202+
my $req = HTTP::Request->new(POST => $url);
203+
$req->header('content-type' => $content_type_v2);
204+
$req->header('Authorization' => $token);
205+
206+
my $post_data = qq({ "data": { "type": "policy", "id": "$policy_name", "attributes": {
207+
"policy": { "policyName": "$policy_name", "policyType": "VMware" } } } });
208+
$req->content($post_data);
209+
210+
print "\n\n**************************************************************";
211+
print "\n\n Making POST Request to create VMWare policy with defaults \n\n";
212+
213+
my $resp = $ua->request($req);
214+
if ($resp->is_success) {
215+
print "Policy [$policy_name] with default values is create with status code: ", $resp->code, "\n";
216+
}
217+
else {
218+
print "HTTP POST error code: ", $resp->code, "\n";
219+
print "HTTP POST error message: ", $resp->message, "\n";
220+
}
221+
}
222+
223+
1;

recipes/perl/create_policy_in_one_step.pl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
my $domainType;
2626
my $base_url;
2727

28-
29-
#change this as per your host name
30-
$fqdn_hostname = "localhost";
31-
3228
# subroutines for printing usage and library information required to run the script.
3329
sub print_usage {
3430
print("\n\nUsage:");

recipes/perl/create_policy_step_by_step.pl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
my $domainType;
2626
my $base_url;
2727

28-
29-
#change this as per your host name
30-
$fqdn_hostname = "localhost";
31-
3228
# subroutines for printing usage and library information required to run the script.
3329
sub print_usage {
3430
print("\n\nUsage:");

0 commit comments

Comments
 (0)