Skip to content

Commit 082bafc

Browse files
author
Tim Hess
authored
Merge pull request #63 from jplebre/pcf-redis-tls-support
Ensure TLS Ports are picked as default, if provided in the creds
2 parents af04080 + c9f47c0 commit 082bafc

7 files changed

Lines changed: 72 additions & 14 deletions

File tree

src/Connectors/src/ConnectorBase/Services/RedisServiceInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class RedisServiceInfo : UriServiceInfo
2121
public const string REDIS_SCHEME = "redis";
2222
public const string REDIS_SECURE_SCHEME = "rediss";
2323

24-
public RedisServiceInfo(string id, string host, int port, string password)
25-
: base(id, REDIS_SCHEME, host, port, null, password, null)
24+
public RedisServiceInfo(string id, string scheme, string host, int port, string password)
25+
: base(id, scheme, host, port, null, password, null)
2626
{
2727
}
2828

src/Connectors/src/ConnectorBase/Services/RedisServiceInfoFactory.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ public override IServiceInfo Create(Service binding)
2929
if (string.IsNullOrEmpty(uri))
3030
{
3131
string host = GetHostFromCredentials(binding.Credentials);
32-
int port = GetPortFromCredentials(binding.Credentials);
3332
string password = GetPasswordFromCredentials(binding.Credentials);
33+
int port = GetPortFromCredentials(binding.Credentials);
34+
int tlsPort = GetTlsPortFromCredentials(binding.Credentials);
35+
bool tlsEnabled = tlsPort != 0;
3436

35-
return new RedisServiceInfo(binding.Name, host, port, password);
37+
return new RedisServiceInfo(
38+
binding.Name,
39+
tlsEnabled ? RedisServiceInfo.REDIS_SECURE_SCHEME : RedisServiceInfo.REDIS_SCHEME,
40+
host,
41+
tlsEnabled ? tlsPort : port,
42+
password);
3643
}
3744
else
3845
{

src/Connectors/src/ConnectorBase/Services/ServiceInfoFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ protected internal virtual int GetPortFromCredentials(Dictionary<string, Credent
158158
return GetIntFromCredentials(credentials, "port");
159159
}
160160

161+
protected internal virtual int GetTlsPortFromCredentials(Dictionary<string, Credential> credentials)
162+
{
163+
return GetIntFromCredentials(credentials, "tls_port");
164+
}
165+
161166
protected internal virtual string GetHostFromCredentials(Dictionary<string, Credential> credentials)
162167
{
163168
return GetStringFromCredentials(credentials, _hostList);

src/Connectors/test/ConnectorBase.Test/Cache/RedisCacheConfigurerTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void UpdateOptions_FromServiceInfo_ReturnsExcpected()
6060
{
6161
RedisCacheConfigurer configurer = new RedisCacheConfigurer();
6262
RedisCacheConnectorOptions connOptions = new RedisCacheConnectorOptions();
63-
RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword")
63+
RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword")
6464
{
6565
ApplicationInfo = new ApplicationInstanceInfo()
6666
{
@@ -104,7 +104,7 @@ public void Configure_ServiceInfoOveridesConfig_ReturnsExpected()
104104
Password = "password",
105105
InstanceName = "instanceId"
106106
};
107-
RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword")
107+
RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword")
108108
{
109109
ApplicationInfo = new ApplicationInstanceInfo()
110110
{
@@ -154,7 +154,7 @@ public void ConfigureConnection_ServiceInfoOveridesConfig_ReturnsExpected()
154154
Port = 1234,
155155
Password = "password"
156156
};
157-
RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword")
157+
RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword")
158158
{
159159
ApplicationInfo = new ApplicationInstanceInfo()
160160
{

src/Connectors/test/ConnectorBase.Test/Cache/RedisServiceConnectorFactoryTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Create_CanReturnRedisCache()
3636
Password = "password",
3737
InstanceName = "instanceId"
3838
};
39-
RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword")
39+
RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword")
4040
{
4141
ApplicationInfo = new ApplicationInstanceInfo()
4242
{
@@ -66,7 +66,7 @@ public void Create_CanReturnConnectionMultiplexer()
6666
AbortOnConnectFail = false,
6767
ConnectTimeout = 1
6868
};
69-
RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword")
69+
RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "127.0.0.1", 4321, "sipassword")
7070
{
7171
ApplicationInfo = new ApplicationInstanceInfo()
7272
{

src/Connectors/test/ConnectorBase.Test/Services/RedisServiceInfoFactoryTest.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Accept_AcceptsValidServiceBinding()
3333
{ "host", new Credential("192.168.0.103") },
3434
{ "password", new Credential("133de7c8-9f3a-4df1-8a10-676ba7ddaa10") },
3535
{ "port", new Credential("60287") }
36-
}
36+
}
3737
};
3838
RedisServiceInfoFactory factory = new RedisServiceInfoFactory();
3939
Assert.True(factory.Accept(s));
@@ -53,7 +53,7 @@ public void Accept_RejectsInvalidServiceBinding()
5353
{ "host", new Credential("192.168.0.103") },
5454
{ "password", new Credential("133de7c8-9f3a-4df1-8a10-676ba7ddaa10") },
5555
{ "port", new Credential("60287") }
56-
}
56+
}
5757
};
5858
RedisServiceInfoFactory factory = new RedisServiceInfoFactory();
5959
Assert.False(factory.Accept(s));
@@ -73,7 +73,7 @@ public void Create_CreatesValidServiceBinding()
7373
{ "host", new Credential("192.168.0.103") },
7474
{ "password", new Credential("133de7c8-9f3a-4df1-8a10-676ba7ddaa10") },
7575
{ "port", new Credential("60287") }
76-
}
76+
}
7777
};
7878
RedisServiceInfoFactory factory = new RedisServiceInfoFactory();
7979
var info = factory.Create(s) as RedisServiceInfo;
@@ -82,6 +82,35 @@ public void Create_CreatesValidServiceBinding()
8282
Assert.Equal("133de7c8-9f3a-4df1-8a10-676ba7ddaa10", info.Password);
8383
Assert.Equal("192.168.0.103", info.Host);
8484
Assert.Equal(60287, info.Port);
85+
Assert.Equal("redis", info.Scheme);
86+
}
87+
88+
[Fact]
89+
public void Create_CreatesValidServiceBindingForTLS()
90+
{
91+
Service s = new Service()
92+
{
93+
Label = "p.redis",
94+
Tags = new string[] { "redis", "pivotal" },
95+
Name = "myRedisService",
96+
Plan = "cache-small",
97+
Credentials = new Credential()
98+
{
99+
{ "host", new Credential("192.168.0.103") },
100+
{ "password", new Credential("133de7c8-9f3a-4df1-8a10-676ba7ddaa10") },
101+
{ "port", new Credential("60287") },
102+
{ "tls_port", new Credential("6287") }
103+
}
104+
};
105+
106+
RedisServiceInfoFactory factory = new RedisServiceInfoFactory();
107+
var info = factory.Create(s) as RedisServiceInfo;
108+
Assert.NotNull(info);
109+
Assert.Equal("myRedisService", info.Id);
110+
Assert.Equal("133de7c8-9f3a-4df1-8a10-676ba7ddaa10", info.Password);
111+
Assert.Equal("192.168.0.103", info.Host);
112+
Assert.Equal(6287, info.Port);
113+
Assert.Equal("rediss", info.Scheme);
85114
}
86115
}
87116
}

src/Connectors/test/ConnectorBase.Test/Services/RedisServiceInfoTest.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class RedisServiceInfoTest
2323
public void Constructor_CreatesExpected()
2424
{
2525
string uri = "redis://joe:joes_password@localhost:1527/";
26-
RedisServiceInfo r1 = new RedisServiceInfo("myId", "localhost", 1527, "joes_password");
26+
RedisServiceInfo r1 = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "localhost", 1527, "joes_password");
2727
RedisServiceInfo r2 = new RedisServiceInfo("myId", uri);
2828

2929
Assert.Equal("myId", r1.Id);
@@ -48,7 +48,7 @@ public void Constructor_CreatesExpected()
4848
public void Constructor_CreatesExpected_withSecure()
4949
{
5050
string uri = "rediss://:joes_password@localhost:6380/";
51-
RedisServiceInfo r1 = new RedisServiceInfo("myId", "localhost", 1527, "joes_password");
51+
RedisServiceInfo r1 = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "localhost", 1527, "joes_password");
5252
RedisServiceInfo r2 = new RedisServiceInfo("myId", uri);
5353

5454
Assert.Equal("myId", r1.Id);
@@ -67,5 +67,22 @@ public void Constructor_CreatesExpected_withSecure()
6767
Assert.Equal(string.Empty, r2.Path);
6868
Assert.Null(r2.Query);
6969
}
70+
71+
[Theory]
72+
[InlineData("redis")]
73+
[InlineData("rediss")]
74+
public void Constructor_CreatesExpected_WithSchema(string scheme)
75+
{
76+
string uri = $"{scheme}://:joes_password@localhost:6380/";
77+
RedisServiceInfo redisInfo = new RedisServiceInfo("myId", scheme, "localhost", 1527, "joes_password");
78+
79+
Assert.Equal("myId", redisInfo.Id);
80+
Assert.Equal(scheme, redisInfo.Scheme);
81+
Assert.Equal("localhost", redisInfo.Host);
82+
Assert.Equal(1527, redisInfo.Port);
83+
Assert.Equal("joes_password", redisInfo.Password);
84+
Assert.Null(redisInfo.Path);
85+
Assert.Null(redisInfo.Query);
86+
}
7087
}
7188
}

0 commit comments

Comments
 (0)