Skip to content

Commit 790164b

Browse files
Bug: StaticCredentialsProvider,StatusCode="Unauthenticated" bug (#91)
* Make ClientBase abstract class * Bug: StaticCredentialsProvider,StatusCode="Unauthenticated" bug * fix linter * fix linter * delete empty auth example * fix commit * fix semantic * fix
1 parent 117a52f commit 790164b

File tree

11 files changed

+111
-123
lines changed

11 files changed

+111
-123
lines changed
File renamed without changes.

src/Ydb.Sdk/src/Auth/Auth.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Ydb.Sdk/src/Auth/ICredentialsProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
public interface ICredentialsProvider
44
{
55
string? GetAuthInfo();
6+
7+
Task ProvideConfig(DriverConfig driverConfig)
8+
{
9+
return Task.CompletedTask;
10+
}
611
}

src/Ydb.Sdk/src/Auth/IUseDriverConfig.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Ydb.Sdk/src/Auth/StaticCredentialsProvider.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55

66
namespace Ydb.Sdk.Auth;
77

8-
public class StaticCredentialsProvider : ICredentialsProvider, IUseDriverConfig
8+
public class StaticCredentialsProvider : ICredentialsProvider
99
{
1010
private readonly ILogger _logger;
1111
private readonly string _user;
1212
private readonly string? _password;
1313

14-
private Driver? _driver;
15-
1614
public int MaxRetries = 5;
1715

1816
private readonly object _lock = new();
@@ -22,6 +20,8 @@ public class StaticCredentialsProvider : ICredentialsProvider, IUseDriverConfig
2220

2321
public float RefreshRatio = .1f;
2422

23+
private AuthClient? _authClient;
24+
2525
/// <summary>
2626
///
2727
/// </summary>
@@ -36,7 +36,7 @@ public StaticCredentialsProvider(string user, string? password, ILoggerFactory?
3636
_logger = loggerFactory.CreateLogger<StaticCredentialsProvider>();
3737
}
3838

39-
private async Task Initialize()
39+
private async Task UpdateToken()
4040
{
4141
_token = await ReceiveToken();
4242
}
@@ -53,7 +53,7 @@ public string GetAuthInfo()
5353
_logger.LogWarning(
5454
"Blocking for initial token acquirement, please use explicit Initialize async method.");
5555

56-
Initialize().Wait();
56+
UpdateToken().Wait();
5757

5858
return _token!.Token;
5959
}
@@ -66,7 +66,7 @@ public string GetAuthInfo()
6666
if (!_token!.IsExpired()) return _token.Token;
6767
_logger.LogWarning("Blocking on expired token.");
6868

69-
_token = ReceiveToken().Result;
69+
UpdateToken().Wait();
7070

7171
return _token.Token;
7272
}
@@ -134,37 +134,29 @@ private async Task<TokenData> ReceiveToken()
134134

135135
private async Task<TokenData> FetchToken()
136136
{
137-
if (_driver is null)
137+
if (_authClient is null)
138138
{
139139
_logger.LogError("Driver in for static auth not provided");
140140
throw new NullReferenceException();
141141
}
142142

143-
var client = new AuthClient(_driver);
144-
var loginResponse = await client.Login(_user, _password);
143+
var loginResponse = await _authClient!.Login(_user, _password);
145144
if (loginResponse.Status.StatusCode == StatusCode.Unauthorized)
146145
{
147146
throw new InvalidCredentialsException(Issue.IssuesToString(loginResponse.Status.Issues));
148147
}
149148

150149
loginResponse.Status.EnsureSuccess();
151150
var token = loginResponse.Result.Token;
152-
var jwt = new JwtSecurityToken(token);
153-
return new TokenData(token, jwt.ValidTo, RefreshRatio);
151+
152+
return new TokenData(token, new JwtSecurityToken(token).ValidTo, RefreshRatio);
154153
}
155154

156155
public async Task ProvideConfig(DriverConfig driverConfig)
157156
{
158-
_driver = await Driver.CreateInitialized(
159-
new DriverConfig(
160-
driverConfig.Endpoint,
161-
driverConfig.Database,
162-
new AnonymousProvider(),
163-
driverConfig.DefaultTransportTimeout,
164-
driverConfig.DefaultStreamingTransportTimeout,
165-
driverConfig.CustomServerCertificate));
166-
167-
await Initialize();
157+
_authClient = new AuthClient(driverConfig, _logger);
158+
159+
await UpdateToken();
168160
}
169161

170162
private class TokenData
@@ -207,3 +199,10 @@ public bool IsRefreshNeeded()
207199
}
208200
}
209201
}
202+
203+
public class InvalidCredentialsException : Exception
204+
{
205+
public InvalidCredentialsException(string message) : base(message)
206+
{
207+
}
208+
}
File renamed without changes.

src/Ydb.Sdk/src/Driver.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Extensions.Logging.Abstractions;
55
using Ydb.Discovery;
66
using Ydb.Discovery.V1;
7-
using Ydb.Sdk.Auth;
87

98
namespace Ydb.Sdk;
109

@@ -70,11 +69,7 @@ public ValueTask DisposeAsync()
7069

7170
public async Task Initialize()
7271
{
73-
if (_config.Credentials is IUseDriverConfig useDriverConfig)
74-
{
75-
await useDriverConfig.ProvideConfig(_config);
76-
_logger.LogInformation("DriverConfig provided to IUseDriverConfig interface");
77-
}
72+
await _config.Credentials.ProvideConfig(_config);
7873

7974
_logger.LogInformation("Started initial endpoint discovery");
8075

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,89 @@
1+
using Microsoft.Extensions.Logging;
2+
using Ydb.Auth;
3+
using Ydb.Auth.V1;
4+
using Ydb.Sdk.Client;
5+
using Ydb.Sdk.Services.Operations;
6+
using Ydb.Sdk.Transport;
7+
18
namespace Ydb.Sdk.Services.Auth;
29

3-
public partial class AuthClient
10+
internal class AuthClient
411
{
5-
private readonly Driver _driver;
12+
private readonly DriverConfig _config;
13+
private readonly ILogger _logger;
14+
15+
public AuthClient(DriverConfig config, ILogger logger)
16+
{
17+
_config = config;
18+
_logger = logger;
19+
}
620

7-
public AuthClient(Driver driver)
21+
public async Task<LoginResponse> Login(string user, string? password, LoginSettings? settings = null)
822
{
9-
_driver = driver;
23+
settings ??= new LoginSettings();
24+
var request = new LoginRequest
25+
{
26+
OperationParams = settings.MakeOperationParams(),
27+
User = user
28+
};
29+
30+
if (password is not null)
31+
{
32+
request.Password = password;
33+
}
34+
35+
try
36+
{
37+
await using var transport = new AuthGrpcChannelTransport(_config, _logger);
38+
39+
var response = await transport.UnaryCall(
40+
method: AuthService.LoginMethod,
41+
request: request,
42+
settings: settings
43+
);
44+
45+
var status = response.Operation.TryUnpack(out LoginResult? resultProto);
46+
47+
LoginResponse.ResultData? result = null;
48+
49+
if (status.IsSuccess && resultProto is not null)
50+
{
51+
result = LoginResponse.ResultData.FromProto(resultProto);
52+
}
53+
54+
return new LoginResponse(status, result);
55+
}
56+
catch (Driver.TransportException e)
57+
{
58+
return new LoginResponse(e.Status);
59+
}
60+
}
61+
}
62+
63+
public class LoginSettings : OperationRequestSettings
64+
{
65+
}
66+
67+
public class LoginResponse : ResponseWithResultBase<LoginResponse.ResultData>
68+
{
69+
internal LoginResponse(Status status, ResultData? result = null) : base(status, result)
70+
{
71+
}
72+
73+
public class ResultData
74+
{
75+
public string Token { get; }
76+
77+
private ResultData(string token)
78+
{
79+
Token = token;
80+
}
81+
82+
83+
internal static ResultData FromProto(LoginResult resultProto)
84+
{
85+
var token = resultProto.Token;
86+
return new ResultData(token);
87+
}
1088
}
1189
}

src/Ydb.Sdk/src/Services/Auth/Login.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Ydb.Sdk/src/Transport/FixedGrpcChannelTransport.cs renamed to src/Ydb.Sdk/src/Transport/AuthGrpcChannelTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Ydb.Sdk.Transport;
66

7-
internal class FixedGrpcChannelTransport : GrpcTransport
7+
internal class AuthGrpcChannelTransport : GrpcTransport
88
{
99
private readonly GrpcChannel _channel;
1010

11-
public FixedGrpcChannelTransport(
11+
public AuthGrpcChannelTransport(
1212
DriverConfig driverConfig,
1313
ILogger logger
1414
) : base(

0 commit comments

Comments
 (0)