/* * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using RestSharp; using RestSharp.Authenticators; using System; using System.IO; /** * This DLP REST API client sample. * The API is subjected to ePO authentication and authorization mechanism, clients have to provide ePO username and password (basic authentication), we recommend to use a dedicated ePO user with ePO authentication. * * Before you begin please make sure you enabled the web-services on the DLP Settings page * and verify the user has DLP permissions for Policy Catalog and for DLP Definitions * (go to permission sets module and edit the Data Loss Prevention permissions for the user that is calling the REST API) * * When working with SSL, make sure either the ePO certificate is issued by well-known CA or add the ePO certificate to your trusted CA list. * * In this sample we're using c# (.net 4.5.2) with RestSharp (http://restsharp.org/) * Please use this Sample as a "How-to" communicate with DLP REST API, the full list of APIs is published in the Java sample provided in KB87855 (https://kc.mcafee.com/corporate/index?page=content&id=KB87855) */ namespace DlpRestClient { public class DlpClient { private RestClient client; /** * Constructs DLP REST Client * * @param host ePO host name, ex: myeposerver * @param port ePO port * @param scheme http or https * @param username ePO user * @param password ePO user password */ public DlpClient(string host, int port, string schema, string username, string password) { client = new RestClient(schema + "://" + host + ":" + port); client.Authenticator = new HttpBasicAuthenticator(username, password); } /** * This method sends a request to apply DLP policy * * HTTP POST format: * https://[SERVER ADDRESS]/rest/dlp/policy/apply * * @param policyName name of the policy to apply, the name can include whitespaces and will be escaped by this method * @param forceApply ignore validation warnings (validation errors cannot not ignore) */ public void ApplyPolicy(string policyName, bool forceApply) { RestRequest request = new RestRequest("/rest/dlp/policy/apply", Method.POST); request.AddHeader("Accept", "text/plain"); request.AddParameter("policyName", policyName); request.AddParameter("forceApply", forceApply); ExecuteRequest(request); } /** * This method sends a request to create (or override existing) Plug and Play Device group * * HTTP POST format: * https://[SERVER ADDRESS]/rest/dlp/definitions/pnpDevice/importGroup * * @param name the definition name, if the definition does not exist a new definition will be created * @param description of the definition * @param fileName the local csv file to read the contents of the definition from * @param appliesTo - defines the operating system on which this device template can be used. * Values are either "WIN" for device templates that can be used to detect devices on Windows systems * or "MAC" for device templates that can be used to detect devices on Mac OS X systems * @param overrideExisting if 'true' then override the existing definition, if 'false' and the definition name exist the operation will fail (HTTP return code 400) */ public void ImportPnPDeviceGroup(string name, string description, string fileName, string appliesTo, bool overrideExisting) { RestRequest request = new RestRequest("/rest/dlp/definitions/pnpDevice/importGroup", Method.POST); request.AlwaysMultipartFormData = true; request.AddHeader("Accept", "text/plain"); request.AddHeader("Content-Type", "multipart/form-data"); request.AddParameter("name", name); request.AddParameter("description", description); request.AddParameter("appliesTo", appliesTo); request.AddParameter("override", overrideExisting); request.AddFileBytes("body", File.ReadAllBytes(fileName), "pnpGroup.csv"); ExecuteRequest(request); } private void ExecuteRequest(RestRequest request) { IRestResponse response = client.Execute(request); Console.Out.WriteLine(response.StatusCode); Console.Out.WriteLine(response.Content); } } }