diff --git a/Makefile b/Makefile index 384204cd466dc7361d915335bf3895487ebdae21..d79d8277ed0710476d767de863b9228f4d8e786e 100644 --- a/Makefile +++ b/Makefile @@ -468,6 +468,7 @@ grpc_completion_queue_benchmark: $(BINDIR)/$(CONFIG)/grpc_completion_queue_bench grpc_completion_queue_test: $(BINDIR)/$(CONFIG)/grpc_completion_queue_test grpc_credentials_test: $(BINDIR)/$(CONFIG)/grpc_credentials_test grpc_fetch_oauth2: $(BINDIR)/$(CONFIG)/grpc_fetch_oauth2 +grpc_create_jwt: $(BINDIR)/$(CONFIG)/grpc_create_jwt grpc_json_token_test: $(BINDIR)/$(CONFIG)/grpc_json_token_test grpc_stream_op_test: $(BINDIR)/$(CONFIG)/grpc_stream_op_test hpack_parser_test: $(BINDIR)/$(CONFIG)/hpack_parser_test @@ -1748,7 +1749,7 @@ test_cxx: buildtests_cxx $(Q) $(BINDIR)/$(CONFIG)/thread_pool_test || ( echo test thread_pool_test failed ; exit 1 ) -tools: privatelibs $(BINDIR)/$(CONFIG)/gen_hpack_tables $(BINDIR)/$(CONFIG)/grpc_fetch_oauth2 +tools: privatelibs $(BINDIR)/$(CONFIG)/gen_hpack_tables $(BINDIR)/$(CONFIG)/grpc_fetch_oauth2 $(BINDIR)/$(CONFIG)/grpc_create_jwt buildbenchmarks: privatelibs $(BINDIR)/$(CONFIG)/grpc_completion_queue_benchmark $(BINDIR)/$(CONFIG)/low_level_ping_pong_benchmark @@ -6453,6 +6454,37 @@ endif endif +GRPC_CREATE_JWT_SRC = \ + test/core/security/create_jwt.c \ + +GRPC_CREATE_JWT_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(GRPC_CREATE_JWT_SRC)))) + +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL with ALPN. + +$(BINDIR)/$(CONFIG)/grpc_create_jwt: openssl_dep_error + +else + +$(BINDIR)/$(CONFIG)/grpc_create_jwt: $(GRPC_CREATE_JWT_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LD) $(LDFLAGS) $(GRPC_CREATE_JWT_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/grpc_create_jwt + +endif + +$(OBJDIR)/$(CONFIG)/test/core/security/create_jwt.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + +deps_grpc_create_jwt: $(GRPC_CREATE_JWT_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(GRPC_CREATE_JWT_OBJS:.o=.dep) +endif +endif + + GRPC_JSON_TOKEN_TEST_SRC = \ test/core/security/json_token_test.c \ diff --git a/build.json b/build.json index 8fdca5a25507e4db560d6130ea08b5df4bd06a8d..7ee6a711f157f537a1d72742b76648e2b6717964 100644 --- a/build.json +++ b/build.json @@ -1157,6 +1157,20 @@ "gpr" ] }, + { + "name": "grpc_create_jwt", + "build": "tool", + "language": "c", + "src": [ + "test/core/security/create_jwt.c" + ], + "deps": [ + "grpc_test_util", + "grpc", + "gpr_test_util", + "gpr" + ] + }, { "name": "grpc_json_token_test", "build": "test", diff --git a/test/core/security/create_jwt.c b/test/core/security/create_jwt.c new file mode 100644 index 0000000000000000000000000000000000000000..614dd1e50c66ad5126db9e21a7cc8bfc7cee70dc --- /dev/null +++ b/test/core/security/create_jwt.c @@ -0,0 +1,110 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <stdio.h> +#include <string.h> + +#include "src/core/security/credentials.h" +#include "src/core/security/json_token.h" +#include "src/core/support/file.h" + +#include <grpc/support/alloc.h> +#include <grpc/support/cmdline.h> +#include <grpc/support/log.h> +#include <grpc/support/slice.h> + +void create_jwt(const char *json_key_file_path, const char *service_url, + const char *scope) { + grpc_auth_json_key key; + int ok = 0; + char *jwt; + gpr_slice json_key_data = gpr_load_file(json_key_file_path, &ok); + if (!ok) { + fprintf(stderr, "Could not read %s.\n", json_key_file_path); + exit(1); + } + key = grpc_auth_json_key_create_from_string( + (const char *)GPR_SLICE_START_PTR(json_key_data)); + gpr_slice_unref(json_key_data); + if (!grpc_auth_json_key_is_valid(&key)) { + fprintf(stderr, "Could not parse json key.\n"); + exit(1); + } + jwt = grpc_jwt_encode_and_sign( + &key, service_url == NULL ? GRPC_JWT_OAUTH2_AUDIENCE : service_url, + grpc_max_auth_token_lifetime, scope); + grpc_auth_json_key_destruct(&key); + if (jwt == NULL) { + fprintf(stderr, "Could not create JWT.\n"); + exit(1); + } + fprintf(stdout, "%s\n", jwt); + gpr_free(jwt); +} + +int main(int argc, char **argv) { + char *scope = NULL; + char *json_key_file_path = NULL; + char *service_url = NULL; + gpr_cmdline *cl = gpr_cmdline_create("create_jwt"); + gpr_cmdline_add_string(cl, "json_key", "File path of the json key.", + &json_key_file_path); + gpr_cmdline_add_string(cl, "scope", + "OPTIONAL Space delimited permissions. Mutually " + "exclusive with service_url", + &scope); + gpr_cmdline_add_string(cl, "service_url", + "OPTIONAL service URL. Mutually exclusive with scope.", + &service_url); + gpr_cmdline_parse(cl, argc, argv); + + if (json_key_file_path == NULL) { + fprintf(stderr, "Missing --json_key option.\n"); + exit(1); + } + if (scope != NULL) { + if (service_url != NULL) { + fprintf(stderr, + "Options --scope and --service_url are mutually exclusive.\n"); + exit(1); + } + } else if (service_url == NULL) { + fprintf(stderr, "Need one of --service_url or --scope options.\n"); + exit(1); + } + + create_jwt(json_key_file_path, service_url, scope); + + gpr_cmdline_destroy(cl); + return 0; +} diff --git a/test/core/security/fetch_oauth2.c b/test/core/security/fetch_oauth2.c index 951281d5b3cebc8840d55ef4296ca8160a2ad04f..748a5982fdeb41343874c06b523c54e96147df37 100644 --- a/test/core/security/fetch_oauth2.c +++ b/test/core/security/fetch_oauth2.c @@ -139,7 +139,7 @@ int main(int argc, char **argv) { } } else { if (json_key_file_path == NULL) { - gpr_log(GPR_ERROR, "missing --json_key option."); + gpr_log(GPR_ERROR, "Missing --json_key option."); exit(1); } if (scope == NULL) { diff --git a/vsprojects/vs2013/Grpc.mak b/vsprojects/vs2013/Grpc.mak index f4e9f46b6851bbe9ba5f670ffbbe5f13a8cccc01..180356181980105da97d99c341a1d30e66e61089 100644 --- a/vsprojects/vs2013/Grpc.mak +++ b/vsprojects/vs2013/Grpc.mak @@ -434,6 +434,14 @@ grpc_fetch_oauth2: grpc_fetch_oauth2.exe echo Running grpc_fetch_oauth2 $(OUT_DIR)\grpc_fetch_oauth2.exe +grpc_create_jwt.exe: grpc_test_util + echo Building grpc_create_jwt + $(CC) $(CFLAGS) /Fo:$(OUT_DIR)\ ..\..\test\core\security\create_jwt.c + $(LINK) $(LFLAGS) /OUT:"$(OUT_DIR)\grpc_create_jwt.exe" Debug\grpc_test_util.lib Debug\grpc.lib Debug\gpr_test_util.lib Debug\gpr.lib $(LIBS) $(OUT_DIR)\create_jwt.obj +grpc_create_jwt: grpc_create_jwt.exe + echo Running grpc_create_jwt + $(OUT_DIR)\grpc_create_jwt.exe + grpc_json_token_test.exe: grpc_test_util echo Building grpc_json_token_test $(CC) $(CFLAGS) /Fo:$(OUT_DIR)\ ..\..\test\core\security\json_token_test.c