public static SessionInfo GetSessionInfo(Coupon coupon)
        {
            SessionInfo sessionInfo = null;
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            Ticket sessionTicket = ticketIssuer.RetrieveIssuedTicket(coupon, TicketTypes.REDEEM_SESSION, ProcessAgentDB.ServiceGuid);
            if (sessionTicket != null)
            {
                if (sessionTicket.isCancelled || sessionTicket.IsExpired())
                {
                    throw new AccessDeniedException("The ticket has expired.");
                }

                sessionInfo = ParseRedeemSessionPayload(sessionTicket.payload);
            }
            return sessionInfo;
        }
        public bool RevokeReservation(string serviceBrokerGuid, string userName, string groupName, string labServerGuid, string labClientGuid,
            DateTime startTime, DateTime endTime, string message)
        {
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            bool status = false;
            if (ticketIssuer.AuthenticateAgentHeader(agentAuthHeader))
            {
                if (agentAuthHeader.coupon.issuerGuid == ProcessAgentDB.ServiceGuid)
                {
                    try
                    {
                        int userId = AdministrativeAPI.GetUserID(userName);
                        if (userId > 0)
                        {
                            User[] users = AdministrativeAPI.GetUsers(new int[] { userId });
                            if (users != null && users.Length > 0)
                            {
                                //SmtpMail.SmtpServer = "127.0.0.1";
                                if (users[0].email != null)
                                {
                                    //MailMessage uMail = new MailMessage();
                                    //uMail.To = users[0].email;
                                    //uMail.From = ConfigurationManager.AppSettings["supportMailAddress"];
                                    //uMail.Subject = "[iLabs] A Reservation has been revoked!";
                                    StringBuilder buf = new StringBuilder();
                                    buf.Append("Your scheduled reservation for ");
                                    buf.Append(AdministrativeAPI.GetLabClientName(AdministrativeAPI.GetLabClientID(labClientGuid)));
                                    buf.Append(", from " + DateUtil.ToUtcString(startTime) + " to " + DateUtil.ToUtcString(endTime));
                                    buf.AppendLine(" has been removed by an external service for the following reason: ");
                                    buf.AppendLine(message);
                                    buf.AppendLine("Please make a new reservation.");

                                    //uMail.Body = buf.ToString(); ;
                                    //SmtpMail.Send(uMail);

                                    string subject = "[iLabs] A Reservation has been revoked!";
                                    string body = buf.ToString();
                                    string from = ConfigurationManager.AppSettings["supportMailAddress"];
                                    string to = users[0].email;
                                    MailMessage mailMessage = new MailMessage(from, to, subject, body);
                                    SmtpClient smtpClient = new SmtpClient(Consts.STR_LocalhostIP);

                                    smtpClient.Send(mailMessage);
                                }
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        // Report detailed SMTP Errors
                        StringBuilder smtpErrorMsg = new StringBuilder();
                        smtpErrorMsg.Append("Exception: SMTP in InterativeSB:" + ex.Message);
                        //check the InnerException
                        if (ex.InnerException != null)
                            smtpErrorMsg.Append("<br>Inner Exceptions:");
                        while (ex.InnerException != null)
                        {
                            smtpErrorMsg.Append("<br>" + ex.InnerException.Message);
                            ex = ex.InnerException;
                        }
                        Utilities.WriteLog(smtpErrorMsg.ToString());

                    }
                    status = true;
                }
            }
            else
            {
                ProcessAgentInfo paInfo = ticketIssuer.GetProcessAgentInfo(agentAuthHeader.coupon.issuerGuid);
                if (paInfo != null)
                {
                    if (paInfo.retired)
                    {
                        throw new Exception("The ProcessAgent is retired");
                    }
                    InteractiveSBProxy proxy = new InteractiveSBProxy();
                    AgentAuthHeader authHeader = new AgentAuthHeader();
                    authHeader.coupon = paInfo.identOut;
                    authHeader.agentGuid = ProcessAgentDB.ServiceGuid;
                    proxy.AgentAuthHeaderValue = authHeader;
                    proxy.Url = paInfo.webServiceUrl;
                    status = proxy.RevokeReservation(serviceBrokerGuid, userName, groupName, labServerGuid, labClientGuid,
                        startTime, endTime, message);
                }
                else
                {
                    throw new Exception("Unknown TicketIssuerDB in RedeemTicket Request");
                }
            }
            return status;
        }
        /*****
         * ProcessAgent Administrative methods actual DB calls should be in either TicketIssuerDB or InternalDBAdmin API.
         *****/
        /// <summary>
        /// Registers the service identified by Name with the Service Broker.
        /// The method is responsible for creating the Qualifiers used to access the service.
        /// May be used for both domain & non-domain services ( ProcessAgents ).
        /// After this call users may be assigned grants to the service.
        /// </summary>
        /// <param name="guid">The GUID by which lab server identifies itself in web service calls.</param>
        /// <param name="name">A name for the service meaningful to humans; it is not required to be unique on the Service Broker instance.</param>       
        /// <param name="type"></param>
        /// <param name="issuerGuid">The service's domain ServiceBroker Guid</param>
        /// <param name="codeBaseURL">The URL for the lab server's web root directory.</param>
        /// <param name="serviceUrl">The fully qualified WebService page</param>
        /// <param name="inCoupon">Optional, not specified for a Service outside of the domain</param>
        /// <param name="outCoupon">Optional, not specified for a Service outside of the domain</param>
        /// <returns>The unique ID which identifies the ProcessAgent internally to the Service Broker.</returns>
        public static int AddProcessAgent(string guid, string name, string type, string domainGuid,
            string codeBaseUrl, string serviceUrl, Coupon inCoupon, Coupon outCoupon)
        {
            int agentID = -1;

            // if an exception is thrown, return false, otherwise true
            try
            {
                TicketIssuerDB ticketing = new TicketIssuerDB();
                //Insert the lab server into the database
                agentID = ticketing.InsertProcessAgent(guid, name, type, domainGuid, codeBaseUrl, serviceUrl,
                    inCoupon, outCoupon);
                string typeUpper = type.ToUpper();
                try
                {
                    int qualifierType = 0;
                    //Add the processAgent to the Qualifiers & Qualifier_Hierarchy table
                    switch (typeUpper)
                    {
                        case ProcessAgentType.SERVICE_BROKER:
                        case ProcessAgentType.BATCH_SERVICE_BROKER:
                        case ProcessAgentType.REMOTE_SERVICE_BROKER:
                            qualifierType = Qualifier.serviceBrokerQualifierTypeID;
                            break;
                        case ProcessAgentType.LAB_SERVER:
                        case ProcessAgentType.BATCH_LAB_SERVER:
                            qualifierType = Qualifier.labServerQualifierTypeID;
                            break;
                        case ProcessAgentType.EXPERIMENT_STORAGE_SERVER:
                            qualifierType = Qualifier.storageServerQualifierTypeID;
                            break;
                        case ProcessAgentType.LAB_SCHEDULING_SERVER:
                            qualifierType = Qualifier.labSchedulingQualifierTypeID;
                            break;
                        case ProcessAgentType.SCHEDULING_SERVER:
                            qualifierType = Qualifier.userSchedulingQualifierTypeID;
                            break;
                        default:
                            break;
                    }

                    Authorization.AuthorizationAPI.AddQualifier(agentID, qualifierType, name, Qualifier.ROOT);
                }
                catch (Exception ex)
                {
                    // rollback lab server insertion
                    InternalAdminDB.DeleteProcessAgents(new int[] { agentID });

                    throw;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return agentID;
        }
        public Coupon RequestExperimentAccess(long experimentID)
        {
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            Coupon coupon = null;
            //first try to recreate session if using a html client
            //if ((Session == null) || (Session["UserID"] == null) || (Session["UserID"].ToString() == ""))

            if (ticketIssuer.AuthenticateIssuedCoupon(opHeader.coupon))
            {
                Ticket sessionTicket = ticketIssuer.RetrieveTicket(opHeader.coupon, TicketTypes.REDEEM_SESSION);
                if (sessionTicket != null)
                {

                    if (sessionTicket.IsExpired())
                    {
                        throw new AccessDeniedException("The ticket has expired.");
                    }

                    //Parse payload
                    XmlQueryDoc expDoc = new XmlQueryDoc(sessionTicket.payload);
                    // Get User & Group
                    int userID = -1;
                    int groupID = -1;
                    string group = expDoc.Query("RedeemSessionPayload/groupID");
                    string user = expDoc.Query("RedeemSessionPayload/userID");
                    if (group != null && group.Length > 0)
                    {
                        groupID = Convert.ToInt32(group);
                    }
                    if (user != null && user.Length > 0)
                    {
                        userID = Convert.ToInt32(user);
                    }

                    //Check Qualifiers on experiment
                    int status = wrapper.GetExperimentAuthorizationWrapper(experimentID, userID, groupID);
                    //if accessable by user create new TicketCollection
                }
            }
            return coupon;
        }
        public bool RequestTicketCancellation(Coupon coupon,
            string type, string redeemerGuid)
        {
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            bool status = false;
            if (ticketIssuer.AuthenticateAgentHeader(agentAuthHeader))
            {
                if (coupon.issuerGuid == ProcessAgentDB.ServiceGuid)
                {
                    return ticketIssuer.RequestTicketCancellation(coupon,
                        type, redeemerGuid);
                }
                else
                {
                    ProcessAgentInfo paInfo = ticketIssuer.GetProcessAgentInfo(coupon.issuerGuid);
                    if (paInfo != null)
                    {
                        if (paInfo.retired)
                        {
                            throw new Exception("The ProcessAgent is retired");
                        }
                        TicketIssuerProxy ticketProxy = new TicketIssuerProxy();
                        AgentAuthHeader authHeader = new AgentAuthHeader();
                        authHeader.coupon = paInfo.identOut;
                        authHeader.agentGuid = ProcessAgentDB.ServiceGuid;
                        ticketProxy.AgentAuthHeaderValue = authHeader;
                        ticketProxy.Url = paInfo.webServiceUrl;
                        status = ticketProxy.RequestTicketCancellation(coupon, type, redeemerGuid);
                    }
                    else
                    {
                        throw new Exception("Unknown TicketIssuerDB in RedeemTicket Request");
                    }
                }
            }

            return status;
        }
        public StorageStatus OpenExperiment(long experimentId, long duration)
        {
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            StorageStatus status = null;
            if (ticketIssuer.AuthenticateAgentHeader(agentAuthHeader))
            {
                Ticket essTicket = ticketIssuer.RetrieveTicket(opHeader.coupon, TicketTypes.ADMINISTER_EXPERIMENT);
                // Check for ESS use
                if (essTicket != null)
                {
                    XmlDocument payload = new XmlDocument();
                    payload.LoadXml(essTicket.payload);
                    string essURL = payload.GetElementsByTagName("essURL")[0].InnerText;

                    long sbExperimentId = Int64.Parse(payload.GetElementsByTagName("experimentID")[0].InnerText);
                    //
                    ExperimentSummary summary = InternalDataDB.SelectExperimentSummary(experimentId);
                    if (summary.HasEss)
                    {
                        // Retrieve the ESS Status info and update as needed
                        ProcessAgentInfo ess = ticketIssuer.GetProcessAgentInfo(summary.essGuid);
                        if (ess.retired)
                        {
                            throw new Exception("The ESS is retired");
                        }
                        ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                        essProxy.AgentAuthHeaderValue = new AgentAuthHeader();
                        essProxy.AgentAuthHeaderValue.coupon = ess.identOut;
                        essProxy.AgentAuthHeaderValue.agentGuid = ProcessAgentDB.ServiceGuid;
                        essProxy.Url = essURL;
                        status = essProxy.OpenExperiment(sbExperimentId, duration);
                    }

                    // Note: store and retrieve tickets are not cancelled.
                }
            }
            if (status != null)
            {
                DataStorageAPI.UpdateExperimentStatus(status);
            }
            return status;
        }
        public string GetAnnotation(int experimentID)
        {
            //first try to recreate session if using an html client
            if (Session == null || (Session["UserID"] == null) || (Session["UserID"].ToString() == ""))
            {
                wrapper.SetServiceSession(opHeader.coupon);
            }
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            if (ticketIssuer.AuthenticateIssuedCoupon(opHeader.coupon))
            {
                try
                {
                    //first try to recreate session if using a html client
                    if (Session == null || (Session["UserID"] == null) || (Session["UserID"].ToString() == ""))
                        wrapper.SetServiceSession(opHeader.coupon);

                    return wrapper.SelectExperimentAnnotationWrapper(experimentID);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
                return null;
        }
 public Coupon CreateTicket(string type, string redeemerGuid,
     long duration, string payload)
 {
     TicketIssuerDB ticketIssuer = new TicketIssuerDB();
     Coupon coupon = null;
     if (ticketIssuer.AuthenticateAgentHeader(agentAuthHeader))
     {
         if (agentAuthHeader.coupon.issuerGuid == ProcessAgentDB.ServiceGuid)
         {
             // Note: may need to find requesting service for sponsor.
             coupon = ticketIssuer.CreateTicket(type, redeemerGuid, agentAuthHeader.agentGuid,
                 duration, payload);
         }
     }
     return coupon;
 }
        public StorageStatus AgentCloseExperiment(Coupon coupon, long experimentId)
        {
            TicketIssuerDB ticketIssuer = new TicketIssuerDB();
            StorageStatus status = null;
            bool experimentClosed = false;

            if (dbTicketing.AuthenticateAgentHeader(agentAuthHeader))
            {
                if (coupon.issuerGuid == ProcessAgentDB.ServiceGuid)
                {

                    // Check for ESS use
                    Ticket essTicket = ticketIssuer.RetrieveTicket(coupon, TicketTypes.ADMINISTER_EXPERIMENT);
                    if (essTicket != null)
                    {
                        ProcessAgentInfo ess = ticketIssuer.GetProcessAgentInfo(essTicket.redeemerGuid);
                        if (ess != null)
                        {
                            if (ess.retired)
                            {
                                throw new Exception("The ProcessAgent is retired");
                            }
                            ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                            essProxy.AgentAuthHeaderValue = new AgentAuthHeader();
                            essProxy.AgentAuthHeaderValue.coupon = ess.identOut;
                            essProxy.AgentAuthHeaderValue.agentGuid = ProcessAgentDB.ServiceGuid;
                            essProxy.Url = ess.webServiceUrl;
                            status = essProxy.CloseExperiment(experimentId);
                            DataStorageAPI.UpdateExperimentStatus(status);
                        }
                        ticketIssuer.CancelIssuedTicket(coupon, essTicket);
                    }
                    else
                    {
                        // Close the local Experiment records
                        // Note: store and retrieve tickets are not cancelled.
                        experimentClosed = DataStorageAPI.CloseExperiment(experimentId, StorageStatus.CLOSED_USER);
                        status = DataStorageAPI.RetrieveExperimentStatus(experimentId);
                    }

                }
                else
                {
                    ProcessAgentInfo paInfo = ticketIssuer.GetProcessAgentInfo(coupon.issuerGuid);
                    if (paInfo != null)
                    {
                        if (paInfo.retired)
                        {
                            throw new Exception("The ProcessAgent is retired");
                        }
                        InteractiveSBProxy ticketProxy = new InteractiveSBProxy();
                        AgentAuthHeader authHeader = new AgentAuthHeader();
                        authHeader.coupon = paInfo.identOut;
                        authHeader.agentGuid = ProcessAgentDB.ServiceGuid;
                        ticketProxy.AgentAuthHeaderValue = authHeader;
                        ticketProxy.Url = paInfo.webServiceUrl;
                        status = ticketProxy.AgentCloseExperiment(coupon, experimentId);
                    }
                    else
                    {
                        throw new Exception("Unknown TicketIssuerDB in RedeemTicket Request");
                    }
                }

            }
            return status;
        }
Пример #10
0
 public static SessionInfo GetSessionInfo(Coupon coupon)
 {
     SessionInfo sessionInfo = null;
     TicketIssuerDB ticketIssuer = new TicketIssuerDB();
     Ticket sessionTicket = ticketIssuer.RetrieveIssuedTicket(coupon, TicketTypes.REDEEM_SESSION, ProcessAgentDB.ServiceGuid);
     if (sessionTicket != null)
     {
         if (sessionTicket.IsExpired())
         {
             throw new AccessDeniedException("The ticket has expired.");
         }
         // Get Session info
         XmlQueryDoc expDoc = new XmlQueryDoc(sessionTicket.payload);
         sessionInfo = new SessionInfo();
         string STR_RedeemSessionPayload = "RedeemSessionPayload/";
         string userID = expDoc.Query(STR_RedeemSessionPayload + "userID");
         sessionInfo.userID = Convert.ToInt32(userID);
         string groupID = expDoc.Query(STR_RedeemSessionPayload + "groupID");
         sessionInfo.groupID = Convert.ToInt32(groupID);
         string clientID = expDoc.Query(STR_RedeemSessionPayload + "clientID");
         sessionInfo.clientID = Convert.ToInt32(clientID);
         sessionInfo.userName = expDoc.Query(STR_RedeemSessionPayload + "userName");
         sessionInfo.groupName = expDoc.Query(STR_RedeemSessionPayload + "groupName");
     }
     return sessionInfo;
 }