CLI によるエージェント操作

2
sm1252sp1jjp
2
リソース保護
ユーザがサイトにログインして、保護されているリソースにアクセスを試みるときに、通常の場合エージェントは以下の質問に答える必要があります。
  • 要求されたリソースは保護されていますか?
  • ユーザはログインに対して認証できますか?
  • リソースにアクセスする権限はユーザに与えられていますか?
以下のスクリプトは、どのようにエージェント API を使用して、これらの基本的なエージェント質問に対処し応答できるかを示しています。
use Netegrity::AgentAPI;
 
#Define script variables
$agent = "agent1";
$secret = "oursecret";
$ip = "127.0.0.1";
$respath = "/mysite/hr/payroll.htm";
$username = "userid";
$pwd = "userpwd";
 
print "\nStep 1. Connecting to Policy Server...\n";
$agentapi = Netegrity::AgentAPI->New($agent, $secret);
$serverconfig = $agentapi->AddServerConfig($ip);
$status=$agentapi->Connect();
die "FATAL: Connect() failed with error code " .
                         $status unless($status==SM_AGENTAPI_YES);
 
$resource = $agentapi->GetResource($respath);  
print "\nStep 2. Is the resource protected?\n";
if ($resource->IsProtected == SM_AGENTAPI_YES) {
   print "Resource ".$respath." is protected.\n\n";
 
   print "\nStep 3. User login...\n";
   $user = $agentapi->CreateUser($username, $pwd);
   print "Logging in user ".$user->Name().".\n";
   $status = $user->Login($resource);
   if($status==SM_AGENTAPI_YES) {
      print $user->Name() . " logged in successfully!\n\n";
 
      print "\nStep 4. User authorized for the resource?\n";
      $status = $user->IsAuthorized($resource);
      if($status==SM_AGENTAPI_YES) {
         print $user->Name()." is authorized for " .
                                                $respath . "\n\n";
      }
      else {
         print $user->Name()." is not authorized for " .
                                                $respath . "\n\n";
      }
   }
   else {
      print "Couldn't log in user " . $username . ".\n\n";
   }
}
else {
      print "Resource ".$respath." is not protected.\n\n";
}
レスポンスとレスポンス属性
エージェントが Login() または IsAuthorized() へのコールを通じて要求を発行した後、要求に対するレスポンスが返されます。ユーザが許可されるかどうかに関係なくレスポンスが返されます。
返されたレスポンス オブジェクトと共に、以下のエージェント API オブジェクトを取得できます。
  • セッション オブジェクト。セッション オブジェクトでは、セッション ID およびタイムアウト値などのセッション情報を取得できます。
  • レスポンス属性オブジェクトを定義します。ポリシー サーバは、エージェントに情報を送信するためにレスポンス属性を使用します。エージェントはその後にクライアント アプリケーションにその情報を渡すことができます。
レスポンス属性の取得
コールのこのシーケンスは、ユーザの識別名、ユーザと関連付けられたディレクトリの名前、およびユーザの認証と関連付けられた任意のテキストなどの既知の属性を取得します。「オンライン エージェント API リファレンス」には、GetAttributes() が取得できる既知の属性のリストがあります。
以下の手順に従います。
  1. AgentUser メソッドの Login() および GetResponse() とコールします。
  2. GetResponse() から返される AgentResponse オブジェクトと共に、GetAttributes() をコールします。
例: レスポンス用の属性の取得
以下のスクリプトは、ログイン要求に対するレスポンス用の属性を取得します。スクリプトはその後に AgentResponseAttr オブジェクトでメソッドをコールし、属性の ID、任意の属性値などの属性情報を表示します。
use Netegrity::AgentAPI;
 
$agentname="agent1";
$ip="127.0.0.1";
$sharedsecret="oursecret";
$username="userid";
$userpwd="userpwd";
 
$agentapi=Netegrity::AgentAPI->New($agentname,$sharedsecret);
#Add Policy Server configuration info...
$serverconfig = $agentapi->AddServerConfig($ip);
$agentapi->Connect();
 
$resource=$agentapi->GetResource("/mysite/hr/payroll.htm");
# Test whether the resource is protected. If it is,
# log in the user and get the attributes of the response.
if($resource->IsProtected() == SM_AGENTAPI_YES) {
   $user = $agentapi->CreateUser($username,$userpwd);
   print "\nLogging in user ".$user->Name()."...\n";
   $status=$user->Login($resource);
   if($status==SM_AGENTAPI_YES) {
      $response=$user->GetResponse();           
      @attr = $response->GetAttributes();
      foreach $attr(@attr) {
         print "\nAttribute ID = " . $attr->GetID()."\n";
         print "TTL = " . $attr->GetTTL()."\n";
         print "Value = " . $attr->GetValue()."\n";
         print "Name = " . $attr->GetName()."\n";
      }
   }
}
else {
   print "\nThe resource is not protected.\n\n";
}
セッション管理
AgentSession オブジェクトを取得した後、セッション管理操作を実行できます。セッション タイムアウト値、セッション ID およびセッション指定情報などのセッションに関する情報を取得できます。
シングル サインオン操作の場合のように、複数のサイトにわたってセッションを識別するために
セッション指定情報
を使用できます。また、GetReason() をコールして、失敗した認証または許可試行の理由コードを取得できます。
例: ユーザのログイン
以下の例ではユーザをログインして、ログイン試行に対するレスポンスを取得し、ユーザのセッションのセッション オブジェクトを取得して、セッションに関するさまざまな詳細を出力します。
use Netegrity::AgentAPI;
 
#Define script variables
$agent = "agent1";
$secret = "oursecret";
$ip = "127.0.0.1";
$respath = "/mysite/hr/payroll.htm";
$username = "userid";
$pwd = "userpwd";
 
#Establish the connection and create needed objects
$agentapi = Netegrity::AgentAPI->New($agent, $secret);
$serverconfig = $agentapi->AddServerConfig($ip);
$agentapi->Connect();
$user = $agentapi->CreateUser($username, $pwd);
print "Logging in user ".$user->Name().".\n";
$resource = $agentapi->GetResource($respath);  
#Log in the user
$status = $user->Login($resource);
if($status==SM_AGENTAPI_YES) {
   print $user->Name() . " logged in successfully!\n\n";
   #Get the login response
   $response=$user->GetResponse();
   #Get the session object
   $session=$response->GetSession();
   if ($session != undef) {
      print "Printing session details:\n";
      print "Session reason=".$session->GetReason()."\n";
      print "Session IdleTimeout=".$session->IdleTimeout()."\n";
      print "Session Maxtimeout=".$session->MaxTimeout()."\n";
      print "Session ID=".$session->GetID()."\n";
      print "Session specification=".$session->GetSpec()."\n\n";
   }
}
else {
      print "Couldn't log in user " . $username . "\n\n";
}
ポリシー サーバ コマンド
管理プロトコルはエージェントとポリシー サーバの間に存在します。エージェントは、ポリシー サーバ上のポリシーおよび管理上の変更と一致するようにそのキャッシュと暗号化キーを管理するためにこのプロトコルを使用します。
対応するエージェントの変更を必要とするポリシー サーバの変更が行われると、ポリシー サーバは、エージェントで変更を行うためのコマンドを発行します。DoManagement() をコールして、これらのコマンドを取得できます。DoManagement() は、AgentResponseAttr オブジェクトの配列を返します。ポリシー サーバから保留中の場合がある管理コマンドを取得するためにこのオブジェクトで GetID() をコールします。
以下のスクリプトは保留中の管理コマンドについて確認します。コマンドが見つかると、スクリプトは以下のように各コマンド ID、および必要に応じてコマンドと関連付けられた値を出力します。
use Netegrity::AgentAPI;
 
$agentname="agent1";
$ip="127.0.0.1";
$secret="oursecret";
 
$agentapi = Netegrity::AgentAPI->New($agentname,$secret);
$serverconfig = $agentapi->AddServerConfig($ip);
$agentapi->Connect();
 
@attr = $agentapi->DoManagement();
 
if (@attr == undef) {
   print "No commands are pending from the Policy Server.";
}
else {
   print "IDs of commands pending from the Policy Server:";
   foreach $attr(@attr) {
      if ($attr->GetID()==SM_AGENTAPI_AFFILIATE_KEY_UPDATE ||
         $attr->GetID()==SM_AGENTAPI_CACHE_FLUSH_THIS_USER ||
         $attr->GetID()==SM_AGENTAPI_CACHE_FLUSH_THIS_REALM) {
         print "\n Command ID: " . $attr->GetID();
         print "\n \tValue: " . $attr->GetValue();
      }
      else {
         #No need to print values for the IDs below. Value
         #will contain either binary data or no data.
         print "\nCommand ID: " . $attr->GetID();
      }
   }
}