Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ubuntu Landscape API client c#
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace Ubuntu.Landscape
{
/// <summary>
/// Ubuntu Landscape Client
/// </summary>
class Client
{
/// <summary>
/// Hostname
/// </summary>
public string hostname { get; set; } = "";
/// <summary>
/// Access Key
/// </summary>
public string accessKey { get; set; } = "";
/// <summary>
/// Secret Key
/// </summary>
public string secretKey { get; set; } = "";
/// <summary>
/// Action URL String
/// </summary>
public string actionString { get; set; } = "";
/// <summary>
/// Use HTTPS or HTTP
/// </summary>
protected bool _sslEnabled = true;
/// <summary>
/// sslEnabled property
/// </summary>
public bool sslEnabled
{
get { return _sslEnabled; }
set { _sslEnabled = value; }
}
/// <summary>
/// Ignore invalid SSL Certificates
/// </summary>
protected bool _ignoreInvalidCerts = false;
public bool ignoreInvalidCerts
{
get { return _ignoreInvalidCerts; }
set { _ignoreInvalidCerts = value; }
}
/// <summary>
/// Constructor
/// </summary>
public Client()
{
}
/// <summary>
/// Input check
/// </summary>
/// <returns></returns>
protected bool checkInputs()
{
if (this.hostname.Trim() == "") throw new InputException("Missing host name!");
if (this.accessKey.Trim() == "") throw new InputException("Missing access key!");
if (this.secretKey.Trim() == "") throw new InputException("Missing secret key!");
if (this.actionString.Trim() == "") throw new InputException("Missing action string!");
return true;
}
/// <summary>
/// Remote Certificate Validation Callback
/// </summary>
protected RemoteCertificateValidationCallback getSslFailureCallback()
{
return new RemoteCertificateValidationCallback(delegate { return this.ignoreInvalidCerts; });
}
/// <summary>
/// Calculate Hash
/// </summary>
/// <param name="key"></param>
/// <param name="message"></param>
/// <returns></returns>
protected static byte[] HashHMAC(byte[] key, byte[] message)
{
var hash = new HMACSHA256(key);
return hash.ComputeHash(message);
}
/// <summary>
/// Build Request String
/// </summary>
/// <returns></returns>
protected string requestString()
{
string tmp = "GET\n";
tmp += this.hostname + "\n";
tmp += "/api/\n";
tmp += "access_key_id=" + this.accessKey;
tmp += "&action=" + this.actionString;
tmp += "&signature_method=HmacSHA256";
tmp += "&signature_version=2";
tmp += "&timestamp=" + HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ")).ToUpper();
tmp += "&version=2011-08-01";
return tmp.Substring(tmp.IndexOf("access_key_id")) + "&signature=" + HttpUtility.UrlEncode(Convert.ToBase64String(HashHMAC(Encoding.ASCII.GetBytes(this.secretKey), Encoding.ASCII.GetBytes(tmp))));
}
/// <summary>
/// Get Result Data as String
/// </summary>
/// <returns></returns>
public string getResult()
{
if(this.checkInputs())
{
string retVal = "{}";
using (new ignoreInvalidCerts(this._ignoreInvalidCerts))
{
using (WebClient client = new WebClient())
{
var protocol = (this._sslEnabled) ? "https" : "http";
using (Stream data = client.OpenRead(protocol + "://" + this.hostname + "/api/?" + this.requestString()))
{
using (StreamReader reader = new StreamReader(data))
{
try
{
retVal = reader.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
reader.Close();
}
}
data.Close();
}
}
}
return retVal;
}
return null;
}
}
/// <summary>
/// InputException
/// </summary>
class InputException : Exception
{
public InputException(string message) : base(message)
{
}
}
class ignoreInvalidCerts : IDisposable
{
/// <summary>
/// Ignore invalid SSL Certificates
/// </summary>
protected bool _ignoreInvalidCerts = false;
/// <summary>
/// Constructor
/// </summary>
/// <param name="yesNo"></param>
public ignoreInvalidCerts(bool yesNo)
{
this._ignoreInvalidCerts = yesNo;
if(this._ignoreInvalidCerts)
{
ServicePointManager.ServerCertificateValidationCallback += this.getSslFailureCallback();
}
}
/// <summary>
/// Check whether callback is active
/// </summary>
/// <returns></returns>
private bool hasCallBackDelegation()
{
if(ServicePointManager.ServerCertificateValidationCallback != null)
{
return true;
}
return false;
}
/// <summary>
/// Remote Certificate Validation Callback
/// </summary>
protected RemoteCertificateValidationCallback getSslFailureCallback()
{
return new RemoteCertificateValidationCallback(delegate { return this._ignoreInvalidCerts; });
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
if(this.hasCallBackDelegation())
{
ServicePointManager.ServerCertificateValidationCallback -= this.getSslFailureCallback();
}
}
}
}
@pierrejochem

This comment has been minimized.

Copy link
Owner Author

commented Apr 6, 2016

Usage:

var client = new Ubuntu.Landscape.Client();
client.hostname = "hostname";
client.secretKey = "secret key";
client.accessKey = "access key";
client.ignoreInvalidCerts = true;
client.actionString = "GetComputers";

string JsonOutput = client.getResult();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.