Search in sources :

Example 26 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class SecureServer method createWebServer.

/**
 * Create the web server.
 */
private static WebServer createWebServer(Config config, Security security) {
    Routing routing = Routing.builder().register(WebSecurity.create(security).securityDefaults(WebSecurity.authenticate())).register(new RestService()).build();
    WebServer webServer = WebServer.create(routing, config);
    webServer.start().thenAccept(s -> {
        System.out.println("Web server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Web server startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return webServer;
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Context(io.helidon.common.context.Context) ResponseHelper.complete(io.helidon.grpc.core.ResponseHelper.complete) Channel(io.grpc.Channel) StringService(io.helidon.grpc.examples.common.StringService) StreamObserver(io.grpc.stub.StreamObserver) GrpcHelper(io.helidon.grpc.core.GrpcHelper) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Service(io.helidon.webserver.Service) Status(io.grpc.Status) LogConfig(io.helidon.common.LogConfig) Http(io.helidon.common.http.Http) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) Greet(io.helidon.grpc.examples.common.Greet) ServerRequest(io.helidon.webserver.ServerRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Strings(io.helidon.grpc.examples.common.Strings) GrpcService(io.helidon.grpc.server.GrpcService) StringServiceGrpc(io.helidon.grpc.examples.common.StringServiceGrpc) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting) Routing(io.helidon.webserver.Routing)

Example 27 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class SecureServer method createGrpcServer.

/**
 * Create the gRPC server.
 */
private static GrpcServer createGrpcServer(Config config, Security security) {
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(new StringService(), GrpcSecurity.rolesAllowed("admin")).register(new GreetService()).build();
    GrpcServer grpcServer = GrpcServer.create(GrpcServerConfiguration.create(config), grpcRouting);
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("gRPC server startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return grpcServer;
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Context(io.helidon.common.context.Context) ResponseHelper.complete(io.helidon.grpc.core.ResponseHelper.complete) Channel(io.grpc.Channel) StringService(io.helidon.grpc.examples.common.StringService) StreamObserver(io.grpc.stub.StreamObserver) GrpcHelper(io.helidon.grpc.core.GrpcHelper) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Service(io.helidon.webserver.Service) Status(io.grpc.Status) LogConfig(io.helidon.common.LogConfig) Http(io.helidon.common.http.Http) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) Greet(io.helidon.grpc.examples.common.Greet) ServerRequest(io.helidon.webserver.ServerRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Strings(io.helidon.grpc.examples.common.Strings) GrpcService(io.helidon.grpc.server.GrpcService) StringServiceGrpc(io.helidon.grpc.examples.common.StringServiceGrpc) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 28 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class SecureGreetClient method main.

/**
 * Main entry point.
 *
 * @param args  the program arguments - {@code arg[0]} is the user name
 *              and {@code arg[1] is the password}
 */
public static void main(String[] args) {
    Channel channel = ManagedChannelBuilder.forAddress("localhost", 1408).usePlaintext().build();
    // Obtain the user name and password from the program arguments
    String user = args.length >= 2 ? args[0] : "Ted";
    String password = args.length >= 2 ? args[1] : "secret";
    Config config = Config.create();
    // configure Helidon security and add the basic auth provider
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    // create the gRPC client security call credentials
    // setting the properties used by the basic auth provider for user name and password
    GrpcClientSecurity clientSecurity = GrpcClientSecurity.builder(security.createContext("test.client")).property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_USER, user).property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_PASSWORD, password).build();
    // Create the client service descriptor and add the call credentials
    ClientServiceDescriptor descriptor = ClientServiceDescriptor.builder(GreetServiceGrpc.getServiceDescriptor()).callCredentials(clientSecurity).build();
    // create the client for the service
    GrpcServiceClient client = GrpcServiceClient.create(channel, descriptor);
    greet(client);
    setGreeting(client);
    greet(client);
}
Also used : GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) Config(io.helidon.config.Config) Channel(io.grpc.Channel) ClientServiceDescriptor(io.helidon.grpc.client.ClientServiceDescriptor) GrpcServiceClient(io.helidon.grpc.client.GrpcServiceClient) Security(io.helidon.security.Security) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity)

Example 29 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class SecureServer method main.

/**
 * Main entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    ServiceDescriptor greetService1 = ServiceDescriptor.builder(new GreetService(config)).name("GreetService").intercept(GrpcSecurity.rolesAllowed("user")).intercept("SetGreeting", GrpcSecurity.rolesAllowed("admin")).build();
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(greetService1).register(new StringService()).build();
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.create(config.get("grpc"));
    GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Security(io.helidon.security.Security) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) GreetService(io.helidon.grpc.examples.common.GreetService) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) Security(io.helidon.security.Security) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 30 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class DigestExampleBuilderMain method main.

/**
 * Starts this example. Programmatical configuration. See standard output for instructions.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    // load logging configuration
    LogConfig.configureRuntime();
    // build routing (same as done in application.conf)
    Routing routing = Routing.builder().register(buildWebSecurity().securityDefaults(WebSecurity.authenticate())).get("/noRoles", WebSecurity.enforce()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny").audit()).any("/noAuthn", WebSecurity.rolesAllowed("admin").authenticationOptional().audit()).get("/{*}", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
    }).build();
    // start server (blocks until started)
    server = DigestExampleUtil.startServer(routing);
}
Also used : Arrays(java.util.Arrays) Security(io.helidon.security.Security) MessageDigest(java.security.MessageDigest) Set(java.util.Set) HashMap(java.util.HashMap) SecurityContext(io.helidon.security.SecurityContext) StandardCharsets(java.nio.charset.StandardCharsets) HttpDigest(io.helidon.security.providers.httpauth.HttpDigest) MediaType(io.helidon.common.http.MediaType) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Map(java.util.Map) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) HttpDigestAuthProvider(io.helidon.security.providers.httpauth.HttpDigestAuthProvider) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) SecureUserStore(io.helidon.security.providers.httpauth.SecureUserStore) Optional(java.util.Optional) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing)

Aggregations

Security (io.helidon.security.Security)48 SecurityContext (io.helidon.security.SecurityContext)25 Config (io.helidon.config.Config)22 BeforeAll (org.junit.jupiter.api.BeforeAll)14 Test (org.junit.jupiter.api.Test)14 LogConfig (io.helidon.common.LogConfig)13 Routing (io.helidon.webserver.Routing)12 Optional (java.util.Optional)12 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)10 WebClientSecurity (io.helidon.webclient.security.WebClientSecurity)10 WebServer (io.helidon.webserver.WebServer)10 Channel (io.grpc.Channel)7 GrpcRouting (io.helidon.grpc.server.GrpcRouting)7 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)7 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)7 Set (java.util.Set)7 CoreMatchers.is (org.hamcrest.CoreMatchers.is)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Context (io.helidon.common.context.Context)6 AuthorizationResponse (io.helidon.security.AuthorizationResponse)6