Example #1
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);
                    }
                }
            }
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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;
                    }
                }
Example #5
0
 public static bool GenerateUserID(out string _NewUserID, Action <string> _ErrorMessageAction)
 {
     return(BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out _NewUserID, _ErrorMessageAction));
 }
Example #6
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);
        }
Example #7
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);
        }
Example #8
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));
        }
Example #9
0
        public bool CreateInstance(
            string _UniqueInstanceName,
            string _Description,
            string _MachineType,
            long _DiskSizeGB,
            int _GpuCount,
            string _GpuName,
            string _OSSourceImageURL,
            EBVMDiskType _DiskType,
            EBVMOSType _OSType,
            IDictionary <string, string> _Labels,
            BVMNetworkFirewall _FirewallSettings,
            string _OptionalStartupScript,
            out int _ErrorCode,
            Action <string> _ErrorMessageAction = null)
        {
            _ErrorCode = 400;

            if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out string RandomFirewallTag, _ErrorMessageAction))
            {
                _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: Firewall tag MD5 generation has failed.");
                return(false);
            }

            RandomFirewallTag = "a-" /*Has to start with a letter*/ + RandomFirewallTag;

            try
            {
                using (var Service = GetService())
                {
                    var NewInstance = new Instance()
                    {
                        Name               = _UniqueInstanceName,
                        Description        = _Description,
                        DeletionProtection = false,
                        Zone               = "projects/" + ProjectID + "/zones/" + ZoneName,
                        Labels             = _Labels,
                        MachineType        = "projects/" + ProjectID + "/zones/" + ZoneName + "/machineTypes/" + _MachineType,
                        Disks              = new List <AttachedDisk>()
                        {
                            new AttachedDisk()
                            {
                                AutoDelete       = true,
                                Boot             = true,
                                Kind             = "compute#attachedDisk",
                                DeviceName       = _UniqueInstanceName,
                                Mode             = "READ_WRITE",
                                InitializeParams = new AttachedDiskInitializeParams()
                                {
                                    SourceImage = _OSSourceImageURL,
                                    DiskType    = "projects/" + ProjectID + "/zones/" + ZoneName + "/diskTypes/" + (_DiskType == EBVMDiskType.SSD ? "pd-ssd" : "pd-standard"),
                                    DiskSizeGb  = _DiskSizeGB
                                },
                                Type = "PERSISTENT"
                            }
                        },
                        NetworkInterfaces = new List <NetworkInterface>()
                        {
                            new NetworkInterface()
                            {
                                AccessConfigs = new List <AccessConfig>()
                                {
                                    new AccessConfig()
                                    {
                                        Kind        = "compute#accessConfig",
                                        Name        = "External NAT",
                                        NetworkTier = "PREMIUM",
                                        Type        = "ONE_TO_ONE_NAT"
                                    }
                                },
                                Kind       = "compute#networkInterface",
                                Name       = "nic0",
                                Network    = "projects/" + ProjectID + "/global/networks/default",
                                Subnetwork = "projects/" + ProjectID + "/regions/" + ZoneName.Substring(0, ZoneName.LastIndexOf('-')) + "/subnetworks/default"
                            }
                        },
                        Tags = new Tags()
                        {
                            Items = new List <string>()
                            {
                                RandomFirewallTag
                            }
                        },
                        Metadata = new Metadata()
                        {
                            Kind  = "compute#metadata",
                            Items = new List <Metadata.ItemsData>()
                        },
                        ShieldedInstanceConfig = new ShieldedInstanceConfig()
                        {
                            EnableVtpm                = true,
                            EnableSecureBoot          = false,
                            EnableIntegrityMonitoring = true
                        },
                        Scheduling = new Scheduling()
                        {
                            AutomaticRestart  = true,
                            Preemptible       = false,
                            OnHostMaintenance = "TERMINATE"
                        }
                    };

                    if (_OptionalStartupScript != null)
                    {
                        NewInstance.Metadata.Items.Add(new Metadata.ItemsData()
                        {
                            Key   = _OSType == EBVMOSType.Linux ? "startup-script" : "windows-startup-script-ps1",
                            Value = _OptionalStartupScript
                        });
                    }

                    if (_GpuCount > 0)
                    {
                        if (NewInstance.GuestAccelerators == null)
                        {
                            NewInstance.GuestAccelerators = new List <AcceleratorConfig>();
                        }
                        NewInstance.GuestAccelerators.Add(
                            new AcceleratorConfig()
                        {
                            AcceleratorCount = _GpuCount,
                            AcceleratorType  = "projects/" + ProjectID + "/zones/" + ZoneName + "/acceleratorTypes/" + _GpuName
                        });
                    }

                    if (_OSType == EBVMOSType.Windows)
                    {
                        if (NewInstance.Disks[0].GuestOsFeatures == null)
                        {
                            NewInstance.Disks[0].GuestOsFeatures = new List <GuestOsFeature>();
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "VIRTIO_SCSI_MULTIQUEUE"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "VIRTIO_SCSI_MULTIQUEUE"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "WINDOWS"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "WINDOWS"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "MULTI_IP_SUBNET"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "MULTI_IP_SUBNET"
                            });
                        }

                        if (!NewInstance.Disks[0].GuestOsFeatures.Any(Item => Item.Type == "UEFI_COMPATIBLE"))
                        {
                            NewInstance.Disks[0].GuestOsFeatures.Add(new GuestOsFeature()
                            {
                                Type = "UEFI_COMPATIBLE"
                            });
                        }
                    }

                    var NewFirewall = new Firewall()
                    {
                        Kind         = "compute#firewall",
                        Name         = RandomFirewallTag,
                        Priority     = 1000,
                        Direction    = "INGRESS",
                        SelfLink     = "projects/" + ProjectID + "/global/firewalls/" + RandomFirewallTag,
                        Network      = "projects/" + ProjectID + "/global/networks/default",
                        SourceRanges = new List <string>(),
                        TargetTags   = new List <string>()
                        {
                            RandomFirewallTag
                        },
                        Allowed = new List <Firewall.AllowedData>()
                    };
                    if (_FirewallSettings.bOpenAll)
                    {
                        NewFirewall.Allowed.Add(new Firewall.AllowedData()
                        {
                            IPProtocol = "tcp"
                        });
                        NewFirewall.Allowed.Add(new Firewall.AllowedData()
                        {
                            IPProtocol = "udp"
                        });
                    }
                    else
                    {
                        foreach (var Current in _FirewallSettings.OpenPorts)
                        {
                            string[] OpenFor;
                            if (Current.OpenFor == BVMNetworkFirewall.EVMNetworkFirewallPortProtocol.TCP)
                            {
                                OpenFor = new string[] { "tcp" }
                            }
                            ;
                            else if (Current.OpenFor == BVMNetworkFirewall.EVMNetworkFirewallPortProtocol.UDP)
                            {
                                OpenFor = new string[] { "udp" }
                            }
                            ;
                            else
                            {
                                OpenFor = new string[] { "tcp", "udp" }
                            };

                            var PortList = new List <string>()
                            {
                                Current.FromPortInclusive + "-" + Current.ToPortInclusive
                            };
                            foreach (var OFor in OpenFor)
                            {
                                NewFirewall.Allowed.Add(new Firewall.AllowedData()
                                {
                                    IPProtocol = OFor,
                                    Ports      = PortList
                                });
                            }
                        }
                    }

                    var FirewallCreationResult = Service.Firewalls.Insert(NewFirewall, ProjectID).Execute();
                    if (FirewallCreationResult == null || (FirewallCreationResult.HttpErrorStatusCode.HasValue && FirewallCreationResult.HttpErrorStatusCode.Value >= 400))
                    {
                        _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: Firewall creation has failed: " + (FirewallCreationResult == null ? "Result is null." : FirewallCreationResult.HttpErrorMessage));
                        _ErrorCode = FirewallCreationResult.HttpErrorStatusCode.Value;
                        return(false);
                    }

                    var VMCreationResult = Service.Instances.Insert(NewInstance, ProjectID, ZoneName).Execute();
                    if (VMCreationResult == null || (VMCreationResult.HttpErrorStatusCode.HasValue && VMCreationResult.HttpErrorStatusCode.Value >= 400))
                    {
                        _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: VM creation has failed: " + (VMCreationResult == null ? "Result is null." : VMCreationResult.HttpErrorMessage));
                        _ErrorCode = VMCreationResult.HttpErrorStatusCode.Value;
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BVMServiceGC->CreateInstance: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }
Example #10
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            if (_Context.Request.HttpMethod != "POST")
            {
                _ErrorMessageAction?.Invoke("LoginRequest: 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));
            }

            JObject ParsedBody;

            using (var InputStream = _Context.Request.InputStream)
            {
                using (var ResponseReader = new StreamReader(InputStream))
                {
                    try
                    {
                        ParsedBody = JObject.Parse(ResponseReader.ReadToEnd());
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("LoginRequest-> 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 (!ParsedBody.ContainsKey(AuthMethod.API_KEY_PROPERTY) &&
                ((!ParsedBody.ContainsKey(AuthMethod.USER_NAME_PROPERTY) && !ParsedBody.ContainsKey(AuthMethod.USER_EMAIL_PROPERTY)) || !ParsedBody.ContainsKey(AuthMethod.PASSWORD_MD5_PROPERTY)))
            {
                _ErrorMessageAction?.Invoke("LoginRequest-> Request does not have required fields.");
                return(BWebResponse.BadRequest("Request does not have required fields."));
            }

            string Method;

            if (ParsedBody.ContainsKey(AuthMethod.API_KEY_PROPERTY))
            {
                var ApiKey = (string)ParsedBody[AuthMethod.API_KEY_PROPERTY];
                Method = ApiKey;
            }
            else
            {
                var PasswordMD5 = ((string)ParsedBody[AuthMethod.PASSWORD_MD5_PROPERTY]).ToLower();

                if (ParsedBody.ContainsKey(UserDBEntry.USER_NAME_PROPERTY))
                {
                    Method = (string)ParsedBody[UserDBEntry.USER_NAME_PROPERTY] + PasswordMD5;
                }
                else
                {
                    Method = ((string)ParsedBody[UserDBEntry.USER_EMAIL_PROPERTY]).ToLower() + PasswordMD5;
                }
            }

            if (!AuthenticationCommon.FetchUserInfoFromMemoryService_ByMethod(MemoryService, Method, out string UserID, out string _, out string _, _ErrorMessageAction))
            {
                if (!AuthenticationCommon.FetchUserInfoFromDatabaseService_ByMethod(DatabaseService, MemoryService, Method, out UserID, out _, out _, out BWebServiceResponse FailureResponse, _ErrorMessageAction))
                {
                    return(FailureResponse);
                }
            }

            if (!BUtility.CalculateStringMD5(BUtility.RandomString(32, true), out string AccessTokenMD5, _ErrorMessageAction))
            {
                return(BWebResponse.InternalError("Hash operation failed."));
            }

            var AccessTokenMD5WithTokenType = "Basic " + AccessTokenMD5;

            var QueryParameters = new BMemoryQueryParameters()
            {
                Domain     = Resources_DeploymentManager.Get().GetDeploymentBranchNameEscapedLoweredWithDash().ToUpper(),
                SubDomain  = "SELF_SIGNED_ACCESS_TOKEN_VALIDATION",
                Identifier = AccessTokenMD5WithTokenType
            };

            MemoryService.SetKeyValue(QueryParameters, new Tuple <string, BPrimitiveType>[]
            {
                new Tuple <string, BPrimitiveType>("method", new BPrimitiveType(Method))
            },
                                      _ErrorMessageAction);

            MemoryService.SetKeyExpireTime(QueryParameters, TimeSpan.FromHours(1), _ErrorMessageAction);

            return(BWebResponse.StatusOK("Login successful.", new JObject()
            {
                ["userId"] = UserID,
                ["token"] = AccessTokenMD5WithTokenType
            }));
        }
Example #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_Messages"></param>
        /// <param name="_LogGroupName"></param>
        /// <param name="_LogStreamName"></param>
        /// <param name="_bAsync"></param>
        /// <param name="_ErrorMessageAction"></param>
        /// <returns></returns>
        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);

                if (!BUtility.CalculateStringMD5(DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds.ToString(), out string Timestamp, _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAzure->WriteLogs: Timestamp generation has failed.");
                    return(false);
                }

                string StreamIDBase = _LogGroupName + "-" + _LogStreamName + "-" + Timestamp;

                try
                {
                    using (AzureLogger.BeginScope(StreamIDBase))
                    {
                        foreach (var Message in _Messages)
                        {
                            var level   = LogLevel.Information;
                            var message = "Info-> " + Message.Message;

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

                            case EBLoggingServiceLogType.Warning:
                                level   = LogLevel.Warning;
                                message = "Warning-> " + Message.Message;
                                break;

                            case EBLoggingServiceLogType.Error:
                                level   = LogLevel.Error;
                                message = "Error-> " + Message.Message;
                                break;

                            case EBLoggingServiceLogType.Critical:
                                level   = LogLevel.Critical;
                                message = "Critical-> " + Message.Message;
                                break;

                            default:
                                level   = LogLevel.Information;
                                message = "Info-> " + Message.Message;
                                break;
                            }

                            AzureLogger.Log(level, message);
                        }
                    }

                    // Explicitly call Flush() followed by sleep is required in Console Apps.
                    // This is to ensure that even if application terminates, telemetry is sent to the back-end.
                    AzureTelemetryChannel?.Flush();
                    Thread.Sleep(1000);

                    return(true);
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAzure->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                }
            }

            return(false);
        }
Example #12
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
            {
                if (!BUtility.CalculateStringMD5(DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds.ToString(), out string Timestamp, _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceGC->WriteLogs: Timestamp generation has failed.");
                    return(false);
                }

                _LogGroupName  = BUtility.EncodeStringForTagging(_LogGroupName);
                _LogStreamName = BUtility.EncodeStringForTagging(_LogStreamName);

                string StreamIDBase = _LogGroupName + "-" + _LogStreamName + "-" + Timestamp;
                try
                {
                    var LogEntries = new LogEntry[_Messages.Count];

                    int i = 0;
                    foreach (var Message in _Messages)
                    {
                        LogEntries[i] = new LogEntry
                        {
                            LogName     = new LogName(ProjectID, StreamIDBase + "-" + (i + 1).ToString()).ToString(),
                            TextPayload = Message.Message
                        };

                        switch (Message.LogType)
                        {
                        case EBLoggingServiceLogType.Debug:
                            LogEntries[i].Severity = LogSeverity.Debug;
                            break;

                        case EBLoggingServiceLogType.Info:
                            LogEntries[i].Severity = LogSeverity.Info;
                            break;

                        case EBLoggingServiceLogType.Warning:
                            LogEntries[i].Severity = LogSeverity.Warning;
                            break;

                        case EBLoggingServiceLogType.Error:
                            LogEntries[i].Severity = LogSeverity.Error;
                            break;

                        case EBLoggingServiceLogType.Critical:
                            LogEntries[i].Severity = LogSeverity.Critical;
                            break;
                        }

                        i++;
                    }

                    LoggingServiceClient.WriteLogEntries(
                        LogNameOneof.From(new LogName(ProjectID, StreamIDBase)),
                        ResourceName,
                        new Dictionary <string, string>()
                    {
                        ["LogGroup"]  = _LogGroupName,
                        ["LogStream"] = _LogStreamName
                    },
                        LogEntries);

                    return(true);
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceGC->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                }
            }
            return(false);
        }