Exemplo n.º 1
0
    public void Decrypt(SecretKeyEncryption ske, byte[] key)
    {
        KpCommon.ThrowIfNull(this.InnerMessageEnc);
        var bytes = ToBytes(this.InnerMessageEnc);

        bytes                = ske.Decrypt(bytes !, key);
        this.InnerMessage    = GrpcMsgPack.DynDes <Types.Inner>(bytes !);
        this.InnerMessageEnc = ByteString.Empty;
    }
Exemplo n.º 2
0
    public void Encrypt(SecretKeyEncryption ske, byte[] key)
    {
        KpCommon.ThrowIfNull(this.InnerMessage);
        var bytes = GrpcMsgPack.DynSer(this.InnerMessage);

        bytes = ske.Encrypt(bytes, key);
        this.InnerMessageEnc = FromBytes(bytes);
        this.InnerMessage    = null;
    }
Exemplo n.º 3
0
    public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(
        TRequest request, ServerCallContext context,
        UnaryServerMethod <TRequest, TResponse> continuation)
    {
        SecretKeyEncryption?ske = null;

        byte[]? key = null;

        if (request is ISessionSecuredMessage ssmRequ)
        {
            if (ske == null || key == null)
            {
                (ske, key) = ResolveSessionKey(context.RequestHeaders);
            }

            try
            {
                KpCommon.ThrowIfNull(ske);
                KpCommon.ThrowIfNull(key);

                ssmRequ.Decrypt(ske, key);
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.Internal,
                                                  "failed to decrypt session-secured input message: " + ex.Message));
            }
        }

        var response = await base.UnaryServerHandler(request, context, continuation);

        if (response is ISessionSecuredMessage ssmResp)
        {
            if (ske == null || key == null)
            {
                (ske, key) = ResolveSessionKey(context.RequestHeaders);
            }

            try
            {
                ssmResp.Encrypt(ske, key);
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.Internal,
                                                  "failed to encrypt session-secured reply message: " + ex.Message));
            }
        }

        return(response);
    }
Exemplo n.º 4
0
    public void ConfigureServices(IServiceCollection services)
    {
        // Local logger outside of the M.E.Logging framework
        var preLogger = Program.GetPrelogger <Startup>();

        var serverConfig = _config
                           .GetSection(ServerConfig.DefaultConfigName)
                           .Get <ServerConfig>();

        KpCommon.ThrowIfNull(serverConfig);

        KpCommon.ThrowIfNull(serverConfig.ConnectionStringName);
        var connName   = serverConfig.ConnectionStringName;
        var connString = _config.GetConnectionString(connName);

        KpCommon.ThrowIfNull(connString,
                             messageFormat: $"connection string missing: [{connName}]");

        switch (serverConfig.DbDriver)
        {
        case "sqlite":
            preLogger.Info("registering DB Context via SQLite");
            //builder.Services.AddSqlite<KyprDbContext>(connString);
            services.AddDbContext <KyprDbContext>(builder =>
            {
                builder.UseSqlite(connString);
            });
            break;

        default:
            throw new Exception($"don't know DbDriver [{serverConfig.DbDriver}]");
        }

        if (serverConfig.MigrateOnStart)
        {
            services.AddHostedService <MigrateOnStartHostedService>();
        }

        // Additional configuration is required to successfully run gRPC on macOS.
        // For instructions on how to configure Kestrel and gRPC clients on macOS,
        // visit https://go.microsoft.com/fwlink/?linkid=2099682
        services.AddGrpc(options =>
        {
            options.Interceptors.Add <GrpcServerInterceptor>();
        });
    }