public IBucket CreateBucket(string bucketName, IClusterCredentials credentials = null)
        {
            var password = string.Empty;

            if (credentials == null)
            {
                //try to find a password in configuration
                BucketConfiguration bucketConfig;
                if (_clientConfig.BucketConfigs.TryGetValue(bucketName, out bucketConfig) &&
                    bucketConfig.Password != null)
                {
                    bucketName = bucketConfig.BucketName;
                    password   = bucketConfig.Password;
                }
            }
            else
            {
                var bucketCreds = credentials.GetCredentials(AuthContext.BucketKv, bucketName);
                if (bucketCreds.ContainsKey(bucketName))
                {
                    password = bucketCreds[bucketName];
                }
                else
                {
                    throw new BucketNotFoundException(string.Format("Could not find credentials for bucket: {0}", bucketName));
                }
            }
            return(CreateBucket(bucketName, password, credentials));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClassicAuthenticator"/> class using a legacy <see cref="IClusterCredentials"/>.
        /// </summary>
        /// <param name="credentials">The cluster credentials.</param>
        internal ClassicAuthenticator(IClusterCredentials credentials)
        {
            var classicCredentials = (ClusterCredentials)credentials;

            ClusterUsername   = classicCredentials.ClusterUsername;
            ClusterPassword   = classicCredentials.ClusterPassword;
            BucketCredentials = credentials.BucketCredentials;
        }
 /// <summary>
 /// Gets the first <see cref="CouchbaseBucket"/> instance found./>
 /// </summary>
 /// <returns></returns>
 public IBucket GetBucket(IClusterCredentials credentials)
 {
     if (_buckets.IsEmpty)
     {
         lock (_syncObject)
         {
             if (_buckets.IsEmpty)
             {
                 var bucketName = credentials.BucketCredentials.First().Key;
                 return(CreateBucket(bucketName, credentials));
             }
         }
     }
     return(_buckets.First().Value);
 }
Пример #4
0
 /// <summary>
 /// Authenticates the specified credentials.
 /// </summary>
 /// <param name="credentials">The credentials.</param>
 /// <exception cref="System.NotImplementedException"></exception>
 public void Authenticate(IClusterCredentials credentials)
 {
     _credentials = credentials;
     _configuration.Credentials = _credentials;
 }
Пример #5
0
 /// <summary>
 /// Authenticates the specified credentials.
 /// </summary>
 /// <param name="credentials">The credentials.</param>
 /// <exception cref="System.ArgumentNullException">authenticator</exception>
 public void Authenticate(IClusterCredentials credentials)
 {
     // create authenticator from legacy credentials
     Authenticate(new ClassicAuthenticator(credentials));
 }
        public IBucket CreateBucket(string bucketName, string password, IClusterCredentials credentials = null)
        {
            var exceptions = new List <Exception>();

            lock (_syncObject)
            {
                //shortcircuit in case lock was waited upon because another thread bootstraped same bucket
                if (_buckets.ContainsKey(bucketName))
                {
                    IBucket existingBucket = _buckets[bucketName];
                    if ((existingBucket as IRefCountable).AddRef() != -1)
                    {
                        Log.Debug("Bootstraping was already done, returning existing bucket {0}", bucketName);
                        return(existingBucket); // This is the only short circuit. All other cases fall through to bootstrapping.
                    }
                    else
                    {
                        Log.Debug("Bucket dictionary contained disposed bucket. Bootstrapping {0}.", bucketName);
                        DestroyBucket(existingBucket);
                    }
                }
                //otherwise bootstrap a new bucket
                var     success = false;
                IBucket bucket  = null;
                foreach (var provider in _configProviders)
                {
                    try
                    {
                        Log.Debug("Trying to bootstrap with {0}.", provider);
                        var           config       = provider.GetConfig(bucketName, password);
                        IRefCountable refCountable = null;
                        switch (config.NodeLocator)
                        {
                        case NodeLocatorEnum.VBucket:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new CouchbaseBucket(this, bucketName, Converter, Transcoder, credentials));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        case NodeLocatorEnum.Ketama:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new MemcachedBucket(this, bucketName, Converter, Transcoder, credentials));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        var configObserver = (IConfigObserver)bucket;
                        if (provider.ObserverExists(configObserver))
                        {
                            Log.Debug("Using existing bootstrap {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);

                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }

                        if (provider.RegisterObserver(configObserver) &&
                            _buckets.TryAdd(bucket.Name, bucket))
                        {
                            Log.Debug("Successfully bootstrapped using {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);
                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }
                        _clientConfig.UpdateBootstrapList(config.BucketConfig);
                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                        exceptions.Add(e);
                    }
                }

                if (!success)
                {
                    throw new AggregateException("Could not bootstrap - check inner exceptions for details.", exceptions);
                }
                return(bucket);
            }
        }
 /// <summary>
 /// Gets the first <see cref="CouchbaseBucket"/> instance found./>
 /// </summary>
 /// <returns></returns>
 public IBucket GetBucket(IClusterCredentials credentials)
 {
     if (_buckets.IsEmpty)
     {
         lock (_syncObject)
         {
             if (_buckets.IsEmpty)
             {
                var bucketName = credentials.BucketCredentials.First().Key;
                 return CreateBucket(bucketName, credentials);
             }
         }
     }
     return _buckets.First().Value;
 }
        public IBucket CreateBucket(string bucketName, string password, IClusterCredentials credentials = null)
        {
            var exceptions = new List<Exception>();
            lock (_syncObject)
            {
                //shortcircuit in case lock was waited upon because another thread bootstraped same bucket
                if (_buckets.ContainsKey(bucketName))
                {
                    IBucket existingBucket = _buckets[bucketName];
                    if ((existingBucket as IRefCountable).AddRef() != -1)
                    {
                        Log.DebugFormat("Bootstraping was already done, returning existing bucket {0}", bucketName);
                        return existingBucket; // This is the only short circuit. All other cases fall through to bootstrapping.
                    }
                    else
                    {
                        Log.DebugFormat("Bucket dictionary contained disposed bucket. Bootstrapping {0}.", bucketName);
                        DestroyBucket(existingBucket);
                    }
                }
                //otherwise bootstrap a new bucket
                var success = false;
                IBucket bucket = null;
                foreach (var provider in _configProviders)
                {
                    try
                    {
                        Log.DebugFormat("Trying to bootstrap with {0}.", provider);
                        var config = provider.GetConfig(bucketName, password);
                        IRefCountable refCountable = null;
                        switch (config.NodeLocator)
                        {
                            case NodeLocatorEnum.VBucket:
                                bucket = _buckets.GetOrAdd(bucketName,
                                    name => new CouchbaseBucket(this, bucketName, Converter, Transcoder, credentials));
                                refCountable = bucket as IRefCountable;
                                if (refCountable != null)
                                {
                                    refCountable.AddRef();
                                }
                                break;

                            case NodeLocatorEnum.Ketama:
                                bucket = _buckets.GetOrAdd(bucketName,
                                    name => new MemcachedBucket(this, bucketName, Converter, Transcoder, credentials));
                                refCountable = bucket as IRefCountable;
                                if (refCountable != null)
                                {
                                    refCountable.AddRef();
                                }
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                        }

                        var configObserver = (IConfigObserver) bucket;
                        if (provider.ObserverExists(configObserver))
                        {
                            Log.DebugFormat("Using existing bootstrap {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);

                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }

                        if (provider.RegisterObserver(configObserver) &&
                            _buckets.TryAdd(bucket.Name, bucket))
                        {
                            Log.DebugFormat("Successfully bootstrapped using {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);
                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }
                        _clientConfig.UpdateBootstrapList(config.BucketConfig);
                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                        exceptions.Add(e);
                    }
                }

                if (!success)
                {
                    throw new AggregateException("Could not bootstrap - check inner exceptions for details.", exceptions);
                }
                return bucket;
            }
        }
 public IBucket CreateBucket(string bucketName, IClusterCredentials credentials = null)
 {
     var password = string.Empty;
     if (credentials == null)
     {
         //try to find a password in configuration
         BucketConfiguration bucketConfig;
         if (_clientConfig.BucketConfigs.TryGetValue(bucketName, out bucketConfig)
             && bucketConfig.Password != null)
         {
             bucketName = bucketConfig.BucketName;
             password = bucketConfig.Password;
         }
     }
     else
     {
         var bucketCreds = credentials.GetCredentials(AuthContext.BucketKv, bucketName);
         if (bucketCreds.ContainsKey(bucketName))
         {
             password = bucketCreds[bucketName];
         }
         else
         {
             throw new BucketNotFoundException(string.Format("Could not find credentials for bucket: {0}", bucketName));
         }
     }
     return CreateBucket(bucketName, password, credentials);
 }