Skip to content

Commit

Permalink
license added
Browse files Browse the repository at this point in the history
  • Loading branch information
y-eight committed Nov 9, 2022
1 parent cad3a67 commit 42bffda
Show file tree
Hide file tree
Showing 17 changed files with 410 additions and 101 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Canary Bot: A single instance running on a dedicated system
Canary Mesh: Multiple Canary Bots connected to each other. Every Canary Bot manages its own mesh instance, knowing about the bots that are accessible by itself.


### Canary Mesh statup
### Canary Mesh startup

![Canary Mesh](mesh.drawio.png)

Expand Down
136 changes: 136 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* MIT License
*
* Copyright (c) 2022 Johan Brandhorst-Satzkorn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

// https://github.com/johanbrandhorst/connect-gateway-example

package api

import (
"context"
"fmt"
"net/http"
"strconv"

connect "github.com/bufbuild/connect-go"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/telekom/canary-bot/data"
h "github.com/telekom/canary-bot/helper"
apiv1 "github.com/telekom/canary-bot/proto/api/v1"
"github.com/telekom/canary-bot/proto/api/v1/apiv1connect"
"go.uber.org/zap"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"
)

func StartApi(data data.Database, config *Configuration, log *zap.SugaredLogger) error {
a := &Api{
data: data,
config: config,
log: log,
}

if config.DebugGrpc {
grpc_zap.ReplaceGrpcLoggerV2(log.Named("grpc").Desugar())
}

var opts []grpc.DialOption

// TLS for http proxy server
tlsCredentials, err := h.LoadServerTLSCredentials(
config.ServerCertPath,
config.ServerKeyPath,
config.ServerCert,
config.ServerKey,
)

if err != nil {
log.Warnw("Cannot load TLS server credentials - using insecure connection for incoming requests")
log.Debugw("Cannot load TLS credentials", "error", err.Error())
}

// TLS for client connect from http proxy server to grpc server
// just load it if TLS is activated, not considered for edge-terminated TLS
var tlsClientCredentials credentials.TransportCredentials
if tlsCredentials != nil {
tlsClientCredentials, err = h.LoadClientTLSCredentials(config.CaCertPath, config.CaCert)

}

if err != nil {
log.Debugw("Cannot load TLS client credentials - starting insecure connection to grpc server")
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
opts = append(opts, grpc.WithTransportCredentials(tlsClientCredentials))
}

addr := config.Address + ":" + strconv.FormatInt(config.Port, 10)
// Note: this will succeed asynchronously, once we've started the server below.
conn, err := grpc.DialContext(
context.Background(),
"dns:///"+addr,
opts...,
)
if err != nil {
return fmt.Errorf("failed to dial server: %w", err)
}

gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption("*", &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{UseProtoNames: true},
},
}),
)

err = apiv1.RegisterApiServiceHandler(context.Background(), gwmux, conn)
if err != nil {
return fmt.Errorf("failed to register gateway: %w", err)
}

// Auth
interceptors := connect.WithInterceptors(a.NewAuthInterceptor())

mux := http.NewServeMux()
mux.Handle("/", getOpenAPIHandler())
mux.Handle(apiv1connect.NewApiServiceHandler(a, interceptors))
mux.Handle("/api/v1/", gwmux)
server := &http.Server{
Addr: addr,
Handler: h2c.NewHandler(mux, &http2.Server{}),
}
log.Info("Serving Connect, gRPC-Gateway and OpenAPI Documentation on ", addr)

// TLS ready
if tlsCredentials != nil {
server.TLSConfig = tlsCredentials
return server.ListenAndServeTLS("", "")
}

return server.ListenAndServe()
}
21 changes: 21 additions & 0 deletions api/auth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* canary-bot
*
* (C) 2022, Maximilian Schubert, Deutsche Telekom IT GmbH
*
* Deutsche Telekom IT GmbH and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package api

import (
Expand Down
100 changes: 0 additions & 100 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,18 @@ package api

import (
"context"
"fmt"
"io/fs"
"mime"
"net/http"
"strconv"
"time"

"github.com/telekom/canary-bot/data"
h "github.com/telekom/canary-bot/helper"

third_party "github.com/telekom/canary-bot/proto/api/third_party"
apiv1 "github.com/telekom/canary-bot/proto/api/v1"
apiv1connect "github.com/telekom/canary-bot/proto/api/v1/apiv1connect"

grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"

connect "github.com/bufbuild/connect-go"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/zap"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"
)

// Api implements the protobuf interface
Expand Down Expand Up @@ -112,90 +99,3 @@ func getOpenAPIHandler() http.Handler {
}
return http.FileServer(http.FS(subFS))
}

func StartApi(data data.Database, config *Configuration, log *zap.SugaredLogger) error {
a := &Api{
data: data,
config: config,
log: log,
}

if config.DebugGrpc {
grpc_zap.ReplaceGrpcLoggerV2(log.Named("grpc").Desugar())
}

var opts []grpc.DialOption

// TLS for http proxy server
tlsCredentials, err := h.LoadServerTLSCredentials(
config.ServerCertPath,
config.ServerKeyPath,
config.ServerCert,
config.ServerKey,
)

if err != nil {
log.Warnw("Cannot load TLS server credentials - using insecure connection for incoming requests")
log.Debugw("Cannot load TLS credentials", "error", err.Error())
}

// TLS for client connect from http proxy server to grpc server
// just load it if TLS is activated, not considered for edge-terminated TLS
var tlsClientCredentials credentials.TransportCredentials
if tlsCredentials != nil {
tlsClientCredentials, err = h.LoadClientTLSCredentials(config.CaCertPath, config.CaCert)

}

if err != nil {
log.Debugw("Cannot load TLS client credentials - starting insecure connection to grpc server")
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
opts = append(opts, grpc.WithTransportCredentials(tlsClientCredentials))
}

addr := config.Address + ":" + strconv.FormatInt(config.Port, 10)
// Note: this will succeed asynchronously, once we've started the server below.
conn, err := grpc.DialContext(
context.Background(),
"dns:///"+addr,
opts...,
)
if err != nil {
return fmt.Errorf("failed to dial server: %w", err)
}

gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption("*", &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{UseProtoNames: true},
},
}),
)

err = apiv1.RegisterApiServiceHandler(context.Background(), gwmux, conn)
if err != nil {
return fmt.Errorf("failed to register gateway: %w", err)
}

// Auth
interceptors := connect.WithInterceptors(a.NewAuthInterceptor())

mux := http.NewServeMux()
mux.Handle("/", getOpenAPIHandler())
mux.Handle(apiv1connect.NewApiServiceHandler(a, interceptors))
mux.Handle("/api/v1/", gwmux)
server := &http.Server{
Addr: addr,
Handler: h2c.NewHandler(mux, &http2.Server{}),
}
log.Info("Serving Connect, gRPC-Gateway and OpenAPI Documentation on ", addr)

// TLS ready
if tlsCredentials != nil {
server.TLSConfig = tlsCredentials
return server.ListenAndServeTLS("", "")
}

return server.ListenAndServe()
}
21 changes: 21 additions & 0 deletions data/data_node.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* canary-bot
*
* (C) 2022, Maximilian Schubert, Deutsche Telekom IT GmbH
*
* Deutsche Telekom IT GmbH and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package data

import (
Expand Down
21 changes: 21 additions & 0 deletions data/data_node_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* canary-bot
*
* (C) 2022, Maximilian Schubert, Deutsche Telekom IT GmbH
*
* Deutsche Telekom IT GmbH and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package data

import (
Expand Down
21 changes: 21 additions & 0 deletions data/data_sample.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* canary-bot
*
* (C) 2022, Maximilian Schubert, Deutsche Telekom IT GmbH
*
* Deutsche Telekom IT GmbH and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package data

import "time"
Expand Down
21 changes: 21 additions & 0 deletions data/data_sample_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* canary-bot
*
* (C) 2022, Maximilian Schubert, Deutsche Telekom IT GmbH
*
* Deutsche Telekom IT GmbH and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package data

import (
Expand Down
Loading

0 comments on commit 42bffda

Please sign in to comment.