Example #1
0
        private async Task Authenticate(IConnection connection)
        {
            var sasl          = new PlainSaslMechanism(_configuration.UserName, _configuration.Password);
            var authenticated = await sasl.AuthenticateAsync(connection).ConfigureAwait(false);

            if (authenticated)
            {
                var completionSource = new TaskCompletionSource <bool>();
                var selectBucketOp   = new SelectBucket
                {
                    Converter  = new DefaultConverter(),
                    Transcoder = new DefaultTranscoder(new DefaultConverter()),
                    Key        = Name,
                    Completed  = s =>
                    {
                        //Status will be Success if bucket select was bueno
                        completionSource.SetResult(s.Status == ResponseStatus.Success);
                        return(completionSource.Task);
                    }
                };

                await connection.SendAsync(selectBucketOp.Write(), selectBucketOp.Completed)
                .ConfigureAwait(false);
            }
            else
            {
                //cache exception for later use when op is used
            }
        }
        /// <summary>
        /// Enables enhanced authentication for RBAC on a connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <exception cref="AuthenticationException"></exception>
        protected void EnableEnhancedAuthentication(IConnection connection)
        {
            if (SupportsEnhancedAuthentication && !connection.CheckedForEnhancedAuthentication) // only execute this if RBAC is enabled on the cluster
            {
                if (string.IsNullOrWhiteSpace(Configuration.BucketName))
                {
                    const string bucketNameIsEmptyMessage = "BucketName cannot be empty when sending SelectBucket operation";
                    Log.Error(bucketNameIsEmptyMessage);
                    throw new AuthenticationException(bucketNameIsEmptyMessage);
                }

                var selectBucketOp = new SelectBucket(Configuration.BucketName, new DefaultTranscoder(), 0);
                var response       = connection.Send(selectBucketOp.Write());
                selectBucketOp.Read(response);

                var selectBucketResult = selectBucketOp.GetResult();
                connection.CheckedForEnhancedAuthentication = true;

                if (!selectBucketResult.Success)
                {
                    Log.Error($"Failed to perform SelectBucket operation for '{Configuration.BucketName}'");
                    throw new AuthenticationException(ExceptionUtil.FailedUserAuthenticationMsg.WithParams(Configuration.BucketName));
                }
            }
        }
Example #3
0
        public static async Task SelectBucket(this IConnection connection, string bucketName, ITypeTranscoder transcoder)
        {
            var completionSource = new TaskCompletionSource <bool>();

            using var selectBucketOp = new SelectBucket
                  {
                      Transcoder = transcoder,
                      Key        = bucketName,
                      Completed  = s =>
                      {
                          completionSource.TrySetResult(s.Status == ResponseStatus.Success);
                          return(completionSource.Task);
                      }
                  };
            await selectBucketOp.SendAsync(connection).ConfigureAwait(false);
        }
Example #4
0
        /// <summary>
        /// Enables enhanced authentication for RBAC on a connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <exception cref="AuthenticationException"></exception>
        protected void EnableEnhancedAuthentication(IConnection connection)
        {
            if (SupportsEnhancedAuthentication && !connection.CheckedForEnhancedAuthentication) // only execute this if RBAC is enabled on the cluster
            {
                var selectBucketOp = new SelectBucket(Configuration.BucketName, new DefaultTranscoder(), 0);
                var response       = connection.Send(selectBucketOp.Write());
                selectBucketOp.Read(response);

                var selectBucketResult = selectBucketOp.GetResult();
                connection.CheckedForEnhancedAuthentication = true;

                if (!selectBucketResult.Success)
                {
                    throw new AuthenticationException(string.Format("Authentication failed for bucket '{0}'", Configuration.BucketName));
                }
            }
        }
        /// <summary>
        /// Enables enhanced authentication for RBAC on a connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <exception cref="AuthenticationException"></exception>
        protected void EnableEnhancedAuthentication(IConnection connection)
        {
            if (SupportsEnhancedAuthentication && !connection.CheckedForEnhancedAuthentication) // only execute this if RBAC is enabled on the cluster
            {
                var selectBucketOp = new SelectBucket(Configuration.BucketName, new DefaultTranscoder(), 0);
                var response       = connection.Send(selectBucketOp.Write());
                selectBucketOp.Read(response);

                var selectBucketResult = selectBucketOp.GetResult();
                connection.CheckedForEnhancedAuthentication = true;

                if (!selectBucketResult.Success)
                {
                    Log.Error($"Failed to perform SelectBucket operation for '{Configuration.BucketName}'");
                    throw new AuthenticationException(ExceptionUtil.FailedBucketAuthenticationMsg.WithParams(Configuration.BucketName));
                }
            }
        }
Example #6
0
        public static async Task SelectBucket(this IConnection connection, string bucketName)
        {
            var completionSource = new TaskCompletionSource <bool>();

            using (var selectBucketOp = new SelectBucket
            {
                Converter = new DefaultConverter(),
                Transcoder = new DefaultTranscoder(new DefaultConverter()),
                Key = bucketName,
                Completed = s =>
                {
                    completionSource.TrySetResult(s.Status == ResponseStatus.Success);
                    return(completionSource.Task);
                }
            })
            {
                await selectBucketOp.SendAsync(connection).ConfigureAwait(false);
            }
        }