Skip to content

Commit bf694a4

Browse files
Merge pull request #31 from VeritasOS/feature/apikeys
How to create, delete and use NetBackup API Key sample snippets
2 parents c850f79 + bf85ec4 commit bf694a4

19 files changed

+1537
-0
lines changed

snippets/curl/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ These scripts are only meant to be used as a reference. If you intend to use the
99
#### Pre-requisites:
1010

1111
- NetBackup 8.1.1 or higher
12+
- NetBackup 8.2 or higher for using API keys related APIs and samples
1213
- curl 7.51.0 or higher
1314
- jq command-line parser (https://github.com/stedolan/jq/releases)
1415

@@ -17,3 +18,17 @@ These scripts are only meant to be used as a reference. If you intend to use the
1718
Use the following commands to run the curl samples.
1819
- `./get_nb_jobs.sh -nbmaster <master_server> -username <username> -password <password> -domainname <dname> -domaintype <unixpwd/nt>`
1920
- `./get_nb_images.sh -nbmaster <master_server> -username <username> -password <password> -domainname <dname> -domaintype <unixpwd/nt>`
21+
22+
#### Scripts for NetBackup 8.2 or higher
23+
24+
- Use the following command to create an API key for yourself on your NetBackup Master server:
25+
- `./apikey_create.sh -nbmaster <master_server> -login_username <login_username> -login_password <login_password> -login_domainname <login_domainname> -login_domaintype <login_domaintype> -expiryindays <expiryindays> -description <description>`
26+
27+
- Use the following command to create an API key for other user on your NetBackup Master server:
28+
- `./apikey_create.sh -nbmaster <master_server> -login_username <login_username> -login_password <login_password> -login_domainname <login_domainname> -login_domaintype <login_domaintype> -apikey_username <apikey_username> [-apikey_domainname <apikey_domain_name>] -apikey_domaintype <apikey_domaintype> -expiryindays <expiry_in_days> -description <description>`
29+
30+
- Use the following command to delete an API key on your NetBackup Master server with apikey tag provided:
31+
- `./apikey_delete.sh -nbmaster <master_server> -login_username <login_username> -login_password <login_password> -login_domainname <login_domainname> -login_domaintype <login_domaintype> -apikey_tag <apikey_tag>`
32+
33+
Use the following command to use API key instead of JWT to trigger a NetBackup REST API on your NetBackup Master server:
34+
- `./apikey_usage.sh -nbmaster <master_server> -apikey <apikey>`

snippets/curl/apikey_create.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/bin/sh
2+
3+
#####################n#####################################################
4+
5+
# This script demonstrates how to create API key for a user (self/others). To
6+
# create API key for other user, a user needs to have proper permissions.
7+
8+
# This script requires jq command-line JSON parser
9+
# if your system does not have jq installed, this will not work.
10+
# jq can be downloaded from here: https://github.com/stedolan/jq/releases
11+
12+
###########################################################################
13+
14+
port=1556
15+
master_server=""
16+
login_username=""
17+
login_password=""
18+
login_domainname=""
19+
login_domaintype=""
20+
apikey_username=""
21+
apikey_domainname=""
22+
apikey_domaintype=""
23+
expiryindays=""
24+
description=""
25+
apikey_other_user=0
26+
27+
showHelp()
28+
{
29+
echo ""
30+
echo "Invalid command parameters"
31+
echo "Usage:"
32+
echo "./apikey_create.sh -nbmaster <master_server> -login_username <login_username> -login_password <login_password> -login_domainname <login_domain_name> -login_domaintype <login_domaintype> [-apikey_username <apikey_username> [-apikey_domainname <apikey_domain_name>] -apikey_domaintype <apikey_domaintype>] -expiryindays <expiry_in_days> -description <description>"
33+
echo "-nbmaster : Name of the NetBackup master server"
34+
echo "-login_username : User name of the user performing action"
35+
echo "-login_password : Password of the user performing action"
36+
echo "-login_domainname : Domain name of the user performing action"
37+
echo "-login_domaintype : Domain type of the user performing action"
38+
echo "-apikey_username : (Optional) User name of the user for whom API key needs to be generated. Optional in case API key is to be generated for self"
39+
echo "-apikey_domainname : Domain name of the user for whom API key needs to be generated. Optional in case API key is to be generated for self. Do not specify this parameter if -apikey_domaintype parameter is 'unixpwd'"
40+
echo "-apikey_domaintype : Domain type of the user for whom API key needs to be generated. Optional in case API key is to be generated for self"
41+
echo "-expiryindays : Number of days from today after which API key should expire"
42+
echo "-description : A textual description to be associated with API key"
43+
echo ""
44+
exit 1
45+
}
46+
47+
parseArguments()
48+
{
49+
if [ $# -ne 14 ] && [ $# -ne 18 ] && [ $# -ne 20 ]; then
50+
showHelp
51+
fi
52+
53+
while [ "$1" != "" ]; do
54+
case $1 in
55+
-nbmaster)
56+
master_server=$2
57+
;;
58+
-login_username)
59+
login_username=$2
60+
;;
61+
-login_password)
62+
login_password=$2
63+
;;
64+
-login_domainname)
65+
login_domainname=$2
66+
;;
67+
-login_domaintype)
68+
login_domaintype=$2
69+
;;
70+
-apikey_username)
71+
apikey_username=$2
72+
apikey_other_user=1
73+
;;
74+
-apikey_domainname)
75+
apikey_domainname=$2
76+
apikey_other_user=1
77+
;;
78+
-apikey_domaintype)
79+
apikey_domaintype=$2
80+
apikey_other_user=1
81+
;;
82+
-expiryindays)
83+
expiryindays=$2
84+
;;
85+
-description)
86+
description=$2
87+
;;
88+
*)
89+
showHelp
90+
;;
91+
esac
92+
shift 2
93+
done
94+
95+
if [ -z "$master_server" ] || [ -z "$login_username" ] || [ -z "$login_password" ] || [ -z "$login_domainname" ] || [ -z "$login_domaintype" ] || [ -z "$expiryindays" ] || [ -z "$description" ]; then
96+
showHelp
97+
fi
98+
99+
if [ $apikey_other_user -eq 1 ]; then
100+
if [ -z "$apikey_username" ] || [ -z "$apikey_domaintype" ]; then
101+
showHelp
102+
fi
103+
fi
104+
105+
if [ "${login_domaintype^^}" = "WINDOWS" ] || [ "${login_domaintype^^}" = "NT" ]; then
106+
login_domaintype="nt"
107+
fi
108+
109+
if [ "${apikey_domaintype^^}" = "WINDOWS" ] || [ "${apikey_domaintype^^}" = "NT" ]; then
110+
apikey_domaintype="nt"
111+
fi
112+
}
113+
114+
###############main############
115+
116+
parseArguments "$@"
117+
118+
basepath="https://$master_server:$port/netbackup"
119+
content_header='content-type:application/json'
120+
121+
##############login#############
122+
123+
uri="$basepath/login"
124+
125+
data=$(jq --arg name $login_username --arg pass $login_password --arg dname $login_domainname --arg dtype $login_domaintype \
126+
--null-input '{userName: $name, password: $pass, domainName: $dname, domainType: $dtype}')
127+
128+
jwt=$(curl --silent -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token')
129+
130+
##############jobs##############
131+
132+
auth_header="authorization:$jwt"
133+
content_header='content-type:application/vnd.netbackup+json; version=3.0'
134+
uri="$basepath/security/api-keys"
135+
136+
# Construct request body
137+
request_body="{"
138+
request_body="${request_body}\"data\": {"
139+
request_body="${request_body}\"type\": \"apiKeyCreationRequest\","
140+
request_body="${request_body}\"attributes\": {"
141+
request_body="${request_body}\"description\" : \"${description}\","
142+
request_body="${request_body}\"expireAfterDays\": \"P${expiryindays}D\""
143+
if [ $apikey_other_user == 1 ]; then
144+
request_body="${request_body},\"userName\": \"${apikey_username}\","
145+
request_body="${request_body}\"userDomain\": \"${apikey_domainname}\","
146+
request_body="${request_body}\"userDomainType\": \"${apikey_domaintype}\""
147+
fi
148+
request_body="${request_body}}}}"
149+
150+
curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -d "$request_body" | jq
151+
152+
exit 0

snippets/curl/apikey_delete.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/sh
2+
3+
#####################n#####################################################
4+
5+
# This script demonstrates how to delete API key of a user (self/others). To
6+
# delete API key for other user, a user needs to have proper permissions.
7+
8+
# This script requires jq command-line JSON parser
9+
# if your system does not have jq installed, this will not work.
10+
# jq can be downloaded from here: https://github.com/stedolan/jq/releases
11+
12+
###########################################################################
13+
14+
port=1556
15+
master_server=""
16+
login_username=""
17+
login_password=""
18+
login_domainname=""
19+
login_domaintype=""
20+
apikey_tag=""
21+
22+
showHelp()
23+
{
24+
echo ""
25+
echo "Invalid command parameters"
26+
echo "Usage:"
27+
echo "./apikey_delete.sh -nbmaster <master_server> -login_username <login_username> -login_password <login_password> -login_domainname <login_domainname> -login_domaintype <login_domaintype> -apikey_tag <apikey_tag>"
28+
echo "-nbmaster : Name of the NetBackup master server"
29+
echo "-login_username : User name of the user performing action"
30+
echo "-login_password : Password of the user performing action"
31+
echo "-login_domainname : Domain name of the user performing action"
32+
echo "-login_domaintype : Domain type of the user performing action"
33+
echo "-apikey_tag : Tag associate with API key to be deleted"
34+
echo ""
35+
exit 1
36+
}
37+
38+
parseArguments()
39+
{
40+
if [ $# -ne 12 ]; then
41+
showHelp
42+
fi
43+
44+
while [ "$1" != "" ]; do
45+
case $1 in
46+
-nbmaster)
47+
master_server=$2
48+
;;
49+
-login_username)
50+
login_username=$2
51+
;;
52+
-login_password)
53+
login_password=$2
54+
;;
55+
-login_domainname)
56+
login_domainname=$2
57+
;;
58+
-login_domaintype)
59+
login_domaintype=$2
60+
;;
61+
-apikey_tag)
62+
apikey_tag=$2
63+
;;
64+
*)
65+
showHelp
66+
;;
67+
esac
68+
shift 2
69+
done
70+
71+
if [ -z "$master_server" ] || [ -z "$login_username" ] || [ -z "$login_password" ] || [ -z "$login_domainname" ] || [ -z "$login_domaintype" ] || [ -z "$apikey_tag" ]; then
72+
showHelp
73+
fi
74+
75+
if [ "${login_domaintype^^}" = "WINDOWS" ] || [ "${login_domaintype^^}" = "NT" ]; then
76+
login_domaintype="nt"
77+
fi
78+
}
79+
80+
###############main############
81+
82+
parseArguments "$@"
83+
84+
basepath="https://$master_server:$port/netbackup"
85+
content_header='content-type:application/json'
86+
87+
##############login#############
88+
89+
uri="$basepath/login"
90+
91+
data=$(jq --arg name $login_username --arg pass $login_password --arg dname $login_domainname --arg dtype $login_domaintype \
92+
--null-input '{userName: $name, password: $pass, domainName: $dname, domainType: $dtype}')
93+
94+
jwt=$(curl --silent -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token')
95+
96+
##############jobs##############
97+
98+
auth_header="authorization:$jwt"
99+
content_header='content-type:application/vnd.netbackup+json; version=3.0'
100+
uri="$basepath/security/api-keys/$apikey_tag"
101+
102+
curl --silent -k -X DELETE "$uri" -H "$content_header" -H "$auth_header" | jq
103+
104+
exit 0

snippets/curl/apikey_usage.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/sh
2+
3+
4+
#####################n#####################################################
5+
6+
# This script demonstrates the usage of API key in NetBackup REST API for listing the jobs
7+
8+
# This script requires jq command-line JSON parser
9+
# if your system does not have jq installed, this will not work
10+
# jq can be downloaded from here: https://github.com/stedolan/jq/releases
11+
12+
###########################################################################
13+
14+
port=1556
15+
master_server=""
16+
apikey=""
17+
18+
showHelp()
19+
{
20+
echo ""
21+
echo "Invalid command parameters"
22+
echo "Usage:"
23+
echo "./apikey_usage.sh -nbmaster <master_server> -apikey <apikey>"
24+
echo "-nbmaster : Name of the NetBackup master server"
25+
echo "-apikey : API key to be used instead of JWT"
26+
echo ""
27+
exit 1
28+
}
29+
30+
parseArguments()
31+
{
32+
if [ $# -lt 4 ]; then
33+
showHelp
34+
fi
35+
36+
while [ "$1" != "" ]; do
37+
case $1 in
38+
-nbmaster)
39+
master_server=$2
40+
;;
41+
-apikey)
42+
apikey=$2
43+
;;
44+
*)
45+
showHelp
46+
;;
47+
esac
48+
shift 2
49+
done
50+
51+
if [ -z "$master_server" ] || [ -z "$apikey" ]; then
52+
showhelp
53+
fi
54+
}
55+
56+
###############main#############
57+
58+
parseArguments "$@"
59+
60+
basepath="https://$master_server:$port/netbackup"
61+
content_header='content-type:application/vnd.netbackup+json; version=3.0'
62+
63+
##############jobs##############
64+
65+
auth_header="authorization:$apikey"
66+
uri="$basepath/admin/jobs"
67+
68+
echo "Using API key [$apikey] instead of JWT token to trigger job REST API"
69+
curl --insecure --request GET --globoff --get "$uri" -H "$content_header" -H "$auth_header" \
70+
| \
71+
jq '[.data[]|{JOBID: .id, TYPE: .attributes.jobType, STATE: .attributes.state, STATUS: .attributes.status}]'
72+
exit 0

snippets/perl/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ These scripts are only meant to be used as a reference. If you intend to use the
99
#### Pre-requisites:
1010

1111
- NetBackup 8.1.1 or higher
12+
- NetBackup 8.2 or higher for using API keys related APIs and samples
1213
- Perl v5.18.2
1314
- Perl modules Text::Table, JSON and LWP
1415

@@ -111,3 +112,17 @@ NetBackup Resource Limits Template:
111112

112113
- Use the following command to obtain the NetBackup resource limits template from your NetBackup Master server:
113114
- `perl get_resource_limits_template.pl -nbmaster <master_server> -username <username> -password <password> -workloadtype <workloadtype> [-domainname <domain_name>] [-domaintype <domain_type>] [--verbose]`
115+
116+
API key Details:
117+
118+
- Use the following command to create an API key for yourself on your NetBackup Master server:
119+
- `perl apikey_create.pl -nbmaster <master_server> -login_username <username> -login_password <password> [-login_domainname <domain_name> -login_domaintype <domain_type>] -expiryindays <expiryindays> -description <description> [--verbose]`
120+
121+
- Use the following command to create an API key for other user on your NetBackup Master server:
122+
- `perl apikey_create.pl -nbmaster <master_server> -login_username <login_username> -login_password <login_password> [-login_domainname <login_domainname> -login_domaintype <login_domaintype>] -apikey_username <apikey_username> -apikey_domainname <apikey_domainname> -apikey_domaintype <apikey_domaintype> -expiryindays <expiryindays> -description <description> [--verbose]`
123+
124+
- Use the following command to delete an API key on your NetBackup Master server with apikey tag provided:
125+
- `perl apikey_delete.pl -nbmaster <master_server> -login_username <username> -login_password <password> [-login_domainname <domain_name> -login_domaintype <domain_type>] -apikey_tag <apikey_tag> [--verbose]`
126+
127+
- Use the following command to use API key instead of JWT to trigger a NetBackup REST API on your NetBackup Master server:
128+
- `perl apikey_usage.pl -nbmaster <master_server> -apikey <apikey> [--verbose]`

0 commit comments

Comments
 (0)