Skip to content

Commit 895aec7

Browse files
committed
* Create separate credentials provider for HTTP API requests.
1 parent 18fa5fa commit 895aec7

File tree

4 files changed

+140
-106
lines changed

4 files changed

+140
-106
lines changed

projects/Test/Common/TestConnectionRecoveryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ internal async Task<AutorecoveringConnection> CreateAutorecoveringConnectionWith
169169

170170
protected Task CloseConnectionAsync(IConnection conn)
171171
{
172-
return _util.CloseConnectionAsync(conn);
172+
return _util.CloseConnectionAsync(conn.ClientProvidedName);
173173
}
174174

175175
protected Task CloseAndWaitForRecoveryAsync()

projects/Test/Common/Util.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Threading.Tasks;
66
using EasyNetQ.Management.Client;
7-
using RabbitMQ.Client;
87

98
namespace Test
109
{
@@ -42,7 +41,7 @@ public Util(string managementUsername, string managementPassword)
4241

4342
public static bool IsWindows => s_isWindows;
4443

45-
public async Task CloseConnectionAsync(IConnection conn)
44+
public async Task CloseConnectionAsync(string connectionClientProvidedName)
4645
{
4746
ushort tries = 1;
4847
EasyNetQ.Management.Client.Model.Connection connectionToClose = null;
@@ -65,7 +64,7 @@ public async Task CloseConnectionAsync(IConnection conn)
6564
} while (connections.Count == 0);
6665

6766
connectionToClose = connections.Where(c0 =>
68-
string.Equals((string)c0.ClientProperties["connection_name"], conn.ClientProvidedName,
67+
string.Equals((string)c0.ClientProperties["connection_name"], connectionClientProvidedName,
6968
StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
7069
}
7170
catch (ArgumentNullException)
@@ -91,7 +90,7 @@ public async Task CloseConnectionAsync(IConnection conn)
9190

9291
if (connectionToClose == null)
9392
{
94-
throw new InvalidOperationException($"Could not delete connection: '{conn.ClientProvidedName}'");
93+
throw new InvalidOperationException($"Could not delete connection: '{connectionClientProvidedName}'");
9594
}
9695
}
9796

projects/Test/OAuth2/OAuth2Options.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
3+
namespace OAuth2Test
4+
{
5+
public enum Mode
6+
{
7+
uaa,
8+
keycloak
9+
}
10+
11+
public abstract class OAuth2OptionsBase
12+
{
13+
protected readonly Mode _mode;
14+
15+
public OAuth2OptionsBase(Mode mode)
16+
{
17+
_mode = mode;
18+
}
19+
20+
public string Name
21+
{
22+
get
23+
{
24+
return _mode switch
25+
{
26+
Mode.uaa => "uaa",
27+
Mode.keycloak => "keycloak",
28+
_ => throw new InvalidOperationException(),
29+
};
30+
}
31+
}
32+
33+
public string Scope
34+
{
35+
get
36+
{
37+
return _mode switch
38+
{
39+
Mode.uaa => string.Empty,
40+
Mode.keycloak => "rabbitmq:configure:*/* rabbitmq:read:*/* rabbitmq:write:*/*",
41+
_ => throw new InvalidOperationException(),
42+
};
43+
}
44+
}
45+
46+
public string TokenEndpoint // => _mode switch
47+
{
48+
get
49+
{
50+
return _mode switch
51+
{
52+
Mode.uaa => "http://localhost:8080/oauth/token",
53+
Mode.keycloak => "http://localhost:8080/realms/test/protocol/openid-connect/token",
54+
_ => throw new InvalidOperationException(),
55+
};
56+
}
57+
}
58+
59+
public abstract string ClientId { get; }
60+
public abstract string ClientSecret { get; }
61+
62+
public static int TokenExpiresInSeconds => 60;
63+
}
64+
65+
public class OAuth2ProducerOptions : OAuth2OptionsBase
66+
{
67+
public OAuth2ProducerOptions(Mode mode) : base(mode)
68+
{
69+
}
70+
71+
public override string ClientId => "producer";
72+
73+
public override string ClientSecret
74+
{
75+
get
76+
{
77+
return _mode switch
78+
{
79+
Mode.uaa => "producer_secret",
80+
Mode.keycloak => "kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn",
81+
_ => throw new InvalidOperationException(),
82+
};
83+
}
84+
}
85+
}
86+
87+
public class OAuth2HttpApiOptions : OAuth2OptionsBase
88+
{
89+
public OAuth2HttpApiOptions(Mode mode) : base(mode)
90+
{
91+
}
92+
93+
public override string ClientId => "mgt_api_client";
94+
95+
public override string ClientSecret
96+
{
97+
get
98+
{
99+
return _mode switch
100+
{
101+
Mode.uaa => "mgt_api_client",
102+
Mode.keycloak => "LWOuYqJ8gjKg3D2U8CJZDuID3KiRZVDa",
103+
_ => throw new InvalidOperationException(),
104+
};
105+
}
106+
}
107+
}
108+
}

projects/Test/OAuth2/TestOAuth2.cs

Lines changed: 28 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -42,90 +42,6 @@
4242

4343
namespace OAuth2Test
4444
{
45-
public enum Mode
46-
{
47-
uaa,
48-
keycloak
49-
}
50-
51-
public class OAuth2Options
52-
{
53-
private readonly Mode _mode;
54-
55-
public OAuth2Options(Mode mode)
56-
{
57-
_mode = mode;
58-
}
59-
60-
public string Name
61-
{
62-
get
63-
{
64-
switch (_mode)
65-
{
66-
case Mode.uaa:
67-
return "uaa";
68-
case Mode.keycloak:
69-
return "keycloak";
70-
default:
71-
throw new InvalidOperationException();
72-
}
73-
}
74-
}
75-
76-
public string ClientId => "producer";
77-
78-
public string ClientSecret
79-
{
80-
get
81-
{
82-
switch (_mode)
83-
{
84-
case Mode.uaa:
85-
return "producer_secret";
86-
case Mode.keycloak:
87-
return "kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn";
88-
default:
89-
throw new InvalidOperationException();
90-
}
91-
}
92-
}
93-
94-
public string Scope
95-
{
96-
get
97-
{
98-
switch (_mode)
99-
{
100-
case Mode.uaa:
101-
return string.Empty;
102-
case Mode.keycloak:
103-
return "rabbitmq:configure:*/* rabbitmq:read:*/* rabbitmq:write:*/*";
104-
default:
105-
throw new InvalidOperationException();
106-
}
107-
}
108-
}
109-
110-
public string TokenEndpoint // => _mode switch
111-
{
112-
get
113-
{
114-
switch (_mode)
115-
{
116-
case Mode.uaa:
117-
return "http://localhost:8080/oauth/token";
118-
case Mode.keycloak:
119-
return "http://localhost:8080/realms/test/protocol/openid-connect/token";
120-
default:
121-
throw new InvalidOperationException();
122-
}
123-
}
124-
}
125-
126-
public int TokenExpiresInSeconds => 60;
127-
}
128-
12945
public class TestOAuth2 : IAsyncLifetime
13046
{
13147
private const string Exchange = "test_direct";
@@ -134,10 +50,10 @@ public class TestOAuth2 : IAsyncLifetime
13450
private readonly ITestOutputHelper _testOutputHelper;
13551
private readonly IConnectionFactory _connectionFactory;
13652
private readonly int _tokenExpiresInSeconds;
137-
private readonly OAuth2ClientCredentialsProvider _credentialsProvider;
53+
private readonly OAuth2ClientCredentialsProvider _producerCredentialsProvider;
54+
private readonly OAuth2ClientCredentialsProvider _httpApiCredentialsProvider;
13855
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
13956

140-
private Util? _util;
14157
private IConnection? _connection;
14258
private CredentialsRefresher? _credentialsRefresher;
14359

@@ -147,18 +63,21 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
14763

14864
string modeStr = Environment.GetEnvironmentVariable("OAUTH2_MODE") ?? "uaa";
14965
Mode mode = (Mode)Enum.Parse(typeof(Mode), modeStr.ToLowerInvariant());
150-
var options = new OAuth2Options(mode);
15166

152-
_credentialsProvider = GetCredentialsProvider(options);
67+
var producerOptions = new OAuth2ProducerOptions(mode);
68+
_producerCredentialsProvider = GetCredentialsProvider(producerOptions);
69+
70+
var httpApiOptions = new OAuth2HttpApiOptions(mode);
71+
_httpApiCredentialsProvider = GetCredentialsProvider(httpApiOptions);
15372

15473
_connectionFactory = new ConnectionFactory
15574
{
15675
AutomaticRecoveryEnabled = true,
157-
CredentialsProvider = _credentialsProvider,
76+
CredentialsProvider = _producerCredentialsProvider,
15877
ClientProvidedName = nameof(TestOAuth2)
15978
};
16079

161-
_tokenExpiresInSeconds = options.TokenExpiresInSeconds;
80+
_tokenExpiresInSeconds = OAuth2OptionsBase.TokenExpiresInSeconds;
16281
}
16382

16483
public async Task InitializeAsync()
@@ -181,7 +100,7 @@ public async Task InitializeAsync()
181100
_testOutputHelper.WriteLine("{0} [INFO] connection recovery succeeded", DateTime.Now);
182101
};
183102

184-
_credentialsRefresher = new CredentialsRefresher(_credentialsProvider,
103+
_credentialsRefresher = new CredentialsRefresher(_producerCredentialsProvider,
185104
OnCredentialsRefreshedAsync,
186105
_cancellationTokenSource.Token);
187106
}
@@ -200,7 +119,7 @@ public async Task DisposeAsync()
200119
finally
201120
{
202121
_doneEvent.Dispose();
203-
_credentialsProvider.Dispose();
122+
_producerCredentialsProvider.Dispose();
204123
_connection?.Dispose();
205124
}
206125
}
@@ -227,12 +146,6 @@ private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? ex
227146
Assert.Fail("credentials arg is unexpectedly null!");
228147
}
229148

230-
if (_util != null)
231-
{
232-
_util.Dispose();
233-
}
234-
_util = new Util("mgt_api_client", credentials.Password);
235-
236149
_testOutputHelper.WriteLine("{0} [INFO] calling UpdateSecretAsync", DateTime.Now);
237150
Console.WriteLine("{0} [INFO] calling UpdateSecretAsync", DateTime.Now);
238151
return _connection.UpdateSecretAsync(credentials.Password, "Token refresh", cancellationToken);
@@ -241,6 +154,7 @@ private Task OnCredentialsRefreshedAsync(Credentials? credentials, Exception? ex
241154
[Fact]
242155
public async void IntegrationTest()
243156
{
157+
Util? closeConnectionUtil = null;
244158
Task? closeConnectionTask = null;
245159

246160
using (IChannel publishChannel = await DeclarePublishChannelAsync())
@@ -260,8 +174,15 @@ public async void IntegrationTest()
260174

261175
if (i == 1)
262176
{
263-
Assert.NotNull(_util);
264-
closeConnectionTask = _util.CloseConnectionAsync(_connection);
177+
async Task CloseConnection()
178+
{
179+
Assert.NotNull(_connection);
180+
Credentials httpApiCredentials = await _httpApiCredentialsProvider.GetCredentialsAsync();
181+
closeConnectionUtil = new Util("mgt_api_client", httpApiCredentials.Password);
182+
await closeConnectionUtil.CloseConnectionAsync(_connection.ClientProvidedName);
183+
}
184+
185+
closeConnectionTask = Task.Run(CloseConnection);
265186
}
266187

267188
await Task.Delay(delaySpan);
@@ -272,6 +193,12 @@ public async void IntegrationTest()
272193
closeConnectionTask = null;
273194
}
274195

196+
if (closeConnectionUtil != null)
197+
{
198+
closeConnectionUtil.Dispose();
199+
closeConnectionUtil = null;
200+
}
201+
275202
_testOutputHelper.WriteLine("{0} [INFO] Resuming ...", DateTime.Now);
276203

277204
await PublishAsync(publishChannel);
@@ -343,7 +270,7 @@ private async Task ConsumeAsync(IChannel consumeChannel)
343270
await consumeChannel.BasicCancelAsync(consumerTag);
344271
}
345272

346-
private OAuth2ClientCredentialsProvider GetCredentialsProvider(OAuth2Options opts)
273+
private OAuth2ClientCredentialsProvider GetCredentialsProvider(OAuth2OptionsBase opts)
347274
{
348275
_testOutputHelper.WriteLine("OAuth2Client ");
349276
_testOutputHelper.WriteLine($"- ClientId: {opts.ClientId}");

0 commit comments

Comments
 (0)