Skip to content

Commit

Permalink
making adding of x_ops_api_info header to the response configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Vinay Satish <vinay.satish@progress.com>
  • Loading branch information
vinay-satish committed Oct 18, 2021
1 parent ead664b commit afd0ec4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
12 changes: 11 additions & 1 deletion oc-chef-pedant/spec/api/header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
describe "Headers", :headers do
let (:request_url) { api_url("users") }
let (:requestor) { platform.admin_user }
let (:config) { JSON.parse(IO.read("/etc/opscode/chef-server-running.json"))['private_chef'] }


context "Request Headers" do
Expand Down Expand Up @@ -43,9 +44,18 @@
end
end # context "Request Headers"

context "Verify nginx default error responses", :nginx_default_error do
context "Verify nginx default error responses", :nginx_default_error do
it "openresty tag should not be present in the nginx default error responses" do
get(request_url.sub!('https', 'http'), superuser).to_s.should_not loosely_match(/.*openresty.*/)
end
end # context "Verify nginx default error response"


context "Responses Headers", :response_headers do
it "x_ops_api_info should be configured" do
response_header = get(request_url, requestor).headers
response_header[:x_ops_api_info].nil?.should eql !config['opscode-erchef']['include_x_ops_api_info']
end
end # context "Responses Headers"

end # describe "Headers"
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
default['private_chef']['opscode-erchef']['keygen_key_size'] = 2048
default['private_chef']['opscode-erchef']['strict_search_result_acls'] = false
default['private_chef']['opscode-erchef']['ssl_session_caching']['enabled'] = false
default['private_chef']['opscode-erchef']['include_x_ops_api_info'] = false

# The amount of milliseconds before we timeout and assume an endpoint is down for
# the /_status endpoint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@
{base_resource_url, <%= @helper.erl_atom_or_string(node['private_chef']['opscode-erchef']['base_resource_url']) %>},
{strict_search_result_acls, <%= @strict_search_result_acls %>},
{include_version_in_status, <%= node['private_chef']['opscode-erchef']['include_version_in_status'] %>},
{allow_email_update_only_from_manage, <%= node['private_chef']['opscode-erchef']['allow_email_update_only_from_manage'] %>}
{allow_email_update_only_from_manage, <%= node['private_chef']['opscode-erchef']['allow_email_update_only_from_manage'] %>},
{include_x_ops_api_info, <%= node['private_chef']['opscode-erchef']['include_x_ops_api_info'] %>}
]},

{chef_authn, [
Expand Down
11 changes: 9 additions & 2 deletions src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ api_info_header_value(#base_state{}=State) ->
-spec add_api_info_header(#wm_reqdata{}, #base_state{}, atom()) ->
#wm_reqdata{}.
add_api_info_header(Req, #base_state{server_api_version = APIVersion} = State, undefined) ->
Req0 = wrq:set_resp_header("X-Ops-API-Info", api_info_header_value(State), Req),
Req0 = add_x_ops_api_info(Req, State),
HeaderBody = build_api_info_header_body(APIVersion, APIVersion),
wrq:set_resp_header("X-Ops-Server-API-Version", HeaderBody, Req0);
add_api_info_header(Req, #base_state{server_api_version = RequestedVersion} = State, not_acceptable) ->
Expand All @@ -951,9 +951,16 @@ add_api_info_header(Req, #base_state{server_api_version = RequestedVersion} = St
_ ->
RequestedVersion
end,
Req0 = wrq:set_resp_header("X-Ops-API-Info", api_info_header_value(State), Req),
Req0 = add_x_ops_api_info(Req, State),
HeaderBody = build_api_info_header_body(RequestedAPIVersion, ActualAPIVersion),
wrq:set_resp_header("X-Ops-Server-API-Version", HeaderBody, Req0).
-spec add_x_ops_api_info(#wm_reqdata{}, #base_state{}) ->
#wm_reqdata{}.
add_x_ops_api_info(Req, State) ->
case envy:get(oc_chef_wm, include_x_ops_api_info, false, boolean) of
true -> wrq:set_resp_header("X-Ops-API-Info", api_info_header_value(State), Req);
_ -> Req
end.
-spec build_api_info_header_body(integer(), integer()) ->
list().
build_api_info_header_body(RequestedAPIVersion, ActualAPIVersion) ->
Expand Down
26 changes: 26 additions & 0 deletions src/oc_erchef/apps/oc_chef_wm/test/oc_chef_wm_base_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,32 @@ verify_email_updates_test_() ->
end}
]}.

verify_add_x_ops_api_info_test_(Include_version_in_status) ->
{
setup,
fun() ->
application:set_env(oc_chef_wm, include_version_in_status, Include_version_in_status),
meck:expect(wrq, get_req_header, fun(_Header, _Req) -> "username" end)
end,
fun(_) ->
meck:unload()
end,
[
fun() ->
Req1 = make_req_data(),
State = make_base_state(),
Req2 = oc_chef_wm_base:add_x_ops_api_info(Req1, State),
HeaderPresent =
case wrq:get_req_header("add_x_ops_api_info", Req2) of
undefined -> false;
"" -> false;
_ -> true
end,
?assertEqual(Include_version_in_status, HeaderPresent)
end
]
}.

make_req_data() ->
#wm_reqdata{}.

Expand Down

0 comments on commit afd0ec4

Please sign in to comment.