-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set warning header for outdated CF CLIs
Introduces a new (optional) middleware which will warn CF CLI users if their version is below `min_cli_version`. Needs to be explicitly enabled by setting the config flag `warn_if_below_min_cli_version` to `true`.
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module CloudFoundry | ||
module Middleware | ||
class BelowMinCliWarning | ||
def initialize(app) | ||
@app = app | ||
@min_cli_version = Gem::Version.new(VCAP::CloudController::Config.config.get(:info, :min_cli_version)) | ||
end | ||
|
||
def call(env) | ||
status, headers, body = @app.call(env) | ||
|
||
included_endpoints = %w[/v3/spaces /v3/organizations /v2/spaces /v2/organizations] | ||
|
||
if included_endpoints.any? { |ep| env['REQUEST_PATH'].include?(ep) } && is_below_min_cli_version?(env['HTTP_USER_AGENT']) | ||
# Ensure existing warnings are appended by ',' (unicode %2C) | ||
new_warning = env['X-Cf-Warnings'].nil? ? escaped_warning : "#{env['X-Cf-Warnings']}%2C#{escaped_warning}" | ||
headers['X-Cf-Warnings'] = new_warning | ||
end | ||
|
||
[status, headers, body] | ||
end | ||
|
||
def escaped_warning | ||
CGI.escape("\u{1F6A8} [WARNING] Outdated CF CLI version - please upgrade: https://github.com/cloudfoundry/cli/releases/latest \u{1F6A8}\n") | ||
end | ||
|
||
def is_below_min_cli_version?(user_agent_string) | ||
regex = %r{ | ||
[cC][fF] # match 'cf', case insensitive | ||
[^/]* # match any character that are not '/' | ||
/ # match '/' character | ||
(\d+\.\d+\.\d+) # capture the version number (expecting 3 groups of digits separated by '.') | ||
(?:\+|\s) # match '+' character or a whitespace, non-capturing group | ||
}x | ||
|
||
match = user_agent_string.match(regex) | ||
return false unless match | ||
|
||
current_version = Gem::Version.new(match[1]) | ||
|
||
current_version < @min_cli_version | ||
rescue StandardError => e | ||
logger.warn("Warning: An error occurred while checking user agent version: #{e.message}") | ||
false | ||
end | ||
|
||
private | ||
|
||
def logger | ||
@logger = Steno.logger('cc.deprecated_cf_cli_warning') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'spec_helper' | ||
require 'below_min_cli_warning' | ||
|
||
module CloudFoundry | ||
module Middleware | ||
RSpec.describe BelowMinCliWarning do | ||
subject { described_class.new(app) } | ||
let(:app) { double(:app, call: [200, {}, 'a body']) } | ||
let(:env) { { 'HTTP_USER_AGENT' => 'mocked-user-agent', 'REQUEST_PATH' => '/v3/organizations' } } | ||
|
||
before { TestConfig.override(info: { min_cli_version: '7.0.0' }, warn_if_below_min_cli_version: true) } | ||
|
||
describe 'deprecated cf cli version middleware is called' do | ||
context 'called with outdated cf cli version' do | ||
before { allow(subject).to receive(:is_below_min_cli_version?).and_return(true) } | ||
|
||
it 'sets X-Cf-Warnings header' do | ||
_, headers = subject.call(env) | ||
expect(headers['X-Cf-Warnings']).to eq(subject.escaped_warning) | ||
end | ||
|
||
it 'appends to the existing X-Cf-Warnings header' do | ||
_, headers = subject.call(env.merge!({ 'X-Cf-Warnings' => 'a-warning' })) | ||
expect(headers['X-Cf-Warnings']).to eq("a-warning%2C#{subject.escaped_warning}") | ||
end | ||
end | ||
|
||
context 'called with current cf cli version' do | ||
before { allow(subject).to receive(:is_below_min_cli_version?).and_return(false) } | ||
|
||
it 'does not add X-Cf-Warnings header' do | ||
_, headers = subject.call(env) | ||
expect(headers['X-Cf-Warnings']).to be_nil | ||
end | ||
end | ||
|
||
context 'checks the request path' do | ||
before { allow(subject).to receive(:is_below_min_cli_version?).and_return(true) } | ||
|
||
%w[/v3/processes /v2/services /something/stats].each do |endpoint| | ||
it "does not set X-Cf-Warnings header for #{endpoint}" do | ||
_, headers = subject.call(env.merge!({ 'REQUEST_PATH' => endpoint })) | ||
expect(headers['X-Cf-Warnings']).to be_nil | ||
end | ||
end | ||
|
||
%w[/v3/spaces /v2/spaces /v3/organizations /v2/organizations].each do |endpoint| | ||
it "sets X-Cf-Warnings header for #{endpoint}" do | ||
_, headers = subject.call(env.merge!({ 'REQUEST_PATH' => endpoint })) | ||
expect(headers['X-Cf-Warnings']).to eq(subject.escaped_warning) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'is_outdated_cf_cli_user_agent?' do | ||
['some-client', | ||
'cf7/7.5.0+0ad1d6398.2022-06-04 (go1.17.10; amd64 windows)', | ||
'cf/7.7.2+b663981.2023-08-31 (go1.20.7; amd64 linux)', | ||
'cf/8.7.5+8aa8625.2023-11-20 (go1.21.4; arm64 darwin)', | ||
'cf8.exe/8.3.0+e6f8a853a.2022-03-11 (go1.17.7; amd64 windows)', | ||
'cf8/8.7.4+db5d612.2023-10-20 (go1.21.3; amd64 linux)', | ||
'cf.exe/7.4.0+e55633fed.2021-11-15 (go1.16.6; amd64 windows)', | ||
'Cf/8.5.0+73aa161.2022-09-12 (go1.18.5; arm64 darwin)'].each do |user_agent| | ||
it("returns false for #{user_agent}") { expect(subject).not_to be_is_below_min_cli_version(user_agent) } | ||
end | ||
|
||
['cf/6.46.0+29d6257f1.2019-07-09 (go1.12.7; amd64 windows)', | ||
'CF/6.46.0+29d6257f1.2019-07-09 (go1.12.7; amd64 windows)', | ||
'Cf/6.46.0+29d6257f1.2019-07-09 (go1.12.7; amd64 windows)', | ||
'cf.exe/6.6.0+e25762999.2023-02-16 (go1.19.5; amd64 windows)', | ||
'cf/6.43.0 (go1.10.8; amd64 linux)', | ||
'cf6/6.53.0+bommel'].each do |user_agent| | ||
it("returns true for #{user_agent}") { expect(subject).to be_is_below_min_cli_version(user_agent) } | ||
end | ||
end | ||
end | ||
end | ||
end |