<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2011, Opscode, Inc.
%%% @doc
%%% REST resource for submitting searches for Chef resources
%%% @end
%% @author <NAME> <<EMAIL>>
%% @author <NAME> <<EMAIL>>
%% @author <NAME> <<EMAIL>>
%% @copyright Copyright 2011 Opscode, Inc.
%% @version 0.1
%%%-------------------------------------------------------------------
-module(chef_rest_search_resource).

-export([init/1,
         malformed_request/2,
         is_authorized/2,
         resource_exists/2,
         allowed_methods/2,
         content_types_provided/2,
         to_json/2]).

-record(state, {start_time,
                resource,
                organization_guid,
                organization_name,
                object_type,
                user_name,
                header_fun = undefined,
                couchbeam = undefined,
                solr_query = undefined,
                end_time
}).

-include_lib("webmachine/include/webmachine.hrl").

-define(db_for_guid(X), [<<"chef_">>, X]).
-define(gv(X,L), proplists:get_value(X, L)).
-define(gv(X,L, D), proplists:get_value(X, L, D)).

init(_Any) ->
    State = #state{start_time = chef_rest_util:iso_8601_utc(),
                   resource = atom_to_list(?MODULE)},
    {ok, State}.

get_header_fun(Req, State = #state{header_fun = HFun})
  when HFun =:= undefined ->
    GetHeader = fun(H) ->
                        case wrq:get_req_header(H, Req) of
                            B when is_binary(B) -> B;
                            S when is_list(S) -> iolist_to_binary(S);
                            undefined -> undefined
                        end
                end,
    {GetHeader, State#state{header_fun = GetHeader}};
get_header_fun(_Req, State) ->
    {State#state.header_fun, State}.

resource_exists(Req, State) ->
    {true, Req, State}.

verify_request_signature(Req, State) ->
    OrgName = wrq:path_info(organization_id, Req),
    UserName = wrq:get_req_header("x-ops-userid", Req),
    % TODO Quit leaking this
    S = chef_otto:connect(),
    case chef_otto:fetch_user_or_client_cert(S, OrgName, UserName) of
        not_found ->
            NoCertMsg = bad_auth_message(no_cert),
            {false,
             wrq:set_resp_body(ejson:encode(NoCertMsg), Req), State};
        CertInfo ->
            Cert = ?gv(cert, CertInfo),
            OrgId = ?gv(org_guid, CertInfo, State#state.organization_guid),
            Body = body_or_default(Req, <<>>),
            HTTPMethod = iolist_to_binary(atom_to_list(wrq:method(Req))),
            Path = iolist_to_binary(wrq:path(Req)),
            {GetHeader, State1} = get_header_fun(Req, State),
            case chef_authn:authenticate_user_request(GetHeader, HTTPMethod,
                                                      Path, Body, Cert, 300) of
                {name, _} ->
                    {true, Req,
                     State1#state{organization_guid = OrgId, couchbeam = S}};
                {no_authn, Reason} ->
                    Msg = bad_auth_message(Reason),
                    Json = ejson:encode(Msg),
                    Req1 = wrq:set_resp_body(Json, Req),
                    % TODO This is a needless mutation
                    {false, Req1, State1#state{couchbeam = S}}
            end
    end.

is_authorized(Req, State) ->
    OrgName = wrq:path_info(organization_id, Req),
    UserName = wrq:get_req_header("x-ops-userid", Req),
    ChefClient = chef_rest_client:make_webui_account_chef_rest_client(UserName),
    case verify_request_signature(Req,State) of
	{true, Req1, State1} ->
	    case chef_permissions:is_user_with_org(ChefClient, UserName, OrgName) of
		true -> {true, Req1, State1};
		false -> 
		    Msg = bad_auth_message(not_member_of_org),
		    Json = ejson:encode(Msg),
                    Req2 = wrq:set_resp_body(Json, Req),
		    {false, Req2, State1}
	    end;
	Other -> Other
    end.	  

malformed_request(Req, State) ->
    {GetHeader, State1} = get_header_fun(Req, State),
    {Malformed, Req1, State2} =
        try
            chef_authn:validate_headers(GetHeader, 300),
            %% transform query first to avoid possible db fetch for org
            %% guid for bad queries
            Query = transform_query(http_uri:decode(wrq:get_qs_value("q", Req))),
            {false, Req, State1#state{solr_query = Query}}
        catch
            throw:Why ->
                Msg = bad_auth_message(Why),
                NewReq = wrq:set_resp_body(ejson:encode(Msg), Req),
                {true, NewReq, State1}
        end,
    {Malformed, Req1, State2}.<commit_msg>Use chef_otto:is_user_with_org instead of chef_rest_client/chef_permissions<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME> <<EMAIL>>
%%% @author <NAME> <<EMAIL>>
%%% @copyright (C) 2011, Opscode, Inc.
%%% @doc
%%% REST resource for submitting searches for Chef resources
%%% @end
%% @author <NAME> <<EMAIL>>
%% @author <NAME> <<EMAIL>>
%% @author <NAME> <<EMAIL>>
%% @copyright Copyright 2011 Opscode, Inc.
%% @version 0.1
%%%-------------------------------------------------------------------
-module(chef_rest_search_resource).

-export([init/1,
         malformed_request/2,
         is_authorized/2,
         resource_exists/2,
         allowed_methods/2,
         content_types_provided/2,
         to_json/2]).

-record(state, {start_time,
                resource,
                organization_guid,
                organization_name,
                object_type,
                user_name,
                header_fun = undefined,
                couchbeam = undefined,
                solr_query = undefined,
                end_time
}).

-include_lib("webmachine/include/webmachine.hrl").

-define(db_for_guid(X), [<<"chef_">>, X]).
-define(gv(X,L), proplists:get_value(X, L)).
-define(gv(X,L, D), proplists:get_value(X, L, D)).

init(_Any) ->
    State = #state{start_time = chef_rest_util:iso_8601_utc(),
                   resource = atom_to_list(?MODULE)},
    {ok, State}.

get_header_fun(Req, State = #state{header_fun = HFun})
  when HFun =:= undefined ->
    GetHeader = fun(H) ->
                        case wrq:get_req_header(H, Req) of
                            B when is_binary(B) -> B;
                            S when is_list(S) -> iolist_to_binary(S);
                            undefined -> undefined
                        end
                end,
    {GetHeader, State#state{header_fun = GetHeader}};
get_header_fun(_Req, State) ->
    {State#state.header_fun, State}.

resource_exists(Req, State) ->
    {true, Req, State}.

verify_request_signature(Req, State) ->
    OrgName = wrq:path_info(organization_id, Req),
    UserName = wrq:get_req_header("x-ops-userid", Req),
    % TODO Quit leaking this
    S = chef_otto:connect(),
    case chef_otto:fetch_user_or_client_cert(S, OrgName, UserName) of
        not_found ->
            NoCertMsg = bad_auth_message(no_cert),
            {false,
             wrq:set_resp_body(ejson:encode(NoCertMsg), Req), State};
        CertInfo ->
            Cert = ?gv(cert, CertInfo),
            OrgId = ?gv(org_guid, CertInfo, State#state.organization_guid),
            Body = body_or_default(Req, <<>>),
            HTTPMethod = iolist_to_binary(atom_to_list(wrq:method(Req))),
            Path = iolist_to_binary(wrq:path(Req)),
            {GetHeader, State1} = get_header_fun(Req, State),
            case chef_authn:authenticate_user_request(GetHeader, HTTPMethod,
                                                      Path, Body, Cert, 300) of
                {name, _} ->
                    {true, Req,
                     State1#state{organization_guid = OrgId, couchbeam = S}};
                {no_authn, Reason} ->
                    Msg = bad_auth_message(Reason),
                    Json = ejson:encode(Msg),
                    Req1 = wrq:set_resp_body(Json, Req),
                    % TODO This is a needless mutation
                    {false, Req1, State1#state{couchbeam = S}}
            end
    end.

is_authorized(Req, State) ->
    OrgName = list_to_binary(wrq:path_info(organization_id, Req)),
    UserName = list_to_binary(wrq:get_req_header("x-ops-userid", Req)),
    S = chef_otto:connect(),
    case verify_request_signature(Req, State) of
	{true, Req1, State1} ->
	    case chef_otto:is_user_in_org(S, UserName, OrgName) of
		true -> {true, Req1, State1};
		false -> 
		    Msg = bad_auth_message(not_member_of_org),
		    Json = ejson:encode(Msg),
                    Req2 = wrq:set_resp_body(Json, Req),
		    {false, Req2, State1}
	    end;
	Other -> Other
    end.	  

malformed_request(Req, State) ->
    {GetHeader, State1} = get_header_fun(Req, State),
    {Malformed, Req1, State2} =
        try
            chef_authn:validate_headers(GetHeader, 300),
            %% transform query first to avoid possible db fetch for org
            %% guid for bad queries
            Query = transform_query(http_uri:decode(wrq:get_qs_value("q", Req))),
            {false, Req, State1#state{solr_query = Query}}
        catch
            throw:Why ->
                Msg = bad_auth_message(Why),
                NewReq = wrq:set_resp_body(ejson:encode(Msg), Req),
                {true, NewReq, State1}
        end,
    {Malformed, Req1, State2}.
{'fraction_non_alphanumeric': 0.1497178149291875, 'fraction_numerical': 0.00979661377915025, 'mean_word_length': 1.9488226059654632, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209683', 'n_tokens_mistral': 3095, 'n_tokens_neox': 2936, 'n_words': 572}