Skip to content

Commit 13faf2a

Browse files
authored
Cache parsed RSA key objects in UaaVerificationKeys (#5272)
1 parent f66d558 commit 13faf2a

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

lib/cloud_controller/uaa/uaa_verification_keys.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def format_keys(validation_hash)
3434
uaa_keys = { keys: [] }
3535

3636
validation_hash.each_value do |key|
37-
uaa_keys[:keys] << key['value']
37+
uaa_keys[:keys] << OpenSSL::PKey::RSA.new(key['value'])
3838
end
3939

4040
uaa_keys[:requested_time] = Time.now

spec/unit/lib/uaa/uaa_verification_keys_spec.rb

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ module VCAP::CloudController
77

88
let(:config_hash) { { url: 'http://uaa-url' } }
99
let(:uaa_info) { double(CF::UAA::Info) }
10-
let(:key_hash) { { 'key-name' => { 'value' => 'value-from-uaa' } } }
10+
let(:rsa_key) { OpenSSL::PKey::RSA.new(2048) }
11+
let(:rsa_pem) { rsa_key.public_key.to_pem }
12+
let(:key_hash) { { 'key-name' => { 'value' => rsa_pem } } }
1113
let(:my_logger) { double(Steno::Logger) }
1214

1315
describe '#value' do
@@ -24,9 +26,11 @@ module VCAP::CloudController
2426
end
2527

2628
context 'when key was never fetched' do
27-
it 'is fetched' do
29+
it 'is fetched and returns parsed RSA key objects' do
2830
expect(uaa_info).to receive(:validation_keys_hash)
29-
expect(subject.value).to eq ['value-from-uaa']
31+
result = subject.value
32+
expect(result.length).to eq(1)
33+
expect(result.first).to be_a(OpenSSL::PKey::RSA)
3034
end
3135
end
3236
end
@@ -38,15 +42,18 @@ module VCAP::CloudController
3842
end
3943

4044
context 'when key was never fetched' do
41-
it 'is fetched' do
45+
it 'is fetched and returns parsed RSA key objects' do
4246
expect(uaa_info).to receive(:validation_keys_hash)
43-
expect(subject.value).to eq ['value-from-uaa']
47+
result = subject.value
48+
expect(result.length).to eq(1)
49+
expect(result.first).to be_a(OpenSSL::PKey::RSA)
4450
end
4551
end
4652
end
4753

4854
context 'when key was fetched more than 30 seconds ago' do
49-
let(:key_hash2) { { 'key-name' => { 'value' => 'another-from-uaa' } } }
55+
let(:rsa_key2) { OpenSSL::PKey::RSA.new(2048) }
56+
let(:key_hash2) { { 'key-name' => { 'value' => rsa_key2.public_key.to_pem } } }
5057

5158
before { allow(uaa_info).to receive(:validation_keys_hash).and_return(key_hash, key_hash2) }
5259

@@ -57,12 +64,16 @@ module VCAP::CloudController
5764
subject.value
5865
end
5966

60-
expect(subject.value).to eq(['another-from-uaa'])
67+
result = subject.value
68+
expect(result.length).to eq(1)
69+
expect(result.first).to be_a(OpenSSL::PKey::RSA)
70+
expect(result.first.public_key.to_pem).to eq(rsa_key2.public_key.to_pem)
6171
end
6272
end
6373

6474
context 'when key was fetched less than 30 seconds ago' do
65-
let(:key_hash2) { { 'key-name' => { 'value' => 'another-from-uaa' } } }
75+
let(:rsa_key2) { OpenSSL::PKey::RSA.new(2048) }
76+
let(:key_hash2) { { 'key-name' => { 'value' => rsa_key2.public_key.to_pem } } }
6677

6778
before { allow(uaa_info).to receive(:validation_keys_hash).and_return(key_hash, key_hash2) }
6879

@@ -72,7 +83,9 @@ module VCAP::CloudController
7283
Timecop.travel(25)
7384
subject.value
7485
end
75-
expect(subject.value).to eq(['value-from-uaa'])
86+
result = subject.value
87+
expect(result.length).to eq(1)
88+
expect(result.first.public_key.to_pem).to eq(rsa_pem)
7689
end
7790
end
7891

@@ -101,9 +114,11 @@ module VCAP::CloudController
101114

102115
it 'returns the previously fetched verification keys' do
103116
Timecop.freeze do
104-
expect(subject.value).to eq(['value-from-uaa'])
117+
result1 = subject.value
118+
expect(result1.first).to be_a(OpenSSL::PKey::RSA)
105119
Timecop.travel(40)
106-
expect(subject.value).to eq(['value-from-uaa'])
120+
result2 = subject.value
121+
expect(result2.first.public_key.to_pem).to eq(rsa_pem)
107122
end
108123
end
109124
end
@@ -131,10 +146,11 @@ module VCAP::CloudController
131146
end
132147

133148
context 'when key was never fetched' do
134-
it 'is fetched' do
149+
it 'is fetched and returns parsed RSA key objects' do
135150
expect(uaa_info).to receive(:validation_keys_hash)
136151
subject.refresh
137-
expect(subject.value).to eq(['value-from-uaa'])
152+
result = subject.value
153+
expect(result.first).to be_a(OpenSSL::PKey::RSA)
138154
end
139155
end
140156

@@ -147,7 +163,8 @@ module VCAP::CloudController
147163
it 'is RE-fetched again' do
148164
expect(uaa_info).to receive(:validation_keys_hash)
149165
subject.refresh
150-
expect(subject.value).to eq(['value-from-uaa'])
166+
result = subject.value
167+
expect(result.first).to be_a(OpenSSL::PKey::RSA)
151168
end
152169
end
153170
end

0 commit comments

Comments
 (0)