Removing a few style issues

pull/11884/head
Yash Tibrewal 7 years ago
parent ad11680be7
commit 78d7125017
  1. 44
      src/core/ext/filters/client_channel/http_proxy.c
  2. 7
      test/core/end2end/fixtures/h2_http_proxy.c
  3. 66
      test/core/end2end/fixtures/http_proxy_fixture.c

@ -34,12 +34,19 @@
#include "src/core/lib/support/string.h"
#include "src/core/lib/slice/b64.h"
static void grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx,
char **name_to_resolve,
/**
* Parses the 'http_proxy' env var and returns the proxy hostname to resolve or
* NULL on error. Also sets 'user_cred' if it is not NULL to user credentials
* if present in the 'http_proxy' env var.
*/
static char *grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx,
char **user_cred) {
*name_to_resolve = NULL;
char *proxy_name = NULL;
if(user_cred != NULL) {
*user_cred = NULL;
}
char* uri_str = gpr_getenv("http_proxy");
if (uri_str == NULL) return;
if (uri_str == NULL) return NULL;
grpc_uri* uri =
grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */);
if (uri == NULL || uri->authority == NULL) {
@ -50,18 +57,33 @@ static void grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx,
gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme);
goto done;
}
char *user_cred_end = strchr(uri->authority, '@');
if (user_cred_end != NULL) {
*name_to_resolve = gpr_strdup(user_cred_end + 1);
*user_cred_end = '\0';
*user_cred = gpr_strdup(uri->authority);
/* Split on '@' to separate user credentials from host */
char **authority_strs = NULL;
size_t authority_nstrs;
gpr_string_split(uri->authority, "@", &authority_strs, &authority_nstrs);
GPR_ASSERT(authority_nstrs != 0); /* should have atleast 1 string */
if(authority_nstrs == 1) {
/* User cred not present in authority */
proxy_name = gpr_strdup(authority_strs[0]);
} else if(authority_nstrs == 2) {
/* User cred found */
if(user_cred != NULL) {
*user_cred = gpr_strdup(authority_strs[0]);
}
proxy_name = gpr_strdup(authority_strs[1]);
gpr_log(GPR_INFO, "userinfo found in proxy URI");
} else {
*name_to_resolve = gpr_strdup(uri->authority);
/* Bad authority */
proxy_name = NULL;
}
for(size_t i = 0; i < authority_nstrs; i++) {
gpr_free(authority_strs[i]);
}
gpr_free(authority_strs);
done:
gpr_free(uri_str);
grpc_uri_destroy(uri);
return proxy_name;
}
static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
@ -71,7 +93,7 @@ static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx,
char** name_to_resolve,
grpc_channel_args** new_args) {
char *user_cred = NULL;
grpc_get_http_proxy_server(exec_ctx, name_to_resolve, &user_cred);
*name_to_resolve = grpc_get_http_proxy_server(exec_ctx, &user_cred);
if (*name_to_resolve == NULL) return false;
grpc_uri* uri =
grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */);

@ -53,11 +53,12 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack(
const int server_port = grpc_pick_unused_port_or_die();
gpr_join_host_port(&ffd->server_addr, "localhost", server_port);
//If we are testing proxy auth, add the proxy auth arg to proxy channel args
/* If we are testing proxy auth, add the proxy auth arg to proxy channel args
*/
grpc_channel_args *proxy_args = NULL;
const grpc_arg *proxy_auth_arg = grpc_channel_args_find(
client_args, GRPC_END2END_HTTP_PROXY_TEST_CONNECT_AUTH_PRESENT);
if(proxy_auth_arg) {
if(proxy_auth_arg != NULL) {
proxy_args = grpc_channel_args_copy_and_add(NULL, proxy_auth_arg, 1);
}
ffd->proxy = grpc_end2end_http_proxy_create(proxy_args);
@ -77,7 +78,7 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture *f,
fullstack_fixture_data *ffd = f->fixture_data;
char *proxy_uri;
// If testing for proxy auth, add credentials to proxy uri
/* If testing for proxy auth, add credentials to proxy uri */
if(grpc_channel_args_find(
client_args, GRPC_END2END_HTTP_PROXY_TEST_CONNECT_AUTH_PRESENT) == NULL) {
gpr_asprintf(&proxy_uri, "http://%s",

@ -47,8 +47,9 @@
#include "src/core/lib/iomgr/tcp_client.h"
#include "src/core/lib/iomgr/tcp_server.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/slice/b64.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/support/string.h"
#include "test/core/util/port.h"
struct grpc_end2end_http_proxy {
@ -306,6 +307,45 @@ static void on_server_connect_done(grpc_exec_ctx* exec_ctx, void* arg,
&conn->on_write_response_done);
}
/**
* Parses the proxy auth header value to check if it matches :-
* Basic <base64_encoded_user_cred>
* Returns true if it matches, false otherwise
*/
static bool proxy_auth_header_matches(grpc_exec_ctx *exec_ctx,
char *proxy_auth_header_val) {
if(proxy_auth_header_val == NULL) {
return false;
}
char **auth_header_strs;
size_t auth_header_nstrs;
bool auth_header_matches = false;
// Split the auth header value on space
gpr_string_split(proxy_auth_header_val, " ", &auth_header_strs,
&auth_header_nstrs);
if(auth_header_nstrs != 2) {
goto done;
}
// Authentication type should be Basic
if(strcmp(auth_header_strs[0], "Basic") != 0) {
goto done;
}
// should match GRPC_END2END_HTTP_PROXY_TEST_CONNECT_CRED after decoding
grpc_slice decoded_slice =
grpc_base64_decode(exec_ctx, auth_header_strs[1], 0);
if(grpc_slice_str_cmp(
decoded_slice, GRPC_END2END_HTTP_PROXY_TEST_CONNECT_CRED) != 0) {
goto done;
}
auth_header_matches = true;
done:
for(size_t i = 0; i < auth_header_nstrs; i++) {
gpr_free(auth_header_strs[i]);
}
gpr_free(auth_header_strs);
return auth_header_matches;
}
// Callback to read the HTTP CONNECT request.
// TODO(roth): Technically, for any of the failure modes handled by this
// function, we should handle the error by returning an HTTP response to
@ -354,34 +394,22 @@ static void on_read_request_done(grpc_exec_ctx* exec_ctx, void* arg,
GRPC_ERROR_UNREF(error);
return;
}
// If proxy auth is being used, check if the header is present
// If proxy auth is being used, check if the header is present and as expected
if(grpc_channel_args_find(
conn->proxy->channel_args,
GRPC_END2END_HTTP_PROXY_TEST_CONNECT_AUTH_PRESENT) != NULL) {
bool found = false, failed = false;
bool auth_header_found = false;
for(size_t i = 0; i < conn->http_request.hdr_count; i++) {
if(strcmp(conn->http_request.hdrs[i].key, "Proxy-Authorization") == 0) {
found = true;
// Authentication type should be Basic
if(strncmp(conn->http_request.hdrs[i].value, "Basic",
strlen("Basic")) != 0) {
failed = true;
break;
}
// Check if encoded string is as expected
char *encoded_str_start =
strchr(conn->http_request.hdrs[i].value, ' ') + 1;
grpc_slice decoded_slice =
grpc_base64_decode(exec_ctx, encoded_str_start, 0);
if(grpc_slice_str_cmp(
decoded_slice, GRPC_END2END_HTTP_PROXY_TEST_CONNECT_CRED) != 0) {
failed = true;
if(!proxy_auth_header_matches(
exec_ctx, conn->http_request.hdrs[i].value)) {
break;
}
auth_header_found = true;
break;
}
}
if(!found || failed) {
if(!auth_header_found) {
const char *msg = "HTTP Connect could not verify authentication";
error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
proxy_connection_failed(exec_ctx, conn, true /* is_client */,

Loading…
Cancel
Save