Beispiel #1
0
        public BWebServiceResponse ProcessRequest(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            bDoNotGetDBClearance =
                BWebUtilities.DoesContextContainHeader(out List <string> DNGDBCs, out string _, _Context, "do-not-get-db-clearance") &&
                BUtility.CheckAndGetFirstStringFromList(DNGDBCs, out string DNGDBC) &&
                DNGDBC == "true";

            BTaskWrapper.Run(() =>
            {
                var Response = OnRequestCallback?.Invoke(_Context, _ErrorMessageAction);
                if (Response.HasValue)
                {
                    Responses.Enqueue(Response.Value);
                }

                try
                {
                    WaitUntilSignal.Set();
                }
                catch (Exception) { }
            });

            try
            {
                WaitUntilSignal.WaitOne();
            }
            catch (Exception) { }

            if (!Responses.TryDequeue(out BWebServiceResponse FirstResponse))
            {
                FirstResponse = BWebResponse.InternalError("Unexpected error in concurrence.");
            }
            return(FirstResponse);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// <para>DeleteCustomTopicGlobally:</para>
        ///
        /// <para>Deletes all messages and the topic of given workspace</para>
        ///
        /// <para>Check <seealso cref="IBPubSubServiceInterface.DeleteCustomTopicGlobally"/> for detailed documentation</para>
        ///
        /// </summary>
        public void DeleteCustomTopicGlobally(string _CustomTopic, Action <string> _ErrorMessageAction = null)
        {
            if (BUtility.CalculateStringMD5(_CustomTopic, out string TopicMD5, _ErrorMessageAction) &&
                CheckQueueExists(TopicMD5, out string QueueUrl))
            {
                try
                {
                    lock (SubscriberThreadsDictionaryLock)
                    {
                        if (SubscriberThreadsDictionary.ContainsKey(_CustomTopic))
                        {
                            var SubscriberThread = SubscriberThreadsDictionary[_CustomTopic];
                            if (SubscriberThread != null)
                            {
                                SubscriberThread.Item2.Set(true);
                            }
                            SubscriberThreadsDictionary.Remove(_CustomTopic);
                        }
                    }

                    using (var DeleteQueueTask = SQSClient.DeleteQueueAsync(QueueUrl))
                    {
                        DeleteQueueTask.Wait();
                    }
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BPubSubServiceAWS->DeleteCustomTopicGlobally->Callback: " + e.Message + ", Trace: " + e.StackTrace);
                    if (e.InnerException != null && e.InnerException != e)
                    {
                        _ErrorMessageAction?.Invoke("BPubSubServiceAWS->DeleteCustomTopicGlobally->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                    }
                }
            }
        }
 public override void OnDrawGizmosSelected()
 {
     UnityEngine.Vector3    position = transform.position;
     UnityEngine.Quaternion rotation = transform.rotation;
     UnityEngine.Vector3    scale    = m_localScaling;
     BUtility.DebugDrawCylinder(position, rotation, scale, halfExtent.x, halfExtent.y, 2, Color.yellow);
 }
Beispiel #4
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            bIsInternalCall =
                BWebUtilities.DoesContextContainHeader(out List <string> ICHVs, out string _, _Context, "internal-call-secret") &&
                BUtility.CheckAndGetFirstStringFromList(ICHVs, out string ICH) &&
                ICH == CommonData.INTERNAL_CALL_PRIVATE_KEY;

            if (_Context.Request.HttpMethod != "DELETE")
            {
                _ErrorMessageAction?.Invoke("User_DeleteUserAccessMethod_ForUser: DELETE method is accepted. But received request method:  " + _Context.Request.HttpMethod);
                return(BWebResponse.MethodNotAllowed("DELETE methods is accepted. But received request method: " + _Context.Request.HttpMethod));
            }

            RequestedUserID        = RestfulUrlParameters[RestfulUrlParameter_UsersKey];
            RequestedAuthMethodKey = WebUtility.UrlDecode(RestfulUrlParameters[RestfulUrlParameter_AccessMethodKey]);

            if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), RequestedUserID, _ErrorMessageAction))
            {
                return(BWebResponse.InternalError("Atomic operation control has failed."));
            }

            var Result = DeleteAccessMethodForUser(_Context, out bool bSetClearanceForApiKey, out string ApiKey, _ErrorMessageAction);

            Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), RequestedUserID, _ErrorMessageAction);
            if (bSetClearanceForApiKey)
            {
                Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY + ":" + ApiKey, _ErrorMessageAction);
            }

            return(Result);
        }
Beispiel #5
0
        public override BWebServiceResponse OnRequest(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            bool bIsInternalCall =
                BWebUtilities.DoesContextContainHeader(out List <string> ICHVs, out string _, _Context, "internal-call-secret") &&
                BUtility.CheckAndGetFirstStringFromList(ICHVs, out string _);

            if (bIsInternalCall)
            {
                var ErrorMessages = new JArray();

                var Result = OnRequestPP(_Context, (string _Message) =>
                {
                    _ErrorMessageAction?.Invoke(_Message);
                    ErrorMessages.Add(_Message);
                });

                try
                {
                    if (Result.ResponseContent.Type == EBStringOrStreamEnum.String)
                    {
                        var Parsed = JObject.Parse(Result.ResponseContent.String);
                        Parsed["internalCallErrorMessages"] = ErrorMessages;
                        Result = new BWebServiceResponse(Result.StatusCode, Result.Headers, new BStringOrStream(Parsed.ToString()), Result.ResponseContentType);
                    }
                }
                catch (Exception) {}

                return(Result);
            }
            return(OnRequestPP(_Context, _ErrorMessageAction));
        }
Beispiel #6
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            if (_Context.Request.HttpMethod != "POST" && _Context.Request.HttpMethod != "DELETE")
            {
                _ErrorMessageAction?.Invoke("User_UpdateDeleteBaseRight_ForUser: POST and DELETE methods are accepted. But received request method:  " + _Context.Request.HttpMethod);
                return(BWebResponse.MethodNotAllowed("POST and DELETE methods are accepted. But received request method: " + _Context.Request.HttpMethod));
            }

            RequestedUserID                  = RestfulUrlParameters[RestfulUrlParameter_UsersKey];
            RequestedBaseRightWildcard       = WebUtility.UrlDecode(RestfulUrlParameters[RestfulUrlParameter_BaseAccessRightsKey]);
            RequestedBaseRightWildcard_Regex = BUtility.WildCardToRegular(RequestedBaseRightWildcard);

            if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), RequestedUserID, _ErrorMessageAction))
            {
                return(BWebResponse.InternalError("Atomic operation control has failed."));
            }

            BWebServiceResponse Result;

            if (_Context.Request.HttpMethod == "POST")
            {
                Result = UpdateBaseRightForUser(_Context, _ErrorMessageAction);
            }
            else
            {
                Result = DeleteBaseRightForUser(_Context, _ErrorMessageAction);
            }

            Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), RequestedUserID, _ErrorMessageAction);

            return(Result);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// <para>ScanTable</para>
        ///
        /// <para>Scans the table for attribute specified by _Key</para>
        ///
        /// <para>Check <seealso cref="IBDatabaseServiceInterface.ScanTable"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool ScanTable(string _Table, out List <JObject> _ReturnItem, Action <string> _ErrorMessageAction = null)
        {
            _ReturnItem = null;

            var Table = GetTable(_Table);

            if (Table == null)
            {
                return(false);
            }

            List <JObject> Results = new List <JObject>();

            var Filter = Builders <BsonDocument> .Filter.Empty;

            IFindFluent <BsonDocument, BsonDocument> ReturnedSearch;

            try
            {
                ReturnedSearch = Table.Find(Filter);
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->ScanTable: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }

            if (ReturnedSearch != null)
            {
                List <JObject> TempResults = new List <JObject>();
                try
                {
                    foreach (var Document in ReturnedSearch.ToList())
                    {
                        var CreatedJson = BsonToJObject(Document);
                        BUtility.SortJObject(CreatedJson, true);
                        TempResults.Add(CreatedJson);
                    }

                    _ReturnItem = TempResults;
                }
                catch (Newtonsoft.Json.JsonReaderException e)
                {
                    _ErrorMessageAction?.Invoke("BDatabaseServiceAWS->ScanTable: JsonReaderException: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }
                return(true);
            }
            else
            {
                _ErrorMessageAction?.Invoke("BDatabaseServiceAWS->ScanTable: TableObject.ScanTable returned null.");
            }

            return(false);
        }
Beispiel #8
0
        private bool MakeQueryParameters(out BMemoryQueryParameters _QueryParameters, out string _PasswordMD5_FromAccessToken)
        {
            _QueryParameters = new BMemoryQueryParameters();
            if (!BUtility.CalculateStringMD5(AccessToken_TokenTypeSpacePrepended, out _PasswordMD5_FromAccessToken, ErrorMessageAction))
            {
                return(false);
            }

            _QueryParameters = MakeSSOQueryParameters(_PasswordMD5_FromAccessToken);
            return(true);
        }
Beispiel #9
0
        /// <summary>
        ///
        /// <para>CustomPublish:</para>
        ///
        /// <para>Publishes the given message to given custom topic</para>
        ///
        /// <para>Check <seealso cref="IBPubSubServiceInterface.CustomPublish"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool CustomPublish(string _CustomTopic, string _CustomMessage, Action <string> _ErrorMessageAction = null)
        {
            if (_CustomTopic != null && _CustomTopic.Length > 0 &&
                _CustomMessage != null && _CustomMessage.Length > 0 &&
                BUtility.CalculateStringMD5(_CustomTopic, out string TopicMD5, _ErrorMessageAction))
            {
                if (EnsureQueueExists(TopicMD5, out string QueueUrl, _ErrorMessageAction))
                {
                    string TimestampHash = null;
                    UniqueMessageDeliveryEnsurer?.Publish_PrependTimestampToMessage(ref _CustomMessage, out TimestampHash);

                    try
                    {
                        if (UniqueMessageDeliveryEnsurer != null)
                        {
                            if (UniqueMessageDeliveryEnsurer.Publish_EnsureUniqueDelivery(_CustomTopic, TimestampHash, _ErrorMessageAction))
                            {
                                using (var SendMessageTask = SQSClient.SendMessageAsync(QueueUrl, _CustomMessage))
                                {
                                    SendMessageTask.Wait();
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish: UniqueMessageDeliveryEnsurer has failed.");
                                return(false);
                            }
                        }
                        else
                        {
                            using (var SendMessageTask = SQSClient.SendMessageAsync(QueueUrl, _CustomMessage))
                            {
                                SendMessageTask.Wait();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish: " + e.Message + ", Trace: " + e.StackTrace);
                        if (e.InnerException != null && e.InnerException != e)
                        {
                            _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomPublish->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                        }
                        return(false);
                    }
                    return(true);
                }
            }
            return(false);
        }
Beispiel #10
0
        public static bool Initialize(
            out BServiceInitializer _Result,
            string[][] _RequiredExtraEnvVars = null)
        {
            var Instance = new BServiceInitializer();

            _Result = null;

            Instance.LoggingService = new BLoggingServiceBasic();

            var RequiredEnvVarKeys = new List <string[]>()
            {
                new string[] { "PORT" },
                new string[] { "PROGRAM_ID" }
            };

            if (_RequiredExtraEnvVars != null)
            {
                RequiredEnvVarKeys.AddRange(_RequiredExtraEnvVars);
            }

            /*
             * Getting environment variables
             */
            if (!BUtility.GetEnvironmentVariables(out Instance._RequiredEnvironmentVariables,
                                                  RequiredEnvVarKeys.ToArray(),
                                                  (string Message) =>
            {
                Instance.LoggingService.WriteLogs(BLoggingServiceMessageUtility.Single(EBLoggingServiceLogType.Critical, Message), Instance.ProgramID, "Initialization");
            }))
            {
                return(false);
            }

            Instance.ProgramID = Instance.RequiredEnvironmentVariables["PROGRAM_ID"];

            /*
             * Parsing http server port
             */
            if (!int.TryParse(Instance.RequiredEnvironmentVariables["PORT"], out int _ServPort))
            {
                Instance.LoggingService.WriteLogs(BLoggingServiceMessageUtility.Single(EBLoggingServiceLogType.Critical, "Given http server port is invalid."), Instance.ProgramID, "Initialization");
                return(false);
            }
            Instance.ServerPort = _ServPort;

            _Result = Instance;
            return(true);
        }
Beispiel #11
0
        private bool TestDeleteLocalFile()
        {
            bool bLocalFailure = false;

            //Test delete local file
            PrintAction?.Invoke("TestDeleteLocalFile->Log-> Testing DeleteLocalFile...");
            BUtility.DeleteFile(FileLocalPath, (string Message) =>
            {
                PrintAction?.Invoke("TestDeleteLocalFile->Error-> " + Message);
                bLocalFailure = true;
            });
            if (bLocalFailure)
            {
                PrintAction?.Invoke("TestDeleteLocalFile->Error-> DeleteLocalFile failed.");
                return(false);
            }
            PrintAction?.Invoke("TestDeleteLocalFile->Log-> DeleteLocalFile succeed.");
            return(true);
        }
Beispiel #12
0
            private BWebServiceResponse OnRequest_CreateTestUser(HttpListenerContext _Context, Action <string> _ErrorMessageAction)
            {
                string OptionalName        = BUtility.RandomString(12, true) + "_test";
                string EmailWithoutPostfix = OptionalName + "@test.com";

                var AccumulatedSSOMethodRightsOrDefault = new JArray()
                {
                    JObject.Parse(JsonConvert.SerializeObject(
                                      new AccessScope()
                    {
                        WildcardPath = "*",
                        AccessRights = new List <string>()
                        {
                            "GET", "POST", "PUT", "DELETE"
                        }
                    }))
                };

                if (!CreateUser(out string _UserID, EmailWithoutPostfix, _ErrorMessageAction, OptionalName))
                {
                    return(BWebResponse.InternalError("User creation process has been failed."));
                }

                if (!CreateAuthMethod(out string _ApiKey, _UserID, _ErrorMessageAction))
                {
                    return(BWebResponse.InternalError("Auth method creation process has been failed."));
                }

                if (!Controller_Rights_Internal.Get().GrantUserWithRights(false, _UserID, AccumulatedSSOMethodRightsOrDefault, _ErrorMessageAction))
                {
                    return(BWebResponse.InternalError("Grant user with rights process has been failed."));
                }

                return(BWebResponse.StatusCreated("User has been created.", new JObject()
                {
                    [UserDBEntry.KEY_NAME_USER_ID] = _UserID,
                    [AuthMethod.API_KEY_PROPERTY] = _ApiKey,
                    [AuthMethod.USER_EMAIL_PROPERTY] = EmailWithoutPostfix
                }));
            }
Beispiel #13
0
        public bool GetCallbackFromRequest(out Func <BWebServiceBase> _Initializer, out string _MatchedPrefix, HttpListenerContext _Context)
        {
            _Initializer   = null;
            _MatchedPrefix = null;

            if (_Context == null || Prefixes_SortedByLength == null || Prefixes_SortedByLength.Length == 0 || ListenerInitializer == null)
            {
                return(false);
            }

            for (var i = (Prefixes_SortedByLength.Length - 1); i >= 0; i--)
            {
                var Prefix = Prefixes_SortedByLength[i];
                if (Regex.IsMatch(_Context.Request.RawUrl, BUtility.WildCardToRegular(Prefix)))
                {
                    _MatchedPrefix = Prefix;
                    _Initializer   = ListenerInitializer;
                    return(_Initializer != null);
                }
            }
            return(false);
        }
Beispiel #14
0
        private static void AnalyzeResponse(
            HttpWebResponse _Response,
            out bool _bHttpRequestSuccess,
            out int _HttpRequestResponseCode,
            out string _HttpRequestResponseContentType,
            out BStringOrStream _HttpRequestResponseContent,
            out Dictionary <string, IEnumerable <string> > _HttpRequestResponseHeaders,
            Action <string> _ErrorMessageAction)
        {
            _bHttpRequestSuccess            = false;
            _HttpRequestResponseCode        = BWebResponse.Error_InternalError_Code;
            _HttpRequestResponseContentType = "";
            _HttpRequestResponseContent     = null;
            _HttpRequestResponseHeaders     = new Dictionary <string, IEnumerable <string> >();

            try
            {
                _HttpRequestResponseCode = (int)_Response.StatusCode;

                BWebUtilities.InjectHeadersIntoDictionary(_Response.Headers, _HttpRequestResponseHeaders);

                _HttpRequestResponseContentType = _Response.ContentType;

                using (var ResStream = _Response.GetResponseStream())
                {
                    var CopyStream = new BMemoryTributary(BUtility.ReadToEnd(ResStream));

                    _HttpRequestResponseContent = new BStringOrStream(CopyStream, CopyStream.Length, () => { try { CopyStream?.Dispose(); } catch { } });
                }

                _bHttpRequestSuccess = true;
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("Error: RequestRedirection-AnalyzeResponse: " + e.Message + ", Trace: " + e.StackTrace);
                _bHttpRequestSuccess = false;
            }
        }
Beispiel #15
0
        public static bool CheckBaseFinalFullContainment(List <AccessScope> _BaseScopeList, List <AccessScope> _FinalScopeList)
        {
            foreach (var FinalScope in _FinalScopeList)
            {
                bool bFound = false;

                foreach (var BaseScope in _BaseScopeList)
                {
                    //Checked
                    if (Regex.IsMatch(FinalScope.WildcardPath, BUtility.WildCardToRegular(BaseScope.WildcardPath)))
                    {
                        bool bAccessRightsExist = true;

                        foreach (var FinalAccessRight in FinalScope.AccessRights)
                        {
                            if (!BaseScope.AccessRights.Contains(FinalAccessRight))
                            {
                                bAccessRightsExist = false;
                                break;
                            }
                        }

                        if (bAccessRightsExist)
                        {
                            bFound = true;
                            break;
                        }
                    }
                }

                if (!bFound)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #16
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            if (_Context.Request.HttpMethod != "POST")
            {
                _ErrorMessageAction?.Invoke("SSOAzureTokenRefreshRequest: POST method is accepted. But received request method:  " + _Context.Request.HttpMethod);

                return(BWebResponse.MethodNotAllowed("POST method is accepted. But received request method:  " + _Context.Request.HttpMethod));
            }

            if (!BWebUtilities.DoesContextContainHeader(out List <string> ClientAuthorizationHeaderValues, out string _, _Context, "client-authorization") ||
                !BUtility.CheckAndGetFirstStringFromList(ClientAuthorizationHeaderValues, out string ClientAuthorization) ||
                ClientAuthorization.Length == 0)
            {
                return(BWebResponse.BadRequest("Authorization header must be set validly."));
            }

            //Check and try refresh if expired
            if (new Controller_SSOAccessToken(ClientAuthorization, DatabaseService, MemoryService, AzureAD_AppID, AzureAD_ClientSecret, SSOSuperAdmins, _ErrorMessageAction)
                .PerformCheckAndRefresh(
                    out Controller_SSOAccessToken.EPerformCheckAndRefreshSuccessStatus SuccessStatus,
                    out ClientAuthorization,
                    out string UserID,
                    out string EmailAddressWithoutPostfix) &&
                ClientAuthorization != null && ClientAuthorization.Length > 0)
            {
                return(BWebResponse.StatusOK("Success.", new JObject()
                {
                    ["token"] = ClientAuthorization,
                    ["status"] = SuccessStatus == Controller_SSOAccessToken.EPerformCheckAndRefreshSuccessStatus.Refreshed ? "Refreshed" : "AlreadyValid",
                    ["userId"] = UserID,
                    ["email"] = EmailAddressWithoutPostfix
                }));
            }

            return(BWebResponse.Unauthorized("Please re-login."));
        }
Beispiel #17
0
        public static bool GenerateNonExistentUniqueID(
            WebServiceBaseTimeoutable _Request,
            IBDatabaseServiceInterface _DatabaseService,
            string _TableName,
            string _TableKeyName,
            string[] _TableEntryMustHaveProperties,
            EGetClearance _GetClearance,
            out string _GeneratedUniqueID,
            out BWebServiceResponse _FailureResponse,
            Action <string> _ErrorMessageAction = null)
        {
            _GeneratedUniqueID = null;
            _FailureResponse   = BWebResponse.InternalError("");

            int ExistenceTrial = 0;

            while (_GeneratedUniqueID == null && ExistenceTrial < 3)
            {
                if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, false), out _GeneratedUniqueID, _ErrorMessageAction))
                {
                    _FailureResponse = BWebResponse.InternalError("Hashing operation has failed.");
                    return(false);
                }

                if (_GetClearance == EGetClearance.Yes && !Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(_Request.InnerProcessor, _TableName, _GeneratedUniqueID, _ErrorMessageAction))
                {
                    _FailureResponse = BWebResponse.InternalError("Atomic operation control has failed.");
                    return(false);
                }

                if (!_DatabaseService.GetItem(
                        _TableName,
                        _TableKeyName,
                        new BPrimitiveType(_GeneratedUniqueID),
                        _TableEntryMustHaveProperties,
                        out JObject ExistenceCheck,
                        _ErrorMessageAction))
                {
                    _FailureResponse = BWebResponse.InternalError("Database existence check operation has failed.");
                    return(false);
                }
                if (ExistenceCheck != null)
                {
                    if (_GetClearance == EGetClearance.Yes)
                    {
                        Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(_Request.InnerProcessor, _TableName, _GeneratedUniqueID, _ErrorMessageAction);
                    }

                    _GeneratedUniqueID = null;
                    ExistenceTrial++;
                }
                else
                {
                    break;
                }
            }
            if (_GeneratedUniqueID == null)
            {
                _FailureResponse = BWebResponse.InternalError("Unique model ID generation operation has failed.");
                return(false);
            }
            return(true);
        }
Beispiel #18
0
        public static InterServicesRequestResponse InterServicesRequest(
            InterServicesRequestRequest _Request,
            bool _bKillProcessOnAddAccessTokenForServiceExecutionFailure = true,
            Action <string> _ErrorMessageAction = null)
        {
            var             bHttpRequestSuccess            = false;
            var             HttpRequestResponseCode        = BWebResponse.Error_InternalError_Code;
            var             HttpRequestResponseContentType = "";
            BStringOrStream HttpRequestResponseContent     = null;
            Dictionary <string, IEnumerable <string> > HttpRequestResponseHeaders = null;

            var Request = (HttpWebRequest)WebRequest.Create(_Request.DestinationServiceUrl);

            Request.Method = _Request.RequestMethod;
            Request.ServerCertificateValidationCallback = (a, b, c, d) => true;
            Request.AllowAutoRedirect = false;

            if (_Request.bWithAuthToken)
            {
                //If context-headers already contain authorization; we must rename it to client-authorization to prevent override.
                if (_Request.UseContextHeaders != null &&
                    BWebUtilities.DoesContextContainHeader(out List <string> AuthorizationHeaderValues, out string CaseSensitive_FoundHeaderKey, _Request.UseContextHeaders, "authorization") &&
                    BUtility.CheckAndGetFirstStringFromList(AuthorizationHeaderValues, out string ClientAuthorization))
                {
                    _Request.UseContextHeaders.Request.Headers.Remove(CaseSensitive_FoundHeaderKey);
                    _Request.UseContextHeaders.Request.Headers.Add("client-authorization", ClientAuthorization);
                }
            }

            var ExcludeHeaderKeysForRequest = LowerContentOfStrings(_Request.ExcludeHeaderKeysForRequest);

            if (_Request.UseContextHeaders != null)
            {
                InsertHeadersFromContextInto(_Request.UseContextHeaders, (string _Key, string _Value) =>
                {
                    if (ExcludeHeaderKeysForRequest != null && ExcludeHeaderKeysForRequest.Contains(_Key.ToLower()))
                    {
                        return;
                    }

                    Request.Headers.Add(_Key, _Value);
                });
            }
            if (_Request.Headers != null)
            {
                InsertHeadersFromDictionaryInto(_Request.Headers, (string _Key, string _Value) =>
                {
                    if (ExcludeHeaderKeysForRequest != null && ExcludeHeaderKeysForRequest.Contains(_Key.ToLower()))
                    {
                        return;
                    }

                    Request.Headers.Add(_Key, _Value);
                });
            }

            try
            {
                if (_Request.RequestMethod != "GET" && /*&& _Request.RequestMethod != "DELETE"*/
                    _Request.Content != null && ((_Request.Content.Type == EBStringOrStreamEnum.Stream && _Request.Content.Stream != null) || (_Request.Content.Type == EBStringOrStreamEnum.String && _Request.Content.String != null && _Request.Content.String.Length > 0)))
                {
                    Request.ContentType = _Request.ContentType;

                    using (var OStream = Request.GetRequestStream())
                    {
                        if (_Request.Content.Type == EBStringOrStreamEnum.Stream)
                        {
                            _Request.Content.Stream.CopyTo(OStream);
                        }
                        else
                        {
                            using (var RStream = new StreamWriter(OStream))
                            {
                                RStream.Write(_Request.Content.String);
                            }
                        }
                    }
                }

                try
                {
                    using (var Response = (HttpWebResponse)Request.GetResponse())
                    {
                        AnalyzeResponse(Response, out bHttpRequestSuccess, out HttpRequestResponseCode, out HttpRequestResponseContentType, out HttpRequestResponseContent, out HttpRequestResponseHeaders, _ErrorMessageAction);
                    }
                }
                catch (Exception e)
                {
                    if (e is WebException)
                    {
                        using (var ErrorResponse = (HttpWebResponse)(e as WebException).Response)
                        {
                            AnalyzeResponse(ErrorResponse, out bHttpRequestSuccess, out HttpRequestResponseCode, out HttpRequestResponseContentType, out HttpRequestResponseContent, out HttpRequestResponseHeaders, _ErrorMessageAction);
                        }
                    }
                    else
                    {
                        _ErrorMessageAction?.Invoke("Error: InterServicesRequest: " + e.Message + ", Trace: " + e.StackTrace);
                        bHttpRequestSuccess = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("Error: InterServicesRequest: " + e.Message + ", Trace: " + e.StackTrace);
                bHttpRequestSuccess = false;
            }

            if (!bHttpRequestSuccess)
            {
                _ErrorMessageAction?.Invoke("Error: Request has failed due to an internal api gateway error. Service endpoint: " + _Request.DestinationServiceUrl);
                return(InterServicesRequestResponse.InternalErrorOccured("Request has failed due to an internal api gateway error."));
            }

            if (_Request.UseContextHeaders != null)
            {
                InsertHeadersFromDictionaryIntoContext(HttpRequestResponseHeaders, _Request.UseContextHeaders);
            }

            return(new InterServicesRequestResponse()
            {
                bSuccess = true,
                ResponseCode = HttpRequestResponseCode,
                ContentType = HttpRequestResponseContentType,
                ResponseHeaders = HttpRequestResponseHeaders,
                Content = HttpRequestResponseContent
            });
        }
Beispiel #19
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            if (!UrlParameters.TryGetValue("redirect_url", out string RedirectUrlEncoded) || RedirectUrlEncoded.Length == 0)
            {
                RedirectUrlEncoded = DEFAULT_REDIRECT_URL_ENCODED;
            }

            if (_Context.Request.HttpMethod != "GET")
            {
                _ErrorMessageAction?.Invoke("SSOLoginRequest: GET method is accepted. But received request method:  " + _Context.Request.HttpMethod);

                return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), true, BWebResponse.Error_BadRequest_Code, "GET method is accepted. But received request method:  " + _Context.Request.HttpMethod));
            }

            if (!UrlParameters.TryGetValue("tenant", out string TenantName) || TenantName.Length == 0)
            {
                TenantName = DEFAULT_TENANT_NAME;
            }
            else
            {
                TenantName = TenantName.ToLower();
            }

            //Existing token from header
            string ClientAuthorization = null;

            if (BWebUtilities.DoesContextContainHeader(out List <string> ClientAuthorizationHeaderValues, out string _, _Context, "client-authorization"))
            {
                BUtility.CheckAndGetFirstStringFromList(ClientAuthorizationHeaderValues, out ClientAuthorization);
                if (ClientAuthorization != null && ClientAuthorization.Length == 0)
                {
                    ClientAuthorization = null;
                }
            }

            //Existing token from url parameters
            //Note: Must be token type prepended. Example: ?existing_token=bearer%20abc123123
            if (!UrlParameters.TryGetValue("existing_token", out string ExistingToken) || ExistingToken.Length == 0)
            {
                ExistingToken = null;
            }
            else
            {
                ExistingToken = WebUtility.UrlDecode(ExistingToken);
            }

            //If both existing tokens are non-null; it is error
            if (ClientAuthorization != null && ExistingToken != null)
            {
                _ErrorMessageAction?.Invoke("Error: SSOLoginRequest: Both existing tokens from url parameters and headers are non-null.");

                return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), true, BWebResponse.Error_BadRequest_Code, "Both existing tokens from url parameters and headers are non-null."));
            }

            //From now on, use ClientAuthorization; not ExistingToken
            if (ExistingToken != null)
            {
                ClientAuthorization = ExistingToken;
            }

            //Check and try refresh if expired
            if (ClientAuthorization != null &&
                new Controller_SSOAccessToken(ClientAuthorization, DatabaseService, MemoryService, AzureAD_AppID, AzureAD_ClientSecret, SSOSuperAdmins, _ErrorMessageAction)
                .PerformCheckAndRefresh(
                    out Controller_SSOAccessToken.EPerformCheckAndRefreshSuccessStatus _,
                    out ClientAuthorization,
                    out string UserID,
                    out string _))
            {
                return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), false, 0, null, UserID, ClientAuthorization));
            }

            //Get api passthrough endpoint from internal set state
            var LocalErrorString = "";

            if (!InternalSetState.GetValueFromMemoryService(
                    out string ApiPassthroughEndpoint,
                    InternalSetState.API_PASSTHROUGH_PUBLIC_ENDPOINT_PROPERTY,
                    MemoryService,
                    (string _Message) =>
            {
                LocalErrorString = _Message;
                _ErrorMessageAction?.Invoke(_Message);
            }))
            {
                return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), true, 500, LocalErrorString));
            }

            string ServersideRedirectUrl = WebUtility.UrlEncode(ApiPassthroughEndpoint + "/auth/login/azure/callback");

            string AzureAuthenticationEndpointBase =
                "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
                + "?client_id=" + AzureAD_AppID
                + "&response_type=id_token code"
                + "&redirect_uri=" + ServersideRedirectUrl;

            var    TrialCount = 0;
            string SSOStateUniqueID;
            BMemoryQueryParameters SSOStateUniqueID_QueryParameters;

            do
            {
                if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out SSOStateUniqueID, _ErrorMessageAction))
                {
                    return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), true, 500, "SSO State ID generation has failed."));
                }

                SSOStateUniqueID_QueryParameters = SSOStateMEntry.ID_SSO_STATE_MEMORY_SERVICE_KEY(SSOStateUniqueID);

                if (!MemoryService.SetKeyValueConditionally(
                        SSOStateUniqueID_QueryParameters,
                        new Tuple <string, BPrimitiveType>(
                            SSOStateMEntry.HASH_KEY,
                            new BPrimitiveType(JsonConvert.SerializeObject(
                                                   new SSOStateMEntry()
                {
                    ServersideRedirectUrl = ServersideRedirectUrl,
                    TenantName = TenantName,
                    Status = SSOStateMEntry.STATUS_AUTHENTICATING
                })
                                               )
                            ),
                        _ErrorMessageAction))
                {
                    SSOStateUniqueID = null;
                }
            } while (SSOStateUniqueID == null && ++TrialCount < 5);

            if (SSOStateUniqueID == null)
            {
                return(SSOCommon.MakeCallerRedirected(WebUtility.UrlDecode(RedirectUrlEncoded), true, 500, "Unique SSO State ID generation has failed."));
            }
            MemoryService.SetKeyExpireTime(SSOStateUniqueID_QueryParameters, TimeSpan.FromSeconds(120), _ErrorMessageAction);

            var AzureAuthenticationEndpoint = AzureAuthenticationEndpointBase
                                              + "&scope=" + SSOCommon.SCOPE_URL_ENCODED
                                              + "&response_mode=form_post"
                                              + "&nonce=" + SSOStateUniqueID
                                              + "&state="
                                              + WebUtility.UrlEncode(
                "redirect_url=" + RedirectUrlEncoded +
                "&tenant=" + TenantName +
                "&state=" + SSOStateUniqueID);

            return(SSOCommon.MakeCallerRedirected(AzureAuthenticationEndpoint, false, 0, null));
        }
Beispiel #20
0
        /// <summary>
        ///
        /// <para>CustomSubscribe:</para>
        ///
        /// <para>Subscribes to given custom topic</para>
        ///
        /// <para>Check <seealso cref="IBPubSubServiceInterface.CustomSubscribe"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool CustomSubscribe(string _CustomTopic, Action <string, string> _OnMessage, Action <string> _ErrorMessageAction = null, bool _SubscribeSingleMessage = false)
        {
            if (_CustomTopic != null && _CustomTopic.Length > 0 && _OnMessage != null && BUtility.CalculateStringMD5(_CustomTopic, out string TopicMD5, _ErrorMessageAction))
            {
                if (EnsureQueueExists(TopicMD5, out string QueueUrl, _ErrorMessageAction))
                {
                    var SubscriptionCancellationVar = new BValue <bool>(false, EBProducerStatus.MultipleProducer);
                    var SubscriptionThread          = new Thread(() =>
                    {
                        Thread.CurrentThread.IsBackground = true;

                        while (!SubscriptionCancellationVar.Get())
                        {
                            ReceiveMessageResponse Response;
                            try
                            {
                                using (var ReceiveMessageTask = SQSClient.ReceiveMessageAsync(QueueUrl))
                                {
                                    ReceiveMessageTask.Wait();
                                    Response = ReceiveMessageTask.Result;
                                }
                            }
                            catch (Exception e)
                            {
                                Response = null;
                                _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomSubscribe: " + e.Message + ", Trace: " + e.StackTrace);
                                if (e.InnerException != null && e.InnerException != e)
                                {
                                    _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomSubscribe->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                                }
                            }

                            if (Response == null || Response.Messages == null || Response.Messages.Count == 0)
                            {
                                Thread.Sleep(1000);
                                continue;
                            }

                            var AckDictionary = new Dictionary <string, string>();

                            foreach (var MessageContainer in Response.Messages)
                            {
                                if (MessageContainer != null)
                                {
                                    if (!AckDictionary.ContainsKey(MessageContainer.MessageId))
                                    {
                                        AckDictionary.Add(MessageContainer.MessageId, MessageContainer.ReceiptHandle);
                                    }

                                    string Data = MessageContainer.Body;

                                    if (UniqueMessageDeliveryEnsurer != null)
                                    {
                                        UniqueMessageDeliveryEnsurer.Subscribe_ClearAndExtractTimestampFromMessage(ref Data, out string TimestampHash);

                                        if (UniqueMessageDeliveryEnsurer.Subscription_EnsureUniqueDelivery(_CustomTopic, TimestampHash, _ErrorMessageAction))
                                        {
                                            _OnMessage?.Invoke(_CustomTopic, Data);
                                        }
                                    }
                                    else
                                    {
                                        _OnMessage?.Invoke(_CustomTopic, Data);
                                    }
                                }
                            }

                            var AckArray = new List <DeleteMessageBatchRequestEntry>();
                            foreach (var Current in AckDictionary)
                            {
                                AckArray.Add(new DeleteMessageBatchRequestEntry(Current.Key, Current.Value));
                            }

                            try
                            {
                                using (var DeleteMessageBatchTask = SQSClient.DeleteMessageBatchAsync(QueueUrl, AckArray))
                                {
                                    DeleteMessageBatchTask.Wait();
                                }
                            }
                            catch (Exception e)
                            {
                                _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomSubscribe: " + e.Message + ", Trace: " + e.StackTrace);
                                if (e.InnerException != null && e.InnerException != e)
                                {
                                    _ErrorMessageAction?.Invoke("BPubSubServiceAWS->CustomSubscribe->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                                }
                            }
                        }
                    });
                    SubscriptionThread.Start();

                    lock (SubscriberThreadsDictionaryLock)
                    {
                        SubscriberThreadsDictionary.Add(_CustomTopic, new BTuple <Thread, BValue <bool> >(SubscriptionThread, SubscriptionCancellationVar));
                    }
                    return(true);
                }
            }
            return(false);
        }
Beispiel #21
0
        private bool AccessCheck(out BWebServiceResponse _FailureResponse, out bool _bSSOTokenRefreshed, out string _NewSSOTokenAfterRefresh, HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            _FailureResponse = new BWebServiceResponse();

            _bSSOTokenRefreshed      = false;
            _NewSSOTokenAfterRefresh = "";

            if (bAuthCheck)
            {
                //Check for authorization header
                if (!BWebUtilities.DoesContextContainHeader(out List <string> Authorization, out string _, _Context, "authorization"))
                {
                    _FailureResponse = BWebResponse.Unauthorized("Authorization header must be set.");
                    return(false);
                }

                var RequestObject = new JObject()
                {
                    ["forUrlPath"]    = _Context.Request.RawUrl,
                    ["requestMethod"] = _Context.Request.HttpMethod
                };
                if (BUtility.CheckAndGetFirstStringFromList(Authorization, out string _Authorization))
                {
                    RequestObject["authorization"] = _Authorization;
                }
                else //Zero length
                {
                    _FailureResponse = BWebResponse.Unauthorized("Authorization header must be set.");
                    return(false);
                }

                GetTracingService()?.On_FromGatewayToService_Sent(_Context, _ErrorMessageAction);
                var Result = BWebServiceExtraUtilities.InterServicesRequest(new BWebServiceExtraUtilities.InterServicesRequestRequest()
                {
                    DestinationServiceUrl = AuthServiceBaseUrl + "/auth/access_check",
                    RequestMethod         = "POST",
                    ContentType           = "application/json",
                    Content           = new BStringOrStream(RequestObject.ToString()),
                    bWithAuthToken    = true,
                    UseContextHeaders = _Context,
                },
                                                                            false,
                                                                            _ErrorMessageAction);
                GetTracingService()?.On_FromServiceToGateway_Received(_Context, _ErrorMessageAction);

                if (!Result.bSuccess || Result.ResponseCode >= 400)
                {
                    if (Result.ResponseCode == BWebResponse.Error_Unauthorized_Code ||
                        Result.ResponseCode == BWebResponse.Error_Forbidden_Code)
                    {
                        _FailureResponse = new BWebServiceResponse(Result.ResponseCode, Result.Content, Result.ContentType);
                        return(false);
                    }

                    _ErrorMessageAction?.Invoke("Access check internal call has failed: Response: " + Result.ResponseCode + " -> " + Result.Content.String + ", Request: " + RequestObject.ToString());
                    _FailureResponse = BWebResponse.InternalError("Internal access check call has failed.");
                    return(false);
                }

                var ResponseContent = Result.Content.ToString();
                try
                {
                    var Parsed = JObject.Parse(ResponseContent);
                    Authenticated_UserID        = (string)Parsed["userId"];
                    Authenticated_UserName      = (string)Parsed["userName"];
                    Authenticated_UserEmail     = (string)Parsed["userEmail"];
                    Authenticated_AuthMethodKey = (string)Parsed["authMethodKey"];
                    _bSSOTokenRefreshed         = (bool)Parsed["ssoTokenRefreshed"];
                    _NewSSOTokenAfterRefresh    = (string)Parsed["newSSOTokenAfterRefresh"];
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("HandleRequest->AccessCheck: Error during content parse: " + ResponseContent + ", Message: " + e.Message + ", Trace: " + e.StackTrace);
                    _FailureResponse = BWebResponse.InternalError("Request has failed due to an internal api gateway error.");
                    return(false);
                }

                _Context.Request.Headers.Set("authorized-u-id", Authenticated_UserID);
                _Context.Request.Headers.Set("authorized-u-name", Authenticated_UserName);
                _Context.Request.Headers.Set("authorized-u-email", Authenticated_UserEmail);
                _Context.Request.Headers.Set("authorized-u-auth-key", Authenticated_AuthMethodKey);
            }
            return(true);
        }
Beispiel #22
0
 public static bool GenerateUserID(out string _NewUserID, Action <string> _ErrorMessageAction)
 {
     return(BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out _NewUserID, _ErrorMessageAction));
 }
Beispiel #23
0
        /// <summary>
        ///
        /// <para>DownloadFile:</para>
        ///
        /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool DownloadFile(string _BucketName, string _KeyInBucket, BStringOrStream _Destination, Action <string> _ErrorMessageAction = null, ulong _StartIndex = 0, ulong _Size = 0)
        {
            BlobContainerClient       ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName);
            BlobClient                Blob            = ContainerClient.GetBlobClient(_KeyInBucket);
            Response <BlobProperties> Response        = Blob.GetProperties();

            if (AServiceClient == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: AServiceClient is null.");
                return(false);
            }

            if (!CheckFileExistence(
                    _BucketName,
                    _KeyInBucket,
                    out bool bExists,
                    _ErrorMessageAction
                    ))
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: CheckFileExistence failed.");
                return(false);
            }

            if (!bExists)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: File does not exist in the File Service.");
                return(false);
            }

            HttpRange Range = default(HttpRange);

            if (_Size > 0)
            {
                Range = new HttpRange((long)_StartIndex, (long)(_StartIndex + _Size));
            }

            try
            {
                if (_Destination.Type == EBStringOrStreamEnum.String)
                {
                    using (FileStream FS = File.Create(_Destination.String))
                    {
                        BlobDownloadInfo DlInfo = Blob.Download(Range).Value;
                        DlInfo.Content.CopyTo(FS);

                        DlInfo.Dispose();
                    }

                    if (!BUtility.DoesFileExist(
                            _Destination.String,
                            out bool bLocalFileExists,
                            _ErrorMessageAction))
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: DoesFileExist failed.");
                        return(false);
                    }

                    if (!bLocalFileExists)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Download finished, but still file does not locally exist.");
                        return(false);
                    }
                }
                else
                {
                    if (_Destination.Stream == null)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Destination stream is null.");
                        return(false);
                    }


                    BlobDownloadInfo DlInfo = Blob.Download(Range).Value;
                    DlInfo.Content.CopyTo(_Destination.Stream);
                    DlInfo.Dispose();

                    try
                    {
                        _Destination.Stream.Position = 0;
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }
Beispiel #24
0
        public static bool RunTests()
        {
            if (!BUtility.GetEnvironmentVariables(out Dictionary <string, string> RequiredEnvVars,
                                                  new string[][]
            {
                new string[] { "GC_PROJECT_ID" }
            },
                                                  Console.WriteLine))
            {
                return(false);
            }

            /*
             * Logging Services tests
             */
            var LoggingTests_GC = new BLoggingServiceTest(
                new BLoggingServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_GC.Start())
            {
                return(false);
            }

            var LoggingTests_AWS = new BLoggingServiceTest(
                new BLoggingServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_AWS.Start())
            {
                return(false);
            }

            var LoggingTests_Azure = new BLoggingServiceTest(
                new BLoggingServiceAzure(
                    RequiredEnvVars["APPINSIGHTS_INSTRUMENTATIONKEY"],
                    Console.WriteLine),
                Console.WriteLine);

            if (!LoggingTests_Azure.Start())
            {
                return(false);
            }

            /*
             * E-mail Services tests
             */
            var Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var MailTests_SendGrid = new BEmailServicesTest(
                new BMailServiceSendGrid(
                    RequiredEnvVars["SENDGRID_API_KEY"],
                    "*****@*****.**",
                    "BTest",
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MailTests_SendGrid.Start())
            {
                return(false);
            }

            /*
             * Database Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var DBTests_GC = new BDatabaseServicesTest(
                new BDatabaseServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_GC.Start())
            {
                return(false);
            }

            Comparator.Next();

            var DBTests_AWS = new BDatabaseServicesTest(
                new BDatabaseServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_AWS.Start())
            {
                return(false);
            }

            Comparator.Next();

            var DBTests_MongoDB = new BDatabaseServicesTest(
                new BDatabaseServiceMongoDB(
                    RequiredEnvVars["MONGO_DB_CONNECTION_STRING"],
                    RequiredEnvVars["MONGO_DB_DATABASE"],
                    Console.WriteLine),
                "BTest",
                "TestKey",
                Comparator.AddLine);

            if (!DBTests_MongoDB.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            /*
             * Memory and Pub/Sub Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var MemTests_WithRedisPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceRedis(
                        RequiredEnvVars["REDIS_ENDPOINT"],
                        int.Parse(RequiredEnvVars["REDIS_PORT"]),
                        RequiredEnvVars["REDIS_PASSWORD"],
                        bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                        true,
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithRedisPubSub.Start())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithGCPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceGC(
                        RequiredEnvVars["GC_PROJECT_ID"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithGCPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithAWSPubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceAWS(
                        RequiredEnvVars["AWS_ACCESS_KEY"],
                        RequiredEnvVars["AWS_SECRET_KEY"],
                        RequiredEnvVars["AWS_REGION"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithAWSPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            Comparator.Next();

            var MemTests_WithAzurePubSub = new BMemoryPubSubServiceTest(
                new BMemoryServiceRedis(
                    RequiredEnvVars["REDIS_ENDPOINT"],
                    int.Parse(RequiredEnvVars["REDIS_PORT"]),
                    RequiredEnvVars["REDIS_PASSWORD"],
                    bool.Parse(RequiredEnvVars["REDIS_SSL_ENABLED"]),
                    new BPubSubServiceAzure(
                        RequiredEnvVars["AZURE_CLIENT_ID"],
                        RequiredEnvVars["AZURE_CLIENT_SECRET"],
                        RequiredEnvVars["AZURE_TENANT_ID"],
                        RequiredEnvVars["AZURE_NAMESPACE_ID"],
                        RequiredEnvVars["AZURE_NAMESPACE_CONNSTR"],
                        Console.WriteLine),
                    true,
                    Console.WriteLine),
                Comparator.AddLine);

            if (!MemTests_WithAWSPubSub.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            /*
             * File Services tests
             */
            Comparator = new BRelativeTestsResultComparator(Console.WriteLine);

            var FSTests_GC = new BFileServiceTest(
                new BFileServiceGC(
                    RequiredEnvVars["GC_PROJECT_ID"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_GC.Start())
            {
                return(false);
            }

            Comparator.Next();

            var FSTests_AWS = new BFileServiceTest(
                new BFileServiceAWS(
                    RequiredEnvVars["AWS_ACCESS_KEY"],
                    RequiredEnvVars["AWS_SECRET_KEY"],
                    RequiredEnvVars["AWS_REGION"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_AWS.Start())
            {
                return(false);
            }

            Comparator.Next();

            var FSTests_AZ = new BFileServiceTest(
                new BFileServiceAZ(
                    RequiredEnvVars["AZ_STORAGE_SERVICE"],
                    RequiredEnvVars["AZ_STORAGE_ACCOUNT"],
                    RequiredEnvVars["AZ_STORAGE_ACCOUNT_KEY"],
                    RequiredEnvVars["AZ_STORAGE_RESOURCE_GROUP"],
                    RequiredEnvVars["AZ_STORAGE_MANAGEMENT_APP_ID"],
                    RequiredEnvVars["AZ_STORAGE_MANAGEMENT_SECRET"],
                    RequiredEnvVars["AZ_SUBSCRIPTION_ID"],
                    RequiredEnvVars["AZ_TENANT_ID"],
                    RequiredEnvVars["AZ_STORAGE_LOCATION"],
                    Console.WriteLine),
                RequiredEnvVars["FILESERVICE_BUCKET"],
                RequiredEnvVars["FILESERVICE_REMOTE_PATH"],
                RequiredEnvVars["FILESERVICE_TEST_FILE_LOCAL_PATH"],
                Comparator.AddLine);

            if (!FSTests_AWS.Start())
            {
                return(false);
            }

            if (!Comparator.Compare())
            {
                return(false);
            }

            return(true);
        }
Beispiel #25
0
        private BWebServiceResponse CreateAccessMethodForUser(HttpListenerContext _Context, out bool _bSetClearanceForApiKey, out string _ApiKey, Action <string> _ErrorMessageAction)
        {
            _bSetClearanceForApiKey = false;
            _ApiKey = null;

            AuthMethod NewMethod = null;

            using (var InputStream = _Context.Request.InputStream)
            {
                using (var ResponseReader = new StreamReader(InputStream))
                {
                    try
                    {
                        NewMethod = JsonConvert.DeserializeObject <AuthMethod>(ResponseReader.ReadToEnd());
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("User_CreateListAccessMethods_ForUser->CreateAccessMethodForUser: Read request body stage has failed. Exception: " + e.Message + ", Trace: " + e.StackTrace);
                        return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form."));
                    }
                }
            }

            if (NewMethod == null)
            {
                return(BWebResponse.BadRequest("Request body does not contain all mandatory information or some fields are invalid."));
            }

            BPrimitiveType AuthMethodKey = null;

            if (NewMethod.Method == AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD)
            {
                if (NewMethod.UserEmail == null || NewMethod.PasswordMD5 == null || NewMethod.UserEmail.Length == 0 || NewMethod.PasswordMD5.Length == 0)
                {
                    return(BWebResponse.BadRequest("Request body does not contain all fields."));
                }
                AuthMethodKey = new BPrimitiveType(NewMethod.UserEmail + NewMethod.PasswordMD5);
            }
            else if (NewMethod.Method == AuthMethod.Methods.USER_NAME_PASSWORD_METHOD)
            {
                if (NewMethod.UserName == null || NewMethod.PasswordMD5 == null || NewMethod.UserName.Length == 0 || NewMethod.PasswordMD5.Length == 0)
                {
                    return(BWebResponse.BadRequest("Request body does not contain all fields."));
                }
                AuthMethodKey = new BPrimitiveType(NewMethod.UserName + NewMethod.PasswordMD5);
            }
            else if (NewMethod.Method == AuthMethod.Methods.API_KEY_METHOD)
            {
                int ExistenceTrial = 0;
                while (ExistenceTrial < 3)
                {
                    if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, false), out NewMethod.ApiKey, _ErrorMessageAction))
                    {
                        return(BWebResponse.InternalError("Hashing error."));
                    }
                    NewMethod.ApiKey = NewMethod.ApiKey.ToUpper();

                    if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY + ":" + NewMethod.ApiKey, _ErrorMessageAction))
                    {
                        return(BWebResponse.InternalError("Atomic operation control has failed."));
                    }

                    if (!DatabaseService.UpdateItem(
                            UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(),
                            UniqueUserFieldsDBEntry.KEY_NAME_API_KEY,
                            new BPrimitiveType(NewMethod.ApiKey),
                            new JObject()
                    {
                        [UserDBEntry.KEY_NAME_USER_ID] = RequestedUserID
                    },
                            out JObject _,
                            EBReturnItemBehaviour.DoNotReturn,
                            DatabaseService.BuildAttributeNotExistCondition(UniqueUserFieldsDBEntry.KEY_NAME_API_KEY),
                            _ErrorMessageAction))
                    {
                        Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY + ":" + NewMethod.ApiKey, _ErrorMessageAction);
                        ExistenceTrial++;
                    }
                    else
                    {
                        _bSetClearanceForApiKey = true;
                        _ApiKey = NewMethod.ApiKey;
                        break;
                    }
                }
Beispiel #26
0
        static int Main()
        {
            Console.WriteLine("Initializing the service...");

#if (Debug || DEBUG)
            if (!ServicesDebugOnlyUtilities.CalledFromMain())
            {
                return(1);
            }
#endif

            // In case of a cloud component dependency or environment variable is added/removed;
            // Relative terraform script and microservice-dependency-map.cs must be updated as well.

            if (!BUtility.GetEnvironmentVariables(out Dictionary <string, string> EnvironmentVariables,
                                                  new string[][]
            {
                new string[] { "PORT" },
                new string[] { "PROGRAM_ID" },

                new string[] { "API_GATEWAY_SERVICE_BASE_URL" },

                new string[] { "STATIC_STATE_BUCKET" },     //Currently set to ignore on local debugging sessions; since it is only used for maintenance check

                new string[] { "DEPLOYMENT_BRANCH_NAME" },
                new string[] { "DEPLOYMENT_BUILD_NUMBER" }
            },
                                                  Console.WriteLine))
            {
                return(1);
            }

            Resources_DeploymentManager.Get().SetDeploymentBranchNameAndBuildNumber(EnvironmentVariables["DEPLOYMENT_BRANCH_NAME"], EnvironmentVariables["DEPLOYMENT_BUILD_NUMBER"]);

            if (!int.TryParse(EnvironmentVariables["PORT"], out int ServerPort))
            {
                Console.WriteLine("Invalid PORT environment variable; it must be an integer.");
                return(1);
            }

            string ApiGatewayServiceEndpoint = EnvironmentVariables["API_GATEWAY_SERVICE_BASE_URL"];

            string MaintenanceModeCheckUrl = "https://storage.googleapis.com/" + EnvironmentVariables["STATIC_STATE_BUCKET"] + "/maintenance_mode_" + Resources_DeploymentManager.Get().GetDeploymentBranchName();

            var DeploymentLoweredBranchName = Resources_DeploymentManager.Get().GetDeploymentBranchNameEscapedLoweredWithDash();
            if (DeploymentLoweredBranchName == "master" || DeploymentLoweredBranchName == "development")
            {
                //If the branch is feature, bugfix, hotfix etc. do not check for maintenance.
                MaintenanceChecker.Get().Start(MaintenanceModeCheckUrl, Console.WriteLine);
            }

            /*
             * Web-http service initialization
             */
            var WebServiceEndpoints = new List <BWebPrefixStructure>()
            {
                new BWebPrefixStructure(new string[] { "*" }, () => new PassThroughRequest(ApiGatewayServiceEndpoint))
            };
            var BWebService = new BWebService(WebServiceEndpoints.ToArray(), ServerPort);
            BWebService.Run((string Message) =>
            {
                Console.WriteLine(Message);
            });

            /*
             * Make main thread sleep forever
             */
            Thread.Sleep(Timeout.Infinite);

            return(0);
        }
Beispiel #27
0
        /// <summary>
        ///
        /// <para>BTracingServiceGC: Parametered Constructor for Managed Service by Google</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProjectID"/>              GC Project ID</para>
        /// <para><paramref name="_ProgramUniqueID"/>        Program Unique ID</para>
        /// <para><paramref name="_ErrorMessageAction"/>     Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BTracingServiceGC(
            string _ProjectID,
            string _ProgramUniqueID,
            Action <string> _ErrorMessageAction = null)
        {
            ProgramUniqueID    = _ProgramUniqueID;
            ErrorMessageAction = _ErrorMessageAction;

            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");
                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    var Scopes = new List <string>();
                    foreach (var Scope in TraceServiceClient.DefaultScopes)
                    {
                        if (!Scopes.Contains(Scope))
                        {
                            Scopes.Add(Scope);
                        }
                    }

                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                     .CreateScoped(
                            Scopes.ToArray())
                                     .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            Credential = GoogleCredential.FromStream(Stream)
                                         .CreateScoped(
                                Scopes.ToArray())
                                         .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (Credential != null)
                    {
                        Channel = new Grpc.Core.Channel(
                            TraceServiceClient.DefaultEndpoint.ToString(),
                            Credential.ToChannelCredentials());
                    }

                    if (Channel != null)
                    {
                        TraceClient            = TraceServiceClient.Create(Channel);
                        ProjectName            = new Google.Api.Gax.ResourceNames.ProjectName(_ProjectID);
                        bInitializationSucceed = TraceClient != null;

                        UploadTimer           = new Timer(1_000);
                        UploadTimer.Elapsed  += OnTimedEvent;
                        UploadTimer.AutoReset = true;
                        UploadTimer.Enabled   = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }
Beispiel #28
0
        /// <summary>
        ///
        /// <para>WriteLogs:</para>
        ///
        /// <para>Writes logs to the logging service</para>
        ///
        /// <para>Check <seealso cref="IBLoggingServiceInterface.WriteLogs"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool WriteLogs(
            List <BLoggingParametersStruct> _Messages,
            string _LogGroupName,
            string _LogStreamName,
            bool _bAsync = true,
            Action <string> _ErrorMessageAction = null)
        {
            if (_Messages == null || _Messages.Count == 0)
            {
                return(false);
            }

            if (_bAsync)
            {
                BTaskWrapper.Run(() =>
                {
                    WriteLogs(_Messages, _LogGroupName, _LogStreamName, false, _ErrorMessageAction);
                });
                return(true);
            }
            else
            {
                _LogGroupName  = BUtility.EncodeStringForTagging(_LogGroupName);
                _LogStreamName = BUtility.EncodeStringForTagging(_LogStreamName);

                string SequenceToken = null;

                bool bLogStreamAndGroupExists = false;
                try
                {
                    var DescribeStreamRequest = new DescribeLogStreamsRequest(_LogGroupName);
                    using (var CreatedDescribeTask = CloudWatchLogsClient.DescribeLogStreamsAsync(DescribeStreamRequest))
                    {
                        CreatedDescribeTask.Wait();
                        if (CreatedDescribeTask.Result != null && CreatedDescribeTask.Result.LogStreams != null && CreatedDescribeTask.Result.LogStreams.Count > 0)
                        {
                            foreach (var Current in CreatedDescribeTask.Result.LogStreams)
                            {
                                if (Current != null && Current.LogStreamName == _LogStreamName)
                                {
                                    SequenceToken            = Current.UploadSequenceToken;
                                    bLogStreamAndGroupExists = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    bLogStreamAndGroupExists = false;
                }

                if (!bLogStreamAndGroupExists)
                {
                    try
                    {
                        var CreateGroupRequest = new CreateLogGroupRequest(_LogGroupName);
                        using (var CreatedGroupTask = CloudWatchLogsClient.CreateLogGroupAsync(CreateGroupRequest))
                        {
                            CreatedGroupTask.Wait();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is ResourceAlreadyExistsException))
                        {
                            _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                            return(false);
                        }
                    }

                    try
                    {
                        var CreateStreamRequest = new CreateLogStreamRequest(_LogGroupName, _LogStreamName);
                        using (var CreatedStreamTask = CloudWatchLogsClient.CreateLogStreamAsync(CreateStreamRequest))
                        {
                            CreatedStreamTask.Wait();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is ResourceAlreadyExistsException))
                        {
                            _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                            return(false);
                        }
                    }
                }

                var LogEvents = new List <InputLogEvent>();
                foreach (var Message in _Messages)
                {
                    var LogEvent = new InputLogEvent()
                    {
                        Message   = Message.Message,
                        Timestamp = DateTime.UtcNow
                    };

                    switch (Message.LogType)
                    {
                    case EBLoggingServiceLogType.Debug:
                        LogEvent.Message = "Debug-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Info:
                        LogEvent.Message = "Info-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Warning:
                        LogEvent.Message = "Warning-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Error:
                        LogEvent.Message = "Error-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Critical:
                        LogEvent.Message = "Critical-> " + LogEvent.Message;
                        break;
                    }

                    LogEvents.Add(LogEvent);
                }

                try
                {
                    var PutRequest = new PutLogEventsRequest(_LogGroupName, _LogStreamName, LogEvents)
                    {
                        SequenceToken = SequenceToken
                    };
                    using (var CreatedPutTask = CloudWatchLogsClient.PutLogEventsAsync(PutRequest))
                    {
                        CreatedPutTask.Wait();
                    }
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }
                return(true);
            }
        }
Beispiel #29
0
        /// <summary>
        ///
        /// <para>UploadFile:</para>
        ///
        /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para>
        ///
        /// <para>EBRemoteFileReadPublicity does nothing as Azure does not support per object authentication, only per container</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool UploadFile(BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null)
        {
            if (AServiceClient == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: GSClient is null.");
                return(false);
            }

            BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName);
            BlobClient          Blob            = ContainerClient.GetBlobClient(_KeyInBucket);

            Response <BlobContentInfo> Response = null;

            if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String)
            {
                if (!BUtility.DoesFileExist(
                        _LocalFileOrStream.String,
                        out bool bLocalFileExists,
                        _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: DoesFileExist failed.");
                    return(false);
                }

                if (!bLocalFileExists)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Local file does not exist.");
                    return(false);
                }

                using (FileStream FS = new FileStream(_LocalFileOrStream.String, FileMode.Open, FileAccess.Read))
                {
                    try
                    {
                        Response = Blob.Upload(FS);

                        if (Response.Value == null)
                        {
                            _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed.");
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace);
                        return(false);
                    }
                }
            }
            else
            {
                try
                {
                    Response = Blob.Upload(_LocalFileOrStream.Stream);

                    if (Response.Value == null)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed.");
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }
            }

            if (Response != null)
            {
                if (_FileTags != null && _FileTags.Length > 0)
                {
                    Dictionary <string, string> NewMetadata = new Dictionary <string, string>();
                    foreach (Tuple <string, string> CurrentTag in _FileTags)
                    {
                        NewMetadata.Add(CurrentTag.Item1, CurrentTag.Item2);
                    }

                    try
                    {
                        Blob.SetMetadata(NewMetadata);
                    }
                    catch (Exception ex)
                    {
                        _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}");
                        return(false);
                    }
                }

                var FileName      = _KeyInBucket;
                var FileNameIndex = _KeyInBucket.LastIndexOf("/") + 1;
                if (FileNameIndex > 0)
                {
                    FileName = _KeyInBucket.Substring(FileNameIndex, _KeyInBucket.Length - FileNameIndex);
                }
                BlobHttpHeaders Header = new BlobHttpHeaders()
                {
                    ContentDisposition = $"inline; filename={FileName}"
                };
                try
                {
                    Blob.SetHttpHeaders(Header);
                }
                catch (Exception ex)
                {
                    _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}");
                    return(false);
                }
            }
            return(true);
        }
Beispiel #30
0
        /// <summary>
        ///
        /// <para>BGoogleSlidesService: Parametered Constructor</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProgramUniqueID"/>           Program Unique ID</para>
        /// <para><paramref name="_ProjectID"/>                 GC Project ID</para>
        /// <para><paramref name="_ErrorMessageAction"/>        Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BGoogleSlidesService(
            string _ProgramUniqueID,
            string _ProjectID,
            Action <string> _ErrorMessageAction = null)
        {
            ProgramUniqueID = _ProgramUniqueID;
            ProjectID       = _ProjectID;
            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");

                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BGoogleSlidesService->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                     .CreateScoped(
                            new string[]
                        {
                            SlidesService.Scope.PresentationsReadonly
                        })
                                     .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            Credential = GoogleCredential.FromStream(Stream)
                                         .CreateScoped(
                                new string[]
                            {
                                SlidesService.Scope.PresentationsReadonly
                            })
                                         .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (Credential != null)
                    {
                        bInitializationSucceed = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BGoogleSlidesService->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }