Beispiel #1
0
        public Connection(string identity, SerConnection config)
        {
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => { return(true); };

            Mode            = QlikAppMode.SERVER;
            IsSharedSession = true;
            Config          = config;
            Identity        = identity;

            var connectUrl = SwitchScheme(Config.ServerUri.AbsoluteUri, SchemeMode.WEBSOCKET);
            var appurl     = Uri.EscapeDataString(HelperUtilities.GetFullAppName(Config.App).TrimStart('/'));

            connectUrl = $"{connectUrl}/app/{appurl}";

            if (identity == null)
            {
                var newIdentity = Guid.NewGuid();
                connectUrl      = $"{connectUrl}/identity/{newIdentity}";
                IsSharedSession = false;
                GlobelIdentity  = newIdentity.ToString();
            }
            else if (!String.IsNullOrEmpty(identity))
            {
                connectUrl     = $"{connectUrl}/identity/{identity}";
                GlobelIdentity = identity;
            }

            ConnectUri = new Uri(connectUrl);
            logger.Info($"Create Qlik connection {ConnId} to {connectUrl} with app {Config.App} and identity {identity}.");
        }
 public SessionInfo CreateNewSession(SerConnection connection, DomainUser qlikUser, string appId)
 {
     try
     {
         var token = GetToken(qlikUser, connection, TimeSpan.FromMinutes(20));
         logger.Debug($"Generate token {token}");
         var cookie = GetJWTSession(connection.ServerUri, token, connection.Credentials.Key);
         logger.Debug($"Generate cookie {cookie?.Name} - {cookie?.Value}");
         if (cookie != null)
         {
             var sessionInfo = new SessionInfo()
             {
                 Cookie     = cookie,
                 ConnectUri = connection.ServerUri,
                 AppId      = appId,
                 User       = qlikUser,
             };
             return(sessionInfo);
         }
         return(null);
     }
     catch (Exception ex)
     {
         logger.Error(ex, "The session could not be created.");
         return(null);
     }
 }
Beispiel #3
0
        public static Connection NewConnection(SerConnection connectionConfig, bool loadPossibleApps = false)
        {
            try
            {
                lock (threadStaticObject)
                {
                    var distinctIdentities = connectionConfig?.Identities?.Distinct()?.ToArray() ?? new string[0];
                    foreach (var identity in distinctIdentities)
                    {
                        var newConnection = new Connection(identity, connectionConfig);
                        if (newConnection.Connect(loadPossibleApps))
                        {
                            newConnection.IsFree = false;
                            return(newConnection);
                        }
                    }

                    if (connectionConfig.Identities == null || connectionConfig.Identities.Count == 0)
                    {
                        var conn = new Connection(null, connectionConfig);
                        if (conn.Connect(loadPossibleApps))
                        {
                            return(conn);
                        }
                    }
                    return(null);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "No new connection to qlik.");
                return(null);
            }
        }
Beispiel #4
0
        private SerConnection CreateConnection(QlikCredentialType type, SessionInfo session, string dataAppId = null)
        {
            try
            {
                logger.Debug("Create new connection.");
                var mainConnection = Options.Config.Connection;
                var token          = Options.SessionHelper.Manager.GetToken(session.User, mainConnection, TimeSpan.FromMinutes(30));
                logger.Debug($"Bearer Token: {token}");

                var conn = new SerConnection()
                {
                    ServerUri = mainConnection.ServerUri,
                };

                switch (type)
                {
                case QlikCredentialType.JWT:
                    conn.Credentials = new SerCredentials()
                    {
                        Type  = type,
                        Key   = "Authorization",
                        Value = $"Bearer { token }"
                    };
                    break;

                case QlikCredentialType.SESSION:
                    conn.Credentials = new SerCredentials()
                    {
                        Type  = type,
                        Key   = session.Cookie?.Name ?? null,
                        Value = session.Cookie?.Value ?? null
                    };
                    break;

                default:
                    logger.Error("Unknown connection type.");
                    break;
                }

                if (!String.IsNullOrEmpty(dataAppId))
                {
                    conn.App = dataAppId;
                }

                return(conn);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "The bearer connection could not create.");
                return(null);
            }
        }
Beispiel #5
0
        private void WaitForDataLoad(ManagedTask task, SerConnection configConn)
        {
            var scriptApp     = configConn?.App;
            var timeout       = configConn?.RetryTimeout ?? 0;
            var dataloadCheck = ScriptCheck.DataLoadCheck(Options.Config.Connection.ServerUri, scriptApp, task, timeout);

            if (dataloadCheck)
            {
                logger.Debug("Transfer script to the rest service...");
                var jobJson = task.JobScript.ToString();
                logger.Trace($"JSON-Script: {jobJson}");
                var taskId = Options.RestClient.RunTask(jobJson, task.Id);
                logger.Info($"The reporting request was successfully transferred and run with id '{taskId}' to the rest service...");
            }
            else
            {
                logger.Debug("Dataload check failed.");
            }
        }
Beispiel #6
0
        public SerConnection GetConnConfig(SerConnection config, string serverUri = null, string appName = null)
        {
            var jsonSerConfig = JsonConvert.SerializeObject(config);
            var configCopy    = JsonConvert.DeserializeObject <SerConnection>(jsonSerConfig);

            if (!String.IsNullOrEmpty(serverUri))
            {
                if (!Uri.TryCreate(serverUri, UriKind.Absolute, out var uriResult))
                {
                    logger.Error($"The qlik server uri {serverUri} is invalid.");
                    return(null);
                }
                configCopy.ServerUri = uriResult;
            }
            if (!String.IsNullOrEmpty(appName))
            {
                configCopy.App = appName;
            }
            configCopy.Identities = null;
            return(configCopy);
        }
        public SessionInfo GetSession(SerConnection connection, QlikRequest request)
        {
            try
            {
                lock (threadObject)
                {
                    var uri        = connection.ServerUri;
                    var oldSession = Sessions?.FirstOrDefault(u => u.ConnectUri.OriginalString == uri.OriginalString &&
                                                              u.User.ToString() == request.QlikUser.ToString() &&
                                                              u.AppId == request.AppId) ?? null;
                    if (oldSession != null)
                    {
                        logger.Debug("Old session found...");
                        var result = ValidateSession(oldSession);
                        if (result)
                        {
                            logger.Debug("Old session is working...");
                            return(oldSession);
                        }
                        logger.Debug("Old session not working, i remove it...");
                        Sessions.Remove(oldSession);
                    }
                }

                logger.Debug("Create new Qlik Session...");
                var sessionInfo = Manager.CreateNewSession(connection, request.QlikUser, request.AppId);
                if (sessionInfo != null)
                {
                    Sessions.Add(sessionInfo);
                }
                return(sessionInfo);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "The session could not recreated or reused.");
                return(null);
            }
        }
Beispiel #8
0
 private Connection FindConnection(SerConnection config)
 {
     try
     {
         var values = Connections.Values.ToArray();
         foreach (var conn in values)
         {
             if (conn.IsFree == true)
             {
                 logger.Trace("The connection is checked for reuse.");
                 logger.Trace($"App \"{conn?.Config?.App}={config?.App}\"");
                 logger.Trace($"Uri: \"{conn?.Config?.ServerUri?.AbsoluteUri}={config?.ServerUri?.AbsoluteUri}\"");
                 logger.Trace($"Identity: \"{conn?.Identity}={String.Join(",", config?.Identities ?? new List<string>())}\"");
                 if (conn.Config.App == config.App && conn.Config.ServerUri.AbsoluteUri == config.ServerUri.AbsoluteUri)
                 {
                     if (config.Identities == null && conn.Identity == null)
                     {
                         conn.IsFree = false;
                         return(conn);
                     }
                     var identity = config.Identities?.FirstOrDefault(i => i == conn.Identity) ?? null;
                     if (identity != null)
                     {
                         conn.IsFree = false;
                         return(conn);
                     }
                 }
             }
         }
         return(null);
     }
     catch (Exception ex)
     {
         logger.Error(ex, "The connection could not find.");
         return(null);
     }
 }
 public string GetToken(DomainUser domainUser, SerConnection connection, TimeSpan untilValid)
 {
     try
     {
         var cert     = new X509Certificate2();
         var certPath = HelperUtilities.GetFullPathFromApp(connection.Credentials.Cert);
         logger.Debug($"CERTPATH: {certPath}");
         var privateKey = HelperUtilities.GetFullPathFromApp(connection.Credentials.PrivateKey);
         logger.Debug($"PRIVATEKEY: {privateKey}");
         cert = cert.LoadPem(certPath, privateKey);
         var claims = new[]
         {
             new Claim("UserDirectory", domainUser.UserDirectory),
             new Claim("UserId", domainUser.UserId),
             new Claim("Attributes", "[SerOnDemand]")
         }.ToList();
         return(cert.GenerateQlikJWToken(claims, untilValid));
     }
     catch (Exception ex)
     {
         logger.Error(ex, "Can´t create a jwt token.");
         return(null);
     }
 }
Beispiel #10
0
        public void StartReportJob(QlikRequest request, ManagedTask newManagedTask)
        {
            Task.Run(() =>
            {
                try
                {
                    newManagedTask.InternalStatus = InternalTaskStatus.CREATEREPORTJOBSTART;
                    logger.Debug("Create new job...");
                    logger.Info($"Memory usage: {GC.GetTotalMemory(true)}");
                    Options.Analyser?.ClearCheckPoints();
                    Options.Analyser?.Start();
                    Options.Analyser?.SetCheckPoint("StartReportJob", "Start report generation");

                    MappedDiagnosticsLogicalContext.Set("jobId", newManagedTask.Id.ToString());

                    //Get Qlik session over jwt
                    logger.Debug("Get cookie over JWT session...");
                    newManagedTask.Session = Options.SessionHelper.GetSession(Options.Config.Connection, request);
                    if (newManagedTask.Session == null)
                    {
                        throw new Exception("No session cookie generated (check qmc settings or connector config).");
                    }

                    //Connect to Qlik app
                    logger.Debug("Connecting to Qlik via websocket...");
                    Options.Analyser?.SetCheckPoint("StartReportJob", "Connect to Qlik");
                    var fullConnectionConfig = new SerConnection
                    {
                        App         = request.AppId,
                        ServerUri   = Options.Config.Connection.ServerUri,
                        Credentials = new SerCredentials()
                        {
                            Type  = QlikCredentialType.SESSION,
                            Key   = newManagedTask.Session.Cookie.Name,
                            Value = newManagedTask.Session.Cookie.Value
                        }
                    };
                    var qlikConnection = ConnectionManager.NewConnection(fullConnectionConfig, true);
                    newManagedTask.Session.QlikConn = qlikConnection ?? throw new Exception("The web socket connection to qlik could not be established (Connector).");

                    //Create full engine config
                    logger.Debug("Create configuration for the engine...");
                    Options.Analyser?.SetCheckPoint("StartReportJob", "Gernerate Config Json");
                    var newEngineConfig = CreateEngineConfig(request, newManagedTask.Session);

                    //Remove emtpy Tasks without report infos
                    newEngineConfig.Tasks.RemoveAll(t => t.Reports.Count == 0);

                    foreach (var configTask in newEngineConfig.Tasks)
                    {
                        if (configTask.Id == Guid.Empty)
                        {
                            configTask.Id = Guid.NewGuid();
                        }
                        else
                        {
                            //Check the task is of unique
                            if (newEngineConfig.Tasks.Count(t => t.Id == configTask.Id) > 1)
                            {
                                throw new Exception("The task id is used twice. Please change the task id. This must always be unique.");
                            }
                        }

                        foreach (var configReport in configTask.Reports)
                        {
                            //Important: Add bearer connection as last connection item.
                            var firstConnection = configReport?.Connections?.FirstOrDefault() ?? null;
                            if (firstConnection != null)
                            {
                                logger.Debug("Create bearer connection.");
                                var newBearerConnection = CreateConnection(QlikCredentialType.JWT, newManagedTask.Session, firstConnection.App);
                                configReport.Connections.Add(newBearerConnection);
                            }

                            //Check app Id
                            var appList   = Q2g.HelperQlik.Connection.PossibleApps;
                            var activeApp = appList.FirstOrDefault(a => a.qDocId == firstConnection.App);
                            if (activeApp == null)
                            {
                                throw new Exception($"The app id {firstConnection.App} was not found. Please check the app id or the security rules.");
                            }

                            //Read content from lib and content libary
                            logger.Debug("Get template data from qlik.");
                            if (configReport.Template != null)
                            {
                                var uploadData = FindTemplatePath(newManagedTask.Session, configReport.Template);
                                logger.Debug("Upload template data to rest service.");
                                var uploadId = Options.RestClient.UploadData(uploadData, configReport.Template.Input);
                                logger.Debug($"Upload with id {uploadId} was successfully.");
                                newManagedTask.FileUploadIds.Add(uploadId);
                            }
                            else
                            {
                                logger.Debug("No Template found. - Use alternative mode.");
                            }

                            // Perfomance analyser for the engine
                            configReport.General.UsePerfomanceAnalyzer = Options.Config.UsePerfomanceAnalyzer;

                            //Add all server uris
                            foreach (var connection in configReport.Connections)
                            {
                                connection.LicenseServers.Add(new SerServer()
                                {
                                    ServerUri = new Uri("https://license.analyticsgate.com"), Location = "de", Priority = 1
                                });
                                connection.LicenseServers.AddRange(Options.Config.Connection.LicenseServers);

                                connection.RendererServers.Add(new SerServer()
                                {
                                    ServerUri = new Uri("https://localhost:40271"), Location = "default", Priority = 100
                                });
                                connection.RendererServers.AddRange(Options.Config.Connection.RendererServers);
                            }
                        }
                    }

                    //Append upload ids on config
                    var jobJson = JObject.FromObject(newEngineConfig);
                    jobJson     = AppendUploadGuids(jobJson, newManagedTask.FileUploadIds);
                    newManagedTask.JobScript = jobJson;

                    //Use the connector in the same App, than wait for data reload
                    Options.Analyser?.SetCheckPoint("StartReportJob", "Start connector reporting task");
                    var scriptConnection = newEngineConfig?.Tasks?.SelectMany(s => s.Reports)
                                           ?.SelectMany(r => r.Connections)
                                           ?.FirstOrDefault(c => c.App == newManagedTask.Session.AppId) ?? null;

                    //Wait for data load in single App mode
                    WaitForDataLoad(newManagedTask, scriptConnection);
                    Options.Analyser?.SetCheckPoint("StartReportJob", "End report generation");
                    newManagedTask.InternalStatus = InternalTaskStatus.CREATEREPORTJOBEND;
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "The reporting order could not be executed properly.");
                    newManagedTask.Endtime        = DateTime.Now;
                    newManagedTask.Status         = -1;
                    newManagedTask.Error          = ex;
                    newManagedTask.InternalStatus = InternalTaskStatus.ERROR;
                    Options.SessionHelper.Manager.MakeSocketFree(newManagedTask?.Session ?? null);
                }
            }, newManagedTask.Cancellation.Token);
        }