示例#1
0
        public CloudMasterServerCache(IEnumerable <Node> nodes)
        {
            foreach (var nodeInfo in nodes)
            {
                var info = new CloudPhotonEndpointInfo(nodeInfo);
                this.servers.Add(info);

                var regionCloud = $"{info.Region}_{info.PrivateCloud}";
                foreach (var serviceType in nodeInfo.ServiceTypes)
                {
                    var key = $"{regionCloud}_{serviceType.ToString()}";
                    if (!this.cluster0Presence.TryGetValue(key, out bool presence) || !presence)
                    {
                        this.cluster0Presence[key] = info.Cluster == Cluster0Name;
                    }
                }
            }

            if (log.IsDebugEnabled)
            {
                foreach (var endpoint in this.servers)
                {
                    log.DebugFormat("Hostname - 2 {0}, UDP: {1}, HTTP: {2}", endpoint.UdpHostname, endpoint.UdpEndPoint, endpoint.HttpEndPoint);
                }
            }
        }
示例#2
0
        private static string FormatRegionCultureString(CloudPhotonEndpointInfo server, List <CloudPhotonEndpointInfo> endpointInfos)
        {
            string regionCluster = server.Region;

            if (!string.IsNullOrEmpty(server.Cluster))
            {
                // check how many entries we have for that region, if it's more than one we need to add cluster:
                var count = endpointInfos.Count(e => regionCluster.Equals(e.Region, StringComparison.OrdinalIgnoreCase));
                if (count > 1)
                {
                    regionCluster = string.Format("{0}/{1}", server.Region, server.Cluster);
                }
            }

            return(regionCluster);
        }
        private object GetEncryptedAuthToken(AuthenticationToken unencryptedToken, CloudPhotonEndpointInfo masterServer)
        {
            var tc = this.application.TokenCreator;

            switch (this.SdkId)
            {
            case SdkIds.DotNet:
                if (this.ClientVersion > clientDotNetBinaryTokenVersion)
                {
                    return(tc.EncryptAuthenticationTokenBinary(unencryptedToken, false));
                }
                break;

            case SdkIds.Native:
                if (this.ClientVersion > clientNativeBinaryTokenVersion)
                {
                    return(tc.EncryptAuthenticationTokenBinary(unencryptedToken, false));
                }
                break;
            }

            return(masterServer.UseV1Token ? tc.EncryptAuthenticationToken(unencryptedToken, false) :
                   tc.EncryptAuthenticationTokenV2(unencryptedToken, false));
        }
 private void CheckEncryptedToken(object authToken, IAuthenticateRequest authenticateRequest, ApplicationAccount applicationAccount, CloudPhotonEndpointInfo master)
 {
     VAppsAuthTokenFactory.CheckEncryptedToken(this.application.TokenCreator, appCheckGuard, authToken,
                                               authenticateRequest, applicationAccount, master.UseV1Token);
 }
        private OperationResponse HandleDefaultAuthenticateRequest(AuthenticateRequest authenticateRequest,
                                                                   ApplicationAccount applicationAccount, string masterEndPoint, CloudPhotonEndpointInfo masterServer)
        {
            // generate a userid if its not set by the client
            var userId = string.IsNullOrEmpty(authenticateRequest.UserId) ? Guid.NewGuid().ToString() : authenticateRequest.UserId;
            // create auth token
            var unencryptedToken = this.application.TokenCreator.CreateAuthenticationToken(authenticateRequest, applicationAccount,
                                                                                           userId, new Dictionary <string, object>());

            var authToken = this.GetEncryptedAuthToken(unencryptedToken, masterServer);

            this.CheckEncryptedToken(authToken, authenticateRequest, applicationAccount, masterServer);

            var authResponse = new AuthenticateResponse
            {
                MasterEndpoint      = masterEndPoint,
                AuthenticationToken = authToken,
                UserId         = userId,
                Cluster        = masterServer.Cluster,
                EncryptionData = GetEncryptionData(unencryptedToken),
            };

            return(new OperationResponse(authenticateRequest.OperationRequest.OperationCode, authResponse));
        }
示例#6
0
        public bool TryGetPhotonEndpoint(IAuthenticateRequest request, ApplicationAccount appAccount, out CloudPhotonEndpointInfo result, out string message)
        {
            result  = null;
            message = null;

            if (string.IsNullOrEmpty(appAccount.PrivateCloud))
            {
                message = string.Format("No private cloud set for applicaton ID {0} - can not get Master", request.ApplicationId);
                log.Error(message);
                return(false);
            }

            if (string.IsNullOrEmpty(request.Region))
            {
                message = string.Format("No region set in authenticate request for application ID {0} - can not determine Master", request.ApplicationId);
                log.Error(message);
                return(false);
            }

            var privateCloud    = appAccount.PrivateCloud;
            var requestedRegion = request.Region.ToLower();

            var cluster = DefaultClusterName;

            // if a cluster "*" has been passed then we return a random cluster
            // if an actual value has been passed we try to return that particular cluster
            if (requestedRegion.Contains("/"))
            {
                var regionArray = requestedRegion.Split(RegionSeparators, StringSplitOptions.RemoveEmptyEntries);
                if (regionArray.Length > 0)
                {
                    requestedRegion = regionArray[0];
                }

                // if account is enterprise account, we allow to select cluster using auth request
                if (regionArray.Length > 1 && appAccount.IsEnterprise)
                {
                    cluster = regionArray[1];
                }
            }

            var defaultAppCluster = DefaultClusterName;

            // app is cluster0 app and there is cluster0 in requested region then we set cluster0
            if (appAccount.IsAppForCluster0 &&
                this.cluster0Presence.TryGetValue(MakeCluster0IndexKey(appAccount, requestedRegion), out bool present) &&
                present)
            {
                defaultAppCluster = Cluster0Name;
                cluster           = Cluster0Name;
            }

            if (appAccount.RegionClusterInfos != null && appAccount.RegionClusterInfos.ContainsKey(requestedRegion))
            {
                var clusters = appAccount.RegionClusterInfos[requestedRegion];

                // if "*" has been passed we just chose a random one:
                if (RandomClusterName.Equals(cluster))
                {
                    cluster = clusters[rnd.Next(0, clusters.Count)];
                }
                else
                {
                    // check if a valid cluster has been found:
                    if (!clusters.Contains(cluster))
                    {
                        if (!clusters.Contains(defaultAppCluster))
                        {
                            cluster = clusters[0];
                        }
                        else
                        {
                            cluster = defaultAppCluster;
                        }
                    }
                }
            }

            //enterprise customer who send "*" and cluster was not replaced with value from RegionClusterInfos (because info is not set)
            if (RandomClusterName.Equals(cluster) && appAccount.IsEnterprise)
            {
                cluster = DefaultClusterName;
            }

            result = this.TryGetPhotonEndpoint(privateCloud, requestedRegion, cluster, appAccount.ServiceType);

            if (log.IsDebugEnabled)
            {
                if (result == null)
                {
                    log.Debug("No endpoint found");
                }
                else
                {
                    log.DebugFormat("Endpoint found 2 - Hostname {0}, UDP: {1}, HTTP: {2}", result.UdpHostname, result.UdpEndPoint, result.HttpEndPoint);
                }
            }

            return(result != null);
        }