A Extracting Credentials Passed as Parameters (Examples)

Credentials that are passed as parameters to user-defined scripts, are available as an input stream. This chapter contains code samples of scripts. The Bash shell script, Perl script, and the Python scripting language used in the sample codes demonstrate how credential parameters can be extracted.

Note:

The scripts in this chapter are samples only. You should change and adapt them to suit your environment. For example, the number of credentials passed from Oracle Site Guard to the script must exactly match the requirements of the script.

extract_credentials_sample_script.sh

#!/bin/bash 

all_users=
all_passwords=
no_of_users=
no_of_passwords=
 
get_user_name() {
        local index=$(expr $1)
        
        if [ "$no_of_users" -lt $index ]; then
                echo ""
        else
                echo $(echo "$all_users" | awk -v userNameIndex="$index" -F'<<SiteGuard_User>>' '{print $userNameIndex}')
        fi
}
 
get_password() {
        local index=$(expr $1)
        
        if [ "$no_of_passwords" -lt $index ]; then
                echo ""
        else
                echo $(echo "$all_passwords" | awk -v passwordIndex="$index" -F'<<SiteGuard_Password>>' '{print $passwordIndex}')
        fi
}
 
load_credentials() {
        read -s all_credentials
        
        all_users=$(echo "${all_credentials}" | awk -F'<<SiteGuard_Credentials>>' '{print $1}')
        all_passwords=$(echo "${all_credentials}" | awk -F'<<SiteGuard_Credentials>>' '{print $2}')
        no_of_users=$(expr $(echo "$all_users" | awk -F'<<SiteGuard_User>>' '{print NF}'))
        no_of_passwords=$(expr $(echo "$all_passwords" | awk -F'<<SiteGuard_Password>>' '{print NF}'))
 
        if [ "$no_of_users" -ne "$no_of_passwords" ]; then
                echo "INFO: Total no. of users : '$no_of_users'"
                echo "INFO: Total no. of passwords : '$no_of_passwords'"
                echo "ERROR: Number of User Names and number of Passwords do not match"
                exit 1
        else
                echo "Total of '$no_of_users' credentials found"
        fi
}
 
load_credentials
 
userName=$(get_user_name '1')
password=$(get_password '1')
 
echo "[1] UserName : '$userName', Password : '$password'"
 
userName=$(get_user_name '2')
password=$(get_password '2')
 
echo "[2] UserName : '$userName', Password : '$password'"
 
userName=$(get_user_name '3')
password=$(get_password '3')
 
echo "[3] UserName : '$userName', Password : '$password'"
 
userName=$(get_user_name '4')
password=$(get_password '4')
 
echo "[4] UserName : '$userName', Password : '$password'"

extract_credentials_sample_script.py

#!/usr/bin/python

# -*- coding: utf-8 -*-
 
import sys
 
class SiteGuardCredentialUtil(object):
    userNames = passwords = ''
    noOfUsers = noOfPasswords = 0
    credentialNotSet = False
 
    def __init__(self):
        credentialsIO = sys.stdin.readlines()[0]
        
        if credentialsIO :
            credentials = credentialsIO.split('<<SiteGuard_Credentials>>')
            self.userNames = credentials[0].split('<<SiteGuard_User>>')
            self.passwords = credentials[1].split('<<SiteGuard_Password>>')
            self.noOfUsers = len(self.userNames)
            self.noOfPasswords = len(self.passwords)
            self.credentialNotSet = True
            
            if self.noOfUsers != self.noOfPasswords :
                print("INFO: Total no. of users : '%s'"%self.noOfUsers)
                print("INFO: Total no. of passwords : '%s'"%self.noOfPasswords)
                print('ERROR: Number of User Names and number of Passwords do not match')
                sys.exit(1)
            else :
                print("INFO: Total of '%s' credentials found"%self.noOfUsers)
 
    def getCredential(self, credentail):
        if self.credentialNotSet :
            if self.noOfUsers < int(credentail) :
                print("ERROR: Credential not found at index '%s'"%credentail)
                sys.exit(1)
            else :
                credentialIndex = credentail - 1;
                return self.userNames[credentialIndex], self.passwords[credentialIndex]
        else :
            print('WARNING: SiteGuard Credentials not set')
            
            return '', ''
 
def main():
    sgUtil = SiteGuardCredentialUtil()
    myUser, myPassword = sgUtil.getCredential(1)
    print("[1] UserName : '"+ myUser + "', Password : '" + myPassword + "'")
 
    myUser, myPassword = sgUtil.getCredential(2)
    print("[2] UserName : '"+ myUser + "', Password : '" + myPassword + "'")
 
    myUser, myPassword = sgUtil.getCredential(3)
    print("[3] UserName : '"+ myUser + "', Password : '" + myPassword + "'")
    
    myUser, myPassword = sgUtil.getCredential(4)
    print("[4] UserName : '"+ myUser + "', Password : '" + myPassword + "'")
 
 
"""
    Starting point...
"""  
main()

extract_credentials_sample_script.pl

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
our @ALL_USERS     = undef;
our @ALL_PASSWORDS = undef;
 
our $NO_OF_USERS     = 0;
our $NO_OF_PASSWORDS = 0;
 
my $CREDENTIALS = <STDIN>;
 
load_credentials($CREDENTIALS);
 
my $userId1   = get_user_name(1);
my $password1 = get_password(1);
 
print_msg("[1] UserName : '$userId1', Password : '$password1'");
 
my $userId2   = get_user_name(2);
my $password2 = get_password(2);
 
print_msg("[2] UserName : '$userId2', Password : '$password2'");
 
my $userId3   = get_user_name(3);
my $password3 = get_password(3);
 
print_msg("[3] UserName : '$userId3', Password : '$password3'");
 
my $userId4   = get_user_name(4);
my $password4 = get_password(4);
 
print_msg("[4] UserName : '$userId4', Password : '$password4'");
 
sub load_credentials {
        my ($credentials) = @_;
        chomp($credentials);
        if ( length($credentials) <= 0 ) {
                print_msg("WARNING: Credentials not found");
                return '';
        }
        else {
                my @userIds   = split( /<<SiteGuard_Credentials>>/, $credentials );
                my @passwords = split( /<<SiteGuard_Credentials>>/, $credentials );
 
                @ALL_USERS     = split( /<<SiteGuard_User>>/,     $userIds[0] );
                @ALL_PASSWORDS = split( /<<SiteGuard_Password>>/, $passwords[1] );
 
                $NO_OF_USERS     = $#ALL_PASSWORDS + 1;
                $NO_OF_PASSWORDS = $#ALL_PASSWORDS + 1;
 
                if ( "$NO_OF_USERS" != "$NO_OF_PASSWORDS" ) {
                        print_msg("INFO: Total no. of users : '$NO_OF_USERS'");
                        print_msg("INFO: Total no. of passwords : '$NO_OF_PASSWORDS'");
                        print_msg("ERROR: Number of User Names and number of Passwords do not match.");
                        exit 1;
                }
                else {
                        print_msg("Total of '$NO_OF_USERS' credentials found.");
                }
        }
}
 
sub get_user_name {
        my ($index) = @_;
 
        my $userName = "";
 
        if ( "$NO_OF_USERS" > $index - 1 ) {
                $userName = $ALL_USERS[ $index - 1 ];
        }
        else {
                print_msg("ERROR: Credential at index '$index' not found.");
                exit 1;
        }
 
        return $userName;
}
 
sub get_password {
        my ($index) = @_;
 
        my $password = "";
 
        if ( "$NO_OF_PASSWORDS" > $index - 1 ) {
                $password = $ALL_PASSWORDS[ $index - 1 ];
        }
        else {
                print_msg("ERROR: Credential at index '$index' not found.");
                exit 1;
        }
        return $password;
}
 
sub print_msg {
        my ($msg) = @_;
        print("$msg \n");
}