Skip to content

Commit 446f16d

Browse files
committed
specs use rsa keys for token validation
Previously specs used HMAC token signing / validation which is not used in production. This commit updates the uaa helper code to use RSA based "pkey" token signing / validation.
1 parent b932a3e commit 446f16d

9 files changed

Lines changed: 62 additions & 7 deletions

File tree

src/bosh-director/spec/support/uaa_helpers.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
require 'openssl'
2+
13
module Support
24
module UaaHelpers
5+
RSA_KEY = OpenSSL::PKey::RSA.generate(2048)
6+
7+
def uaa_token_public_key
8+
RSA_KEY.public_key.to_pem
9+
end
10+
311
def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
412
token_data = {
513
'jti' => token_id,
@@ -15,7 +23,7 @@ def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
1523
'iss' => 'https://fake-url/oauth/token',
1624
'aud' =>['test']
1725
}
18-
access_token = CF::UAA::TokenCoder.encode(token_data, {verify: false, skey: 's3cr3t'})
26+
access_token = CF::UAA::TokenCoder.encode(token_data, pkey: RSA_KEY, algorithm: 'RS256')
1927

2028
CF::UAA::TokenInfo.new(
2129
token_type: 'bearer',

src/bosh-director/spec/unit/bosh/director/config_server/uaa_auth_provider_spec.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
'client_id' => 'fake-client',
1212
'client_secret' => 'fake-client-secret',
1313
'url' => uaa_url,
14-
'ca_cert_path' => 'fake-ca-cert-path'
14+
'ca_cert_path' => 'fake-ca-cert-path',
15+
'public_key' => uaa_token_public_key,
1516
}
1617
end
1718
let(:logger) { double(:logger) }
@@ -101,8 +102,19 @@
101102

102103
context 'token decoding' do
103104
context 'when public_key is not configured' do
105+
let(:config) do
106+
{
107+
'client_id' => 'fake-client',
108+
'client_secret' => 'fake-client-secret',
109+
'url' => uaa_url,
110+
'ca_cert_path' => 'fake-ca-cert-path',
111+
}
112+
end
113+
104114
it 'decodes UAA tokens without verifying the signature' do
105-
expect(CF::UAA::TokenCoder).to receive(:decode).with(anything, { verify: false }).and_call_original
115+
expect(CF::UAA::TokenCoder).to receive(:decode)
116+
.with(anything, { verify: false })
117+
.and_return('exp' => Time.now.to_i + 3600)
106118
token.auth_header
107119
end
108120
end

src/bosh-monitor/spec/support/uaa_helpers.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
require 'openssl'
2+
13
module Support
24
module UaaHelpers
5+
RSA_KEY = OpenSSL::PKey::RSA.generate(2048)
6+
7+
def uaa_token_public_key
8+
RSA_KEY.public_key.to_pem
9+
end
10+
311
def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
412
token_data = {
513
'jti' => token_id,
@@ -15,7 +23,7 @@ def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
1523
'iss' => 'https://fake-url/oauth/token',
1624
'aud' => ['test'],
1725
}
18-
access_token = CF::UAA::TokenCoder.encode(token_data, verify: false, skey: 's3cr3t')
26+
access_token = CF::UAA::TokenCoder.encode(token_data, pkey: RSA_KEY, algorithm: 'RS256')
1927

2028
CF::UAA::TokenInfo.new(
2129
token_type: 'bearer',

src/bosh-monitor/spec/unit/bosh/monitor/auth_provider_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
context 'user provides director_ca_cert' do
6262
before do
6363
config['director_ca_cert'] = 'fake-ca-cert-path'
64+
config['uaa_public_key'] = uaa_token_public_key
6465

6566
allow(File).to receive(:exist?).with('fake-ca-cert-path').and_return(true)
6667
allow(File).to receive(:read).with('fake-ca-cert-path').and_return('test')
@@ -78,6 +79,7 @@
7879
before do
7980
config['director_ca_cert'] = 'fake-dir-cert-path'
8081
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
82+
config['uaa_public_key'] = uaa_token_public_key
8183

8284
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(true)
8385
allow(File).to receive(:read).with('fake-uaa-cert-path').and_return('uaa-pem')
@@ -95,6 +97,7 @@
9597
before do
9698
config['director_ca_cert'] = 'fake-dir-cert-path'
9799
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
100+
config['uaa_public_key'] = uaa_token_public_key
98101

99102
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(true)
100103
allow(File).to receive(:read).with('fake-uaa-cert-path').and_return(" \n")
@@ -114,6 +117,7 @@
114117
before do
115118
config['director_ca_cert'] = 'fake-dir-cert-path'
116119
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
120+
config['uaa_public_key'] = uaa_token_public_key
117121

118122
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(false)
119123
allow(File).to receive(:exist?).with('fake-dir-cert-path').and_return(true)
@@ -132,6 +136,8 @@
132136
let(:cert_store) { instance_double(OpenSSL::X509::Store) }
133137

134138
before do
139+
config['uaa_public_key'] = uaa_token_public_key
140+
135141
allow(OpenSSL::X509::Store).to receive(:new).and_return(cert_store)
136142
allow(cert_store).to receive(:set_default_paths)
137143
allow(CF::UAA::TokenIssuer).to receive(:new).with(
@@ -155,7 +161,9 @@
155161

156162
context 'when uaa_public_key is not configured' do
157163
it 'decodes UAA tokens without verifying the signature' do
158-
expect(CF::UAA::TokenCoder).to receive(:decode).with(anything, { verify: false }).and_call_original
164+
expect(CF::UAA::TokenCoder).to receive(:decode)
165+
.with(anything, { verify: false })
166+
.and_return('exp' => Time.now.to_i + 3600)
159167
auth_provider.auth_header
160168
end
161169
end

src/bosh-monitor/spec/unit/bosh/monitor/director_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'client_secret' => 'secret',
1818
'director_ca_cert' => 'fake-ca-cert',
1919
'uaa_ca_cert' => '',
20+
'uaa_public_key' => uaa_token_public_key,
2021
}, double(:logger)
2122
)
2223
end

src/bosh-monitor/spec/unit/bosh/monitor/plugins/event_logger_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'client_secret' => 'client-secret',
1414
'director_ca_cert' => 'ca-cert',
1515
'uaa_ca_cert' => '',
16+
'uaa_public_key' => uaa_token_public_key,
1617
},
1718
}
1819
end

src/bosh-monitor/spec/unit/bosh/monitor/plugins/resurrector_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Bosh::Monitor::Plugins
1313
'client_secret' => 'client-secret',
1414
'director_ca_cert' => 'ca-cert',
1515
'uaa_ca_cert' => '',
16+
'uaa_public_key' => uaa_token_public_key,
1617
},
1718
}
1819
end

src/bosh-nats-sync/spec/nats_sync/auth_provider_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
context 'user provides director_ca_cert' do
6969
before do
7070
config['director_ca_cert'] = 'fake-ca-cert-path'
71+
config['uaa_public_key'] = uaa_token_public_key
7172

7273
allow(File).to receive(:exist?).with('fake-ca-cert-path').and_return(true)
7374
allow(File).to receive(:read).with('fake-ca-cert-path').and_return('test')
@@ -85,6 +86,7 @@
8586
before do
8687
config['director_ca_cert'] = 'fake-dir-cert-path'
8788
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
89+
config['uaa_public_key'] = uaa_token_public_key
8890

8991
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(true)
9092
allow(File).to receive(:read).with('fake-uaa-cert-path').and_return('uaa-pem')
@@ -102,6 +104,7 @@
102104
before do
103105
config['director_ca_cert'] = 'fake-dir-cert-path'
104106
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
107+
config['uaa_public_key'] = uaa_token_public_key
105108

106109
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(true)
107110
allow(File).to receive(:read).with('fake-uaa-cert-path').and_return(" \n")
@@ -121,6 +124,7 @@
121124
before do
122125
config['director_ca_cert'] = 'fake-dir-cert-path'
123126
config['uaa_ca_cert'] = 'fake-uaa-cert-path'
127+
config['uaa_public_key'] = uaa_token_public_key
124128

125129
allow(File).to receive(:exist?).with('fake-uaa-cert-path').and_return(false)
126130
allow(File).to receive(:exist?).with('fake-dir-cert-path').and_return(true)
@@ -139,6 +143,8 @@
139143
let(:cert_store) { instance_double(OpenSSL::X509::Store) }
140144

141145
before do
146+
config['uaa_public_key'] = uaa_token_public_key
147+
142148
allow(OpenSSL::X509::Store).to receive(:new).and_return(cert_store)
143149
allow(cert_store).to receive(:set_default_paths)
144150
allow(CF::UAA::TokenIssuer).to receive(:new).with(
@@ -162,7 +168,9 @@
162168

163169
context 'when uaa_public_key is not configured' do
164170
it 'decodes UAA tokens without verifying the signature' do
165-
expect(CF::UAA::TokenCoder).to receive(:decode).with(anything, { verify: false }).and_call_original
171+
expect(CF::UAA::TokenCoder).to receive(:decode)
172+
.with(anything, { verify: false })
173+
.and_return('exp' => Time.now.to_i + 3600)
166174
auth_provider.auth_header
167175
end
168176
end

src/bosh-nats-sync/spec/support/uaa_helpers.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
require 'openssl'
2+
13
module Support
24
module UaaHelpers
5+
RSA_KEY = OpenSSL::PKey::RSA.generate(2048)
6+
7+
def uaa_token_public_key
8+
RSA_KEY.public_key.to_pem
9+
end
10+
311
def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
412
token_data = {
513
'jti' => token_id,
@@ -15,7 +23,7 @@ def uaa_token_info(token_id, expiration_time = Time.now.to_i + 3600)
1523
'iss' => 'https://fake-url/oauth/token',
1624
'aud' => ['test'],
1725
}
18-
access_token = CF::UAA::TokenCoder.encode(token_data, verify: false, skey: 's3cr3t')
26+
access_token = CF::UAA::TokenCoder.encode(token_data, pkey: RSA_KEY, algorithm: 'RS256')
1927

2028
CF::UAA::TokenInfo.new(
2129
token_type: 'bearer',

0 commit comments

Comments
 (0)