2015-06-02 15:48:59 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using XenAdmin.Network;
|
2015-05-18 16:23:32 +02:00
|
|
|
|
using XenAPI;
|
2015-06-02 15:48:59 +02:00
|
|
|
|
using System.Web.Script.Serialization;
|
2015-05-18 16:23:32 +02:00
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2015-06-26 00:51:11 +02:00
|
|
|
|
public class CallHomeAuthenticationAction : AsyncAction
|
2015-05-18 16:23:32 +02:00
|
|
|
|
{
|
2015-06-02 15:48:59 +02:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2015-05-18 16:23:32 +02:00
|
|
|
|
private readonly Pool pool;
|
2015-06-02 15:48:59 +02:00
|
|
|
|
private readonly string username;
|
2015-06-26 00:51:11 +02:00
|
|
|
|
private readonly string password;
|
|
|
|
|
|
|
|
|
|
private readonly bool saveTokenAsSecret;
|
|
|
|
|
private readonly long tokenExpiration;
|
|
|
|
|
|
|
|
|
|
private string uploadToken;
|
|
|
|
|
|
|
|
|
|
private const string identityTokenUrl = "/auth/api/create_identity/";
|
|
|
|
|
private const string uploadGrantTokenUrl = "/feeds/api/create_grant/";
|
|
|
|
|
private const string uploadTokenUrl = "/feeds/api/create_upload/";
|
|
|
|
|
|
2015-06-02 15:48:59 +02:00
|
|
|
|
private readonly string identityTokenDomainName = "http://cis-daily.citrite.net";
|
|
|
|
|
private readonly string uploadGrantTokenDomainName = "https://rttf-staging.citrix.com";
|
|
|
|
|
private readonly string uploadTokenDomainName = "https://rttf-staging.citrix.com";
|
2015-06-26 00:51:11 +02:00
|
|
|
|
|
2015-06-02 15:48:59 +02:00
|
|
|
|
private const string productKey = "eb1b224c461038baf1f08dfba6b8d4b4413f96c7";
|
2015-05-18 16:23:32 +02:00
|
|
|
|
|
2015-06-26 00:51:11 +02:00
|
|
|
|
public CallHomeAuthenticationAction(Pool pool, string username, string password, bool saveTokenAsSecret, long tokenExpiration, bool suppressHistory)
|
|
|
|
|
: base(pool != null ? pool.Connection : null, Messages.ACTION_CALLHOME_AUTHENTICATION, Messages.ACTION_CALLHOME_AUTHENTICATION_PROGRESS, suppressHistory)
|
2015-05-18 16:23:32 +02:00
|
|
|
|
{
|
|
|
|
|
this.pool = pool;
|
2015-06-02 15:48:59 +02:00
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
2015-06-26 00:51:11 +02:00
|
|
|
|
this.saveTokenAsSecret = saveTokenAsSecret;
|
|
|
|
|
this.tokenExpiration = tokenExpiration;
|
|
|
|
|
#region RBAC Dependencies
|
|
|
|
|
if (saveTokenAsSecret)
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pool.set_health_check_config");
|
|
|
|
|
#endregion
|
|
|
|
|
|
2015-05-18 16:23:32 +02:00
|
|
|
|
}
|
2015-06-02 15:48:59 +02:00
|
|
|
|
|
2015-06-26 00:51:11 +02:00
|
|
|
|
public CallHomeAuthenticationAction(Pool pool, string username, string password,
|
|
|
|
|
string identityTokenDomainName, string uploadGrantTokenDomainName, string uploadTokenDomainName, bool saveTokenAsSecret, long tokenExpiration, bool suppressHistory)
|
|
|
|
|
: this(pool, username, password, saveTokenAsSecret, tokenExpiration, suppressHistory)
|
2015-06-02 15:48:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(identityTokenDomainName))
|
|
|
|
|
this.identityTokenDomainName = identityTokenDomainName;
|
|
|
|
|
if (!string.IsNullOrEmpty(identityTokenDomainName))
|
|
|
|
|
this.uploadGrantTokenDomainName = uploadGrantTokenDomainName;
|
|
|
|
|
if (!string.IsNullOrEmpty(identityTokenDomainName))
|
|
|
|
|
this.uploadTokenDomainName = uploadTokenDomainName;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 16:23:32 +02:00
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2015-06-26 00:51:11 +02:00
|
|
|
|
System.Diagnostics.Trace.Assert(pool != null || !saveTokenAsSecret, "Pool is null! Cannot save token as secret");
|
2015-06-02 15:48:59 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string identityToken = GetIdentityToken();
|
|
|
|
|
string uploadGrantToken = GetUploadGrantToken(identityToken);
|
2015-06-26 00:51:11 +02:00
|
|
|
|
uploadToken = GetUploadToken(uploadGrantToken);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
|
2015-06-26 00:51:11 +02:00
|
|
|
|
if (saveTokenAsSecret && pool != null)
|
|
|
|
|
{
|
|
|
|
|
log.Info("Saving upload token as xapi secret");
|
2015-07-01 04:35:59 +02:00
|
|
|
|
Dictionary<string, string> newConfig = pool.health_check_config;
|
|
|
|
|
SetSecretInfo(Connection, newConfig, CallHomeSettings.UPLOAD_TOKEN_SECRET, uploadToken);
|
|
|
|
|
SetSecretInfo(Connection, newConfig, CallHomeSettings.UPLOAD_CREDENTIAL_USER_SECRET, username);
|
|
|
|
|
SetSecretInfo(Connection, newConfig, CallHomeSettings.UPLOAD_CREDENTIAL_PASSWORD_SECRET, password);
|
2015-06-26 00:51:11 +02:00
|
|
|
|
Pool.set_health_check_config(Connection.Session, pool.opaque_ref, newConfig);
|
|
|
|
|
}
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2015-06-02 17:47:19 +02:00
|
|
|
|
log.Error("Exception trying to authenticate", e);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
Exception = e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 00:51:11 +02:00
|
|
|
|
public string UploadToken
|
|
|
|
|
{
|
|
|
|
|
get { return uploadToken; }
|
2015-07-01 04:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetSecretInfo(IXenConnection connection, Dictionary<string, string> config, string infoKey, string infoValue)
|
|
|
|
|
{
|
|
|
|
|
SetInfoSecret(connection, config, infoKey, infoValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetInfoSecret(IXenConnection connection, Dictionary<string, string> config, string infoKey, string infoValue)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(infoKey))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (infoValue == null)
|
|
|
|
|
{
|
|
|
|
|
config.Remove(infoKey);
|
|
|
|
|
}
|
|
|
|
|
else if (config.ContainsKey(infoKey))
|
2015-06-02 15:48:59 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
2015-07-01 04:35:59 +02:00
|
|
|
|
{
|
|
|
|
|
string secretRef = Secret.get_by_uuid(connection.Session, config[infoKey]);
|
|
|
|
|
Secret.set_value(connection.Session, secretRef, infoValue);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Failure)
|
2015-07-01 04:35:59 +02:00
|
|
|
|
{
|
|
|
|
|
config[infoKey] = Secret.CreateSecret(connection.Session, infoValue);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
catch (WebException)
|
2015-07-01 04:35:59 +02:00
|
|
|
|
{
|
|
|
|
|
config[infoKey] = Secret.CreateSecret(connection.Session, infoValue);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2015-07-01 04:35:59 +02:00
|
|
|
|
{
|
|
|
|
|
config[infoKey] = Secret.CreateSecret(connection.Session, infoValue);
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetIdentityToken()
|
|
|
|
|
{
|
|
|
|
|
var json = new JavaScriptSerializer().Serialize(new
|
|
|
|
|
{
|
|
|
|
|
username,
|
|
|
|
|
password
|
|
|
|
|
});
|
|
|
|
|
var urlString = string.Format("{0}{1}", identityTokenDomainName, identityTokenUrl);
|
|
|
|
|
return GetToken(urlString, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetUploadGrantToken(string identityToken)
|
|
|
|
|
{
|
|
|
|
|
var json = new JavaScriptSerializer().Serialize(new
|
|
|
|
|
{
|
2015-06-26 00:51:11 +02:00
|
|
|
|
identity_token = identityToken,
|
|
|
|
|
expiration = tokenExpiration
|
2015-06-02 15:48:59 +02:00
|
|
|
|
});
|
|
|
|
|
var urlString = string.Format("{0}{1}", uploadGrantTokenDomainName, uploadGrantTokenUrl);
|
|
|
|
|
return GetToken(urlString, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetUploadToken(string grantToken)
|
|
|
|
|
{
|
|
|
|
|
var json = new JavaScriptSerializer().Serialize(new
|
|
|
|
|
{
|
|
|
|
|
grant_token = grantToken,
|
2015-06-26 00:51:11 +02:00
|
|
|
|
product_key = productKey,
|
|
|
|
|
expiration = tokenExpiration
|
2015-06-02 15:48:59 +02:00
|
|
|
|
});
|
|
|
|
|
var urlString = string.Format("{0}{1}", uploadTokenDomainName, uploadTokenUrl);
|
|
|
|
|
return GetToken(urlString, json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetToken(string urlString, string jsonParameters)
|
|
|
|
|
{
|
|
|
|
|
var httpWebRequest = (HttpWebRequest) WebRequest.Create(urlString);
|
|
|
|
|
httpWebRequest.ContentType = "application/json";
|
|
|
|
|
httpWebRequest.Method = "POST";
|
|
|
|
|
|
|
|
|
|
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
|
|
|
|
|
{
|
|
|
|
|
streamWriter.Write(jsonParameters);
|
|
|
|
|
}
|
|
|
|
|
string result;
|
|
|
|
|
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
|
|
|
|
|
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
|
|
|
|
{
|
|
|
|
|
result = streamReader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
TemplateResponse response = new JavaScriptSerializer().Deserialize<TemplateResponse>(result);
|
2015-06-03 16:14:39 +02:00
|
|
|
|
return response.Token;
|
2015-06-02 15:48:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TemplateResponse
|
|
|
|
|
{
|
2015-06-03 16:14:39 +02:00
|
|
|
|
public string Token { get; set; }
|
2015-05-18 16:23:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|