Quantcast
Channel: Symantec Connect - Articles
Viewing all articles
Browse latest Browse all 1863

Workflow - How To Create Authenticated REST Components

$
0
0

We will be creating (3) SEP Manager 14.x REST API Service Components.

  1. Version
  2. Authenticate
  3. Groups

Workflow includes a plethora of SEP Components (Symantec.Components.SEP.dll); This technique can be applied to any REST Service.

Summary of Work

  1. Using your favorite 3rd party REST Interrogator (Postman), capture the Response Body JSON for the given service call.
  2. Create a Workflow REST Generator Component using the captured JSON as sample Response Body content.
  3. Use a Code (Scripting) Component to output an Object as defined by your REST Component.
  • Required C# Assemblies

Workflow 8.x includes a copy of Json.NET in: Workflow\Shared\lib\Newtonsoft.Json.dll

One can also find it here: https://www.newtonsoft.com/json

This (Json.NET) and your REST Generator assemblies need to be included with your project.

SEPM Version

This example does not require a REST Generator Data Type since the data returned is not that complicated.

NOTE: You will likely need to add the REST Service Certificate to the Workflow Server's 'Trusted Root Certification Authorities' Certificate store.

Here is the variable definition for the 'Code (Scripting) Component':

Here is the source code:

System
System.IO
System.Net


ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
        | SecurityProtocolType.Tls11
        | SecurityProtocolType.Tls12
        | SecurityProtocolType.Ssl3;
WebRequest request = WebRequest.Create( "https://sepm.your.domain:8446/sepm/api/v1/version" );
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader (dataStream);string responseFromServer = reader.ReadToEnd();return responseFromServer;

Here is the data:

SEPM Authenticate

In this example, the REST Generator Component is configured with:

NamespaceSepmRestAuthenticate
Response Body Object TypeAuthenticateResponse

Here is the variable definition for the 'Code (Scripting) Component':

Here is the source code:

System
System.IO
System.Net
Newtonsoft.Json
Newtonsoft.Json.Linq
SepmRestAuthenticate


ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
        | SecurityProtocolType.Tls11
        | SecurityProtocolType.Tls12
        | SecurityProtocolType.Ssl3;
AuthenticateResponse sepmAuth = new AuthenticateResponse();try
{
        WebRequest request = WebRequest.Create("https://sepm.your.domain:8446/sepm/api/v1/identity/authenticate");
        request.ContentType = "application/json";
        request.Method = "POST";using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {string json = @"{
                        username: 'admin',
                        password: 'password',
                        domain: ''
                }";
                JObject o = JObject.Parse(json);
                streamWriter.Write(o);
        }
        WebResponse response = request.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        sepmAuth = JsonConvert.DeserializeObject<AuthenticateResponse>(streamReader.ReadToEnd());
}catch (Exception e)
{// Hijack 'bannerText' for Exception message
        sepmAuth.bannerText = e.Message;
}return sepmAuth;

Here is the data:

SEPM Groups

In this example, the REST Generator Component is configured with:
 

NamespaceSepmRestGroups
Response Body Object TypeGroupsResponse

Here is the input mapping for the 'Code (Scripting) Component':

NOTE: The 'token' variable is mapped from the previous example: SepmAuthenticated.token

Here is the variable definition:

Here is the source code:
 

System
System.IO
System.Net
Newtonsoft.Json
SepmRestGroups


ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
	| SecurityProtocolType.Tls11
	| SecurityProtocolType.Tls12
	| SecurityProtocolType.Ssl3;
GroupsResponse sepmGroups = new GroupsResponse();try {
	WebRequest request = WebRequest.Create("https://sepm.your.domain:8446/sepm/api/v1/groups");
	request.Headers.Add("Authorization", "Bearer " + token);
	request.Method = "GET";
	WebResponse response = request.GetResponse();
	StreamReader streamReader = new StreamReader(response.GetResponseStream());
	sepmGroups = JsonConvert.DeserializeObject<GroupsResponse>(streamReader.ReadToEnd());
}catch (Exception)
{// Hijack 'size' for Exception... most likely: JSON integer <value> is too large or small// Changed the following from (Number Int), located in the SEPM->Groups Request Response configuration (REST Generator).//// GroupsResponse//   |-GroupsResponse_content//     |-created(Number Long)//     |-lastModified(Number Long)//     |-policyDate(Number Long)

	sepmGroups.size = -1;
}return sepmGroups;

Here is the data:

DEBUG:

When manually adding JSON to the REST Generator Response Configuration, numbers are assumed to be 32bit Integers.
In the SEPM Groups example, the following had to be changed:

GroupsResponse
  |- GroupsResponse_content
    |- created(Number Long)
    |- lastModified(Number Long)
    |- policyDate(Number Long)
 


Viewing all articles
Browse latest Browse all 1863

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>