|
| 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 |
0 commit comments