Accessing Secret Server programmatically - Perl sample script

For instructions with using Perl and Integrated Windows Authentication see: http://support.thycotic.com/KB/a180/using-web-services-with-windows-authentication-perl.aspx

Secret Server Webservices can be called using scripts. This example demonstrates how to authenticate and retrieve a secret programatically in Perl. There is also a more detailed PHP example script.

This is a working example for Secret Server Online; a test user infromation has been filled in and OrganizationCode passed in. The test user has been restricted to only viewing Secrets.

If connecting to an Installed instance change the url to match your site and pass in empty string for organizationCode.

This example runs using standard perl libs with our exact version being Strawberry Perl 5.10.1.1.

------------------------------------------------------------------------------------

use LWP::UserAgent;
use HTTP::Request;
use XML::Parser;

#
#Update these value to match your user settings and instance URL
#
my $username = "ThycoticTest";
my $password= "passwordt";
my $organizationCode= "RT9R";
my $domain = "";
my $WebServiceUrl = "https://www.secretserveronline.com/webservices/SSWebService.asmx";


# The main script calls
my $authtoken = GetToken($username ,$password,$organizationCode,$domain);
my $secretInXML = GetSecret($authtoken, "88395");
print $secretInXML;

# 1) Authentication Token
sub GetToken()
{
 my($username, $password, $organizationCode, $domain) = @_;
 my $url = $WebServiceUrl."/Authenticate?username=$username&password=$password&organization=$organizationCode&domain=$domain";
 my $response = WSRequest($url);
 my $token = "";
 if ($response->is_success){
  my $tokenXML = $response->content;
  if( $tokenXML =~ m/<Token>(.+)<\/Token>/)
  {
   $token = $1;
  }
  if($token eq "")
  {
   die "Login failed";
  }
 }
 else{
  print $response->content;
 }
 return $token;
}

# 2) Load a Secret

Note: The SecretId can be found by going to the SecretView page and in the QueryString will be SecretId=#

sub GetSecret()
{
  my($token, $secretId) = @_;
  my $url = $WebServiceUrl."/GetSecret?token=". $token . "&secretId=" . $secretId;
  my $response = WSRequest($url);
  my $secret;
  if ($response->is_success){
   $secret = $response->content;
  }
  return $secret;
}

sub WSRequest()
{
 my($url) = @_;
 my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
 my $header = HTTP::Request->new(GET => $url);
 my $request = HTTP::Request->new('GET', $url, $header);
 my $response = $agent->request($request);
 return $response;
}

Article ID: 86, Created On: 3/4/2010, Modified: 3/27/2013

Feedback (0)